Exemple #1
0
        void InitializeHandler(SocketAsyncEventArgs args)
        {
            if (args.SocketError != SocketError.Success)
            {
                CloseSocket();
                return;
            }

            if (args.BytesTransferred > 0)
            {
                if (_packetBuffer.GetRemainingSpace() > 0)
                {
                    // need to receive the header
                    int readHeaderSize = Math.Min(args.BytesTransferred, _packetBuffer.GetRemainingSpace());
                    _packetBuffer.Write(args.Buffer, 0, readHeaderSize);

                    if (_packetBuffer.GetRemainingSpace() > 0)
                    {
                        // Couldn't receive the whole header this time.
                        AsyncReadWithCallback(InitializeHandler);
                        return;
                    }

                    ByteBuffer buffer      = new ByteBuffer(_packetBuffer.GetData());
                    string     initializer = buffer.ReadString((uint)ClientConnectionInitialize.Length);
                    if (initializer != ClientConnectionInitialize)
                    {
                        CloseSocket();
                        return;
                    }

                    byte terminator = buffer.ReadUInt8();
                    if (terminator != '\n')
                    {
                        CloseSocket();
                        return;
                    }

                    // Initialize the zlib stream
                    _compressionStream = new ZLib.z_stream();

                    // Initialize the deflate algo...
                    var z_res1 = ZLib.deflateInit2(_compressionStream, 1, 8, -15, 8, 0);
                    if (z_res1 != 0)
                    {
                        CloseSocket();
                        Log.outError(LogFilter.Network, "Can't initialize packet compression (zlib: deflateInit2_) Error code: {0}", z_res1);
                        return;
                    }

                    _packetBuffer.Reset();
                    HandleSendAuthSession();
                    AsyncRead();
                    return;
                }
            }

            AsyncReadWithCallback(InitializeHandler);
        }
Exemple #2
0
        public void InitializeHandler(object sender, SocketAsyncEventArgs args)
        {
            args.Completed -= InitializeHandler;
            if (args.SocketError != SocketError.Success)
            {
                CloseSocket();
                return;
            }

            if (args.BytesTransferred > 0)
            {
                ByteBuffer connBuffer = new ByteBuffer(GetReceiveBuffer());

                string initializer = connBuffer.ReadString((uint)ClientConnectionInitialize.Length);
                if (initializer != ClientConnectionInitialize)
                {
                    CloseSocket();
                    return;
                }

                // Initialize the zlib stream
                _compressionStream = new ZLib.z_stream();

                // Initialize the deflate algo...
                var z_res1 = ZLib.deflateInit2(_compressionStream, 1, 8, -15, 8, 0);
                if (z_res1 != 0)
                {
                    CloseSocket();
                    Log.outError(LogFilter.Network, "Can't initialize packet compression (zlib: deflateInit2_) Error code: {0}", z_res1);
                    return;
                }

                HandleSendAuthSession();
                AsyncRead();
                return;
            }

            AsyncReadWithCallback(InitializeHandler);
        }