void OnApplicationQuit() { if (Socket != null) { Socket.Close(); Socket = null; } }
void OnError(int Port, string Message) { //SetStatus("Error: " + Message ); Debug_Log(Port + " error: " + Message); OnDisconnected.Invoke(Port, Message); SocketConnecting = false; if (Socket != null) { // pop variable to avoid recursion var s = Socket; Socket = null; s.Close(); s = null; } }
// gr: change this to throw on immediate error void Connect() { // already connected if (Socket != null) { return; } if (SocketConnecting) { return; } var Port = CurrentPort; Debug_Log("Listening on " + Port + "..."); OnConnecting.Invoke(Port); // any failure from here should have a corresponding fail try { System.Action <int> OnOpen = (ListeningPort) => { QueueJob(() => { Debug.Log("Listening " + Port + " (actual " + ListeningPort + ")"); SocketConnecting = false; //Socket = NewSocket; OnListening.Invoke(ListeningPort); }); }; System.Action <string> OnClose = (Error) => { QueueJob(() => { SocketConnecting = false; OnError(Port, Error); }); }; /* * System.Action<byte[]> OnPacketReady = (Packet) => * { * QueueJob(() => * { * OnBinaryMessage(Packet); * }); * }; */ System.Action OnPacketReady = null; System.Action <float> OnKbpsReport = (Kbps) => { QueueJob(() => { this.OnKbpsReport(Kbps); }); }; SocketConnecting = true; if (UseAsyncServer) { Socket = new PopX.UdpServerSocket_Async(Port, OnPacketReady, OnOpen, OnClose, OnKbpsReport); } else { Socket = new PopX.UdpServerSocket_Threaded(Port, OnPacketReady, OnOpen, OnClose, OnKbpsReport); } } catch (System.Exception e) { SocketConnecting = false; OnError(Port, e.Message); } }