Example #1
0
        internal static Packet Instantiate(byte[] source, PacketDefinitionRegistry packetRegistry = null)
        {
            packetRegistry = packetRegistry ?? PacketDefinitionRegistryFactory.CreateClassicClient();

            var processingStream = new MemoryStream(source);
            var received         = new byte[source.Length];

            var packetReader = new StreamPacketReader(processingStream, received);
            int packetId     = packetReader.ReadByte();

            if ((packetId < 0) || (packetId > 255))
            {
                throw new EndOfStreamException();
            }

            var packetDefinition = packetRegistry.Find(packetId);
            var packetSize       = packetDefinition.GetSize(packetReader);

            packetReader.ReadBytes(packetSize - packetReader.Position);
            var payload = new byte[packetSize];

            Array.Copy(received, 0, payload, 0, packetSize);

            var packet = new Packet(packetId, payload);

            return(packet);
        }
Example #2
0
        public Packet Receive(Stream processingStream)
        {
            var packetReader = new StreamPacketReader(processingStream, receiveBuffer);
            int packetId     = packetReader.ReadByte();

            if ((packetId < 0) || (packetId > 255))
            {
                throw new EndOfStreamException();
            }

            diagnosticPullStream.StartPacket();
            var packetDefinition = packetRegistry.Find(packetId);
            var packetSize       = packetDefinition.GetSize(packetReader);

            packetReader.ReadBytes(packetSize - packetReader.Position);
            var payload = new byte[packetSize];

            Array.Copy(receiveBuffer, 0, payload, 0, packetSize);

            var packet = new Packet(packetId, payload);

            if (packetId == PacketDefinitions.ConnectToGameServer.Id)
            {
                Status = ServerConnectionStatus.PreGame;
            }

            diagnosticPullStream.FinishPacket(packet);

            return(packet);
        }
        public void DumpPacket(Packet packet)
        {
            if (needsNewLine)
            {
                builder.AppendLine();
                needsNewLine = false;
            }

            builder.AppendFormat(
                $"{DateTime.UtcNow:hh:mm:ss.fff} >>>> {header}: RawPacket {packetRegistry.Find(packet.Id).Name}, length = {packet.Length}");
            builder.AppendLine();

            bool justAppendedNewLine = true;

            for (var i = 0; i < packet.Length; i++)
            {
                justAppendedNewLine = false;
                builder.AppendFormat("0x{0:X2}, ", packet.Payload[i]);
                if ((i + 1) % MaxColumns == 0)
                {
                    builder.AppendLine();
                    justAppendedNewLine = true;
                }
            }

            if (!justAppendedNewLine)
            {
                builder.AppendLine();
            }

            columns        = 0;
            requiresHeader = true;
        }
        public PacketLogEntry ReadNextLogEntry()
        {
            try
            {
                var ticks         = inputStreamReader.ReadInt64();
                var directionCode = (DiagnosticStreamDirection)inputStreamReader.ReadUInt32();
                var direction     = directionCode == DiagnosticStreamDirection.ClientToServer
                    ? PacketDirection.ProxyServer
                    : directionCode == DiagnosticStreamDirection.ServerToClient
                        ? PacketDirection.ProxyClient
                        : throw new InvalidOperationException();

                var packet = packetLogParser.ParsePacket(pullStream);
                var name   = packetRegistry.Find(packet.Id).Name;

                return(new PacketLogEntry(new DateTime(ticks), name, direction, packet.Payload));
            }
            catch (EndOfStreamException)
            {
                return(null);
            }
        }
Example #5
0
        public IEnumerable <PacketLogEntry> ParseFile(string fileName)
        {
            using (var stream = new FileStream(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    using (var pullStream = new StreamToPullStreamAdapter(stream))
                    {
                        while (stream.Position < stream.Length)
                        {
                            var             time            = new DateTime(reader.ReadInt64(), DateTimeKind.Utc);
                            var             binaryDirection = (DiagnosticStreamDirection)reader.ReadInt32();
                            PacketDirection packetDirection;

                            switch (binaryDirection)
                            {
                            case DiagnosticStreamDirection.ClientToServer:
                                packetDirection = PacketDirection.ProxyServer;
                                break;

                            case DiagnosticStreamDirection.ServerToClient:
                                packetDirection = PacketDirection.ProxyClient;
                                break;

                            default:
                                throw new NotImplementedException($"Unknown binnary packet direction: {((int)binaryDirection):X8} at position {stream.Position}");
                            }

                            var packet     = packetLogParser.ParsePacket(pullStream);
                            var definition = packetRegistry.Find(packet.Id);

                            yield return(new PacketLogEntry(time, definition.Name, packetDirection, packet.Payload));
                        }
                    }
                }
            }
        }