private void LogSocketObject(BaseSocketObject socketObject, string message) { if (socketObject.Action != SocketConstants.SocketAction.Ping || ConfigValues.LogPings) Logger.LogDebug(message); }
/// <summary> Writes the Socket Object to the clients, if any are connected. Consumes all exceptions. </summary> private void Write(BaseSocketObject socketObject) { if (ClientsCount == 0) return; try { LogSocketObject(socketObject, string.Format("Server Socket - Writing Socket Object '{0}'.", socketObject)); var bytes = socketObject.GetBytes(); var sendBytes = BitConverter.GetBytes(bytes.Length).Concat(bytes).ToArray(); LogSocketObject(socketObject, string.Format("Server Socket - Writing {0} total bytes.", sendBytes.Length)); lock (clients) { for (var c = 0; c < clients.Count; c++) { var client = clients[c]; try { LogSocketObject(socketObject, string.Format("Server Socket - Writing to '{0}'.", client.Address)); client.Send(sendBytes); if (OnSocketEvent != null) { switch (socketObject.Action) { // Pings never raise up the event, otherwise we'd be flooded with events. case SocketConstants.SocketAction.Ping: break; default: OnSocketEvent(new ServerEvent(client, socketObject.Action)); break; } } } catch (Exception e) { if (e is SocketException && ((SocketException)e).SocketErrorCode != SocketError.ConnectionAborted) { Logger.LogError(string.Format("Server Socket - Client socket '{0}' has been closed already. Fully disconnecting client now.", client.Address), e); } else { Logger.LogError(string.Format("Server Socket - Failed to write Socket Object '{0}' to Client '{1}'. Disconnecting client.", socketObject, client.Address), e); } // Remove the element from the list and repeat the index, since the subsequent items would shift down. SafeCloseClient(client); clients.RemoveAt(c); if (client is ClientNetSocket) NetClientsCount--; else if (client is ClientWebSocket) WebClientsCount--; if (OnClientCountChanged != null) OnClientCountChanged(ClientsCount); c--; } } } LogSocketObject(socketObject, string.Format("Server Socket - Done writing Socket Object '{0}'.", socketObject)); } catch (Exception e) { Logger.LogError(string.Format("Server Socket - Failed to write Socket Object '{0}'.", socketObject), e); } }
/// <summary> Adds the Socket Object to the Write Queue and notifies the Write thread. </summary> private void EnqueueWrite(BaseSocketObject socketObject) { lock (this.writeQueue) { this.writeQueue.Enqueue(socketObject); this.writeQueueEvent.Set(); } }