//TODO handle nagle, protect against OOM by defining a max packet size. public EzPacket Read(byte[] data) { IBuffer buffer = EzServer.Buffer.Provide(data); EzPacket packet = null; if (_isFinished) { buffer.SetPositionStart(); byte id = buffer.ReadByte(); int size = buffer.ReadInt16(Endianness.Big); int desiredSize = size + EzPacket.HeaderSize; if (buffer.Size == desiredSize) { IBuffer packetBuffer = buffer.Clone(EzPacket.HeaderSize, size); packet = new EzPacket(id, packetBuffer); } else if (buffer.Size < desiredSize) { _currentId = id; _currentDesiredSize = desiredSize; _isFinished = false; _currentPacketBuffer = buffer; } else { _logger.Error("TODO"); } } else { long dataSize = _currentPacketBuffer.Size + buffer.Size; if (dataSize == _currentDesiredSize) { _isFinished = true; IBuffer packetBuffer = _currentPacketBuffer.Clone(EzPacket.HeaderSize, _currentDesiredSize); packet = new EzPacket(_currentId, packetBuffer); } else if (dataSize < _currentDesiredSize) { _currentPacketBuffer.SetPositionEnd(); _currentPacketBuffer.WriteBuffer(buffer); } else { _logger.Error("TODO1"); } } return(packet); }
private string CreatePacketLog(NecLogPacket logPacket) { ServerType serverType = logPacket.ServerType; ushort packetId = logPacket.Id; if (ExcludeLog(serverType, packetId)) { return(null); } int dataSize = logPacket.Data.Size; StringBuilder sb = new StringBuilder(); sb.Append($"{logPacket.ClientIdentity} Packet Log"); sb.Append(Environment.NewLine); sb.Append("----------"); sb.Append(Environment.NewLine); sb.Append($"[{logPacket.TimeStamp:HH:mm:ss}][Typ:{logPacket.LogType}]"); sb.Append($"[{serverType}]"); sb.Append(Environment.NewLine); sb.Append( $"[Id:0x{packetId:X2}|{packetId}][Len(Data/Total):{dataSize}/{dataSize + logPacket.Header.Length}][Header:{logPacket.HeaderHex}]"); sb.Append($"[{logPacket.PacketIdName}]"); sb.Append(Environment.NewLine); if (!NoData) { IBuffer data = logPacket.Data; int maxPacketSize = MaxPacketSize; if (maxPacketSize > 0 && dataSize > maxPacketSize) { data = data.Clone(0, maxPacketSize); sb.Append($"- Truncated Data showing {maxPacketSize} of {dataSize} bytes"); sb.Append(Environment.NewLine); } sb.Append("ASCII:"); sb.Append(Environment.NewLine); sb.Append(data.ToAsciiString(" ")); sb.Append(Environment.NewLine); sb.Append("HEX:"); sb.Append(Environment.NewLine); sb.Append(data.ToHexString(" ")); sb.Append(Environment.NewLine); } sb.Append("----------"); return(sb.ToString()); }
public override object Clone( File context ) { PdfStream clone = (PdfStream)MemberwiseClone(); { // Deep cloning... clone.header = (PdfDictionary)header.Clone(context); clone.body = (IBuffer)body.Clone(); } return(clone); }
private string CreatePacketLog(MhfLogPacket logPacket) { ushort packetId = logPacket.Id; if (ExcludeLog(packetId)) { return(null); } int dataSize = logPacket.Data.Size; StringBuilder sb = new StringBuilder(); sb.Append($"{logPacket.ClientIdentity} Packet Log"); sb.Append(Environment.NewLine); sb.Append("----------"); sb.Append(Environment.NewLine); sb.Append($"[{logPacket.TimeStamp:HH:mm:ss}][Typ:{logPacket.LogType}]"); sb.Append(Environment.NewLine); sb.Append( $"[Id:0x{logPacket.Id:X2}|{logPacket.Id}][BodyLen:{logPacket.Data.Size}][{logPacket.PacketIdName}]"); sb.Append(Environment.NewLine); sb.Append(logPacket.Header.ToLogText()); sb.Append(Environment.NewLine); if (!NoData) { IBuffer data = logPacket.Data; int maxPacketSize = MaxPacketSize; if (maxPacketSize > 0 && dataSize > maxPacketSize) { data = data.Clone(0, maxPacketSize); sb.Append($"- Truncated Data showing {maxPacketSize} of {dataSize} bytes"); sb.Append(Environment.NewLine); } sb.Append("ASCII:"); sb.Append(Environment.NewLine); sb.Append(data.ToAsciiString(true)); sb.Append(Environment.NewLine); sb.Append("HEX:"); sb.Append(Environment.NewLine); sb.Append(data.ToHexString(' ')); sb.Append(Environment.NewLine); } sb.Append("----------"); return(sb.ToString()); }