Exemplo n.º 1
0
        protected void OnSocketSent(SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                lock (this._lockServer)
                {
                    ServicesAPI.OnDisconnected(e.AcceptSocket);
                    if (e.AcceptSocket.Connected)
                    {
                        e.AcceptSocket.Shutdown(SocketShutdown.Both);
                        e.AcceptSocket.Disconnect(false);
                    }
                    e.AcceptSocket.Close();
                    Common.WriteLine(this._threadName + ": send error: " + this._connected, new string[0]);
                    goto IL_11A;
                }
            }
            this._speedMeter.Sent(e.BytesTransferred);
            Socket acceptSocket = e.AcceptSocket;
            C      connection   = base.GetConnection(acceptSocket);

            if (connection != null)
            {
                ulong num = BitConverter.ToUInt64(e.Buffer, 0);
                num = ecoServerProtocol.swap64(num);
                uint packetType = ecoServerProtocol.getPacketType(num);
                if (packetType == 32u)
                {
                    try
                    {
                        Thread.Sleep(200);
                    }
                    catch (Exception)
                    {
                    }
                    if (acceptSocket.Connected)
                    {
                        acceptSocket.Shutdown(SocketShutdown.Both);
                        acceptSocket.Disconnect(false);
                    }
                    acceptSocket.Close();
                    e.Dispose();
                    return;
                }
                this.IsDispatch(connection);
                this.OnBaseSent(connection, e);
            }
IL_11A:
            e.Dispose();
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        private void OnSocketReceived(SocketAsyncEventArgs e)
        {
            bool   flag   = false;
            bool   flag2  = true;
            Socket socket = e.AcceptSocket;

            if (e.LastOperation == SocketAsyncOperation.Receive)
            {
                socket = (e.UserToken as Socket);
            }
            C connection = base.GetConnection(socket);

            lock (this._lockServer)
            {
                if (e.SocketError != SocketError.Success || e.BytesTransferred == 0 || !socket.Connected)
                {
                    if (connection != null)
                    {
                        base.RemoveConnection(socket);
                    }
                    ServicesAPI.OnDisconnected(socket);
                    if (e.LastOperation == SocketAsyncOperation.Receive)
                    {
                        if (socket.Connected)
                        {
                            socket.Shutdown(SocketShutdown.Both);
                            socket.Disconnect(false);
                        }
                        socket.Close();
                        e.AcceptSocket = null;
                        e.UserToken    = null;
                        e.Completed   -= new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                        this._receiveSocketArgs.Enqueue(e);
                    }
                    Common.WriteLine(this._threadName + ": connection down: " + this._connected, new string[0]);
                    return;
                }
                this._speedMeter.Received(e.BytesTransferred);
                if (connection != null)
                {
                    BufferState bufferState = new BufferState();
                    bufferState._ctx    = connection;
                    bufferState._buffer = e.Buffer;
                    if (bufferState._buffer != null)
                    {
                        Interlocked.Increment(ref this._totalRequests);
                        SessionAPI.Update(connection._uid);
                        int           num  = 0;
                        List <byte[]> list = connection.ReceivedBuffer(bufferState, e.BytesTransferred, ref num);
                        if (list != null)
                        {
                            flag2 = false;
                            foreach (byte[] current in list)
                            {
                                if (!socket.Connected)
                                {
                                    break;
                                }
                                this.StartSend(connection, current);
                            }
                        }
                    }
                }
            }
            if (!flag2)
            {
                if (e.LastOperation == SocketAsyncOperation.Receive)
                {
                    flag = true;
                    lock (this._lockServer)
                    {
                        if (socket.Connected)
                        {
                            flag = socket.ReceiveAsync(e);
                        }
                        else
                        {
                            ServicesAPI.OnDisconnected(socket);
                            if (connection != null)
                            {
                                base.RemoveConnection(socket);
                            }
                            e.AcceptSocket = null;
                            e.UserToken    = null;
                            e.Completed   -= new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                            this._receiveSocketArgs.Enqueue(e);
                        }
                    }
                    if (!flag)
                    {
                        this.OnSocketReceived(e);
                    }
                }
                return;
            }
            if (connection != null)
            {
                base.RemoveConnection(socket);
            }
            ServicesAPI.OnDisconnected(socket);
            lock (this._lockServer)
            {
                if (socket.Connected)
                {
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Disconnect(false);
                }
                socket.Close();
            }
            if (e.LastOperation == SocketAsyncOperation.Receive)
            {
                e.AcceptSocket = null;
                e.UserToken    = null;
                e.Completed   -= new EventHandler <SocketAsyncEventArgs>(this.IO_Completed);
                this._receiveSocketArgs.Enqueue(e);
            }
            Common.WriteLine("Receive Processing Error", new string[0]);
        }
Exemplo n.º 4
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();
                }
            }
        }