void OnApplicationQuit() { if (Socket != null) { Socket.Close(); Socket = null; } }
void OnError(string Host, string Message) { //SetStatus("Error: " + Message ); Debug_Log(Host + " error: " + Message); OnDisconnected.Invoke(Host, 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 Host = CurrentHost; if (Host == null) { OnDisconnected.Invoke(null, "No hostname specified"); return; } Debug_Log("Connecting to " + Host + "..."); OnConnecting.Invoke(Host); // any failure from here should have a corresponding fail try { System.Action OnOpen = () => { QueueJob(() => { var HelloBytes = System.Text.Encoding.ASCII.GetBytes("hello"); Socket.Send(HelloBytes); Debug.Log("Connected"); SocketConnecting = false; //Socket = NewSocket; OnConnected.Invoke(Host); }); }; System.Action <string> OnClose = (Error) => { QueueJob(() => { SocketConnecting = false; OnError(Host, Error); }); }; System.Action <byte[]> OnPacket = (Packet) => { QueueJob(() => { OnBinaryMessage(Packet); }); }; string Hostname; int Port; SplitHostnameAndPort(Host, out Hostname, out Port); SocketConnecting = true; Socket = new PopX.UdpSocket(Hostname, Port, OnPacket, OnOpen, OnClose); } catch (System.Exception e) { SocketConnecting = false; OnError(Host, e.Message); } }