Exemplo n.º 1
0
        /// <summary>
        /// Executes this simulation
        /// </summary>
        public Result Execute()
        {
            foreach (Packet sendpacket in PacketsSend)
            {
                if (sendpacket.Result.ErrorId == 0)
                {
                    Packet p = sendpacket.Send();

                    if (p != null)
                    {
                        PacketsReceived.Add(p);
                    }
                    else
                    {
                        return(sendpacket.Result);
                    }
                }
            }

            foreach (Packet backpacket in PacketsReceived)
            {
                if (backpacket.Result.ErrorId == 0)
                {
                    backpacket.Send();
                }
            }
            if (PacketsReceived.Count > 0)
            {
                return(PacketsReceived[PacketsReceived.Count - 1].Result);
            }
            return(PacketsSend[PacketsSend.Count - 1].Result);
        }
        private void RaisePacketHandled(int id, IPacket packet, ServerState?state)
        {
            if (DebugPackets)
            {
                PacketsReceived.Add(packet);
            }

            if (OnPacketHandled != null)
            {
                OnPacketHandled(id, packet, state);
            }
        }
Exemplo n.º 3
0
        public override void Dispose()
        {
            CancellationToken?.Cancel();

            Stream?.Dispose();

#if DEBUG
            PacketsReceived?.Clear();
            PacketsSended?.Clear();
#endif

            CancellationToken?.Dispose();
        }
Exemplo n.º 4
0
        public void Dispose()
        {
            if (_stream != null)
            {
                _stream.Dispose();
            }

            if (PacketsReceived != null)
            {
                PacketsReceived.Clear();
            }

            if (PacketsSended != null)
            {
                PacketsSended.Clear();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Packets are handled here. Compression and encryption are handled here too
        /// </summary>
        /// <param name="id">Packet ID</param>
        /// <param name="stream"></param>
        private void HandlePacket(int id, IProtocolStream stream)
        {
            using (var reader = new TrueCraftDataReader(stream as Stream))
            {
                IPacket packet = null;

                switch (State)
                {
                    #region Login

                case ConnectionState.Joining:
                    if (ServerResponse.JoiningServer[id] == null)
                    {
                        throw new ProtocolException("Reading error: Wrong Login packet ID.");
                    }

                    packet = ServerResponse.JoiningServer[id]().ReadPacket(reader);

                    OnPacketHandled(id, packet, ConnectionState.Joining);
                    break;

                    #endregion Login

                    #region Play

                case ConnectionState.Joined:
                    if (ServerResponse.JoinedServer[id] == null)
                    {
                        throw new ProtocolException("Reading error: Wrong Play packet ID.");
                    }

                    packet = ServerResponse.JoinedServer[id]().ReadPacket(reader);

                    OnPacketHandled(id, packet, ConnectionState.Joined);
                    break;

                    #endregion Play
                }

                if (SavePackets)
                {
                    PacketsReceived.Add(packet);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Packets are handled here.
        /// </summary>
        /// <param name="id">Packet ID</param>
        /// <param name="data">Packet byte[] data</param>
        private void HandlePacket(int id, byte[] data)
        {
            using (var reader = new ClassicDataReader(data))
            {
                if (ServerResponseClassic.ServerResponse[id] == null)
                {
                    return;
                }

                var packet = ServerResponseClassic.ServerResponse[id]().ReadPacket(reader);

                OnPacketHandled(id, packet, null);

                if (SavePackets)
                {
                    PacketsReceived.Add(packet);
                }
            }
        }
Exemplo n.º 7
0
        public Client()
        {
            SetState(State.Offline);

            packetProcessor = new NetPacketProcessor();

            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Position>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Shapes.Rectangle>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Character>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Projectile>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Item>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Door>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Orientations.Cardinal>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Playable>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDict <Ecs.Components.Inventory>()
                );

            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Position>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Shapes.Rectangle>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Character>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Projectile>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Item>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Door>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Orientations.Cardinal>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Playable>()
                );
            packetProcessor.RegisterNestedType(
                () => new EcDictDelta <Ecs.Components.Inventory>()
                );

            packetProcessor.SubscribeReusable <Packets.GameSnapshot>(
                StoreGameSnapshot
                );
            packetProcessor.SubscribeReusable <Packets.GameSnapshotDelta>(
                StoreGameSnapshotDelta
                );
            packetProcessor.SubscribeReusable <Packets.Welcome>(
                StoreWelcome
                );

            packetsReceived = new PacketsReceived();
        }
Exemplo n.º 8
0
 private void _OnPacketsReceived(IEnumerable <ZPacket> packets) => PacketsReceived?.Invoke(packets);
Exemplo n.º 9
0
        /// <summary>
        /// Data is handled here. Compression and encryption are handled here too
        /// </summary>
        /// <param name="data">Packet byte[] data</param>
        private void HandleData(byte[] data)
        {
            using (var reader = new ProtobufDataReader(data))
            {
                var id     = reader.Read <VarInt>();
                var packet = default(ProtobufPacket);

                switch (State)
                {
                    #region Status

                case ClientState.InfoRequest:
                    if (ClientPackets.StatusPacketResponses.Packets[id] == null)
                    {
                        throw new ProtocolException("Reading error: Wrong Status packet ID.");
                    }

                    packet = ClientPackets.StatusPacketResponses.Packets[id]().ReadPacket(reader);

                    OnPacketHandled(id, packet, ClientState.InfoRequest);
                    break;

                    #endregion Status

                    #region Login

                case ClientState.Joining:
                    if (ClientPackets.LoginPacketResponses.Packets[id] == null)
                    {
                        throw new ProtocolException("Reading error: Wrong Login packet ID.");
                    }

                    packet = ClientPackets.LoginPacketResponses.Packets[id]().ReadPacket(reader);

                    OnPacketHandled(id, packet, ClientState.Joining);
                    break;

                    #endregion Login

                    #region Play

                case ClientState.Joined:
                    if (id < 0 || ClientPackets.PlayPacketResponses.Packets[id] == null)
                    {
                        break;
                    }
                    //throw new ProtocolException("Reading error: Wrong Play packet ID.");

                    packet = ClientPackets.PlayPacketResponses.Packets[id]().ReadPacket(reader);

                    OnPacketHandled(id, packet, ClientState.Joined);
                    break;

                    #endregion Play
                }

                if (packet == null)
                {
                    return;
                }

#if DEBUG
                if (packet is PluginMessagePacket)
                {
                    PluginMessage.Add(packet);
                }

                PacketsReceived.Add(packet);
#endif
            }
        }