83 lines
2.5 KiB
C#
83 lines
2.5 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 ConnectionCloseFrame : Frame
|
|
{
|
|
public byte ActualType { get; set; }
|
|
public override byte Type => 0x1c;
|
|
public IntegerVar ErrorCode { get; set; }
|
|
public IntegerVar FrameType { get; set; }
|
|
public IntegerVar ReasonPhraseLength { get; set; }
|
|
public string ReasonPhrase { get; set; }
|
|
|
|
public ConnectionCloseFrame()
|
|
{
|
|
ErrorCode = 0;
|
|
ReasonPhraseLength = new IntegerVar(0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 0x1d not yet supported (Application Protocol Error)
|
|
/// </summary>
|
|
public ConnectionCloseFrame(ErrorCode error, byte frameType, string reason)
|
|
{
|
|
ActualType = 0x1c;
|
|
|
|
ErrorCode = (ulong)error;
|
|
FrameType = new IntegerVar(frameType);
|
|
if (!string.IsNullOrWhiteSpace(reason))
|
|
{
|
|
ReasonPhraseLength = new IntegerVar((ulong)reason.Length);
|
|
}
|
|
else
|
|
{
|
|
ReasonPhraseLength = new IntegerVar(0);
|
|
}
|
|
|
|
ReasonPhrase = reason;
|
|
}
|
|
|
|
public override void Decode(ByteArray array)
|
|
{
|
|
ActualType = array.ReadByte();
|
|
ErrorCode = array.ReadIntegerVar();
|
|
if (ActualType == 0x1c)
|
|
{
|
|
FrameType = array.ReadIntegerVar();
|
|
}
|
|
|
|
ReasonPhraseLength = array.ReadIntegerVar();
|
|
|
|
byte[] rp = array.ReadBytes((int)ReasonPhraseLength.Value);
|
|
ReasonPhrase = ByteHelpers.GetString(rp);
|
|
}
|
|
|
|
public override byte[] Encode()
|
|
{
|
|
List<byte> result = new List<byte>();
|
|
result.Add(ActualType);
|
|
result.AddRange(ErrorCode.ToByteArray());
|
|
if (ActualType == 0x1c)
|
|
{
|
|
result.AddRange(FrameType.ToByteArray());
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(ReasonPhrase) == false)
|
|
{
|
|
byte[] rpl = new IntegerVar((ulong)ReasonPhrase.Length);
|
|
result.AddRange(rpl);
|
|
|
|
byte[] reasonPhrase = ByteHelpers.GetBytes(ReasonPhrase);
|
|
result.AddRange(reasonPhrase);
|
|
}
|
|
|
|
return result.ToArray();
|
|
}
|
|
}
|
|
} |