Skip to content

Commit 0d4208a

Browse files
committed
Commit Ssl Client-Server, Project Refactor
1 parent 0a0e0c6 commit 0d4208a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1688
-466
lines changed

Benchmarks/SslBenchmark/Program.cs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using NetworkLibrary.TCP.ByteMessage;
2+
using NetworkLibrary.Utils;
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Net;
10+
using System.Text;
11+
using System.Threading;
12+
using System.Threading.Tasks;
13+
using NetworkLibrary.TCP.SSL.ByteMessage;
14+
using System.Security.Cryptography.X509Certificates;
15+
16+
namespace SslBenchmark
17+
{
18+
19+
internal class Program
20+
{
21+
static void Main(string[] args)
22+
{
23+
Bench();
24+
25+
}
26+
private static void Bench()
27+
{
28+
MiniLogger.AllLog += (log) => Console.WriteLine(log);
29+
30+
int NumFinishedClients = 0;
31+
int totMsgClient = 0;
32+
int totMsgServer = 0;
33+
int lastTimeStamp = 1;
34+
int clientAmount = 100;
35+
36+
var scert = new X509Certificate2("server.pfx", "greenpass");
37+
var ccert = new X509Certificate2("client.pfx", "greenpass");
38+
39+
SSlByteMessageServer server = new SSlByteMessageServer(2008, clientAmount * 2, scert);
40+
List<SsLByteMessageClient> clients = new List<SsLByteMessageClient>();
41+
42+
Stopwatch sw2 = new Stopwatch();
43+
AutoResetEvent testCompletionEvent = new AutoResetEvent(false);
44+
45+
var message = new byte[32];
46+
var response = new byte[32];
47+
48+
server.MaxMemoryPerClient = 1280000000;
49+
server.DropOnCongestion = false;
50+
server.OnBytesReceived += OnServerReceviedMessage;
51+
server.StartServer();
52+
53+
Task[] toWait = new Task[clientAmount];
54+
for (int i = 0; i < clientAmount; i++)
55+
{
56+
var client = new SsLByteMessageClient(ccert);
57+
client.OnBytesReceived += (buffer, offset, count) => OnClientReceivedMessage(client, buffer, offset, count);
58+
client.MaxIndexedMemory = 1280000000;
59+
client.Connect("127.0.0.1", 2008);
60+
clients.Add(client);
61+
}
62+
63+
// ----------------------- Bechmark ---------------------------
64+
Console.WriteLine("Press enter to start");
65+
Console.Read();
66+
sw2.Start();
67+
68+
const int numMsg = 100000;
69+
Parallel.ForEach(clients, client =>
70+
{
71+
for (int i = 0; i < numMsg; i++)
72+
{
73+
//message = new byte[32000];
74+
client.SendAsync(message);
75+
76+
}
77+
78+
});
79+
// final msg to get the tıme elapsed.
80+
foreach (var cl in clients)
81+
{
82+
cl.SendAsync(new byte[502]);
83+
}
84+
85+
86+
Console.WriteLine("All messages are dispatched in :" + sw2.ElapsedMilliseconds +
87+
"ms. Press enter to see status");
88+
Console.ReadLine();
89+
90+
Console.WriteLine("Press e to Exit");
91+
while (Console.ReadLine()!= "e")
92+
{
93+
ShowStatus();
94+
}
95+
96+
void ShowStatus()
97+
{
98+
Console.WriteLine("Press E to Exit");
99+
100+
Console.WriteLine("Total Messages on server: " + totMsgServer);
101+
Console.WriteLine("Total Messages on clients: " + totMsgClient);
102+
103+
lastTimeStamp = (int)sw2.ElapsedMilliseconds;
104+
Console.WriteLine("Elapsed " + lastTimeStamp);
105+
106+
var elapsedSeconds = (float)lastTimeStamp / 1000;
107+
var messagePerSecond = totMsgClient / elapsedSeconds;
108+
109+
Console.WriteLine(" Request-Response Per second " + totMsgClient / elapsedSeconds);
110+
Console.WriteLine("Data transmissıon rate Inbound " + message.Length * messagePerSecond / 1000000 + " Megabytes/s");
111+
Console.WriteLine("Data transmissıon rate Outbound " + response.Length * messagePerSecond / 1000000 + " Megabytes/s");
112+
}
113+
114+
void OnClientReceivedMessage(SsLByteMessageClient client, byte[] arg2, int offset, int count)
115+
{
116+
Interlocked.Increment(ref totMsgClient);
117+
118+
if (count == 502)
119+
{
120+
lastTimeStamp = (int)sw2.ElapsedMilliseconds;
121+
Interlocked.Increment(ref NumFinishedClients);
122+
123+
if(Volatile.Read(ref NumFinishedClients) == clientAmount)
124+
{
125+
Console.WriteLine("--- All Clients are finished receiving response --- \n");
126+
ShowStatus();
127+
sw2.Stop();
128+
Console.WriteLine("\n--- All Clients are finished receiving response --- \n");
129+
130+
}
131+
}
132+
}
133+
134+
void OnServerReceviedMessage(Guid id, byte[] arg2, int offset, int count)
135+
{
136+
Interlocked.Increment(ref totMsgServer);
137+
if (count == 502)
138+
{
139+
server.SendBytesToClient(id, new byte[502]);
140+
return;
141+
}
142+
// response = new byte[32000];
143+
server.SendBytesToClient(id, response);
144+
}
145+
146+
}
147+
148+
149+
150+
151+
}
152+
}
153+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Reference Include="NetworkLibrary">
12+
<HintPath>..\..\NetworkLibrary\bin\Debug\netstandard2.0\NetworkLibrary.dll</HintPath>
13+
</Reference>
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="client.pfx">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
<None Update="server.pfx">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>

Benchmarks/TcpBenchmark/Program.cs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using CustomNetworkLib;
2-
using CustomNetworkLib.SocketEventArgsTests;
3-
using NetworkSystem;
1+
using NetworkLibrary.TCP.ByteMessage;
2+
using NetworkLibrary.Utils;
43
using System;
54
using System.Collections.Concurrent;
65
using System.Collections.Generic;
@@ -23,9 +22,12 @@ static void Main(string[] args)
2322
TcpTest();
2423

2524
}
26-
//----------TCP --------------------------------------------------------
25+
2726
private static void TcpTest()
2827
{
28+
// dont change.
29+
int NumFinishedClients = 0;
30+
2931
MiniLogger.AllLog+=(string log)=>Console.WriteLine(log);
3032

3133
int totMsgClient = 0;
@@ -71,7 +73,8 @@ private static void TcpTest()
7173
Console.Read();
7274
sw2.Start();
7375

74-
const int numMsg = 1000000;
76+
// We parallely send the messages here
77+
const int numMsg = 500000;
7578
Parallel.ForEach(clients, client =>
7679
{
7780
for (int i = 0; i < numMsg; i++)
@@ -81,45 +84,60 @@ private static void TcpTest()
8184
}
8285

8386
});
87+
8488
// final msg to get the tıme elapsed.
8589
foreach (var cl in clients)
8690
{
8791
cl.SendAsync(new byte[502]);
8892
}
8993

