示例#1
0
        /// <summary>
        /// A loop to check for received packets
        /// </summary>
        private void ReceiveLoop()
        {
            if (this.TcpConnection.Connected)
            {
                while (this.TcpConnection.Available < 0x10)
                {
                    Thread.Sleep(50);
                }
                byte[] buffer = new byte[0x10];
                this.TcpConnection.GetStream().Read(buffer, 0, 0x10);

                PacketReader Reader = new PacketReader(buffer);
                Reader.ReadShort();
                this.MapleVersion = Reader.ReadShort();
                Reader.ReadMapleString();
                this.SendCrypto    = new MapleCrypto(Reader.ReadBytes(4), (short)this.MapleVersion);
                this.ReceiveCrypto = new MapleCrypto(Reader.ReadBytes(4), (short)this.MapleVersion);

                this.Ready = true;
                while (this.TcpConnection.Connected)
                {
                    if (this.TcpConnection.Available >= 4)
                    {
                        byte[] buffer2 = new byte[4];
                        this.TcpConnection.GetStream().Read(buffer2, 0, 4);
                        int packetLength = MapleCrypto.GetPacketLength(buffer2);
                        while (this.TcpConnection.Available < packetLength)
                        {
                            Thread.Sleep(20);
                        }
                        byte[] buffer3 = new byte[packetLength];
                        this.TcpConnection.GetStream().Read(buffer3, 0, packetLength);
                        this.ReceiveCrypto.Crypt(buffer3);
                        MapleCustomEncryption.Decrypt(buffer3);
                        Client.HandlePacket(buffer3);
                    }
                    Thread.Sleep(5);
                }
                this.Ready = false;
            }
        }
示例#2
0
        /// <summary>
        ///   Data received event handler
        /// </summary>
        /// <param name="iar"> IAsyncResult of the data received event </param>
        private void DataReceived(IAsyncResult iar)
        {
            ReceiveManager manager = (ReceiveManager)iar.AsyncState;

            try
            {
                int received = Socket.EndReceive(iar);
                manager.Index += received;
                if (manager.Index == manager.Buffer.Length)
                {
                    byte[] data = manager.Buffer;
                    switch (manager.State)
                    {
                    case ReceiveState.Header:
                        int packetLength = MapleCrypto.GetPacketLength(data);
                        manager.State  = ReceiveState.Content;
                        manager.Buffer = new byte[packetLength];
                        manager.Index  = 0;
                        WaitForData(manager);
                        break;

                    case ReceiveState.Content:
                        ReceiveCrypto.Crypt(data);
                        MapleCustomEncryption.Decrypt(data);
                        PacketReceived(new MaplePacketReader(data));
                        WaitForData(new ReceiveManager());
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("[Warning] Not enough data");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Close("Exception while receiving data.");
            }
        }
示例#3
0
        /// <summary>
        /// Data received event handler
        /// </summary>
        /// <param name="iar">IAsyncResult of the data received event</param>
        private void OnDataReceived(IAsyncResult iar)
        {
            var socketInfo = (SocketInfo)iar.AsyncState;

            try
            {
                var received = socketInfo.Socket.EndReceive(iar);
                if (received == 0)
                {
                    OnClientDisconnected?.Invoke(this);
                    return;
                }

                socketInfo.Index += received;

                if (socketInfo.Index == socketInfo.DataBuffer.Length)
                {
                    switch (socketInfo.State)
                    {
                    case SocketInfo.StateEnum.Header:
                        if (socketInfo.IsNoEncryption)
                        {
                            var headerReader = new PacketReader(socketInfo.DataBuffer);
                            var packetHeader = headerReader.ReadShort();
                            socketInfo.State      = SocketInfo.StateEnum.Content;
                            socketInfo.DataBuffer = new byte[packetHeader];
                            socketInfo.Index      = 0;
                            WaitForData(socketInfo);
                        }
                        else
                        {
                            var headerReader  = new PacketReader(socketInfo.DataBuffer);
                            var packetHeaderB = headerReader.ToArray();
                            var packetHeader  = headerReader.ReadInt();
                            var packetLength  = (short)MapleCrypto.GetPacketLength(packetHeader);
                            if (Type == SessionType.SERVER_TO_CLIENT &&
                                !RIV.CheckPacketToServer(BitConverter.GetBytes(packetHeader)))
                            {
                                Log.LogError("Packet check failed. Disconnecting client");
                                Socket.Close();
                            }

                            socketInfo.State      = SocketInfo.StateEnum.Content;
                            socketInfo.DataBuffer = new byte[packetLength];
                            socketInfo.Index      = 0;
                            WaitForData(socketInfo);
                        }

                        break;

                    case SocketInfo.StateEnum.Content:
                        var data = socketInfo.DataBuffer;

                        if (socketInfo.IsNoEncryption)
                        {
                            socketInfo.IsNoEncryption = false;
                            var reader  = new PacketReader(data);
                            var version = reader.ReadShort();
                            var unknown = reader.ReadMapleString();
                            SIV = new MapleCrypto(reader.ReadBytes(4), version);
                            RIV = new MapleCrypto(reader.ReadBytes(4), version);
                            var 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?.Invoke(new PacketReader(data), false);
                            }

                            WaitForData();
                        }

                        break;
                    }
                }
                else
                {
                    Log.LogWarning("Not enough data");
                    WaitForData(socketInfo);
                }
            }
            catch (ObjectDisposedException e)
            {
                Log.LogError("Socket has been closed", e);
            }
            catch (SocketException se)
            {
                if (se.ErrorCode != 10054)
                {
                    Log.LogError("Session.OnDataReceived", se);
                }
            }
            catch (Exception e)
            {
                Log.LogError("Session.OnDataReceived", e);
            }
        }
示例#4
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);
            }
        }