Skip to content

Commit 3179cc7

Browse files
committed
Further Development
1 parent 48a2121 commit 3179cc7

Some content is hidden

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

53 files changed

+3082
-1337
lines changed

Benchmarks/SslBenchmark/Program.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,27 @@ private static void Bench()
3636
var scert = new X509Certificate2("server.pfx", "greenpass");
3737
var ccert = new X509Certificate2("client.pfx", "greenpass");
3838

39-
SSlByteMessageServer server = new SSlByteMessageServer(2008, clientAmount * 2, scert);
40-
List<SsLByteMessageClient> clients = new List<SsLByteMessageClient>();
39+
SslByteMessageServer server = new SslByteMessageServer(2008, clientAmount * 2, scert);
40+
List<SslByteMessageClient> clients = new List<SslByteMessageClient>();
4141

4242
Stopwatch sw2 = new Stopwatch();
4343
AutoResetEvent testCompletionEvent = new AutoResetEvent(false);
4444

45-
var message = new byte[32];
46-
var response = new byte[32];
45+
var message = new byte[3200];
46+
var response = new byte[3200];
4747

48-
server.MaxMemoryPerClient = 1280000000;
49-
server.DropOnCongestion = false;
48+
server.MaxIndexedMemoryPerClient = 1280000000;
49+
server.DropOnBackPressure = false;
5050
server.OnBytesReceived += OnServerReceviedMessage;
5151
server.StartServer();
5252

5353
Task[] toWait = new Task[clientAmount];
5454
for (int i = 0; i < clientAmount; i++)
5555
{
56-
var client = new SsLByteMessageClient(ccert);
56+
var client = new SslByteMessageClient(ccert);
57+
client.BufferProvider = server.BufferProvider;
5758
client.OnBytesReceived += (buffer, offset, count) => OnClientReceivedMessage(client, buffer, offset, count);
58-
client.MaxIndexedMemory = 1280000000;
59+
client.MaxIndexedMemory = server.MaxIndexedMemoryPerClient;
5960
client.Connect("127.0.0.1", 2008);
6061
clients.Add(client);
6162
}
@@ -65,7 +66,7 @@ private static void Bench()
6566
Console.Read();
6667
sw2.Start();
6768

68-
const int numMsg = 100000;
69+
const int numMsg = 10000;
6970
Parallel.ForEach(clients, client =>
7071
{
7172
for (int i = 0; i < numMsg; i++)
@@ -111,7 +112,7 @@ void ShowStatus()
111112
Console.WriteLine("Data transmissıon rate Outbound " + response.Length * messagePerSecond / 1000000 + " Megabytes/s");
112113
}
113114

114-
void OnClientReceivedMessage(SsLByteMessageClient client, byte[] arg2, int offset, int count)
115+
void OnClientReceivedMessage(SslByteMessageClient client, byte[] arg2, int offset, int count)
115116
{
116117
Interlocked.Increment(ref totMsgClient);
117118

Benchmarks/SslBenchmark/SslBenchmark.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<Reference Include="NetworkLibrary">
12-
<HintPath>..\..\NetworkLibrary\bin\Debug\netstandard2.0\NetworkLibrary.dll</HintPath>
12+
<HintPath>..\..\NetworkLibrary\bin\Release\netstandard2.0\NetworkLibrary.dll</HintPath>
1313
</Reference>
1414
</ItemGroup>
1515

Benchmarks/SslBenchmark2/Program.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using NetworkLibrary.TCP.SSL.ByteMessage;
1414
using NetworkLibrary.TCP.SSL.Custom;
15+
using System.Security.Cryptography.X509Certificates;
1516

1617
namespace ConsoleTest
1718
{
@@ -32,30 +33,35 @@ private static void CustomSslTest()
3233
int totMsgServer = 0;
3334
int lastTimeStamp = 1;
3435
int clientAmount = 100;
36+
const int numMsg = 10000;
37+
3538

36-
CustomSslServer server = new CustomSslServer(2008, "server.pfx",clientAmount * 2 );
39+
var scert = new X509Certificate2("server.pfx", "greenpass");
40+
var ccert = new X509Certificate2("client.pfx", "greenpass");
41+
42+
CustomSslServer server = new CustomSslServer(2008, scert, clientAmount * 2 );
3743
List<CustomSslClient> clients = new List<CustomSslClient>();
3844

3945
Stopwatch sw2 = new Stopwatch();
4046
AutoResetEvent testCompletionEvent = new AutoResetEvent(false);
4147

42-
var message = new byte[32];
43-
var response = new byte[32];
48+
var message = new byte[3200];
49+
var response = new byte[3200];
4450

45-
server.MaxIndexedMemoryPerClient = 1280000;
51+
server.MaxIndexedMemoryPerClient = 1280000000;
4652

4753
server.ClientSendBufsize = 128000;
4854
server.ClientReceiveBufsize = 128000;
4955
server.DropOnBackPressure = false;
50-
server.OnBytesRecieved += OnServerReceviedMessage;
56+
server.OnBytesReceived += OnServerReceviedMessage;
5157
server.StartServer();
5258

5359
Task[] toWait = new Task[clientAmount];
5460
for (int i = 0; i < clientAmount; i++)
5561
{
56-
var client = new CustomSslClient("client.pfx");
62+
var client = new CustomSslClient(ccert);
5763
client.BufferManager = server.BufferManager;
58-
client.OnBytesRecieved += ( byte[] arg2, int offset, int count) => OnClientReceivedMessage(client, arg2, offset, count);
64+
client.OnBytesReceived += ( byte[] arg2, int offset, int count) => OnClientReceivedMessage(client, arg2, offset, count);
5965
client.MaxIndexedMemory = server.MaxIndexedMemoryPerClient;
6066
client.ConnectAsyncAwaitable("127.0.0.1", 2008).Wait();
6167
clients.Add(client);
@@ -67,7 +73,6 @@ private static void CustomSslTest()
6773
Console.Read();
6874
sw2.Start();
6975

70-
const int numMsg = 10000;
7176
Parallel.ForEach(clients, client =>
7277
{
7378
for (int i = 0; i < numMsg; i++)
@@ -91,8 +96,10 @@ private static void CustomSslTest()
9196
Console.WriteLine("Press E to Exit");
9297
while (Console.ReadLine() != "e")
9398
{
94-
95-
99+
ShowStatus();
100+
}
101+
void ShowStatus()
102+
{
96103
Console.WriteLine("Press E to Exit");
97104

98105
Console.WriteLine("Total Messages on server: " + totMsgServer);
@@ -105,7 +112,6 @@ private static void CustomSslTest()
105112
Console.WriteLine(" Request-Response Per second " + totMsgClient / elapsedSeconds);
106113
Console.WriteLine("Data transmissıon rate Inbound " + (message.Length * messagePerSecond) / 1000000 + " Megabytes/s");
107114
Console.WriteLine("Data transmissıon rate Outbound " + (response.Length * messagePerSecond) / 1000000 + " Megabytes/s");
108-
//Console.WriteLine("Elapsed total MS " + sw2.ElapsedMilliseconds);
109115
}
110116

111117
void OnClientReceivedMessage(CustomSslClient client, byte[] arg2, int offset, int count)
@@ -115,6 +121,13 @@ void OnClientReceivedMessage(CustomSslClient client, byte[] arg2, int offset, in
115121
if (count == 502)
116122
{
117123
lastTimeStamp = (int)sw2.ElapsedMilliseconds;
124+
if(Volatile.Read(ref totMsgClient) == numMsg * clientAmount + clientAmount)
125+
{
126+
Console.WriteLine("--- All Clients are finished receiving response --- \n");
127+
ShowStatus();
128+
sw2.Stop();
129+
Console.WriteLine("\n--- All Clients are finished receiving response --- \n");
130+
}
118131
}
119132
}
120133

Benchmarks/SslBenchmark2/SslBenchmark2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>

Benchmarks/TcpBenchmark/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ private static void TcpTest()
3434
int totMsgServer = 0;
3535
int lastTimeStamp = 1;
3636
int clientAmount = 100;
37+
const int numMsg = 1000000;
38+
3739

3840
ByteMessageTcpServer server = new ByteMessageTcpServer(2008, clientAmount*2);
3941
List<ByteMessageTcpClient> clients = new List<ByteMessageTcpClient>();
@@ -50,7 +52,7 @@ private static void TcpTest()
5052
server.ClientSendBufsize = 128000;
5153
server.ClientReceiveBufsize = 128000;
5254
server.DropOnBackPressure = false;
53-
server.OnBytesRecieved += OnServerReceviedMessage;
55+
server.OnBytesReceived += OnServerReceviedMessage;
5456
server.StartServer();
5557

5658
Task[] toWait = new Task[clientAmount];
@@ -61,20 +63,20 @@ private static void TcpTest()
6163
client.MaxIndexedMemory = server.MaxIndexedMemoryPerClient;
6264

6365
client.DropOnCongestion = false;
64-
client.OnBytesRecieved += (byte[] arg2, int offset, int count) => OnClientReceivedMessage(client, arg2, offset, count);
66+
client.OnBytesReceived += (byte[] arg2, int offset, int count) => OnClientReceivedMessage(client, arg2, offset, count);
6567

6668
toWait[i] = client.ConnectAsyncAwaitable("127.0.0.1", 2008);
6769
clients.Add(client);
6870
}
6971

7072
Task.WaitAll(toWait);
73+
7174
// ----------------------- Bechmark ---------------------------
7275
Console.WriteLine("Press any key to start");
7376
Console.Read();
7477
sw2.Start();
7578

7679
// We parallely send the messages here
77-
const int numMsg = 500000;
7880
Parallel.ForEach(clients, client =>
7981
{
8082
for (int i = 0; i < numMsg; i++)
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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 AesAlgorithm
9+
{
10+
protected Aes algorithm;
11+
protected ICryptoTransform encryptor;
12+
protected ICryptoTransform decryptor;
13+
14+
public int EncryptorInputBlockSize { get => encryptor.InputBlockSize; }
15+
public int EncryptorOutputBlockSize { get => encryptor.OutputBlockSize; }
16+
public int DecryptorInputBlockSize { get => decryptor.InputBlockSize; }
17+
public int DecryptorOutputBlockSize { get => decryptor.OutputBlockSize; }
18+
19+
private byte[] finalBlock= new byte[0];
20+
public AesAlgorithm(byte[] Key, byte[] IV,string algorithmName = null)
21+
{
22+
if(algorithmName == null)
23+
{
24+
algorithm = Aes.Create();
25+
}
26+
else
27+
{
28+
algorithm = Aes.Create(algorithmName);
29+
}
30+
31+
algorithm.Key = Key;
32+
algorithm.IV = IV;
33+
34+
encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);
35+
decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
36+
}
37+
38+
public int GetEncriptorOutputSize(int inputSize)
39+
{
40+
return inputSize + (EncryptorOutputBlockSize - (inputSize % EncryptorOutputBlockSize));
41+
}
42+
43+
/// <summary>
44+
/// Decrypts a message into new byte array
45+
/// </summary>
46+
/// <param name="message"></param>
47+
/// <returns></returns>
48+
public byte[] Decrypt(byte[] message)
49+
{
50+
byte[] output = decryptor.TransformFinalBlock(message, 0, message.Length);
51+
return output;
52+
53+
}
54+
/// <summary>
55+
/// Decrypts a message from buffer region into new byte array
56+
/// </summary>
57+
/// <param name="buffer"></param>
58+
/// <param name="offset"></param>
59+
/// <param name="count"></param>
60+
/// <returns></returns>
61+
public byte[] Decrypt(byte[] buffer, int offset, int count)
62+
{
63+
byte[] output = decryptor.TransformFinalBlock(buffer, offset, count);
64+
return output;
65+
66+
}
67+
68+
/// <summary>
69+
/// Decryps a message partially into output buffer without the final block.
70+
/// </summary>
71+
/// <param name="source"></param>
72+
/// <param name="sourceOffset"></param>
73+
/// <param name="sourceCount"></param>
74+
/// <param name="output"></param>
75+
/// <param name="outputOffset"></param>
76+
/// <returns></returns>
77+
public int PartialDecrpytInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset)
78+
{
79+
return decryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset);
80+
81+
}
82+
83+
/// <summary>
84+
/// Decrypts a message into output destination array fully.
85+
/// </summary>
86+
/// <param name="source"></param>
87+
/// <param name="sourceOffset"></param>
88+
/// <param name="sourceCount"></param>
89+
/// <param name="output"></param>
90+
/// <param name="outputOffset"></param>
91+
/// <returns></returns>
92+
public int DecryptInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset)
93+
{
94+
int amountDecripted = decryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset);
95+
96+
var lastChunk = Decrypt(finalBlock);
97+
98+
if (lastChunk.Length != 0)
99+
{
100+
Buffer.BlockCopy(lastChunk, 0, output, outputOffset + amountDecripted, lastChunk.Length);
101+
amountDecripted += lastChunk.Length;
102+
}
103+
104+
return amountDecripted;
105+
}
106+
107+
/// <summary>
108+
/// Encrypts a message and retuns the encripted array as a new byte array
109+
/// </summary>
110+
/// <param name="message"></param>
111+
/// <returns></returns>
112+
public byte[] Encrypt(byte[] message)
113+
{
114+
return encryptor.TransformFinalBlock(message, 0, message.Length);
115+
116+
}
117+
118+
/// <summary>
119+
/// Encrypts a message from buffer region and retuns the encripted array as a new byte array
120+
/// </summary>
121+
/// <param name="message"></param>
122+
/// <returns></returns>
123+
public byte[] Encrypt(byte[] buffer, int offset, int count)
124+
{
125+
return encryptor.TransformFinalBlock(buffer, offset, count);
126+
127+
}
128+
129+
/// <summary>
130+
/// Encrypts a message partially into output buffer without the final block.
131+
/// </summary>
132+
/// <param name="source"></param>
133+
/// <param name="sourceOffset"></param>
134+
/// <param name="sourceCount"></param>
135+
/// <param name="output"></param>
136+
/// <param name="outputOffset"></param>
137+
/// <returns></returns>
138+
public int PartialEncrpytInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset)
139+
{
140+
return encryptor.TransformBlock(source, sourceOffset, sourceCount, output, outputOffset);
141+
142+
}
143+
/// <summary>
144+
/// Decrypts a message into output destination array fully.
145+
/// </summary>
146+
/// <param name="source"></param>
147+
/// <param name="sourceOffset"></param>
148+
/// <param name="sourceCount"></param>
149+
/// <param name="output"></param>
150+
/// <param name="outputOffset"></param>
151+
/// <returns></returns>
152+
public int EncryptInto(byte[] source, int sourceOffset, int sourceCount, byte[] output, int outputOffset)
153+
{
154+
// it has to be multiple of block size to do partial.
155+
if (sourceCount < EncryptorOutputBlockSize)
156+
{
157+
byte[] oneShot = Encrypt(source, sourceOffset, sourceCount);
158+
Buffer.BlockCopy(oneShot, 0, output, outputOffset, oneShot.Length);
159+
return oneShot.Length;
160+
}
161+
162+
int partialTransformAmount = sourceCount - sourceCount % EncryptorOutputBlockSize;
163+
int amountDecripted = encryptor.TransformBlock(source, sourceOffset, partialTransformAmount, output, outputOffset);
164+
165+
byte[] lastChunk = Encrypt(source, sourceOffset + amountDecripted, sourceCount % EncryptorOutputBlockSize);
166+
167+
if (lastChunk.Length != 0)
168+
{
169+
Buffer.BlockCopy(lastChunk, 0, output, outputOffset + amountDecripted, lastChunk.Length);
170+
amountDecripted += lastChunk.Length;
171+
}
172+
173+
return amountDecripted;
174+
}
175+
176+
177+
}
178+
}

0 commit comments

Comments
 (0)