90-
//------------------- 100m message 100 client 8.03 second
91-
Console.WriteLine("All messages are dispatched in :"+ sw2.ElapsedMilliseconds+
94+
// -------- Messages are sent by clients ------
95+
96+
Console.WriteLine("All messages are dispatched in :" + sw2.ElapsedMilliseconds +
9297
"ms. Press enter to see status");
9398
Console.ReadLine();
9499

95-
Console.WriteLine("Press E to Exit");
100+
Console.WriteLine("Press e to Exit");
96101
while (Console.ReadLine() != "e")
97102
{
98-
99-
103+
ShowStatus();
104+
}
105+
//----- End ---
106+
void ShowStatus()
107+
{
100108
Console.WriteLine("Press E to Exit");
101109

102110
Console.WriteLine("Total Messages on server: " + totMsgServer);
103111
Console.WriteLine("Total Messages on clients: " + totMsgClient);
104-
Console.WriteLine("Last Timestamp " + lastTimeStamp);
105-
Console.WriteLine("Elapsed " + sw2.ElapsedMilliseconds);
106-
var elapsedSeconds = ((float)lastTimeStamp / 1000);
112+
113+
lastTimeStamp = (int)sw2.ElapsedMilliseconds;
114+
Console.WriteLine("Elapsed " + lastTimeStamp);
115+
116+
var elapsedSeconds = (float)lastTimeStamp / 1000;
107117
var messagePerSecond = totMsgClient / elapsedSeconds;
108118

109-
Console.WriteLine(" Request-Response Per second " + totMsgClient/elapsedSeconds);
110-
Console.WriteLine("Data transmissıon rate Inbound " + (message.Length*messagePerSecond) / 1000000+ " Megabytes/s");
111-
Console.WriteLine("Data transmissıon rate Outbound " + (response.Length*messagePerSecond) / 1000000+ " Megabytes/s");
112-
//Console.WriteLine("Elapsed total MS " + sw2.ElapsedMilliseconds);
119+
Console.WriteLine(" Request-Response Per second " + totMsgClient / elapsedSeconds);
120+
Console.WriteLine("Data transmissıon rate Inbound " + message.Length * messagePerSecond / 1000000 + " Megabytes/s");
121+
Console.WriteLine("Data transmissıon rate Outbound " + response.Length * messagePerSecond / 1000000 + " Megabytes/s");
113122
}
114123

115-
server.StopServer();
116124
void OnClientReceivedMessage(ByteMessageTcpClient client, byte[] arg2, int offset, int count)
117125
{
118126
Interlocked.Increment(ref totMsgClient);
119-
//client.SendAsync(response);
120-
if(count == 502)
127+
128+
if (count == 502)
121129
{
122130
lastTimeStamp = (int)sw2.ElapsedMilliseconds;
131+
Interlocked.Increment(ref NumFinishedClients);
132+
133+
if (Volatile.Read(ref NumFinishedClients) == clientAmount)
134+
{
135+
Console.WriteLine("\n--- All Clients are finished receiving response --- \n");
136+
ShowStatus();
137+
sw2.Stop();
138+
Console.WriteLine("\n--- All Clients are finished receiving response --- \n");
139+
140+
}
123141
}
124142
}
125143

@@ -131,15 +149,13 @@ void OnServerReceviedMessage(Guid id, byte[] arg2, int offset, int count)
131149
server.SendBytesToClient(id, new byte[502]);
132150
return;
133151
}
134-
152+
// response = new byte[32000];
135153
server.SendBytesToClient(id, response);
136154
}
137155

138156
}
139157

140-
141-
142-
143158
}
159+
144160
}
145161

File renamed without changes.

Benchmarks/UdpBenchmark/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CustomNetworkLib;
2-
using NetworkSystem;
1+
using NetworkLibrary.UDP;
2+
using NetworkLibrary.Utils;
33
using System.Diagnostics;
44
using System.Net;
55

File renamed without changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
6+
namespace NetworkLibrary.Components
7+
{
8+
public class AesEncryptor
9+
{
10+
protected Aes algorithm;
11+
protected ICryptoTransform encryptor;
12+
protected ICryptoTransform decryptor;
13+
public AesEncryptor(byte[] Key, byte[] IV)
14+
{
15+
algorithm = Aes.Create();
16+
algorithm.Key = Key;
17+
algorithm.IV = IV;
18+
19+
encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);
20+
decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
21+
}
22+
23+
public byte[] Encrypt(byte[] message)
24+
{
25+
return encryptor.TransformFinalBlock(message, 0, message.Length);
26+
27+
}
28+
29+
public byte[] Encrypt(byte[] buffer, int offset, int count)
30+
{
31+
return encryptor.TransformFinalBlock(buffer, offset, count);
32+
33+
}
34+
35+
public byte[] Decrypt(byte[] message)
36+
{
37+
byte[] output = decryptor.TransformFinalBlock(message, 0, message.Length);
38+
return output;
39+
40+
}
41+
public byte[] Decrypt(byte[] buffer,int offset, int count)
42+
{
43+
byte[] output = decryptor.TransformFinalBlock(buffer, offset, count);
44+
return output;
45+
46+
}
47+
48+
public int DecryptInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset)
49+
{
50+
int amountDecripted = decryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset);
51+
52+
var lastChunk = Decrypt(new byte[0]);
53+
54+
if (lastChunk.Length != 0)
55+
{
56+
Buffer.BlockCopy(lastChunk, 0, source, sourceOffset + amountDecripted, lastChunk.Length);
57+
amountDecripted += lastChunk.Length;
58+
}
59+
60+
return amountDecripted;
61+
}
62+
63+
public int EncryptInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset)
64+
{
65+
int amountDecripted = encryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset);
66+
67+
var lastChunk = Encrypt(new byte[0]);
68+
69+
if (lastChunk.Length != 0)
70+
{
71+
Buffer.BlockCopy(lastChunk, 0, source, sourceOffset + amountDecripted, lastChunk.Length);
72+
amountDecripted += lastChunk.Length;
73+
}
74+
75+
return amountDecripted;
76+
}
77+
}
78+
}
File renamed without changes.

0 commit comments

Comments
 (0)