Using the unity networking example, I’m working with UDP connections.
I found that the application hangs for a few seconds when I start a thread. At least it’s when I press a button to start a thread.
I’ve spent the day modifying the UDPConnectionGUI.cs script trying to find a work-around or something to hide the fact that there is a delay.
Is there a fix or something else I can do to resolve this hanging?
Let me post my revised script below…
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPConnectionGUI : MonoBehaviour {
private UdpClient server;
private UdpClient client;
private IPEndPoint receivePoint;
private string port = "6767";
private int listenPort = 25001;
private string ip = "0.0.0.0";
private string ip_broadcast = "255.255.255.255";
private bool startedClient = false;
private bool startedServer = false;
private string server_name = "";
private ArrayList list = new ArrayList();
private ArrayList listTest = new ArrayList();
private GUIText Wait;
private float clearedTimer = 0;
private float clearedTimerPause = 2;
private bool clientListRunning = false;
public void Update() {
if(Time.time > clearedTimer)
list = new ArrayList();
}
public void Start() {
if(this.enabled == false) return;
clearedTimer = (float) (Time.time + clearedTimerPause);
LoadClient();
}
public void LoadClient() {
if(startedServer) server.Close();
startedServer = false;
client = new UdpClient(System.Convert.ToInt32(port));
receivePoint = new IPEndPoint(IPAddress.Parse(ip),System.Convert.ToInt32(port));
Thread startClient = new Thread(new ThreadStart(start_client));
startClient.Start();
startedClient = true;
}
public void start_client() {
try {
while(true) {
clientListRunning = true;
listTest = new ArrayList();
int loopCount = 0;
while(loopCount++<10) {
byte[] recData = client.Receive(ref receivePoint);
System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
server_name = encode.GetString(recData);
if(!listTest.Contains(server_name)) {
listTest.Add(server_name);
}
if(!list.Contains(server_name)) {
list.Add(server_name);
}
}
foreach(string server_name in list) {
if(!listTest.Contains(server_name)) {
list.Remove(server_name);
}
}
Thread.Sleep(100);
clearedTimer = (float) (Time.time + clearedTimerPause);
clientListRunning = false;
}
} catch {}
}
public void start_server() {
try {
while(true) {
System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
byte[] sendData = encode.GetBytes(Network.player.ipAddress.ToString());
server.Send(sendData,sendData.Length,ip_broadcast,System.Convert.ToInt32(port));
Thread.Sleep(100);
}
} catch {}
}
void disconnectServer(bool shutDown) {
Time.timeScale = 1;
if(startedServer) server.Close();
if(startedClient) client.Close();
Network.Disconnect();
MasterServer.UnregisterHost();
LoadClient();
if(shutDown) {
if(startedClient) client.Close();
PlayerPrefs.SetString("LevelName", "MainMenu");
Application.LoadLevel("LinkScene");
Destroy(gameObject);
}
}
void OnGUI() {
if(this.enabled == false) return;
if(Network.peerType == NetworkPeerType.Disconnected)
{
if (GUI.Button(new Rect(200,50,90,25),"Start"))
{
Network.InitializeServer(4, listenPort);
string ipaddress = Network.player.ipAddress.ToString();
ip = ipaddress;
client.Close();
startedClient = false;
server = new UdpClient(System.Convert.ToInt32(port));
receivePoint = new IPEndPoint(IPAddress.Parse(ipaddress),System.Convert.ToInt32(port));
Thread startServer = new Thread(new ThreadStart(start_server));
startServer.Start();
startedServer = true;
}
if (GUI.Button(new Rect(300,50,60,25),"Quit"))
{
disconnectServer(true);
}
int _cnt = 0;
foreach(string serverIP in list) {
if(GUI.Button(new Rect(160,(_cnt*45)+110,200,40), serverIP)) {
Wait = (GUIText) GameObject.Find("Wait").GetComponent(typeof(GUIText));
Wait.enabled = true;
client.Close();
startedClient = false;
Network.Connect(serverIP, listenPort);
}
_cnt++;
}
}
}
}