-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
396 lines (328 loc) · 17.1 KB
/
Program.cs
File metadata and controls
396 lines (328 loc) · 17.1 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
390
391
392
393
394
395
396
using NetworkLibrary;
using NetworkLibrary.P2P;
using NetworkLibrary.TCP.ByteMessage;
using NetworkLibrary.TCP.SSL.ByteMessage;
using NetworkLibrary.Utils;
using ProtoBuf;
using Protobuff;
using Protobuff.P2P;
using Protobuff.Pure;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using NetworkLibrary.Components.Crypto.Certificate;
using NetworkLibrary.TCP.Base;
using NetworkLibrary.TCP.SSL.Base;
namespace Examples
{
internal class Program
{
static void Main(string[] args)
{
MiniLogger.AllLog += (log)=>Console.WriteLine(log);
//ExampleByteMessage();
//ExampleSecureByteMessage();
//ExamplePureProto();
//ExamplePureSecureProto();
//ExampleProtoMessageProtocol();
ExampleProtoMessageProtocolSecure();
//ExampleSecureP2P();
//ExampleRoomServer();
var msg = new MessageEnvelope();
Console.ReadLine();
}
#region AsyncTcp
private static void ExampleAsyncTcp()
{
AsyncTcpServer server = new AsyncTcpServer(20008);
server.OnBytesReceived += ServerBytesReceived;
server.StartServer();
AsyncTpcClient client = new AsyncTpcClient();
client.OnBytesReceived += ClientBytesReceived;
client.Connect("127.0.0.1", 20008);
client.SendAsync(Encoding.UTF8.GetBytes("Hello I'm a client!"));
void ServerBytesReceived(Guid clientId, byte[] bytes, int offset, int count)
{
Console.WriteLine(Encoding.UTF8.GetString(bytes, offset, count));
server.SendBytesToClient(clientId, Encoding.UTF8.GetBytes("Hello I'm the server"));
}
void ClientBytesReceived(byte[] bytes, int offset, int count)
{
Console.WriteLine(Encoding.UTF8.GetString(bytes, offset, count));
}
}
private static void ExampleSSL()
{
//var scert = new X509Certificate2("server.pfx", "greenpass");
//var cert = new X509Certificate2("client.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var cert = CertificateGenerator.GenerateSelfSignedCertificate();
SslServer server = new SslServer(20008,scert);
server.OnBytesReceived += ServerBytesReceived;
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.StartServer();
SslClient client = new SslClient(cert);
client.OnBytesReceived += ClientBytesReceived;
client.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
client.Connect("127.0.0.1", 20008);
client.SendAsync(Encoding.UTF8.GetBytes("Hello I'm a client!"));
void ServerBytesReceived(Guid clientId, byte[] bytes, int offset, int count)
{
Console.WriteLine(Encoding.UTF8.GetString(bytes, offset, count));
server.SendBytesToClient(clientId, Encoding.UTF8.GetBytes("Hello I'm the server"));
}
void ClientBytesReceived(byte[] bytes, int offset, int count)
{
Console.WriteLine(Encoding.UTF8.GetString(bytes, offset, count));
}
}
#endregion
#region Byte Message
private static void ExampleByteMessage()
{
ByteMessageTcpServer server = new ByteMessageTcpServer(20008);
server.OnBytesReceived += ServerBytesReceived;
server.StartServer();
ByteMessageTcpClient client = new ByteMessageTcpClient();
client.OnBytesReceived += ClientBytesReceived;
client.Connect("127.0.0.1", 20008);
client.SendAsync(Encoding.UTF8.GetBytes("Hello I'm a client!"));
void ServerBytesReceived(Guid clientId, byte[] bytes, int offset, int count)
{
Console.WriteLine(Encoding.UTF8.GetString(bytes, offset, count));
server.SendBytesToClient(clientId, Encoding.UTF8.GetBytes("Hello I'm the server"));
}
void ClientBytesReceived(byte[] bytes, int offset, int count)
{
Console.WriteLine(Encoding.UTF8.GetString(bytes, offset, count));
}
}
private static void ExampleSecureByteMessage()
{
//var scert = new X509Certificate2("server.pfx", "greenpass");
//var cert = new X509Certificate2("client.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var cert = CertificateGenerator.GenerateSelfSignedCertificate();
var server = new SslByteMessageServer(20008, scert);
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.OnBytesReceived += ServerBytesReceived;
// since certificate is self-signed it will give chain errors, here we bypass it
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.StartServer();
var client = new SslByteMessageClient(cert);
client.OnBytesReceived += ClientBytesReceived;
client.RemoteCertificateValidationCallback += (a, b, c, d) => true;
client.Connect("127.0.0.1", 20008);
client.SendAsync(UTF8Encoding.UTF8.GetBytes("Hello I'm a client!"));
// Callback handlers
void ServerBytesReceived(Guid clientId, byte[] bytes, int offset, int count)
{
Console.WriteLine(UTF8Encoding.UTF8.GetString(bytes, offset, count));
server.SendBytesToClient(clientId, UTF8Encoding.UTF8.GetBytes("Hello I'm the server"));
}
void ClientBytesReceived(byte[] bytes, int offset, int count)
{
Console.WriteLine(UTF8Encoding.UTF8.GetString(bytes, offset, count));
}
}
#endregion
#region Pure Seialized Server/Client
[ProtoContract]
class SampleMessage : IProtoMessage
{
[ProtoMember(1)]
public string sample;
}
private static void ExamplePureProto()
{
PureProtoServer server = new PureProtoServer(11234);
server.StartServer();
server.BytesReceived += (clientId,bytes, offset, count) =>
{
SampleMessage msg = server.Serializer.Deserialize<SampleMessage>(bytes, offset, count);
Console.WriteLine(msg.sample);
msg.sample = "Jesse Lets cook";
server.SendAsync(clientId,msg);
};
PureProtoClient client = new PureProtoClient();
client.Connect("127.0.0.1", 11234);
client.BytesReceived += (bytes, offset, count) =>
{
SampleMessage msg = client.Serializer.Deserialize<SampleMessage>(bytes, offset, count);
Console.WriteLine(msg.sample);
};
client.SendAsync(new SampleMessage() { sample = "Yo! Mr White" });
Console.ReadLine();
}
private static void ExamplePureSecureProto()
{
//var scert = new X509Certificate2("server.pfx", "greenpass");
//var ccert = new X509Certificate2("client.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var ccert = CertificateGenerator.GenerateSelfSignedCertificate();
var server = new PureSecureProtoServer(11111,scert);
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.StartServer();
server.BytesReceived += (clientId, bytes, offset, count) =>
{
SampleMessage msg = server.Serializer.Deserialize<SampleMessage>(bytes, offset, count);
Console.WriteLine(msg.sample);
msg.sample = "Jesse Lets cook";
server.SendAsync(clientId, msg);
};
var client = new PureSecureProtoClient(ccert);
client.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
client.Connect("127.0.0.1", 11111);
client.BytesReceived += (bytes, offset, count) =>
{
SampleMessage msg = client.Serializer.Deserialize<SampleMessage>(bytes, offset, count);
Console.WriteLine(msg.sample);
};
client.SendAsync(new SampleMessage() { sample = "Yo! Mr White" });
Console.ReadLine();
}
#endregion
#region Message Protocol
[ProtoContract]
class SamplePayload : IProtoMessage
{
[ProtoMember(1)]
public string sample;
}
private static async Task ExampleProtoMessageProtocolSecure()
{
//var scert = new X509Certificate2("server.pfx", "greenpass");
//var cert = new X509Certificate2("client.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var cert = CertificateGenerator.GenerateSelfSignedCertificate();
SecureProtoMessageServer server = new SecureProtoMessageServer(20008, scert);
server.StartServer();
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.OnMessageReceived += ServerMessageReceived;
var client = new SecureProtoMessageClient(cert);
client.OnMessageReceived += ClientMessageReceived;
client.Connect("127.0.0.1", 20008);
var Payload = new SamplePayload() { sample = "Hello" };
var messageEnvelope = new MessageEnvelope();
messageEnvelope.Header = "PayloadTest";
// You can just send a message, get replies on ClientMessageReceived.
client.SendAsyncMessage(messageEnvelope);
client.SendAsyncMessage(messageEnvelope, Payload);
// Or you can wait for a reply async.
MessageEnvelope result = await client.SendMessageAndWaitResponse(messageEnvelope, Payload);
var payload = result.UnpackPayload<SamplePayload>();
Console.WriteLine($"Client Got Response {payload.sample}");
void ServerMessageReceived(Guid clientId, MessageEnvelope message)
{
Console.WriteLine($"Server Received message {message.Header}");
server.SendAsyncMessage(clientId, message);
}
void ClientMessageReceived(MessageEnvelope message)
{
}
}
private static async Task ExampleProtoMessageProtocol()
{
var server = new ProtoMessageServer(20008);
server.OnMessageReceived += ServerMessageReceived;
var client = new ProtoMessageClient();
client.OnMessageReceived += ClientMessageReceived;
client.Connect("127.0.0.1", 20008);
var Payload = new SamplePayload() { sample = "Hello" };
var messageEnvelope = new MessageEnvelope();
messageEnvelope.Header = "PayloadTest";
// You can just send a message, get replies on ClientMessageReceived.
client.SendAsyncMessage(messageEnvelope);
client.SendAsyncMessage(messageEnvelope, Payload);
// Or you can wait for a reply async.
MessageEnvelope result = await client.SendMessageAndWaitResponse(messageEnvelope, Payload);
var payload = result.UnpackPayload<SamplePayload>();
Console.WriteLine($"Client Got Response {payload.sample}");
void ServerMessageReceived(Guid clientId, MessageEnvelope message)
{
Console.WriteLine($"Server Received message {message.Header}");
server.SendAsyncMessage(clientId, message);
}
void ClientMessageReceived(MessageEnvelope message)
{
}
}
#endregion
#region P2P
private static void ExampleSecureP2P()
{
//var cert = new X509Certificate2("client.pfx", "greenpass");
//var scert = new X509Certificate2("server.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var cert = CertificateGenerator.GenerateSelfSignedCertificate();
var server = new SecureProtoRelayServer(20010, scert);
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.StartServer();
var clients = new List<RelayClient>();
for (int i = 0; i < 2; i++)
{
var client = new RelayClient(cert);
client.OnMessageReceived += (reply) => ClientMsgReceived(client, reply);
client.OnUdpMessageReceived += (reply) => ClientUdpReceived(client, reply);
client.OnPeerRegistered += (peerId) => OnPeerRegistered(client, peerId);
client.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
client.Connect("127.0.0.1", 20010);
clients.Add(client);
}
Thread.Sleep(3000);
Guid destinationId = clients[0].Peers.First().Key;
var succes = clients[0].RequestHolePunchAsync(destinationId, 5000).Result;
var message = new SamplePayload() { sample = "hello" };
clients[0].SendAsyncMessage(destinationId, message);
// message handling
void OnPeerRegistered(RelayClient client, Guid peerId)
{
Console.WriteLine(client.SessionId + " Says: A peer registered: " + peerId);
}
void ClientMsgReceived(RelayClient client, MessageEnvelope reply)
{
Console.WriteLine(client.SessionId + " Received Message From " + reply.From);
Console.WriteLine(client.SessionId + " Sending Udp message to: " + reply.From);
client.SendUdpMessage(reply.From, reply);
}
void ClientUdpReceived(RelayClient client, MessageEnvelope reply)
{
Console.WriteLine(client.SessionId + "Udp Message Received from: " + reply.From);
}
}
//private static void ExampleRoomServer()
//{
// var cert = new X509Certificate2("client.pfx", "greenpass");
// var scert = new X509Certificate2("server.pfx", "greenpass");
// var server = new RoomServer(20010, scert);
// server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
// server.StartServer();
// var client1 = new SecureProtoRoomClient(cert);
// client1.OnTcpRoomMesssageReceived += (roomName, m) => Console.WriteLine($"Tcp Received from Room [{roomName}] -> " + m.Header);
// client1.OnUdpRoomMesssageReceived += (roomName, m) => Console.WriteLine($"Udp Received from Room [{roomName}] -> " + m.Header);
// client1.OnTcpMessageReceived += (m) => Console.WriteLine("Private Tcp Received -> " + m.Header);
// client1.OnUdpMessageReceived += (m) => Console.WriteLine("Private Udp Received ->" + m.Header);
// client1.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
// client1.Connect("127.0.0.1", 20010);
// client1.CreateOrJoinRoom("MyGarage");
// var client2 = new SecureProtoRoomClient(cert);
// client2.OnTcpRoomMesssageReceived += (roomName, m) => Console.WriteLine($"Tcp Received from Room [{roomName}] -> " + m.Header);
// client2.OnUdpRoomMesssageReceived += (roomName, m) => Console.WriteLine($"Udp Received from Room [{roomName}] -> " + m.Header);
// client2.OnTcpMessageReceived += (m) => Console.WriteLine("Private Tcp Received -> " + m.Header);
// client2.OnUdpMessageReceived += (m) => Console.WriteLine("Private Udp Received ->" + m.Header);
// client2.Connect("127.0.0.1", 20010);
// client2.CreateOrJoinRoom("MyGarage");
// Thread.Sleep(1000);
// Guid peerId = client2.SessionId;
// var succes = client1.RequestHolePunchAsync(peerId, 5000).Result;
// Thread.Sleep(1000);
// Console.WriteLine("");
// client1.BroadcastUdpMessageToRoom("MyGarage", new MessageEnvelope() { Header = "Hello Udp", Payload = new byte[128000] });
// client1.BroadcastRudpMessageToRoom("MyGarage", new MessageEnvelope() { Header = "Hello Rudp", Payload = new byte[128000] });
// client1.BroadcastMessageToRoom("MyGarage", new MessageEnvelope() { Header = "Hello Tcp", Payload = new byte[128000] });
// client1.SendUdpMessage(peerId, new MessageEnvelope() { Header = "Hello Private Udp", Payload = new byte[128000] });
// client1.SendRudpMessage(peerId, new MessageEnvelope() { Header = "Hello Private Rudp", Payload = new byte[128000] });
// client1.SendAsyncMessage(peerId, new MessageEnvelope() { Header = "Hello Private Tcp", Payload = new byte[128000] });
//}
#endregion
}
}