Exemple #1
0
        /// <summary>
        /// Used as IAsyncResult parser for ContinueReading().
        /// </summary>
        /// <param name="pIAR">The result AsyncCallback makes</param>
        private void EndReading(IAsyncResult pIAR)
        {
            int amountReceived = 0;
            try
            {
                amountReceived = _socket.EndReceive(pIAR);
            }
            catch (Exception ex)
            {
                Console.WriteLine(TypeName + " : " + ex.ToString());
                amountReceived = 0;
            }
            if (amountReceived == 0)
            {
                // We got a disWvsBeta.Common.Sessions here!
                OnDisconnectINTERNAL();
                return;
            }

            // Add amount of bytes received to _bufferpos so we know if we got everything.
            _bufferpos += amountReceived;

            try
            {

                // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period.
                if (_bufferpos == _bufferlen)
                {
                    // It seems we have all data we need
                    // Now check if we got a header
                    if (_header)
                    {
                        if (!_encryption && _receivingFromServer)
                        {
                            // Unencrypted packets have a short header with plain length.
                            ushort length = (ushort)(_buffer[0] | _buffer[1] << 8);
                            StartReading(length);
                        }
                        else
                        {
                            int length = GetHeaderLength(_buffer);
                            StartReading(length);
                        }
                    }
                    else
                    {
                        Packet packet;
                        if (_encryption)
                        {
                            _buffer = Decrypt(_buffer);
                            packet = new Packet(_buffer);

                            DoAction((date) =>
                            {
                                try
                                {
                                    OnPacketInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }
                        else
                        {
                            _encryption = true; // First packet received or sent is unencrypted. All others are.
                            packet = new Packet(_buffer);

                            _mapleVersion = packet.ReadUShort();
                            _maplePatchLocation = _maplePatchLocation = packet.ReadString();
                            _encryptIV = packet.ReadBytes(4);
                            _decryptIV = packet.ReadBytes(4);
                            _mapleLocale = packet.ReadByte();
                            Console.WriteLine(TypeName + " MapleVersion: {0}; Patch Location: {1}; Locale: {2}", _mapleVersion, _maplePatchLocation, _mapleLocale);


                            _rijndaelAES.Key = GMSKeys.GetKeyForVersion(_mapleVersion);
                            _rijndaelAES.Mode = CipherMode.ECB;
                            _rijndaelAES.Padding = PaddingMode.PKCS7;
                            _transformer = _rijndaelAES.CreateEncryptor();

                            packet.Reset();
                            DoAction((date) =>
                            {
                                try
                                {
                                    OnHandshakeInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }

                        StartReading(4, true);
                    }
                }
                else
                {
                    ContinueReading();
                }

            }
            catch (SocketException socketException)
            {
                Console.WriteLine(TypeName + " Socket Exception while receiving data: {0}", socketException.Message);
                OnDisconnectINTERNAL();
            }
            catch (Exception ex)
            {
                Console.WriteLine(TypeName + " [ERROR] EndReading(): {0}", ex.ToString());
                OnDisconnectINTERNAL();
            }
        }