Пример #1
0
        private void AcceptAsyncCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket socket = null;

            try
            {
                socket = e.AcceptSocket;
                socket.SendBufferSize = BaseServer.SEND_BUFF_SIZE;
                BaseClient newClient = this.GetNewClient();
                try
                {
                    if (BaseServer.log.IsInfoEnabled)
                    {
                        string str = socket.Connected ? socket.RemoteEndPoint.ToString() : "socket disconnected";
                        BaseServer.log.Info("Incoming connection from " + str);
                    }
                    object syncRoot;
                    Monitor.Enter(syncRoot = this._clients.SyncRoot);
                    try
                    {
                        this._clients.Add(newClient, newClient);
                        newClient.Disconnected += new ClientEventHandle(this.client_Disconnected);
                    }
                    finally
                    {
                        Monitor.Exit(syncRoot);
                    }
                    newClient.Connect(socket);
                    newClient.ReceiveAsync();
                }
                catch (Exception arg)
                {
                    BaseServer.log.ErrorFormat("create client failed:{0}", arg);
                    newClient.Disconnect();
                }
            }
            catch
            {
                if (socket != null)
                {
                    try
                    {
                        socket.Close();
                    }
                    catch
                    {
                    }
                }
            }
            finally
            {
                e.Dispose();
                this.AcceptAsync();
            }
        }
Пример #2
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();
            }
        }
Пример #3
0
        /// <summary>
        /// Accepts complete event's callback funciton.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AcceptAsyncCompleted(object sender, SocketAsyncEventArgs e)
        {
            Socket sock = null;

            try
            {
                sock = e.AcceptSocket;
                sock.SendBufferSize = SEND_BUFF_SIZE;

                BaseClient client = GetNewClient();

                //TrieuLSL

                try
                {
                    if (log.IsInfoEnabled)
                    {
                        string ip = sock.Connected ? sock.RemoteEndPoint.ToString() : "socket disconnected";
                        log.Info("Incoming connection from " + ip);
                    }

                    lock (_clients.SyncRoot)
                    {
                        _clients.Add(client, client);//Add the client instance to a hy dictionary.
                        client.Disconnected += client_Disconnected;
                    }

                    client.Connect(sock);
                    client.ReceiveAsync();
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("create client failed:{0}", ex);
                    client.Disconnect();
                }
            }
            catch
            {
                if (sock != null) // don't leave the socket open on exception
                {
                    try { sock.Close(); }
                    catch { }
                }
            }
            finally
            {
                e.Dispose();
                AcceptAsync();
            }
        }
Пример #4
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();
            }
        }