void OnApplicationQuit() { if (Socket != null) { Socket.Close(); Socket = 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[]> OnPacket = (Packet) => { QueueJob(() => { OnBinaryMessage(Packet); }); }; SocketConnecting = true; Socket = new PopX.TcpServerSocket(Port, OnPacket, OnOpen, OnClose); } catch (System.Exception e) { SocketConnecting = false; OnError(Port, e.Message); } }
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; } }