Example #1
0
 private void EndReceive(IAsyncResult ar)
 {
     if (this.Connected)
     {
         int pLength = 0;
         try
         {
             pLength = this._socket.EndReceive(ar);
         }
         catch
         {
             this.ForceDisconnect();
             return;
         }
         if (pLength <= 0)
         {
             this.ForceDisconnect();
         }
         else
         {
             this.Append(this.mSharedBuffer, 0, pLength);
             while (true)
             {
                 if (this.mCursor < 4)
                 {
                     break;
                 }
                 ushort count = MapleCrypto.getPacketLength(this.mBuffer, 0);
                 if (this.mCursor < (count + 4))
                 {
                     break;
                 }
                 byte[] dst = new byte[count];
                 Buffer.BlockCopy(this.mBuffer, 4, dst, 0, count);
                 this.RIV.Decrypt(dst);
                 this.mCursor -= count + 4;
                 if (this.mCursor > 0)
                 {
                     Buffer.BlockCopy(this.mBuffer, count + 4, this.mBuffer, 0, this.mCursor);
                 }
                 if (this.OnPacketReceived != null)
                 {
                     if (!this.Connected)
                     {
                         return;
                     }
                     this.OnPacketReceived(dst);
                 }
             }
             this.BeginReceive();
         }
     }
 }
Example #2
0
        private void EndReceive(IAsyncResult ar)
        {
            if (!Connected)
            {
                return;
            }
            int 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;
                }

                ushort packetSize = MapleCrypto.getPacketLength(mBuffer, 0);
                if (mCursor < (packetSize + 4))
                {
                    break;
                }
                byte[] packetBuffer = new byte[packetSize];
                Buffer.BlockCopy(mBuffer, 4, packetBuffer, 0, packetSize);
                RIV.Decrypt(packetBuffer);
                mCursor -= (packetSize + 4);
                if (mCursor > 0)
                {
                    Buffer.BlockCopy(mBuffer, packetSize + 4, mBuffer, 0, mCursor);
                }
                if (OnPacketReceived != null)
                {
                    if (!Connected)
                    {
                        return;
                    }
                    OnPacketReceived(packetBuffer);
                }
            }
            BeginReceive();
        }
Example #3
0
        /// <summary>
        /// Data received event handler
        /// </summary>
        /// <param name="iar">IAsyncResult of the data received event</param>
        private void OnDataReceived(IAsyncResult iar)
        {
            SocketInfo socketInfo = (SocketInfo)iar.AsyncState;

            try
            {
                int received = socketInfo.Socket.EndReceive(iar);
                if (received == 0)
                {
                    if (OnClientDisconnected != null)
                    {
                        OnClientDisconnected(this);
                    }
                    return;
                }

                socketInfo.Index += received;

                if (socketInfo.Index == socketInfo.DataBuffer.Length)
                {
                    switch (socketInfo.State)
                    {
                    case SocketInfo.StateEnum.Header:
                        if (socketInfo.NoEncryption)
                        {
                            PacketReader headerReader = new PacketReader(socketInfo.DataBuffer);
                            short        packetHeader = headerReader.ReadShort();
                            socketInfo.State      = SocketInfo.StateEnum.Content;
                            socketInfo.DataBuffer = new byte[packetHeader];
                            socketInfo.Index      = 0;
                            WaitForData(socketInfo);
                        }
                        else
                        {
                            PacketReader headerReader  = new PacketReader(socketInfo.DataBuffer);
                            byte[]       packetHeaderB = headerReader.ToArray();
                            int          packetHeader  = headerReader.ReadInt();
                            short        packetLength  = (short)MapleCrypto.getPacketLength(packetHeader);
                            if (_type == SessionType.SERVER_TO_CLIENT && !_RIV.checkPacketToServer(BitConverter.GetBytes(packetHeader)))
                            {
                                Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Packet check failed. Disconnecting client.");
                                //this.Socket.Close();
                            }
                            socketInfo.State      = SocketInfo.StateEnum.Content;
                            socketInfo.DataBuffer = new byte[packetLength];
                            socketInfo.Index      = 0;
                            WaitForData(socketInfo);
                        }
                        break;

                    case SocketInfo.StateEnum.Content:
                        byte[] data = socketInfo.DataBuffer;
                        if (socketInfo.NoEncryption)
                        {
                            socketInfo.NoEncryption = false;
                            PacketReader reader  = new PacketReader(data);
                            short        version = reader.ReadShort();
                            string       unknown = reader.ReadMapleString();
                            _SIV = new MapleCrypto(reader.ReadBytes(4), version);
                            _RIV = new MapleCrypto(reader.ReadBytes(4), version);
                            byte serverType = reader.ReadByte();
                            if (_type == SessionType.CLIENT_TO_SERVER)
                            {
                                OnInitPacketReceived(version, serverType);
                            }
                            OnPacketReceived(new PacketReader(data), true);
                            WaitForData();
                        }
                        else
                        {
                            _RIV.crypt(data);
                            MapleCustomEncryption.Decrypt(data);
                            if (data.Length != 0 && OnPacketReceived != null)
                            {
                                OnPacketReceived(new PacketReader(data), false);
                            }
                            WaitForData();
                        }
                        break;
                    }
                }
                else
                {
                    Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Warning] Not enough data");
                    WaitForData(socketInfo);
                }
            }
            catch (ObjectDisposedException)
            {
                Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: Socket has been closed");
            }
            catch (SocketException se)
            {
                if (se.ErrorCode != 10054)
                {
                    Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + se);
                }
            }
            catch (Exception e)
            {
                Helpers.ErrorLogger.Log(Helpers.ErrorLevel.Critical, "[Error] Session.OnDataReceived: " + e);
            }
        }