// server callbacks //////////////////////////////////////////////////// void OnLibuvServerConnected(TcpStream handle, Exception error) { // setup callbacks handle.onMessage = OnLibuvServerMessage; handle.onError = OnLibuvServerError; handle.onClosed = OnLibuvServerClosed; // close if errors (AFTER setting up onClosed callback!) if (error != null) { Debug.Log($"libuv sv: client connection failed {error}"); handle.CloseHandle(); return; } // assign a connectionId via UserToken. // this is better than using handle.InternalHandle.ToInt32() because // the InternalHandle isn't available in OnLibuvClosed anymore. handle.UserToken = nextConnectionId++; connections[(int)handle.UserToken] = handle; Debug.Log("libuv sv: client connected with connectionId=" + (int)handle.UserToken); // dotsnet event OnServerConnected.Invoke((int)handle.UserToken); }
void OnLibuvServerError(TcpStream handle, Exception error) { Debug.Log($"libuv sv: error {error}"); connections.Remove((int)handle.UserToken); handle.CloseHandle(); // TODO invoke OnDisconnected or does OnLibuvClosed get called anyway? }
void OnLibuvClientError(TcpStream handle, Exception error) { Debug.Log($"libuv cl: read error {error}"); handle.CloseHandle(); // Mirror event OnClientDisconnected.Invoke(); }
// client callbacks //////////////////////////////////////////////////// void OnLibuvClientConnected(TcpStream handle, Exception exception) { // setup callbacks handle.onMessage = OnLibuvClientMessage; handle.onError = OnLibuvClientError; handle.onClosed = OnLibuvClientClosed; // close if errors (AFTER setting up onClosed callback!) if (exception != null) { Debug.Log($"libuv cl: client error {exception}"); handle.CloseHandle(); return; } // Mirror event OnClientConnected.Invoke(); Debug.Log($"libuv cl: client connected."); }
public override void ClientDisconnect() { client?.CloseHandle(); client = null; }