/// <summary> /// Registers a new connection to the server. /// </summary> /// <param name="connection">The new connection.</param> protected void RegisterConnection(NetworkServerConnection connection) { Action <NetworkServerConnection> handler = RegisteredConnection; if (handler != null) { handler.Invoke(connection); } else { Logger.Error("A connection was registered by the network listener while no hooks were subscribed to handle the registration. The connection has been dropped. This suggests the network listener is erroneously accepting connections before the StartListening() method has been called."); connection.Disconnect(); } }
/// <summary> /// Disconnects the connection without calling back to the server manager. /// </summary> internal bool DropConnection() { return(connection.Disconnect()); }
/// <summary> /// Called when a new client connects. /// </summary> /// <param name="connection">The new client.</param> internal void HandleNewConnection(NetworkServerConnection connection) { //Allocate ID and add to list ushort id; try { id = ReserveID(); } catch (InvalidOperationException) { logger.Info($"New client could not be connected as there were no IDs available to allocate to them [{connection.RemoteEndPoints.Format()}]."); connection.Disconnect(); return; } Client client; try { client = Client.Create( connection, id, this, threadHelper, clientLogger #if PRO , clientMetricsCollector #endif ); } catch (Exception e) { logger.Error("An exception ocurred while connecting a client. The client has been dropped.", e); connection.Disconnect(); DeallocateID(id, out int _); return; } AllocateIDToClient(id, client, out int noClients); // TODO if a client sends immediately after connecting then the message will be missed as the Connected event has not yet fired connection.Client = client; logger.Info($"New client [{client.ID}] connected [{client.RemoteEndPoints.Format()}]."); #if PRO clientsConnectedGauge.Report(noClients); #endif //Inform plugins of the new connection EventHandler <ClientConnectedEventArgs> handler = ClientConnected; if (handler != null) { threadHelper.DispatchIfNeeded( delegate() { #if PRO long startTimestamp = Stopwatch.GetTimestamp(); #endif try { handler.Invoke(this, new ClientConnectedEventArgs(client)); } catch (Exception e) { logger.Error("A plugin encountered an error whilst handling the ClientConnected event. The client will be disconnected. (See logs for exception)", e); client.DropConnection(); #if PRO clientConnectedEventFailuresCounter.Increment(); #endif return; } #if PRO double time = (double)(Stopwatch.GetTimestamp() - startTimestamp) / Stopwatch.Frequency; clientConnectedEventTimeHistogram.Report(time); #endif }, (_) => client.StartListening() ); } }