Example #1
0
        /// <summary>
        /// Reads the rest of the incoming header.
        /// </summary>
        private void ReadHeaderCallback(IAsyncResult result)
        {
            //if (ReceiveData.Length != 4 && ReceiveData.Length != 5)
            //  throw new Exception("ReceiveData.Length not in order");

            int bytesRead = this.connection.Client.EndReceive(result);

            if (bytesRead == 0 && result.IsCompleted)
            {
                // TODO: world server disconnect
                Game.UI.LogLine("Server has closed the connection");
                Game.Exit();
                return;
            }

            Interlocked.Add(ref transferred, bytesRead);
            Interlocked.Add(ref received, bytesRead);

            if (bytesRead == Remaining)
            {
                // finished reading header
                // the first byte was decrypted already, so skip it
                AuthenticationCrypto.Decrypt(ReceiveData, 1, ReceiveData.Length - 1);
                ServerHeader header = new ServerHeader(ReceiveData);

                Game.UI.LogLine(header.ToString(), LogLevel.Debug);
                if (header.InputDataLength > 5 || header.InputDataLength < 4)
                {
                    Game.UI.LogException(String.Format("Header.InputataLength invalid: {0}", header.InputDataLength));
                }

                if (header.Size > 0)
                {
                    // read the packet payload
                    Index       = 0;
                    Remaining   = header.Size;
                    ReceiveData = new byte[header.Size];
                    BeginRead(new AsyncCallback(ReadPayloadCallback), header);
                }
                else
                {
                    // the packet is just a header, start next packet
                    HandlePacket(new InPacket(header));
                    Start();
                }
            }
            else
            {
                // more header to read
                Index     += bytesRead;
                Remaining -= bytesRead;
                BeginRead(new AsyncCallback(ReadHeaderCallback));
            }
        }
Example #2
0
        /// <summary>
        /// Reads the rest of the incoming header.
        /// </summary>
        private void ReadHeaderCallback(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                int bytesRead = e.BytesTransferred;
                if (bytesRead == 0)
                {
                    // TODO: world server disconnect
                    Game.UI.LogLine("Server has closed the connection");
                    Game.Reconnect();
                    return;
                }

                Interlocked.Add(ref transferred, bytesRead);
                Interlocked.Add(ref received, bytesRead);

                if (bytesRead == Remaining)
                {
                    // finished reading header
                    // the first byte was decrypted already, so skip it
                    authenticationCrypto.Decrypt(ReceiveData, 1, ReceiveDataLength - 1);
                    ServerHeader header = new ServerHeader(ReceiveData, ReceiveDataLength);

                    Game.UI.LogDebug(header.ToString());
                    if (header.InputDataLength > 5 || header.InputDataLength < 4)
                    {
                        Game.UI.LogException(String.Format("Header.InputDataLength invalid: {0}", header.InputDataLength));
                    }

                    if (header.Size > 0)
                    {
                        // read the packet payload
                        Index     = 0;
                        Remaining = header.Size;
                        ReserveData(header.Size);
                        ReadAsync(ReadPayloadCallback, header);
                    }
                    else
                    {
                        // the packet is just a header, start next packet
                        QueuePacket(new InPacket(header));
                        Start();
                    }
                }
                else
                {
                    // more header to read
                    Index     += bytesRead;
                    Remaining -= bytesRead;
                    ReadAsync(ReadHeaderCallback);
                }
            }
            // these exceptions can happen as race condition on shutdown
            catch (ObjectDisposedException ex)
            {
                Game.UI.LogException(ex);
            }
            catch (NullReferenceException ex)
            {
                Game.UI.LogException(ex);
            }
            catch (SocketException ex)
            {
                Game.UI.LogException(ex);
            }
        }
Example #3
0
        /// <summary>
        /// Reads the rest of the incoming header.
        /// </summary>
        private void ReadHeaderCallback(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                int bytesRead = e.BytesTransferred;
                if (bytesRead == 0)
                {
                    // TODO: world server disconnect
                    Game.UI.LogLine("Server has closed the connection");
                    Game.Reconnect();
                    return;
                }

                Interlocked.Add(ref transferred, bytesRead);
                Interlocked.Add(ref received, bytesRead);

                if (bytesRead == Remaining)
                {
                    // finished reading header
                    // the first byte was decrypted already, so skip it
                    authenticationCrypto.Decrypt(ReceiveData, 1, ReceiveDataLength - 1);
                    ServerHeader header = new ServerHeader(ReceiveData, ReceiveDataLength);

                    Game.UI.LogDebug(header.ToString());
                    if (header.InputDataLength > 5 || header.InputDataLength < 4)
                        Game.UI.LogException(String.Format("Header.InputDataLength invalid: {0}", header.InputDataLength));

                    if (header.Size > 0)
                    {
                        // read the packet payload
                        Index = 0;
                        Remaining = header.Size;
                        ReserveData(header.Size);
                        ReadAsync(ReadPayloadCallback, header);
                    }
                    else
                    {
                        // the packet is just a header, start next packet
                        QueuePacket(new InPacket(header));
                        Start();
                    }
                }
                else
                {
                    // more header to read
                    Index += bytesRead;
                    Remaining -= bytesRead;
                    ReadAsync(ReadHeaderCallback);
                }
            }
            // these exceptions can happen as race condition on shutdown
            catch (ObjectDisposedException ex)
            {
                Game.UI.LogException(ex);
            }
            catch (NullReferenceException ex)
            {
                Game.UI.LogException(ex);
            }
            catch (SocketException ex)
            {
                Game.UI.LogException(ex);
            }
        }
Example #4
0
        /// <summary>
        /// Reads the rest of the incoming header.
        /// </summary>
        private void ReadHeaderCallback(IAsyncResult result)
        {
            //if (ReceiveData.Length != 4 && ReceiveData.Length != 5)
              //  throw new Exception("ReceiveData.Length not in order");

            int bytesRead = this.connection.Client.EndReceive(result);
            if (bytesRead == 0 && result.IsCompleted)
            {
                // TODO: world server disconnect
                Game.UI.LogLine("Server has closed the connection");
                Game.Exit();
                return;
            }

            Interlocked.Add(ref transferred, bytesRead);
            Interlocked.Add(ref received, bytesRead);

            if (bytesRead == Remaining)
            {
                // finished reading header
                // the first byte was decrypted already, so skip it
                AuthenticationCrypto.Decrypt(ReceiveData, 1, ReceiveData.Length - 1);
                ServerHeader header = new ServerHeader(ReceiveData);

                Game.UI.LogLine(header.ToString(), LogLevel.Debug);
                if (header.InputDataLength > 5 || header.InputDataLength < 4)
                    Game.UI.LogException(String.Format("Header.InputataLength invalid: {0}", header.InputDataLength));

                if (header.Size > 0)
                {
                    // read the packet payload
                    Index = 0;
                    Remaining = header.Size;
                    ReceiveData = new byte[header.Size];
                    BeginRead(new AsyncCallback(ReadPayloadCallback), header);
                }
                else
                {
                    // the packet is just a header, start next packet
                    HandlePacket(new InPacket(header));
                    Start();
                }
            }
            else
            {
                // more header to read
                Index += bytesRead;
                Remaining -= bytesRead;
                BeginRead(new AsyncCallback(ReadHeaderCallback));
            }
        }