Skip to content

Commit 148fc2a

Browse files
committed
c
1 parent 720ab8c commit 148fc2a

File tree

7 files changed

+307
-67
lines changed

7 files changed

+307
-67
lines changed

ConsoleTest/Program.cs

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,41 @@ internal class Program
3030

3131
static void Main(string[] args)
3232
{
33-
TcpTest();
33+
TcpTest();
34+
return;
3435
//UdpTest();
36+
byte[] buffer = new byte[1024];
37+
ArraySegment<byte> seg = new ArraySegment<byte>(buffer);
38+
Queue<byte[]> qq = new Queue<byte[]>();
39+
Queue<ArraySegment<byte>> qq1 = new Queue<ArraySegment<byte>>();
40+
Console.ReadLine();
41+
42+
for (int i = 0; i < 50000000; i++)
43+
{
44+
qq.Enqueue(buffer);
45+
}
46+
Console.ReadLine();
47+
while (qq.Count>1)
48+
{
49+
var a = qq.Dequeue();
50+
a = null;
51+
}
52+
Console.WriteLine("dq");
53+
Console.ReadLine();
54+
55+
56+
Console.WriteLine("eqdq");
57+
58+
Console.ReadLine();
59+
Console.WriteLine("gc");
60+
GC.Collect();
61+
Console.ReadLine();
62+
return;
63+
for (int i = 0; i < 55000000; i++)
64+
{
65+
qq.Enqueue(buffer);
66+
}
67+
Console.ReadLine();
3568
}
3669

3770
static void UdpTest()
@@ -192,46 +225,55 @@ private static void TcpTest()
192225
List<ByteProtocolTcpClient> clients = new List<ByteProtocolTcpClient>();
193226

194227

195-
int clAmount = 5;
196-
bool V2 = false;
228+
int clAmount = 1;
229+
bool V2 = true;
230+
server.V2 = V2;
231+
197232
for (int i = 0; i < clAmount; i++)
198233
{
234+
199235
var client = new ByteProtocolTcpClient();
236+
client.V2 = V2;
237+
200238
client.ConnectAsyncAwaitable("127.0.0.1", 2008).Wait();
201239
Console.WriteLine(server.Sessions.Count);
202240

203241
client.OnBytesRecieved += clientMsgRec;
204-
client.V2 = V2;
205242
clients.Add(client);
206243
}
207244
//client.SendAsync(new byte[123]);
208245

209246
server.OnBytesRecieved += SWOnMsgRecieved;
210-
server.V2 = V2;
211247
Console.ReadLine();
212248
Console.WriteLine(server.Sessions.Count);
213249
Console.ReadLine();
214-
215-
var msg = new byte[500];
250+
var msg = new byte[32];
216251
for (int i = 0; i < msg.Length; i++)
217252
{
218253
msg[i] = 11;
219254
}
255+
clients[0].SendAsync(new byte[50000000]);
256+
//Thread.Sleep(300);
257+
clients[0].SendAsync(new byte[50000000]);
258+
220259
var t1 = new Thread(() =>
221260
{
222-
for (int i = 0; i < 200000; i++)
261+
for (int i = 0; i < 20000000; i++)
223262
{
224263
foreach (var client in clients)
225264
{
226265
client.SendAsync(msg);
266+
if (i == 200000)
267+
client.SendAsync(new byte[50000000]);
268+
227269
}
228270
}
229271

230272
foreach (var client in clients)
231273
{
232274
client.SendAsync(new byte[502]);
233275
}
234-
276+
235277
});
236278
t1.Start();
237279
sw2.Start();
@@ -253,6 +295,7 @@ private static void TcpTest()
253295
Console.WriteLine("2-- "+sw2.ElapsedMilliseconds);
254296

255297
Console.ReadLine();
298+
GC.Collect();
256299
Console.WriteLine(totMsgsw);
257300
Console.WriteLine(totMsgCl);
258301
Console.ReadLine();
@@ -267,7 +310,6 @@ private static void TcpTest()
267310
{
268311

269312
server.BroadcastByteMsg(new byte[500]);
270-
271313
}
272314
server.BroadcastByteMsg(new byte[502]);
273315

