Skip to content

Instantly share code, notes, and snippets.

@hyakugei
Created January 29, 2020 17:52
Show Gist options
  • Save hyakugei/39ab8dc1b88829c8b18153840c4b3bc8 to your computer and use it in GitHub Desktop.
Save hyakugei/39ab8dc1b88829c8b18153840c4b3bc8 to your computer and use it in GitHub Desktop.
async udp class for Unity
// taken from https://forum.unity.com/threads/simple-udp-implementation-send-read-via-mono-c.15900/#post-3645256
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
public class UdpConnection
{
private UdpClient udpClient;
private readonly Queue<string> incomingQueue = new Queue<string>();
Thread receiveThread;
private bool threadRunning = false;
private string senderIp;
private int senderPort;
public void StartConnection(string sendIp, int sendPort, int receivePort)
{
try { udpClient = new UdpClient(receivePort); }
catch (Exception e)
{
Debug.Log("Failed to listen for UDP at port " + receivePort + ": " + e.Message);
return;
}
Debug.Log("Created receiving client at ip and port " + receivePort);
this.senderIp = sendIp;
this.senderPort = sendPort;
Debug.Log("Set sendee at ip " + sendIp + " and port " + sendPort);
StartReceiveThread();
}
private void StartReceiveThread()
{
receiveThread = new Thread(() => ListenForMessages(udpClient));
receiveThread.IsBackground = true;
threadRunning = true;
receiveThread.Start();
}
private void ListenForMessages(UdpClient client)
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (threadRunning)
{
try
{
Byte[] receiveBytes = client.Receive(ref remoteIpEndPoint); // Blocks until a message returns on this socket from a remote host.
string returnData = Encoding.UTF8.GetString(receiveBytes);
lock (incomingQueue)
{
incomingQueue.Enqueue(returnData);
}
}
catch (SocketException e)
{
// 10004 thrown when socket is closed
if (e.ErrorCode != 10004) Debug.Log("Socket exception while receiving data from udp client: " + e.Message);
}
catch (Exception e)
{
Debug.Log("Error receiving data from udp client: " + e.Message);
}
Thread.Sleep(1);
}
}
public string[] getMessages()
{
string[] pendingMessages = new string[0];
lock (incomingQueue)
{
pendingMessages = new string[incomingQueue.Count];
int i = 0;
while (incomingQueue.Count != 0)
{
pendingMessages[i] = incomingQueue.Dequeue();
i++;
}
}
return pendingMessages;
}
public void Send(string message)
{
Debug.Log(String.Format("Send msg to ip:{0} port:{1} msg:{2}",senderIp,senderPort,message));
IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Parse(senderIp), senderPort);
Byte[] sendBytes = Encoding.UTF8.GetBytes(message);
udpClient.Send(sendBytes, sendBytes.Length, serverEndpoint);
}
public void Stop()
{
threadRunning = false;
receiveThread.Abort();
udpClient.Close();
}
}
private UdpConnection connection;
void Start()
{
string sendIp = "127.0.0.1";
int sendPort = 8881;
int receivePort = 11000;
connection = new UdpConnection();
connection.StartConnection(sendIp, sendPort, receivePort);
}
void Update()
{
foreach (var message in connection.getMessages()) Debug.Log(message);
connection.Send("Hi!");
}
void OnDestroy()
{
connection.Stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment