UdpConnection.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using UnityEngine;
  8. public class UdpConnection
  9. {
  10. private UdpClient udpClient;
  11. private readonly Queue<string> incomingQueue = new Queue<string>();
  12. Thread receiveThread;
  13. private bool threadRunning = false;
  14. private string senderIp;
  15. private int senderPort;
  16. public void StartConnection(string sendIp, int sendPort, int receivePort)
  17. {
  18. try { udpClient = new UdpClient(receivePort); }
  19. catch (Exception e)
  20. {
  21. Debug.Log("Failed to listen for UDP at port " + receivePort + ": " + e.Message);
  22. return;
  23. }
  24. Debug.Log("Created receiving client at ip and port " + receivePort);
  25. this.senderIp = sendIp;
  26. this.senderPort = sendPort;
  27. Debug.Log("Set sendee at ip " + sendIp + " and port " + sendPort);
  28. StartReceiveThread();
  29. }
  30. private void StartReceiveThread()
  31. {
  32. receiveThread = new Thread(() => ListenForMessages(udpClient));
  33. receiveThread.IsBackground = true;
  34. threadRunning = true;
  35. receiveThread.Start();
  36. }
  37. private void ListenForMessages(UdpClient client)
  38. {
  39. IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  40. while (threadRunning)
  41. {
  42. try
  43. {
  44. Byte[] receiveBytes = client.Receive(ref remoteIpEndPoint); // Blocks until a message returns on this socket from a remote host.
  45. string returnData = Encoding.UTF8.GetString(receiveBytes);
  46. lock (incomingQueue)
  47. {
  48. incomingQueue.Enqueue(returnData);
  49. }
  50. }
  51. catch (SocketException e)
  52. {
  53. // 10004 thrown when socket is closed
  54. if (e.ErrorCode != 10004) Debug.Log("Socket exception while receiving data from udp client: " + e.Message);
  55. }
  56. catch (Exception e)
  57. {
  58. Debug.Log("Error receiving data from udp client: " + e.Message);
  59. }
  60. Thread.Sleep(1);
  61. }
  62. }
  63. public string[] getMessages()
  64. {
  65. string[] pendingMessages = new string[0];
  66. lock (incomingQueue)
  67. {
  68. pendingMessages = new string[incomingQueue.Count];
  69. int i = 0;
  70. while (incomingQueue.Count != 0)
  71. {
  72. pendingMessages[i] = incomingQueue.Dequeue();
  73. i++;
  74. }
  75. }
  76. return pendingMessages;
  77. }
  78. public void Send(string message)
  79. {
  80. Debug.Log(String.Format("Send msg to ip:{0} port:{1} msg:{2}",senderIp,senderPort,message));
  81. IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Parse(senderIp), senderPort);
  82. Byte[] sendBytes = Encoding.UTF8.GetBytes(message);
  83. udpClient.Send(sendBytes, sendBytes.Length, serverEndpoint);
  84. }
  85. public void Stop()
  86. {
  87. threadRunning = false;
  88. receiveThread.Abort();
  89. udpClient.Close();
  90. }
  91. }