Пример #1
0
        private void OnReceive(BufferData data)
        {
            // Reset the timeout after every action
            Timer.ResetTimer("timeout");

            var packetType = PacketRouter.GetPacketType((ClientOpcode)data.Buffer[data.BaseOffset + data.Offset++]);

            if (packetType == null)
            {
                return;
            }

            var packet = Activator.CreateInstance(packetType) as IBasePacket;

            if (packet == null)
            {
                return;
            }

            packet.Read(data.GetReader());

            _packetQueue.EnqueueIncoming(packet);
        }
Пример #2
0
        private bool DecodePacket(BufferData data, out ushort length)
        {
            using (var br = data.GetReader(data.Offset, data.RemainingLength))
            {
                var rawPacket = new ProtocolPacket();

                rawPacket.Read(br);

                if (rawPacket.Channel != 0)
                {
                    if (rawPacket.SequenceNumber < ReceiveSequence[rawPacket.Channel])
                    {
                        Debugger.Break();

                        length = rawPacket.Size; // throw away the packet
                        return(true);
                    }

                    ReceiveSequence[rawPacket.Channel] = rawPacket.SequenceNumber;
                }

                length = rawPacket.Size;

                data.Offset += (int)br.BaseStream.Position;

                if (rawPacket.Type == ClientMessageOpcode.None)
                {
                    if (length != 4)
                    {
                        Debugger.Break(); // If it's not send timeout check, let's investigate...
                    }
                    return(true);
                }

                _packetQueue.EnqueueIncoming(rawPacket);
            }

            /*var packet = new PythonCallPacket(length);
             * using (var br = data.GetReader())
             * {
             *  packet.Read(br);
             *
             *  if (packet.Return.HasValue)
             *      return packet.Return.Value;
             *
             *  if (packet.Type == 2)
             *  {
             *      State = ClientState.LoggedIn;
             *      Entry = Server.AuthenticateClient(this, packet.AccountId, packet.OneTimeKey);
             *      if (Entry == null)
             *      {
             *          Logger.WriteLog(LogType.Error, "Client with ip: {0} tried to log in with invalid session data! User Id: {1} | OneTimeKey: {2}", Socket.RemoteAddress, packet.AccountId, packet.OneTimeKey);
             *          Close(false);
             *          return false;
             *      }
             *
             *      CharacterManager.Instance.StartCharacterSelection(this);
             *      return true;
             *  }
             *
             *  if (packet.DataSize > 0 && br.BaseStream.Position + packet.DataSize < br.BaseStream.Length)
             *  {
             *      if (br.ReadByte() != 0x4F) // 'O' format
             *          throw new Exception("Unsupported serialization format!");
             *
             *      var packetType = PacketRouter.GetPacketType(packet.Opcode);
             *      if (packetType != null)
             *      {
             *          var pythonPacket = Activator.CreateInstance(packetType) as IBasePacket;
             *          if (pythonPacket == null)
             *              return false;
             *
             *          pythonPacket.Read(br);
             *
             *          Server.PacketQueue.EnqueueIncoming(this, pythonPacket);
             *      }
             *      else
             *          Logger.WriteLog(LogType.Error, $"Unhandled game opcode: {packet.Opcode}");
             *  }
             *  else
             *      Logger.WriteLog(LogType.Error, $"Invalid data found in Python method call! Off: {br.BaseStream.Position} | Len: {packet.DataSize} | Array len: {br.BaseStream.Length}");
             * }*/

            return(true);
        }