public void Update() { try { if (client == null && pendingClient == null && port.IsOpen && port.BytesToRead > 0) { Logger.WriteLine( "SerialIO.Server[" + Address + "]: client requesting connection", Logger.Severity.Debug); pendingClient = new ByteClient(port); } } catch (IOException) { Stop(); } catch (TimeoutException) { Stop(); } catch (ObjectDisposedException) { Stop(); } if (client == null && pendingClient != null) { // Trigger OnClientRequestingConnection events to verify the connection var args = new ClientRequestingConnectionEventArgs <byte, byte> (pendingClient); EventHandlerExtensions.Invoke(OnClientRequestingConnection, this, args); // Deny the connection if (args.Request.ShouldDeny) { Logger.WriteLine( "SerialIO.Server[" + Address + "]: client connection denied", Logger.Severity.Debug); DisconnectClient(pendingClient, true); pendingClient = null; } // Allow the connection else if (args.Request.ShouldAllow) { client = pendingClient; pendingClient = null; Logger.WriteLine( "SerialIO.Server[" + Address + "]: " + "client connection accepted", Logger.Severity.Debug); EventHandlerExtensions.Invoke(OnClientConnected, this, new ClientConnectedEventArgs <byte, byte> (client)); } // Still pending, will either be denied or allowed on a subsequent called to Update else { Logger.WriteLine( "SerialIO.Server[" + Address + "]: " + "client connection still pending", Logger.Severity.Debug); } } else if (client != null && !client.Connected) { DisconnectClient(client); client = null; } }
void Close() { if (client != null) { DisconnectClient(client); client = null; } if (port != null) { port.Close(); port = null; } }
/// <summary> /// Called by RPCStream.Read when a client sends a connection request message, /// but another client is already connected. /// </summary> internal void ClientConnectionRequest(byte[] data) { if (client != null) { DisconnectClient(client); client = null; } if (pendingClient != null) { DisconnectClient(pendingClient, true); pendingClient = null; } Logger.WriteLine( "SerialIO.Server[" + Address + "]: " + "client requesting connection (overriding previous client connection)", Logger.Severity.Debug); pendingClient = new ByteClient(port, data); }