Пример #1
0
        public void SendPacket(IPacket packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }
            if (NetworkManager == null)
            {
                throw new InvalidOperationException("Tried to send a packet before NetworkManager was initialized or before socket was connected.");
            }

            PacketLog.LogData(packet, PacketDirection.Server);
            NetworkManager.SendPacket(packet);
        }
Пример #2
0
        private void OnPacketReceived(object sender, PacketReceivedEventArgs e)
        {
            if (e.Exception == null)
            {
                PacketLog.LogData(e.Packet, PacketDirection.Client);
                var defaultHandler = (PacketHandler)null;
                var handler        = (PacketHandler)null;

                if (DefaultPacketHandlers.TryGetValue(e.Packet.ID, out defaultHandler))
                {
                    defaultHandler(this, e.Packet); // use default handler
                }
                if (PacketHandlers.TryGetValue(e.Packet.ID, out handler))
                {
                    handler(this, e.Packet); // use custom handler
                }
            }
            else
            {
                Console.WriteLine("Failed to read packet: {0}", e.Exception.Message);
            }
        }
Пример #3
0
        private void HandleNetwork()
        {
            while (true)
            {
                if (ShuttingDown)
                {
                    return;
                }

                for (int i = 0; i < Clients.Count; i++)
                {
                    //TODO: Kick client due to keep alive timeouts.
                    while (Clients[i].Client.NetworkManager.DataAvailable)
                    {
                        try
                        {
                            /* S <- P <- C
                             * Proxying data from client to server.
                             */

                            var rawPacket       = (byte[])null;                                                                    // raw encrypted packet
                            var decryptedPacket = (byte[])null;
                            var packet          = Clients[i].Client.NetworkManager.ReadPacket(out rawPacket, out decryptedPacket); // receive data from client

                            if (packet != null)
                            {
                                HandlePacket(Clients[i], packet);
                                if (rawPacket != null)
                                {
                                    Clients[i].Server.NetworkManager.CoCStream.Write(rawPacket, 0, rawPacket.Length); // sends data back to server
                                    PacketDumper.LogPacket(packet, PacketDirection.Server, decryptedPacket);
                                    PacketLog.LogData(packet, PacketDirection.Server);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ExceptionLog.LogData(ex);
                            Clients.RemoveAt(i);
                            goto ResetLoop;
                        }
                    }

                    if (Clients[i].Server == null)
                    {
                        continue;
                    }

                    while (Clients[i].Server.NetworkManager.DataAvailable)
                    {
                        try
                        {
                            /* S -> P -> C
                             * Proxying data from server to client.
                             */

                            var rawPacket       = (byte[])null;
                            var decryptedPacket = (byte[])null;
                            var packet          = Clients[i].Server.NetworkManager.ReadPacket(out rawPacket, out decryptedPacket); // receive data from server

                            if (packet != null)
                            {
                                HandlePacket(Clients[i], packet);
                                if (rawPacket != null)
                                {
                                    Clients[i].Client.NetworkManager.CoCStream.Write(rawPacket, 0, rawPacket.Length); // sends data back to client
                                    PacketDumper.LogPacket(packet, PacketDirection.Client, decryptedPacket);
                                    PacketLog.LogData(packet, PacketDirection.Client);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ExceptionLog.LogData(ex);
                            Clients.RemoveAt(i);
                            goto ResetLoop;
                        }
                    }

                    continue;
ResetLoop:
                    break;
                }
                Thread.Sleep(1);
            }
        }