-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packet.cs
151 lines (124 loc) · 3.72 KB
/
Packet.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace tbsgNetLib{
public class Packet
{
public Packet()
{
this.bytes.Clear();
readPos = 0;
}
public Packet(byte[] data)
{
Create(data);
}
public void Write<T>(T data) where T : IComparable<T>
{
var byteArray = GetBytes(data);
Array.Reverse(byteArray, 0, byteArray.Length);
this.bytes.AddRange(byteArray);
}
public void Write(string data)
{
var byteArray = Encoding.UTF8.GetBytes(NetUtils.Utf16ToUtf8(data));
Write((uint)byteArray.Length);
this.bytes.AddRange(byteArray);
}
public void Write(Packet packet)
{
this.bytes.AddRange(packet.Bytes);
}
public void Write(byte[] data)
{
this.bytes.AddRange(data);
}
public void Create(byte[] data)
{
this.bytes.Clear();
this.bytes.AddRange(data);
readPos = 0;
}
private int SizeOf<T>()
{
if (typeof(T).IsClass)
{
return Marshal.SizeOf(typeof(T));
}else if (typeof(T) == typeof(bool))
{
return 1;
}
return Marshal.SizeOf(default(T));
}
public T Read<T>()
{
var size = SizeOf<T>();//Marshal.SizeOf(default(T));
var range = bytes.GetRange(readPos, size).ToArray();
Array.Reverse(range, 0, size);
readPos += size;
return FromBytes<T>(range);
}
public uint ReadUint()
{
var size = Marshal.SizeOf(default(uint));
var range = bytes.GetRange(readPos, size).ToArray();
Array.Reverse(range,0, size);
var value = BitConverter.ToUInt32(range, 0);
readPos += size;
return value;
}
public string ReadString()
{
var size = Read<int>();
char[] chars = new char[size];
var range = bytes.GetRange(readPos, size).ToArray();
//Array.Reverse(range, 0, size);
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(range, 0, size, chars, 0);
System.String result = new System.String(chars);
readPos += size;
return NetUtils.Utf8ToUtf16(result);
}
private static byte[] GetBytes<T>(T obj)
{
int size = Marshal.SizeOf(obj);
byte[] arr = new byte[size];
GCHandle h = default(GCHandle);
try
{
h = GCHandle.Alloc(arr, GCHandleType.Pinned);
Marshal.StructureToPtr<T>(obj, h.AddrOfPinnedObject(), false);
}
finally
{
if (h.IsAllocated)
{
h.Free();
}
}
return arr;
}
private static T FromBytes<T>(byte[] arr)
{
T str;
GCHandle h = default(GCHandle);
try
{
h = GCHandle.Alloc(arr, GCHandleType.Pinned);
str = Marshal.PtrToStructure<T>(h.AddrOfPinnedObject());
}
finally
{
if (h.IsAllocated)
{
h.Free();
}
}
return str;
}
public byte[] Bytes => bytes.ToArray();
private List<byte> bytes = new List<byte>();
private int readPos = 0;
};
}