@@ -287,6 +329,10 @@ private static void clientMsgRec( byte[] arg2)
287329
sw.Reset();
288330
return;
289331
}
332+
if (arg2.Length != 5000)
333+
{
334+
// throw new Exception();
335+
}
290336
//for (int i = 0; i < arg2.Length; i++)
291337
//{
292338
// if (arg2[i] != 11)
@@ -295,14 +341,12 @@ private static void clientMsgRec( byte[] arg2)
295341
Interlocked.Increment(ref totMsgCl);
296342
}
297343

344+
public static byte[] response = new byte[32];
298345
private static void SWOnMsgRecieved(Guid arg1, byte[] arg2)
299346
{
300347

301348
//Console.WriteLine(arg2.Length);
302-
if (arg2.Length < 5000)
303-
{
304-
// throw new Exception();
305-
}
349+
306350
if (arg2.Length == 502)
307351
{
308352
//sw.Stop();
@@ -312,6 +356,10 @@ private static void SWOnMsgRecieved(Guid arg1, byte[] arg2)
312356
//sw2.Reset();
313357
return;
314358
}
359+
if (arg2.Length != 5000)
360+
{
361+
// throw new Exception();
362+
}
315363
server.SendBytesToClient(arg1, arg2);
316364
///* cq.Enqueue*/Task.Run(() => {
317365
//for (int i = 0; i < arg2.Length; i++)

CustomNetworkLib/ByteMessageManager.cs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ internal class MessageBuffer
1414
public int CurrentMsgBufferPos;
1515
public int CurrentHeaderBufferPos;
1616
public int ExpectedMsgLenght;
17-
17+
private int originalCapacity;
1818

1919
public MessageBuffer(int capacity, int currentWriteIndex = 0)
2020
{
2121
this.messageBuffer = new byte[capacity];
22+
originalCapacity=capacity;
2223
CurrentMsgBufferPos = currentWriteIndex;
2324
}
2425

@@ -36,11 +37,11 @@ private void Extend(int size)
3637
Array.Resize(ref messageBuffer, size + messageBuffer.Length);
3738
}
3839

39-
public void AppendHeaderChunk(byte[] headerPart,int count)
40+
public void AppendHeaderChunk(byte[] headerPart,int offset,int count)
4041
{
4142
for (int i = 0; i < count; i++)
4243
{
43-
AppendHeaderByte(headerPart[i]);
44+
AppendHeaderByte(headerPart[i+offset]);
4445
}
4546
}
4647

@@ -63,6 +64,7 @@ public byte[] GetMessageBytes()
6364
{
6465
byte[] bytes = new byte[ExpectedMsgLenght];
6566
Buffer.BlockCopy(messageBuffer, 0,bytes, 0,ExpectedMsgLenght);
67+
6668
return bytes;
6769
}
6870

@@ -71,6 +73,11 @@ internal void Reset()
7173
CurrentHeaderBufferPos = 0;
7274
CurrentMsgBufferPos= 0;
7375
ExpectedMsgLenght = 0;
76+
if (messageBuffer.Length > originalCapacity)
77+
{
78+
this.messageBuffer = new byte[originalCapacity];
79+
GC.Collect();
80+
}
7481
}
7582
}
7683
internal class ByteMessageManager
@@ -119,31 +126,32 @@ private void HandleBytes(byte[] incomingBytes, int offset, int count)
119126

120127
private void HandleHeader(byte[] incomingBytes, int offset, int count)
121128
{
122-
if (count >= currentExpectedByteLenght)
129+
if (count-offset >= currentExpectedByteLenght)
123130
{
124-
messageBuffer.AppendHeaderChunk(incomingBytes, currentExpectedByteLenght);
131+
messageBuffer.AppendHeaderChunk(incomingBytes, 0,currentExpectedByteLenght);
125132

126133
// perfect msg
127134
if (count - currentExpectedByteLenght == messageBuffer.ExpectedMsgLenght)
128135
{
129-
MessageReady(incomingBytes, MessageBuffer.HeaderLenght, messageBuffer.ExpectedMsgLenght);
136+
MessageReady(incomingBytes, currentExpectedByteLenght, messageBuffer.ExpectedMsgLenght);
130137
messageBuffer.Reset();
131138
}
132139
// multiple msgs or partial incomplete msg.
133140
else
134141
{
142+
offset += currentExpectedByteLenght;
143+
//count -= currentExpectedByteLenght;
144+
135145
currentState = OperationState.AwaitingMsgBody;
136146
currentExpectedByteLenght = messageBuffer.ExpectedMsgLenght;
137-
offset += MessageBuffer.HeaderLenght;
138-
count -= MessageBuffer.HeaderLenght;
139147
HandleBody(incomingBytes, offset, count);
140148
}
141149
}
142150
// Fragmented header. we will get on next call,
143151
else
144152
{
145-
messageBuffer.AppendHeaderChunk(incomingBytes, count);
146-
currentExpectedByteLenght = MessageBuffer.HeaderLenght - count;
153+
messageBuffer.AppendHeaderChunk(incomingBytes,0, count);
154+
currentExpectedByteLenght = MessageBuffer.HeaderLenght - (count-offset);
147155
}
148156

149157
}
@@ -152,7 +160,7 @@ private void HandleHeader(byte[] incomingBytes, int offset, int count)
152160
private void HandleBody(byte[] incomingBytes, int offset, int count)
153161
{
154162
// overflown message
155-
while (count >= currentExpectedByteLenght)
163+
while (count-offset >= currentExpectedByteLenght)
156164
{
157165
// nothing from prev call
158166
if (messageBuffer.CurrentMsgBufferPos == 0)
@@ -169,49 +177,46 @@ private void HandleBody(byte[] incomingBytes, int offset, int count)
169177
}
170178

171179
offset += currentExpectedByteLenght;
172-
count -= currentExpectedByteLenght;
180+
//count -= currentExpectedByteLenght;
173181
// can read byte frame.
174-
if (count >= MessageBuffer.HeaderLenght)
182+
if (count -offset>= MessageBuffer.HeaderLenght)
175183
{
176184
currentExpectedByteLenght = BufferManager.ReadByteFrame(incomingBytes, offset);
177185
offset += MessageBuffer.HeaderLenght;
178-
count -= MessageBuffer.HeaderLenght;
186+
//count -= MessageBuffer.HeaderLenght;
179187
}
188+
//if (count < 4)
189+
//{ }
180190
// incomplete byte frame
181191
else
182192
{
183-
messageBuffer.AppendMessageChunk(incomingBytes, offset, count);
193+
messageBuffer.AppendHeaderChunk(incomingBytes, offset, count-offset);
184194
currentState = OperationState.AwaitingMsgHeader;
185-
currentExpectedByteLenght = MessageBuffer.HeaderLenght - count;
195+
currentExpectedByteLenght = MessageBuffer.HeaderLenght - (count-offset);
186196

187197
return;
188198
}
189199
}
190200

191201
// incomplete msg,
192-
if (count < currentExpectedByteLenght)
202+
if (count-offset < currentExpectedByteLenght)
193203
{
194-
messageBuffer.AppendMessageChunk(incomingBytes, offset, count);
195-
currentExpectedByteLenght = currentExpectedByteLenght - count;
204+
messageBuffer.AppendMessageChunk(incomingBytes, offset, count-offset);
205+
currentExpectedByteLenght = currentExpectedByteLenght - (count-offset);
196206

197207
}
198-
//// perfect size
199-
//else
200-
//{
201-
// MessageReady(incomingBytes, offset, count);
202-
// messageBuffer.Reset();
203-
204-
// currentExpectedByteLenght = MessageBuffer.HeaderLenght;
205-
// currentState = OperationState.AwaitingMsgHeader;
206-
//}
208+
207209
}
208210
private void MessageReady(MessageBuffer buffer)
209211
{
210212
//var byteMsg = buffer.GetMessageBytes();
211-
OnMessageReady?.Invoke(buffer.messageBuffer, 0, buffer.CurrentMsgBufferPos);
213+
//OnMessageReady?.Invoke(buffer.messageBuffer, 0, buffer.CurrentMsgBufferPos);
214+
MessageReady(buffer.messageBuffer, 0, buffer.CurrentMsgBufferPos);
212215
}
213-
private void MessageReady(byte[] byteMsg, int offset,int count)
216+
private void MessageReady(byte[] byteMsg, int offset, int count)
214217
{
218+
if (count != 5)
219+
{ }
215220
OnMessageReady?.Invoke(byteMsg, offset, count);
216221
}
217222
}

0 commit comments

Comments
 (0)