View difference between Paste ID: Vh6nwJ1u and NEKV8hSQ
SHOW: | | - or go back to the newest paste.
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);
44+
		this.udpRequestReceiver = new UdpClient();
45
		this.udpRequestReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
46
		IPEndPoint requestGroupEP = new IPEndPoint(IPAddress.Any, this.requestPort);
47
		this.udpRequestReceiver.Client.Bind(requestGroupEP);
48
		this.udpRequestReceiver.BeginReceive(new AsyncCallback(AsyncRequestReceiveData), null);
49
		
50
		// Listen for the request
51
		this.OnRequestReceived += (message) => {
52
			Debug.Log("Request Received: " + message);
53
			// Do some more stuff when we get a request
54
			// Use `Network.maxConnections` for example
55
		};
56
		
57
		// Send out the request
58
		this.SendRequest();
59
60
	}
61
62
	void Update () {
63
		this.HandleTasks();
64
	}
65
66
	void HandleTasks() {
67
		while (tasks.Count > 0)
68
		{
69
			Action task = null;
70
			
71
			lock (tasks)
72
			{
73
				if (tasks.Count > 0)
74
				{
75
					task = tasks.Dequeue();
76
				}
77
			}
78
			
79
			task();
80
		}
81
	}
82
83
	public void QueueOnMainThread(Action task)
84
	{
85
		lock (tasks)
86
		{
87
			tasks.Enqueue(task);
88
		}
89
	}
90
91
	void SendRequest()
92
	{
93
		try
94
		{
95
			string message = "requestingServers";
96
			
97
			if (message != "") 
98
			{
99
				Debug.Log("Sendering Request: " + message);
100
				this.udpRequestSender.Send(System.Text.Encoding.ASCII.GetBytes(message), message.Length);
101
			}
102
		}
103
		catch (ObjectDisposedException e)
104
		{
105
			Debug.LogWarning("Trying to send data on already disposed UdpCleint: " + e);
106
			return;
107
		}
108
	}
109
	
110
	
111
	void AsyncRequestReceiveData(IAsyncResult result)
112
	{
113
		IPEndPoint receiveIPGroup = new IPEndPoint(IPAddress.Any, this.requestPort);
114
		byte[] received;
115
		if (this.udpRequestReceiver != null) {
116
			received = this.udpRequestReceiver.EndReceive(result, ref receiveIPGroup);
117
		} else {
118
			return;
119
		}
120
		this.udpRequestReceiver.BeginReceive (new AsyncCallback(AsyncRequestReceiveData), null);
121
		string receivedString = System.Text.Encoding.ASCII.GetString(received);
122
123
124
		this.QueueOnMainThread(() => {
125
			// Fire the event
126
			this.ThisRequestReceived(receivedString);
127
		});
128
		
129
	}
130
131
132
}