-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathHeader.cs
More file actions
61 lines (48 loc) · 2.29 KB
/
Copy pathHeader.cs
File metadata and controls
61 lines (48 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using Bedrock.Framework.Experimental.Protocols.Framing.VariableSized.LengthFielded;
namespace ServerApplication.Framing.VariableSized.LengthFielded
{
internal class Header : IHeader, IEquatable<Header>
{
private byte[] _headerAsArray;
public int PayloadLength { get; }
public int SomeCustomData { get; }
public Header(int payloadLength, int someCustomData)
{
PayloadLength = payloadLength;
SomeCustomData = someCustomData;
}
public Header(ReadOnlySpan<byte> headerAsSpan)
{
PayloadLength = BitConverter.ToInt32(headerAsSpan.Slice(0, 4));
SomeCustomData = BitConverter.ToInt32(headerAsSpan.Slice(4));
}
public ReadOnlySpan<byte> AsSpan()
{
// Lazy creating the array.
if (_headerAsArray is null)
{
var payloadLengthAsArray = BitConverter.GetBytes(PayloadLength);
var someCustomDataAsArray = BitConverter.GetBytes(SomeCustomData);
_headerAsArray = new byte[Helper.HeaderLength];
_headerAsArray[0] = payloadLengthAsArray[0];
_headerAsArray[1] = payloadLengthAsArray[1];
_headerAsArray[2] = payloadLengthAsArray[2];
_headerAsArray[3] = payloadLengthAsArray[3];
_headerAsArray[4] = someCustomDataAsArray[0];
_headerAsArray[5] = someCustomDataAsArray[1];
_headerAsArray[6] = someCustomDataAsArray[2];
_headerAsArray[7] = someCustomDataAsArray[3];
}
return _headerAsArray.AsSpan();
}
public override string ToString() => $"Payload length: {PayloadLength} - Some custom data: {SomeCustomData}";
#region IEquatable
public override bool Equals(object obj) => Equals((Header)obj);
public override int GetHashCode() => HashCode.Combine(PayloadLength, SomeCustomData);
public bool Equals(Header other) => PayloadLength == other.PayloadLength && SomeCustomData.Equals(other.SomeCustomData);
public static bool operator ==(Header left, Header right) => left.Equals(right);
public static bool operator !=(Header left, Header right) => !(left == right);
#endregion
}
}