Ejemplo n.º 1
0
        private void AcceptAsyncCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket sock = null;

            try
            {
                sock = e.AcceptSocket;
                sock.SendBufferSize = BaseServer.SEND_BUFF_SIZE;
                BaseClient client = this.GetNewClient();
                try
                {
                    string ip = sock.Connected ? sock.RemoteEndPoint.ToString() : "socket disconnected";
                    BaseServer.log.Debug("Incoming connection from " + ip);

                    object syncRoot;
                    Monitor.Enter(syncRoot = this._clients.SyncRoot);
                    try
                    {
                        this._clients.Add(client, client);
                        client.Disconnected += new ClientEventHandle(this.client_Disconnected);
                    }
                    finally
                    {
                        Monitor.Exit(syncRoot);
                    }
                    client.Connect(sock);
                    client.ReceiveAsync();
                }
                catch (Exception ex)
                {
                    BaseServer.log.ErrorFormat("create client failed:{0}", ex);
                    client.Disconnect();
                }
            }
            catch
            {
                if (sock != null)
                {
                    try
                    {
                        sock.Close();
                    }
                    catch
                    {
                    }
                }
            }
            finally
            {
                e.Dispose();
                this.AcceptAsync();
            }
        }
Ejemplo n.º 2
0
        private void AcceptAsyncCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket connectedSocket = null;

            try
            {
                connectedSocket = e.AcceptSocket;
                connectedSocket.SendBufferSize = SEND_BUFF_SIZE;
                BaseClient newClient = this.GetNewClient();
                try
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info("Incoming connection from " + (connectedSocket.Connected ? connectedSocket.RemoteEndPoint.ToString() : "socket disconnected"));
                    }
                    lock (this._clients.SyncRoot)
                    {
                        this._clients.Add(newClient, newClient);
                        newClient.Disconnected += new ClientEventHandle(this.client_Disconnected);
                    }
                    newClient.Connect(connectedSocket);
                    newClient.ReceiveAsync();
                }
                catch (Exception exception)
                {
                    log.ErrorFormat("create client failed:{0}", exception);
                    newClient.Disconnect();
                }
            }
            catch
            {
                if (connectedSocket != null)
                {
                    try
                    {
                        connectedSocket.Close();
                    }
                    catch
                    {
                    }
                }
            }
            finally
            {
                e.Dispose();
                this.AcceptAsync();
            }
        }