/// <summary>
        /// Called when [client connection].
        /// </summary>
        /// <param name="asyn">The asyn.</param>
        private void OnClientConnection(IAsyncResult asyn)
        {
            try
                {
                    // Create a new StateObject to hold the connected client
                    LogClient connectedClient = new LogClient()
                    {
                        ClientSocket = connectionSocket.EndAccept(asyn),
                        ClientId = !connectedClients.Any() ? 1 : connectedClients.Count + 1
                    };

                    IPEndPoint remote = connectedClient.ClientSocket.RemoteEndPoint as IPEndPoint;
                    if (connectedClients.ContainsKey(remote.Address.ToString()))
                    {
                        closeSocket(connectedClient.UniqueAddress, true);
                        return;
                    }
                    connectedClients.Add(remote.Address.ToString(), connectedClient);

                    // TODO: consider if we can instead do this at the beginning of the method.
                    // Check against limit
                    if (connectedClients.Count > connectionsLimit)
                    {
                        // No connection event is sent so close socket silently
                        closeSocket(connectedClient.UniqueAddress, true);
                        return;
                    }

                    // Dispatch Event
                    if (ClientConnected != null)
                    {
                        SocketConnectArgs args = new SocketConnectArgs()
                        {
                            ClientAddress = IPAddress.Parse(((IPEndPoint)connectedClient.ClientSocket.RemoteEndPoint).Address.ToString()),
                            ClientId = connectedClient.ClientId
                        };
                        ClientConnected(this, args);
                    }

                    // Release connectionSocket to keep listening if limit is not reached
                    connectionSocket.BeginAccept(new AsyncCallback(OnClientConnection), null);

                    // Allow connected client to receive data and designate a callback method
                    connectedClient.ClientSocket.BeginReceive(connectedClient.buffer, 0, LogClient.BufferSize, 0, new AsyncCallback(OnDataReceived), connectedClient);
                }
                catch (SocketException)
                {
                    // TODO: should we closeSocketSilent()? Or?
                }
                catch (ObjectDisposedException)
                {
                    // TODO: should we closeSocketSilent()? Or?
                }
        }
 /// <summary>
 /// Server_s the client connected.
 /// </summary>
 /// <param name="socketServer">The socket server.</param>
 /// <param name="e">The e.</param>
 void Server_ClientConnected(ILoggerServer socketServer, SocketConnectArgs e)
 {
     LogMessageHandler handler = new LogMessageHandler(ProcessLogMessage);
     ConnectedClients.Add(e.ClientAddress.ToString(),handler);
 }
Example #3
0
        /// <summary>
        /// Server_s the client connected.
        /// </summary>
        /// <param name="socketServer">The socket server.</param>
        /// <param name="e">The e.</param>
        void Server_ClientConnected(ILoggerServer socketServer, SocketConnectArgs e)
        {
            LogMessageHandler handler = new LogMessageHandler(ProcessLogMessage);

            ConnectedClients.Add(e.ClientAddress.ToString(), handler);
        }