Exemplo n.º 1
0
        private void OnInitPacketRecv(IAsyncResult ar)
        {
            if (!Connected)
            {
                return;
            }

            var data = (byte[])ar.AsyncState;
            var len  = Socket.EndReceive(ar);

            if (len < 15)
            {
                OnClientDisconnected?.Invoke(this);
                Connected = false;
                return;
            }

            PacketReader reader = new PacketReader(data);

            reader.ReadShort();
            reader.ReadShort();
            reader.ReadMapleString();

            SIV = new MapleCrypto(reader.ReadBytes(4), 227); // kms
            RIV = new MapleCrypto(reader.ReadBytes(4), 227); // kms
            // byte serverType = reader.ReadByte();

            if (Type == SessionType.CLIENT_TO_SERVER)
            {
                OnInitPacketReceived(227, 1);
            }

            WaitForData();
        }
Exemplo n.º 2
0
        private void EndReceive(IAsyncResult ar)
        {
            if (!Connected)
            {
                return;
            }

            var recvLen = 0;

            try
            {
                recvLen = Socket.EndReceive(ar);
            }

            catch
            {
                ForceDisconnect();
                return;
            }

            if (recvLen <= 0)
            {
                ForceDisconnect();
                return;
            }

            Append(_mSharedBuffer, 0, recvLen);

            while (true)
            {
                if (_mCursor < 4)
                {
                    break;
                }

                var packetSize = MapleCrypto.getPacketLength(_mBuffer, 0);

                if (_mCursor < packetSize + 4)
                {
                    break;
                }

                var packetBuffer = new byte[packetSize];
                Buffer.BlockCopy(_mBuffer, 4, packetBuffer, 0, packetSize);
                RIV.Transform(packetBuffer);
                _mCursor -= (packetSize + 4);

                if (_mCursor > 0)
                {
                    Buffer.BlockCopy(_mBuffer, packetSize + 4, _mBuffer, 0, _mCursor);
                }

                if (OnPacketReceived != null)
                {
                    if (!Connected)
                    {
                        return;
                    }
                    OnPacketReceived(packetBuffer);
                }
            }

            BeginReceive();
        }