Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.Collections.Generic;
- public class TestUDP : MonoBehaviour {
- public delegate void RequestReceivedEventHandler(string message);
- public event RequestReceivedEventHandler OnRequestReceived;
- // Use this to trigger the event
- protected virtual void ThisRequestReceived(string message)
- {
- RequestReceivedEventHandler handler = OnRequestReceived;
- if(handler != null)
- {
- handler(message);
- }
- }
- // We use this to keep tasks needed to run in the main thread
- private static readonly Queue<Action> tasks = new Queue<Action>();
- public int requestPort = 55795;
- UdpClient udpRequestSender;
- UdpClient udpRequestReceiver;
- // Use this for initialization
- void Start () {
- // Set up the sender for requests
- this.udpRequestSender = new UdpClient();
- IPEndPoint requestGroupEP = new IPEndPoint(IPAddress.Broadcast, this.requestPort);
- this.udpRequestSender.Connect(requestGroupEP);
- // Set up the receiver for the requests
- // Listen for anyone looking for us
- this.udpRequestReceiver = new UdpClient();
- this.udpRequestReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- IPEndPoint requestGroupEP = new IPEndPoint(IPAddress.Any, this.requestPort);
- this.udpRequestReceiver.Client.Bind(requestGroupEP);
- this.udpRequestReceiver.BeginReceive(new AsyncCallback(AsyncRequestReceiveData), null);
- // Listen for the request
- this.OnRequestReceived += (message) => {
- Debug.Log("Request Received: " + message);
- // Do some more stuff when we get a request
- // Use `Network.maxConnections` for example
- };
- // Send out the request
- this.SendRequest();
- }
- void Update () {
- this.HandleTasks();
- }
- void HandleTasks() {
- while (tasks.Count > 0)
- {
- Action task = null;
- lock (tasks)
- {
- if (tasks.Count > 0)
- {
- task = tasks.Dequeue();
- }
- }
- task();
- }
- }
- public void QueueOnMainThread(Action task)
- {
- lock (tasks)
- {
- tasks.Enqueue(task);
- }
- }
- void SendRequest()
- {
- try
- {
- string message = "requestingServers";
- if (message != "")
- {
- Debug.Log("Sendering Request: " + message);
- this.udpRequestSender.Send(System.Text.Encoding.ASCII.GetBytes(message), message.Length);
- }
- }
- catch (ObjectDisposedException e)
- {
- Debug.LogWarning("Trying to send data on already disposed UdpCleint: " + e);
- return;
- }
- }
- void AsyncRequestReceiveData(IAsyncResult result)
- {
- IPEndPoint receiveIPGroup = new IPEndPoint(IPAddress.Any, this.requestPort);
- byte[] received;
- if (this.udpRequestReceiver != null) {
- received = this.udpRequestReceiver.EndReceive(result, ref receiveIPGroup);
- } else {
- return;
- }
- this.udpRequestReceiver.BeginReceive (new AsyncCallback(AsyncRequestReceiveData), null);
- string receivedString = System.Text.Encoding.ASCII.GetString(received);
- this.QueueOnMainThread(() => {
- // Fire the event
- this.ThisRequestReceived(receivedString);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement