1
using System.IO;
2
using System.Text;
3

4
namespace Libplanet.Stun.Attributes
5
{
6
    public class ErrorCode : Attribute
7
    {
8 1
        public ErrorCode(int code, string reason)
9 1
        {
10 1
            Code = code;
11 1
            Reason = reason;
12 1
        }
13

14 1
        public override AttributeType Type => AttributeType.ErrorCode;
15

16 1
        public int Code { get; }
17

18 1
        public string Reason { get; }
19

20
        internal static ErrorCode Parse(byte[] payload)
21 1
        {
22 1
            byte @class = payload[2];
23 1
            byte code = payload[3];
24 1
            string reason = Encoding.ASCII.GetString(
25 1
                payload, 4, payload.Length - 4);
26

27 1
            return new ErrorCode(@class * 100 + code, reason);
28 1
        }
29

30
        protected override byte[] EncodePayload(byte[] transactionId)
31 1
        {
32 1
            using var ms = new MemoryStream();
33 1
            ms.WriteByte(0x00);
34 1
            ms.WriteByte(0x00);
35 1
            ms.WriteByte((byte)(Code / 100));
36 1
            ms.WriteByte((byte)(Code % 100));
37

38 1
            var reason = Encoding.ASCII.GetBytes(Reason);
39 1
            ms.Write(reason, 0, reason.Length);
40

41 1
            return ms.ToArray();
42 1
        }
43
    }
44
}

Read our documentation on viewing source code .

Loading