public static PacketPayload.CursorWritingContext StartWriting(MemoryCursor cursor, PacketVersion version, PacketConnectionId destinationConnectionId, PacketConnectionId sourceConnectionId, PacketNumber packetNumber, PacketToken token) { var startOffset = cursor.AsOffset(); var firstByte = new PacketFirstByte() .SetInitial() .SetMaxPacketNumberLength(); firstByte.Write(cursor); version.WriteBytes(cursor); destinationConnectionId.WriteBytes(cursor); sourceConnectionId.WriteBytes(cursor); token.WriteBytes(cursor); var context = PacketPayload.StartPacketWriting(cursor, startOffset); var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor); packetNumber.Fill(packetNumberBytes.Span); return(context); }
public static PacketPayload.CursorWritingContext StartWriting(MemoryCursor cursor, PacketVersion version, PacketConnectionId destinationConnectionId, PacketConnectionId sourceConnectionId, PacketNumber largestAcknowledgedPacketNumber, PacketNumber packetNumber) { var startOffset = cursor.AsOffset(); var firstByte = new PacketFirstByte() .SetHandshake() .SetMaxPacketNumberLength(); firstByte.Write(cursor); version.WriteBytes(cursor); destinationConnectionId.WriteBytes(cursor); sourceConnectionId.WriteBytes(cursor); var context = PacketPayload.StartPacketWriting(cursor, startOffset); var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor); packetNumber .EncodeByLargestAcknowledged(largestAcknowledgedPacketNumber) .Fill(packetNumberBytes.Span); return(context); }
public void Dispose() { var payloadLength = cursor - startPayloadOffset; var payload = cursor.Peek(-payloadLength); Span <byte> payloadBuffer = stackalloc byte[payloadLength]; Span <byte> tagBuffer = stackalloc byte[aead.TagLength]; payload.Span.CopyTo(payloadBuffer); packetFirstByte.SlicePacketNumberBytes(cursor); var packetLength = cursor - startPacketOffset; cursor.Set(startPayloadOffset); cursor.EncodeVariable32(packetLength); var packetNumberBytes = packetFirstByte.SlicePacketNumberBytes(cursor); var encodedPacketNumber = largestAcknowledgedPacketNumber.HasValue ? packetNumber.EncodeByLargestAcknowledged(largestAcknowledgedPacketNumber.Value) : packetNumber; encodedPacketNumber.Fill(packetNumberBytes.Span); var headerLength = cursor - startPacketOffset; var header = cursor.Peek(-headerLength); packetNumber.Encrypt(aead, payloadBuffer, tagBuffer, header.Span); payloadBuffer.CopyTo(cursor); tagBuffer.CopyTo(cursor); var endPacketOffset = cursor.AsOffset(); var encryptedLength = payloadBuffer.Length + tagBuffer.Length; var encryptedData = cursor.Peek(-encryptedLength); var sample = encryptedData.Slice(0, SampleLength); var mask = cipher.CreateMask(sample.Span); encodedPacketNumber.Fill(packetNumberBytes.Span, mask); cursor.Set(startPacketOffset); packetFirstByte.Mask(mask).Write(cursor); cursor.Set(endPacketOffset); }
public static MemoryBuffer SlicePacketBytes( MemoryCursor cursor, PacketFirstByte firstByte, int startPacketOffset, PacketNumber?largestAcknowledgedPacketNumber, out PacketNumber packetNumber) { var readLength = cursor - startPacketOffset; var packetLength = cursor.DecodeVariable32(); var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor); var payloadLength = packetLength - readLength - packetNumberBytes.Length; packetNumber = PacketNumber.Parse(packetNumberBytes.Span); if (largestAcknowledgedPacketNumber.HasValue) { packetNumber = packetNumber.DecodeByLargestAcknowledged(largestAcknowledgedPacketNumber.Value); } return(cursor.Slice(payloadLength)); }