-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking.cs
More file actions
389 lines (343 loc) · 14.3 KB
/
Networking.cs
File metadata and controls
389 lines (343 loc) · 14.3 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Runtime.InteropServices;
using Venomcc.Utility.Networking;
using Venomcc.UI;
using System.Data;
namespace Venomcc.Networking
{
public enum SendMessageType
{
Normal = 0,
Broadcast = 1,
}
public struct connSockInfo
{
public readonly object _connectionsLock = new object();
public Dictionary<string, Socket> connections = new Dictionary<string, Socket>();
public Socket? connSock = null;
public readonly ushort port;
public IPAddress? connSockIPAddress = null;
public IPEndPoint? connSockEndPoint = null;
public SocketAddress? connSockAddress;
public ushort chunkSize = 1024;
public string IpAsString()
{
if (connSockIPAddress != null) return connSockIPAddress.ToString();
else return "NOT AN IP";
}
public connSockInfo(ushort port)
{
if (connSock == null)
{
connSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
this.port = port;
}
}
public void SetEndPoint()
{
if (connSockIPAddress != null)
{
connSockEndPoint = new IPEndPoint(connSockIPAddress, this.port);
connSockAddress = connSockEndPoint.Serialize();
connSockEndPoint = (IPEndPoint)connSockEndPoint.Create(connSockAddress);
}
}
}
public abstract class NetUser
{
static public Dictionary<string, string> MessageHeaderTags = new Dictionary<string, string>()
{
// <SM> = starting point of a message (STARTMESSAGE)
// <EM> = ending point of a message (ENDMESSAGE)
// <SCD> = signifies the start of continues data as it could not be contained in one message (START CONTINUES DATA)
// <CCD> = signifies the continuation of a continues data stream (CONTINUATION CONTINUES DATA)
// <SC> = signifies the start of a command to be executed (STARTCOMMAND)
// <EC> = signifies the end of a command to be executed (ENDCOMMAND)
["SM"] = "<~SM~>",
["EM"] = "<~EM~>",
//["SCD"] = "<~SCD~>",
["CCD"] = "<~CCD~>",
["SC"] = "<~SC~>",
["EC"] = "<~EC~>",
};
public enum MessageHeaderTagTypes
{
SM, EM, SCD, CCD, SC, EC
};
static public Queue<(IPEndPoint endPoint, ArraySegment<byte> data)> nextOutData = new Queue<(IPEndPoint endPoint, ArraySegment<byte> data)>();
readonly protected object _lock;
protected connSockInfo netUserInfo;
public NetUser(object _lock, ushort port)
{
netUserInfo = new connSockInfo(port);
if (netUserInfo.connSock != null) netUserInfo.connSock.ReceiveBufferSize = netUserInfo.chunkSize;
this._lock = _lock;
}
public IPEndPoint? GetEndPoint()
{
if (netUserInfo.connSockEndPoint != null)
{
return netUserInfo.connSockEndPoint;
}
return null;
}
public void GetConnections()
{
uint idx = 1;
lock (netUserInfo._connectionsLock)
{
foreach (KeyValuePair<string, Socket> entry in netUserInfo.connections)
{
ConsoleUI.enqueueConsoleData(ConsoleMessageType.NEWSECTION,
"[/] Client[" + idx + "]: " + entry.Key + " . . .\n");
idx++;
}
}
}
public int GetConnectionsCount()
{
return netUserInfo.connections.Count();
}
public void ConsumeNextOutgoingData()
{
if (netUserInfo.connSock != null && netUserInfo.connSockEndPoint != null)
{
lock (netUserInfo._connectionsLock)
{
(string? endPoint, ArraySegment<byte>? data) outgoing = ConsoleUI.ConsumeNextOutgoingData();
if (outgoing.endPoint != null && outgoing.data != null)
{
netUserInfo.connSock.SendTo(outgoing.data.Value, IPEndPoint.Parse(outgoing.endPoint));
}
}
}
}
private async Task Send(SendMessageType typeData, dynamic outData, string? endPoint = null)
{
await Task.Run(() =>
{
lock (netUserInfo._connectionsLock)
{
if (netUserInfo.connections.Count() > 0 && netUserInfo.connSock != null)
{
List<byte[]> dataChunks = NetworkUtilities.generateDataChunksWithHeaderTags(outData, 1024);
switch (typeData)
{
case SendMessageType.Normal:
if (endPoint != null)
{
foreach (byte[] chunk in dataChunks)
{
ConsoleUI.enqueueOutData(ConsoleMessageType.None, chunk, endPoint.ToString());
}
}
break;
case SendMessageType.Broadcast:
foreach (KeyValuePair<string, Socket> entry in netUserInfo.connections)
{
foreach (byte[] chunk in dataChunks)
{
ConsoleUI.enqueueOutData(ConsoleMessageType.None, chunk, entry.Key.ToString());
}
}
break;
}
}
}
});
}
public async Task SendTo(SendMessageType typeData, string outData, [Optional] string endPoint)
{
string formattedOutData = MessageHeaderTags["SM"] + outData + MessageHeaderTags["EM"];
await Send(typeData, formattedOutData, endPoint);
}
public async Task SendTo(SendMessageType typeData, ArraySegment<byte> outData, [Optional] string endPoint)
{
byte[]? formattedOutData = null;
if (outData.Array != null)
{
formattedOutData = new byte[MessageHeaderTags["SM"].Length +
outData.Array.Length +
MessageHeaderTags["EM"].Length];
Buffer.BlockCopy(Encoding.UTF8.GetBytes(MessageHeaderTags["SM"]), 0, formattedOutData, 0, MessageHeaderTags["SM"].Length);
Buffer.BlockCopy(outData.Array, 0, formattedOutData, MessageHeaderTags["SM"].Length, outData.Array.Length);
Buffer.BlockCopy(Encoding.UTF8.GetBytes(MessageHeaderTags["EM"]), 0, formattedOutData, MessageHeaderTags["SM"].Length + outData.Array.Length, MessageHeaderTags["EM"].Length);
}
ArraySegment<byte>? formattedOutDataSegment = null;
if (formattedOutData != null)
{
formattedOutDataSegment = new ArraySegment<byte>(formattedOutData);
}
if (formattedOutDataSegment != null)
{
await Send(typeData, formattedOutDataSegment, endPoint);
}
}
public abstract void ReceiveData();
public abstract Task Connect();
public abstract Task Disconnect();
}
public class Server : NetUser
{
public Server(object _lock, string ip, ushort port) : base(_lock, port)
{
IPAddress.TryParse(ip, out netUserInfo.connSockIPAddress);
netUserInfo.SetEndPoint();
}
public Server(object _lock, ushort port) : base(_lock, port)
{
netUserInfo.connSockIPAddress = IPAddress.Any;
netUserInfo.SetEndPoint();
}
public void AcceptIncomingConnections()
{
while (true)
{
Socket newConnection;
IPEndPoint? newConnectionEndPoint;
if (netUserInfo.connSock != null)
{
newConnection = netUserInfo.connSock.Accept();
newConnectionEndPoint = newConnection.RemoteEndPoint as IPEndPoint;
if (newConnectionEndPoint != null)
{
lock (netUserInfo._connectionsLock)
{
netUserInfo.connections.Add(newConnectionEndPoint.ToString(), newConnection);
ConsoleUI.enqueueConsoleData(ConsoleMessageType.NEWSECTION,
"[+] Received new connection from: " + newConnection.RemoteEndPoint + " . . .");
GetConnections();
}
}
}
}
}
public override void ReceiveData()
{
while (true)
{
if (netUserInfo.connSock != null)
{
ArraySegment<byte> incomingData = new ArraySegment<byte>(new byte[netUserInfo.chunkSize]);
int receivedAmount = 0;
string? currentConnectionSelected = null;
List<string> savedConnectionData = new List<string>();
string finalData = "";
string dataAsString;
byte[] resizedData;
lock (netUserInfo._connectionsLock)
{
foreach (KeyValuePair<string, Socket> connectionKVP in netUserInfo.connections)
{
while (connectionKVP.Value.Available > 0)
{
currentConnectionSelected = connectionKVP.Key;
receivedAmount = connectionKVP.Value.Receive(incomingData);
resizedData = incomingData.Take(receivedAmount).ToArray();
dataAsString = Encoding.UTF8.GetString(resizedData);
savedConnectionData.Add(dataAsString);
if (incomingData.Array != null) Array.Clear(incomingData.Array, 0, incomingData.Array.Length);
}
if (currentConnectionSelected != null && savedConnectionData.Count() > 0)
{
for (int dataIdx = 0; dataIdx < savedConnectionData.Count(); dataIdx++)
{
finalData += savedConnectionData[dataIdx];
}
finalData = NetworkUtilities.parseMessageIgnoringHeaderTags(finalData);
ConsoleUI.enqueueConsoleData(ConsoleMessageType.None, finalData);
finalData = "";
savedConnectionData.Clear();
}
}
}
}
}
}
public async override Task Connect()
{
await Bind();
await Listen();
}
public async override Task Disconnect()
{
await Task.Run(() =>
{
if (netUserInfo.connSock != null)
{
netUserInfo.connSock.Shutdown(SocketShutdown.Both);
netUserInfo.connSock.Close();
ConsoleUI.enqueueConsoleData(ConsoleMessageType.NEWSECTION,
"[!] Socket powering down. . .");
}
});
}
protected async Task Bind()
{
await Task.Run(() =>
{
if (netUserInfo.connSock != null && netUserInfo.connSockEndPoint != null)
{
netUserInfo.connSock.Bind(netUserInfo.connSockEndPoint);
ConsoleUI.enqueueConsoleData(ConsoleMessageType.NEWLINE,
"[+] Bound to " + netUserInfo.connSockEndPoint.ToString() + " . . .");
}
});
}
protected async Task Listen()
{
await Task.Run(() =>
{
if (netUserInfo.connSock != null)
{
netUserInfo.connSock.Listen(10);
ConsoleUI.enqueueConsoleData(ConsoleMessageType.NEWSECTION,
"[+] Listening for incoming connection(s) . . .");
}
});
}
}
public class Client : NetUser
{
public Client(object _lock, string ip, ushort port) : base(_lock, port)
{
IPAddress.TryParse(ip, out netUserInfo.connSockIPAddress);
netUserInfo.SetEndPoint();
}
public override void ReceiveData()
{
}
public async override Task Connect()
{
await Task.Run(() =>
{
if (netUserInfo.connSock != null && netUserInfo.connSockEndPoint != null)
{
netUserInfo.connSock.ConnectAsync(netUserInfo.connSockEndPoint);
lock (netUserInfo._connectionsLock)
{
netUserInfo.connections.Add(netUserInfo.connSockEndPoint.ToString(), netUserInfo.connSock);
}
}
});
}
public async override Task Disconnect()
{
await Task.Run(() =>
{
if (netUserInfo.connSock != null)
{
netUserInfo.connSock.Shutdown(SocketShutdown.Both);
netUserInfo.connSock.Close();
ConsoleUI.enqueueConsoleData(ConsoleMessageType.NEWSECTION,
"[!] Socket powering down . . .");
}
});
}
}
}