public static ConnectionRequestDatagram TranslateFromMemory(Memory <byte> data) { var span = data.Span; var result = new ConnectionRequestDatagram { Tkpt = new TpktDatagram { Sync1 = span[0], Sync2 = span[1], Length = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(2, 2)) }, Li = span[4], PduType = span[5], DstRef = BinaryPrimitives.ReadInt16BigEndian(span.Slice(6, 2)), SrcRef = BinaryPrimitives.ReadInt16BigEndian(span.Slice(8, 2)), ClassOption = span[10], ParmCodeTpduSize = span[11], Unknown = span[12], SizeTpduReceiving = span[13], }; var offset = 14; result.ParmCodeSrcTsap = span[offset++]; result.SourceTsapLength = span[offset++]; result.SourceTsap = data.Slice(offset, (int)result.SourceTsapLength); offset += (int)result.SourceTsapLength; result.ParmCodeDestTsap = span[offset++]; result.DestTsapLength = span[offset++]; result.DestTsap = data.Slice(offset, result.DestTsapLength); offset += (int)result.DestTsapLength; return(result); }
public static Memory <byte> TranslateToMemory(ConnectionRequestDatagram datagram) { var length = datagram.Tkpt.Length; var result = new Memory <byte>(new byte[length]); // check if we could use ArrayBuffer var span = result.Span; span[0] = datagram.Tkpt.Sync1; span[1] = datagram.Tkpt.Sync2; BinaryPrimitives.WriteUInt16BigEndian(span.Slice(2, 2), datagram.Tkpt.Length); span[4] = datagram.Li; span[5] = datagram.PduType; BinaryPrimitives.WriteInt16BigEndian(span.Slice(6, 2), datagram.DstRef); BinaryPrimitives.WriteInt16BigEndian(span.Slice(8, 2), datagram.DstRef); span[10] = datagram.ClassOption; span[11] = datagram.ParmCodeTpduSize; span[12] = datagram.Unknown; span[13] = datagram.SizeTpduReceiving; var offset = 14; span[offset++] = datagram.ParmCodeSrcTsap; span[offset++] = datagram.SourceTsapLength; datagram.SourceTsap.CopyTo(result.Slice(offset)); offset += (int)datagram.SourceTsapLength; span[offset++] = datagram.ParmCodeDestTsap; span[offset++] = datagram.DestTsapLength; datagram.DestTsap.CopyTo(result.Slice(offset)); offset += (int)datagram.DestTsapLength; return(result); }
public static bool Correlate(ConnectionRequestDatagram o1, ConnectionConfirmedDatagram o2) { //Test ack if (o1.SourceTsap.Span.SequenceEqual(o2.SourceTsap.Span) && o1.DestTsap.Span.SequenceEqual(o2.DestTsap.Span)) { return(true); } return(false); }
public static ConnectionRequestDatagram BuildCr(Rfc1006ProtocolContext context) { context.CalcLength(context, out var li, out var length); var result = new ConnectionRequestDatagram { Li = li, SizeTpduReceiving = context.SizeTpduReceiving, SourceTsapLength = Convert.ToByte(context.SourceTsap.Length), SourceTsap = context.SourceTsap, DestTsapLength = Convert.ToByte(context.DestTsap.Length), DestTsap = context.DestTsap }; result.Tkpt.Length = length; return(result); }
public ConnectionConfirmedDatagram BuildCc(Rfc1006ProtocolContext context, ConnectionRequestDatagram req) { context.CalcLength(context, out byte li, out ushort length); context.SizeTpduSending = req.SizeTpduReceiving; var result = new ConnectionConfirmedDatagram { Li = li, SizeTpduReceiving = context.SizeTpduReceiving, SourceTsapLength = req.DestTsapLength, SourceTsap = req.SourceTsap, DestTsapLength = req.DestTsapLength, DestTsap = req.DestTsap }; result.Tkpt.Length = length; return(result); }
public static IMemoryOwner <byte> TranslateToMemory(ConnectionRequestDatagram datagram, out int memoryLength) { var length = memoryLength = datagram.Tkpt.Length; var result = MemoryPool <byte> .Shared.Rent(length); // check if we could use ArrayBuffer var mem = result.Memory; var span = mem.Span; span[0] = datagram.Tkpt.Sync1; span[1] = datagram.Tkpt.Sync2; BinaryPrimitives.WriteUInt16BigEndian(span.Slice(2, 2), datagram.Tkpt.Length); span[4] = datagram.Li; span[5] = datagram.PduType; BinaryPrimitives.WriteInt16BigEndian(span.Slice(6, 2), datagram.DstRef); BinaryPrimitives.WriteInt16BigEndian(span.Slice(8, 2), datagram.SrcRef); span[10] = datagram.ClassOption; var offset = 11; span[offset++] = datagram.ParmCodeTpduSize; span[offset++] = datagram.SizeTpduReceivingLength; datagram.SizeTpduReceiving.CopyTo(mem.Slice(offset)); offset += datagram.SizeTpduReceivingLength; span[offset++] = datagram.ParmCodeSrcTsap; span[offset++] = datagram.SourceTsapLength; datagram.SourceTsap.CopyTo(mem.Slice(offset)); offset += datagram.SourceTsapLength; span[offset++] = datagram.ParmCodeDestTsap; span[offset++] = datagram.DestTsapLength; datagram.DestTsap.CopyTo(mem.Slice(offset)); //offset += datagram.DestTsapLength; return(result); }
public static ConnectionRequestDatagram TranslateFromMemory(Memory <byte> data, out int processed) { var span = data.Span; var result = new ConnectionRequestDatagram { Tkpt = new TpktDatagram { Sync1 = span[0], Sync2 = span[1], Length = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(2, 2)) }, Li = span[4], PduType = span[5], DstRef = BinaryPrimitives.ReadInt16BigEndian(span.Slice(6, 2)), SrcRef = BinaryPrimitives.ReadInt16BigEndian(span.Slice(8, 2)), ClassOption = span[10] }; int offset; for (offset = 11; offset < data.Length;) { switch (span[offset]) { case 0xc0: { result.ParmCodeTpduSize = span[offset++]; result.SizeTpduReceivingLength = span[offset++]; result._sizeTpduReceiving = MemoryPool <byte> .Shared.Rent(result.SizeTpduReceivingLength); data.Slice(offset, result.SizeTpduReceivingLength).CopyTo(result._sizeTpduReceiving.Memory); result.SizeTpduReceiving = result._sizeTpduReceiving.Memory.Slice(0, result.SizeTpduReceivingLength); offset += result.SizeTpduReceivingLength; } break; case 0xc1: { result.ParmCodeSrcTsap = span[offset++]; result.SourceTsapLength = span[offset++]; result._sourceTsap = MemoryPool <byte> .Shared.Rent(result.SourceTsapLength); data.Slice(offset, result.SourceTsapLength).CopyTo(result._sourceTsap.Memory); result.SourceTsap = result._sourceTsap.Memory.Slice(0, result.SourceTsapLength); offset += result.SourceTsapLength; } break; case 0xc2: { result.ParmCodeDestTsap = span[offset++]; result.DestTsapLength = span[offset++]; result._destTsap = MemoryPool <byte> .Shared.Rent(result.DestTsapLength); data.Slice(offset, result.DestTsapLength).CopyTo(result._destTsap.Memory); result.DestTsap = result._destTsap.Memory.Slice(0, result.DestTsapLength); offset += result.DestTsapLength; } break; default: break; } } processed = offset; return(result); }