Ejemplo n.º 1
0
        private void OnClientConnection(IAsyncResult asyn)
        {
            if (m_Closed)
            {
                return;
            }

            try
            {
                Socket clientSocket = m_socket.EndAccept(asyn);

                RaiseClientConnected(clientSocket);
                
                ConnectedClient connectedClient = new ConnectedClient(clientSocket);

                connectedClient.MessageRecived += OnMessageRecived;
                connectedClient.Disconnected += OnClientDisconnection;

                connectedClient.StartListen();

                long key = clientSocket.Handle.ToInt64();
                if (m_clients.ContainsKey(key))
                {
                    Debug.Fail(string.Format(
                        "Client with handle key '{0}' already exist!", key));
                }

                m_clients[key] = connectedClient;

                // create the call back for any client connections...
                m_socket.BeginAccept(new AsyncCallback(OnClientConnection), null);

            }
            catch (ObjectDisposedException odex)
            {
                Debug.Fail(odex.ToString(),
                    "OnClientConnection: Socket has been closed");
            }
            catch (Exception sex)
            {
                Debug.Fail(sex.ToString(), 
                    "OnClientConnection: Socket failed");
            }

        }