-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketReader.cs
57 lines (44 loc) · 1.74 KB
/
PacketReader.cs
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
using System;
public class PacketReader<T> {
public PacketReader(string data, Func<string, T> converter, int unit_size = 1) {
Data = data;
UnitSize = unit_size;
Converter = converter;
}
public string Data { get; private set; }
public int UnitSize { get; }
public Func<string, T> Converter { get; set; }
public T Read(int length, Func<string, T> converter = null) {
ValidateLength(length);
var value = Data.Substring(0, UnitSize * length);
Data = Data.Substring(UnitSize * length);
return Process(value, converter);
}
public N CustomRead <N> (int length, Func<string, N> converter) {
ValidateLength(length);
var value = Data.Substring(0, UnitSize * length);
Data = Data.Substring(UnitSize * length);
return converter(value);
}
public T ReadEnd (int length, Func<string, T> converter = null) {
ValidateLength(length);
var value = Data.Substring(Data.Length - (length*UnitSize));
Data = Data.Substring(0, Data.Length - (length*UnitSize));
return Process(value, converter);
}
void ValidateLength (int length) {
if (length < 0) throw new Exception($"Read failed because the length of the data is invalid.");
if (length > Data.Length) throw new Exception($"Length of substring was greater than the string length.");
}
T Process(string value, Func<string, T> converter = null) {
if (converter == null) {
return Converter(value);
} else {
return converter(value);
}
}
}
public class PacketReader : PacketReader<string> {
public PacketReader(string data, int index = 0, int unit_size = 1) : base(data, (x) => x, unit_size) {
}
}