예제 #1
0
        public virtual void ProcessStream(SocketStateObject state, DataStream ds)
        {
            var goodPos = 0;

            while (true)
            {
                var id     = 0U;
                var length = 0U;
                if (!ds.TryReadCompactUInt32(out id) || !ds.TryReadCompactUInt32(out length) || !ds.CanReadBytes((int)length))
                {
                    if (length > PacketSizeLimit)
                    {
                        Logger.Process("packetId = {0}, packetLength = {1}, packetSizeLimit = {2}", id, length, PacketSizeLimit);
                        Stop();
                    }
                    break;
                }
                var packetId     = new PacketIdentifier(id, state.IsC2S ? PacketType.ClientPacket : PacketType.ServerPacket);
                var packetStream = new DataStream(ds.ReadBytes((int)length));
                packetStream.IsLittleEndian = false;
                goodPos = ds.Position;

                ProcessPacketStream(state, packetId, packetStream);
            }
            ds.Position = goodPos;
            ds.Flush();
        }
예제 #2
0
        public virtual void ProcessPacketStream(SocketStateObject state, PacketIdentifier packetId, DataStream packetStream)
        {
            if (Handler.Contains(packetId) || CompleteHandler.Contains(packetId))
            {
                GamePacket packet;
                if (!PacketsRegistry.TryGetPacket(packetId, out packet))
                {
                    packet = new BasePacket();
                }

                packet.Deserialize(packetStream);
                var args = Handler.HandlePacket(packetId, packet);
                if (args == null || !args.Cancel)
                {
                    if (args == null)
                    {
                        Send(state, packetId.PacketId, packetStream.Reset());
                    }
                    else
                    {
                        Send(state, packetId, packet);
                    }
                    CompleteHandler.HandlePacket(packetId, packet);
                }
            }
            else
            {
                Send(state, packetId.PacketId, packetStream);
            }
        }
예제 #3
0
 public virtual void OnDisconnected(SocketStateObject state)
 {
     if (!Server.Connected && !Client.Connected)
     {
         Stopped(this, new EventArgs());
     }
     else
     {
         Stop();
     }
 }
예제 #4
0
        public virtual void Send(SocketStateObject state, uint packetId, DataStream packetStream)
        {
            lock (sendStream)
            {
                sendStream.Clear();
                sendStream.WriteCompactUInt32(packetId);
                sendStream.Write(packetStream);

                state.Send(sendStream.Buffer, 0, sendStream.Count);
            }
        }
예제 #5
0
        public virtual void Send(SocketStateObject state, PacketIdentifier packetId, GamePacket packet)
        {
            switch (packetId.PacketType)
            {
            case PacketType.ServerPacket:
            case PacketType.ClientPacket:
                Send(state, packetId.PacketId, packet.Serialize(new DataStream {
                    IsLittleEndian = false
                }));
                return;

            case PacketType.ServerContainer:
                Send(state, new ServerContainerS00(new[] { new PacketEventArgs(packetId, packet) }));
                return;

            case PacketType.ClientContainer:
                Send(state, new ClientContainerC22(packetId.PacketId, packet));
                return;
            }
        }
예제 #6
0
        public virtual void Send(SocketStateObject state, GamePacket packet)
        {
            var packetId = GamePacket.GetOnePacketIdentifier(packet);

            Send(state, packetId, packet);
        }