Guest User

Untitled

a guest
May 11th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Collections.Generic;
  7.  
  8. public class TestUDP : MonoBehaviour {
  9.    
  10.    
  11.     public delegate void RequestReceivedEventHandler(string message);
  12.     public event RequestReceivedEventHandler OnRequestReceived;
  13.    
  14.     // Use this to trigger the event
  15.     protected virtual void ThisRequestReceived(string message)
  16.     {
  17.         RequestReceivedEventHandler handler = OnRequestReceived;
  18.         if(handler != null)
  19.         {
  20.             handler(message);
  21.         }
  22.     }
  23.  
  24.     // We use this to keep tasks needed to run in the main thread
  25.     private static readonly Queue<Action> tasks = new Queue<Action>();
  26.    
  27.     public int requestPort = 55795;
  28.    
  29.     UdpClient udpRequestSender;
  30.     UdpClient udpRequestReceiver;
  31.    
  32.    
  33.     // Use this for initialization
  34.     void Start () {
  35.        
  36.         // Set up the sender for requests
  37.         this.udpRequestSender = new UdpClient();
  38.         IPEndPoint requestGroupEP = new IPEndPoint(IPAddress.Broadcast, this.requestPort);
  39.         this.udpRequestSender.Connect(requestGroupEP);
  40.        
  41.        
  42.         // Set up the receiver for the requests
  43.         // Listen for anyone looking for us
  44.         this.udpRequestReceiver = new UdpClient(this.requestPort);
  45.         this.udpRequestReceiver.BeginReceive(new AsyncCallback(AsyncRequestReceiveData), null);
  46.        
  47.         // Listen for the request
  48.         this.OnRequestReceived += (message) => {
  49.             Debug.Log("Request Received: " + message);
  50.             // Do some more stuff when we get a request
  51.             // Use `Network.maxConnections` for example
  52.         };
  53.        
  54.         // Send out the request
  55.         this.SendRequest();
  56.  
  57.     }
  58.  
  59.     void Update () {
  60.         this.HandleTasks();
  61.     }
  62.  
  63.     void HandleTasks() {
  64.         while (tasks.Count > 0)
  65.         {
  66.             Action task = null;
  67.            
  68.             lock (tasks)
  69.             {
  70.                 if (tasks.Count > 0)
  71.                 {
  72.                     task = tasks.Dequeue();
  73.                 }
  74.             }
  75.            
  76.             task();
  77.         }
  78.     }
  79.  
  80.     public void QueueOnMainThread(Action task)
  81.     {
  82.         lock (tasks)
  83.         {
  84.             tasks.Enqueue(task);
  85.         }
  86.     }
  87.  
  88.     void SendRequest()
  89.     {
  90.         try
  91.         {
  92.             string message = "requestingServers";
  93.            
  94.             if (message != "")
  95.             {
  96.                 Debug.Log("Sendering Request: " + message);
  97.                 this.udpRequestSender.Send(System.Text.Encoding.ASCII.GetBytes(message), message.Length);
  98.             }
  99.         }
  100.         catch (ObjectDisposedException e)
  101.         {
  102.             Debug.LogWarning("Trying to send data on already disposed UdpCleint: " + e);
  103.             return;
  104.         }
  105.     }
  106.    
  107.    
  108.     void AsyncRequestReceiveData(IAsyncResult result)
  109.     {
  110.         IPEndPoint receiveIPGroup = new IPEndPoint(IPAddress.Any, this.requestPort);
  111.         byte[] received;
  112.         if (this.udpRequestReceiver != null) {
  113.             received = this.udpRequestReceiver.EndReceive(result, ref receiveIPGroup);
  114.         } else {
  115.             return;
  116.         }
  117.         this.udpRequestReceiver.BeginReceive (new AsyncCallback(AsyncRequestReceiveData), null);
  118.         string receivedString = System.Text.Encoding.ASCII.GetString(received);
  119.  
  120.  
  121.         this.QueueOnMainThread(() => {
  122.             // Fire the event
  123.             this.ThisRequestReceived(receivedString);
  124.         });
  125.        
  126.     }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment