コード例 #1
0
        protected virtual void OnSocketAccepted(SocketAsyncEventArgs e)
        {
            Socket acceptSocket = e.AcceptSocket;

            if (e.SocketError != SocketError.Success)
            {
                lock (this._lockServer)
                {
                    if (acceptSocket != null)
                    {
                        if (acceptSocket.Connected)
                        {
                            acceptSocket.Shutdown(SocketShutdown.Both);
                            acceptSocket.Disconnect(false);
                        }
                        acceptSocket.Close();
                    }
                    e.AcceptSocket = null;
                    e.Completed   -= new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                    this._listenSocketArgs.Enqueue(e);
                    try
                    {
                        if (this.semListenQueue != null)
                        {
                            this.semListenQueue.Release();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                return;
            }
            this._tLastPacket = (long)Environment.TickCount;
            C c = default(C);

            lock (this._lockServer)
            {
                c = base.CreateConnection(acceptSocket);
                if (c != null)
                {
                    c._owner = this;
                    ServicesAPI.OnAccepted(acceptSocket);
                    Common.WriteLine(this._threadName + ": connection accepted: " + this._connected, new string[0]);
                }
                else
                {
                    Common.WriteLine(this._threadName + ": failed to craete connection: " + this._connected, new string[0]);
                }
            }
            if (e.BytesTransferred > 0 && acceptSocket.Connected)
            {
                this.OnSocketReceived(e);
            }
            lock (this._lockServer)
            {
                e.AcceptSocket = null;
                e.Completed   -= new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                this._listenSocketArgs.Enqueue(e);
                try
                {
                    if (this.semListenQueue != null)
                    {
                        this.semListenQueue.Release();
                    }
                }
                catch (Exception)
                {
                }
            }
            SocketAsyncEventArgs socketAsyncEventArgs = null;
            bool flag4 = true;

            lock (this._lockServer)
            {
                if (this._receiveSocketArgs.Count == 0)
                {
                    c = base.GetConnection(acceptSocket);
                    if (c != null)
                    {
                        base.RemoveConnection(acceptSocket);
                    }
                    ServicesAPI.OnDisconnected(acceptSocket);
                    lock (this._lockServer)
                    {
                        if (acceptSocket.Connected)
                        {
                            acceptSocket.Shutdown(SocketShutdown.Both);
                            acceptSocket.Disconnect(false);
                        }
                        acceptSocket.Close();
                    }
                    Common.WriteLine(this._threadName + ": connection limited to: " + this._connected, new string[0]);
                    return;
                }
                if (acceptSocket.Connected)
                {
                    socketAsyncEventArgs            = this._receiveSocketArgs.Dequeue();
                    socketAsyncEventArgs.UserToken  = acceptSocket;
                    socketAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                    flag4 = acceptSocket.ReceiveAsync(socketAsyncEventArgs);
                }
                else
                {
                    ServicesAPI.OnDisconnected(acceptSocket);
                    acceptSocket.Close();
                }
            }
            if (!flag4 && socketAsyncEventArgs != null)
            {
                this.OnSocketReceived(socketAsyncEventArgs);
            }
        }
コード例 #2
0
        protected override void OnSocketAccepted(SocketAsyncEventArgs e)
        {
            Socket acceptSocket = e.AcceptSocket;

            if (e.SocketError != SocketError.Success)
            {
                lock (this._lockServer)
                {
                    if (acceptSocket != null)
                    {
                        if (acceptSocket.Connected)
                        {
                            acceptSocket.Shutdown(SocketShutdown.Both);
                            acceptSocket.Disconnect(false);
                        }
                        acceptSocket.Close();
                    }
                    e.AcceptSocket = null;
                    e.Completed   -= new EventHandler <SocketAsyncEventArgs>(base.IO_Completed);
                    this._listenSocketArgs.Enqueue(e);
                    try
                    {
                        if (this.semListenQueue != null)
                        {
                            this.semListenQueue.Release();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                return;
            }
            this._tLastPacket = (long)Environment.TickCount;
            C c = default(C);

            lock (this._lockServer)
            {
                c = base.CreateConnection(acceptSocket);
                if (c != null)
                {
                    c._owner = this;
                    ServicesAPI.OnAccepted(acceptSocket);
                    Common.WriteLine(this._threadName + ": connection accepted: " + this._connected, new string[0]);
                }
                else
                {
                    Common.WriteLine(this._threadName + ": failed to craete connection: " + this._connected, new string[0]);
                }
            }
            lock (this._lockServer)
            {
                e.AcceptSocket = null;
                e.Completed   -= new EventHandler <SocketAsyncEventArgs>(base.IO_Completed);
                this._listenSocketArgs.Enqueue(e);
                try
                {
                    if (this.semListenQueue != null)
                    {
                        this.semListenQueue.Release();
                    }
                }
                catch (Exception)
                {
                }
            }
            lock (this._lockServer)
            {
                if (this._receiveSocketArgs.Count == 0)
                {
                    c = base.GetConnection(acceptSocket);
                    if (c != null)
                    {
                        base.RemoveConnection(acceptSocket);
                    }
                    ServicesAPI.OnDisconnected(acceptSocket);
                    lock (this._lockServer)
                    {
                        if (acceptSocket.Connected)
                        {
                            acceptSocket.Shutdown(SocketShutdown.Both);
                            acceptSocket.Disconnect(false);
                        }
                        acceptSocket.Close();
                    }
                    Common.WriteLine(this._threadName + ": connection limited to: " + this._connected, new string[0]);
                }
                else
                {
                    if (acceptSocket.Connected)
                    {
                        Common.WriteLine(this._threadName + ": SSL accepting...", new string[0]);
                        try
                        {
                            c._netStream = new NetworkStream(acceptSocket);
                            c._sslStream = new SslStream(c._netStream, false, new RemoteCertificateValidationCallback(ServerSSL <C, T> .ValidateServerCertificate), null);
                            c._sslStream.BeginAuthenticateAsServer(this._config.TlsCertificate, new AsyncCallback(this.OnSslConnected), c);
                            return;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception: {0}", ex.Message);
                            if (ex.InnerException != null)
                            {
                                Console.WriteLine("Inner exception: {0}", ex.InnerException.Message);
                            }
                            Console.WriteLine("Authentication failed - closing the connection.");
                            if (acceptSocket.Connected)
                            {
                                acceptSocket.Shutdown(SocketShutdown.Both);
                            }
                            acceptSocket.Close();
                            return;
                        }
                    }
                    ServicesAPI.OnDisconnected(acceptSocket);
                    acceptSocket.Close();
                }
            }
        }