EonaCat.Network/EonaCat.Network/System/Quic/Infrastructure/Frames/StreamFrame.cs

104 lines
2.7 KiB
C#

using EonaCat.Quic.Helpers;
using System;
using System.Collections.Generic;
namespace EonaCat.Quic.Infrastructure.Frames
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
public class StreamFrame : Frame
{
public byte ActualType = 0x08;
public override byte Type => 0x08;
public IntegerVar StreamId { get; set; }
public IntegerVar Offset { get; set; }
public IntegerVar Length { get; set; }
public byte[] StreamData { get; set; }
public bool EndOfStream { get; set; }
public StreamFrame()
{
}
public StreamFrame(ulong streamId, byte[] data, ulong offset, bool eos)
{
StreamId = streamId;
StreamData = data;
Offset = offset;
Length = (ulong)data.Length;
EndOfStream = eos;
}
public override void Decode(ByteArray array)
{
byte type = array.ReadByte();
byte OFF_BIT = (byte)(type & 0x04);
byte LEN_BIT = (byte)(type & 0x02);
byte FIN_BIT = (byte)(type & 0x01);
StreamId = array.ReadIntegerVar();
if (OFF_BIT > 0)
{
Offset = array.ReadIntegerVar();
}
if (LEN_BIT > 0)
{
Length = array.ReadIntegerVar();
}
if (FIN_BIT > 0)
{
EndOfStream = true;
}
StreamData = array.ReadBytes((int)Length.Value);
}
public override byte[] Encode()
{
if (Offset != null && Offset.Value > 0)
{
ActualType = (byte)(ActualType | 0x04);
}
if (Length != null && Length.Value > 0)
{
ActualType = (byte)(ActualType | 0x02);
}
if (EndOfStream == true)
{
ActualType = (byte)(ActualType | 0x01);
}
byte OFF_BIT = (byte)(ActualType & 0x04);
byte LEN_BIT = (byte)(ActualType & 0x02);
byte FIN_BIT = (byte)(ActualType & 0x01);
List<byte> result = new List<byte>
{
ActualType
};
byte[] streamId = StreamId;
result.AddRange(streamId);
if (OFF_BIT > 0)
{
result.AddRange(Offset.ToByteArray());
}
if (LEN_BIT > 0)
{
result.AddRange(Length.ToByteArray());
}
result.AddRange(StreamData);
return result.ToArray();
}
}
}