-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStunMessage.cs
More file actions
49 lines (37 loc) · 1.46 KB
/
StunMessage.cs
File metadata and controls
49 lines (37 loc) · 1.46 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace IceColdMirror.Stun {
public class StunMessage {
public StunMessageHeader header = new StunMessageHeader();
public List<StunAttribute> attributes = new List<StunAttribute>();
public StunMessage() {
this.header = new StunMessageHeader();
}
public StunMessage(Stream stream) {
this.header = new StunMessageHeader(stream);
var pos = 0;
while (pos < header.Length) {
var attr = new StunAttribute(stream, this);
pos += attr.AttrbiuteLength;
this.attributes.Add(attr);
}
}
public byte[] Serialize() {
var serializedAttributes = this.attributes.Select(a => a.Serialize());
this.header.Length = (ushort)serializedAttributes.Sum(a => a.Length);
var message = new byte[this.header.Length + 20];
var curIndex = 20;
foreach (var attr in serializedAttributes) {
Array.Copy(attr, 0, message, curIndex, attr.Length);
curIndex += attr.Length;
}
var header = this.header.Serialize();
Array.Copy(header, 0, message, 0, 20);
return message;
}
public override string ToString() => $"{this.header}\n{string.Join("\n", this.attributes.Select(a=>a.ToString()))}";
}
}