-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetMessage.cs
258 lines (231 loc) · 7.82 KB
/
NetMessage.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using VKanave.Networking.NetObjects;
using VKanaveServer;
namespace VKanave.Networking.NetMessages
{
public abstract class NetMessage
{
public static NetMessage Create(byte[] newBuffer)
{
int position = 0;
int size = 0;
byte[] buffer = new byte[4];
buffer[0] = newBuffer[position];
buffer[1] = newBuffer[position + 1];
buffer[2] = newBuffer[position + 2];
buffer[3] = newBuffer[position + 3];
size = BitConverter.ToInt32(buffer);
position += 4;
byte[] nmType = new byte[size];
for (int i = 0; i < size; i++)
{
nmType[i] = newBuffer[position + i];
}
position += size;
string nmTypeStr = Encoding.UTF8.GetString(nmType, 0, size);
Type type = Type.GetType(nmTypeStr);
NetMessage message = (NetMessage)Activator.CreateInstance(type);
message._buffer = newBuffer;
message._position = position;
return message;
}
public void Serialize()
{
_position = 0;
Type t = GetType();
Write(t.FullName);
foreach (var fieldInfo in t.GetFields())
{
if (fieldInfo.FieldType == typeof(int))
{
var value = (int)fieldInfo.GetValue(this);
Program.Log(LogType.SrlzLow, $"Serialized {fieldInfo.Name}. Value: {value}");
Write(value);
}
else if (fieldInfo.FieldType == typeof(string))
{
var value = (string)fieldInfo.GetValue(this);
Program.Log(LogType.SrlzLow, $"Serialized {fieldInfo.Name}. Value: {value}");
Write(value);
}
else if (fieldInfo.FieldType == typeof(long))
{
var value = (long)fieldInfo.GetValue(this);
Program.Log(LogType.SrlzLow, $"Serialized {fieldInfo.Name}. Value: {value}");
Write(value);
}
}
OnSerialize();
Write(new byte[BUFFER_SIZE]);
}
public void Deserialize()
{
Type t = GetType();
var fields = t.GetFields();
Program.Log(LogType.SrlzHight, $"type: {t}. fields: {fields.Length}.");
foreach (var field in fields)
{
if (field.FieldType == typeof(string))
{
var value = (string)ReadString();
Program.Log(LogType.SrlzLow, $"Deserialized {field.Name}. Value: {value}");
field.SetValue(this, value);
}
else if (field.FieldType == typeof(int))
{
var value = (int)ReadInt();
Program.Log(LogType.SrlzLow, $"Deserialized {field.Name}. Value: {value}");
field.SetValue(this, value);
}
else if (field.FieldType == typeof(long))
{
var value = (long)ReadLong();
Program.Log(LogType.SrlzLow, $"Deserialized {field.Name}. Value: {value}");
field.SetValue(this, value);
}
}
OnDeserialize();
}
protected virtual void OnSerialize()
{
}
protected virtual void OnDeserialize()
{
}
#region Write
protected void Write(string value)
{
byte[] data = Encoding.UTF8.GetBytes(value);
Write((short)data.Length);
Write(data);
}
protected void Write(int value)
{
byte size = sizeof(int);
Resize(size);
byte[] bytes = BitConverter.GetBytes(value);
for (int i = 0; i < bytes.Length; i++)
{
_buffer[_position + i] = bytes[i];
}
_position += bytes.Length;
}
protected void Write(long value)
{
byte size = sizeof(long);
Resize(size);
byte[] bytes = BitConverter.GetBytes(value);
for (int i = 0; i < bytes.Length; i++)
{
_buffer[_position + i] = bytes[i];
}
_position += bytes.Length;
}
protected void Write(ChatMessage message)
{
Write(message.User);
Write(message.ID);
Write(message.Content);
Write(message.Date);
Write(message.Flags);
}
protected void Write(ChatUser user)
{
Write(user.ID);
Write(user.Username);
Write(user.LastActive);
Write(user.DisplayName);
}
protected void Write(byte[] bytes)
{
int size = bytes.Length;
Resize(size);
for (int i = 0; i < bytes.Length; i++)
{
_buffer[_position + i] = bytes[i];
}
_position += bytes.Length;
}
#endregion
#region Read
protected string ReadString()
{
int lenght = ReadInt();
byte[] data = ReadBytes(lenght);
return Encoding.UTF8.GetString(data, 0, lenght);
}
protected byte[] ReadBytes(int size)
{
byte[] data = new byte[size];
for (int i = 0; i < size; i++)
{
data[i] = _buffer[_position + i];
}
_position += size;
return data;
}
protected int ReadInt()
{
byte[] buffer = new byte[4];
buffer[0] = _buffer[_position];
buffer[1] = _buffer[_position + 1];
buffer[2] = _buffer[_position + 2];
buffer[3] = _buffer[_position + 3];
int value = BitConverter.ToInt32(buffer);
_position += 4;
return value;
}
protected long ReadLong()
{
byte[] buffer = new byte[8];
buffer[0] = _buffer[_position];
buffer[1] = _buffer[_position + 1];
buffer[2] = _buffer[_position + 2];
buffer[3] = _buffer[_position + 3];
buffer[4] = _buffer[_position + 4];
buffer[5] = _buffer[_position + 5];
buffer[6] = _buffer[_position + 6];
buffer[7] = _buffer[_position + 7];
long value = BitConverter.ToInt64(buffer);
_position += 8;
return value;
}
protected ChatUser ReadUser()
{
long id = ReadLong();
string username = ReadString();
int lastActive = ReadInt();
string displayName = ReadString();
return new ChatUser(id, username, lastActive, displayName);
}
protected ChatMessage ReadChatMessage()
{
ChatUser user = ReadUser();
long id = ReadLong();
string content = ReadString();
int date = ReadInt();
int flags = ReadInt();
return new ChatMessage(user, id, content, date, flags);
}
#endregion
private void Resize(int needBytes)
{
while (_position + needBytes > _buffer.Length)
Array.Resize(ref _buffer, _buffer.Length + BUFFER_SIZE);
}
public byte[] Buffer
{
get => _buffer;
}
public const int BUFFER_SIZE = 64;
private byte[] _buffer = new byte[BUFFER_SIZE];
private int _position;
}
}