Exemplo n.º 1
0
        public PacketLogEntry Read()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            try
            {
                var time = _reader.ReadInt64();

                DateTime stamp;

                try
                {
                    stamp = DateTimeOffset.FromUnixTimeMilliseconds(time).LocalDateTime;
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new InvalidDataException($"Invalid timestamp value {time}.");
                }

                var id = _reader.ReadInt32();

                if (!Servers.ContainsKey(id))
                {
                    throw new InvalidDataException($"Invalid server ID {id}.");
                }

                var direction = (Direction)_reader.ReadByte();

                if (!Enum.IsDefined(typeof(Direction), direction))
                {
                    throw new InvalidDataException($"Unknown direction value {direction}.");
                }

                var code = _reader.ReadUInt16();

                if (!GameMessages.CodeToName.ContainsKey(code))
                {
                    throw new InvalidDataException($"Unknown message code {code}.");
                }

                var length  = _reader.ReadUInt16();
                var payload = _reader.ReadBytes(length);

                return(new PacketLogEntry(stamp, id, direction, code, payload));
            }
            catch (EndOfStreamException)
            {
                return(null);
            }
        }