@@ -26,13 +26,19 @@ internal class Program
2626 //static AutoResetEvent are = new AutoResetEvent (false);
2727 static int totMsgCl = 0 ;
2828 static int totMsgsw = 0 ;
29- private static ByteProtocolTcpServer server ;
29+ private static ByteMessageTcpServer server ;
30+ static byte [ ] resp = new byte [ 32 ] ;
31+ static bool lastSW = false ;
32+ private static int prev = - 1 ;
33+ private static bool pause ;
3034
3135 static void Main ( string [ ] args )
3236 {
3337 TcpTest ( ) ;
34- return ;
38+
3539 //UdpTest();
40+
41+ return ;
3642 byte [ ] buffer = new byte [ 1024 ] ;
3743 ArraySegment < byte > seg = new ArraySegment < byte > ( buffer ) ;
3844 Queue < byte [ ] > qq = new Queue < byte [ ] > ( ) ;
@@ -69,7 +75,7 @@ static void Main(string[] args)
6975
7076 static void UdpTest ( )
7177 {
72- //// TcpTest();
78+ // TcpTest();
7379 //test();
7480 //return;
7581 AsyncUdpServer sw = new AsyncUdpServer ( 2008 ) ;
@@ -142,7 +148,7 @@ static void UdpSWBytsRec(int id, byte[] bytes)
142148
143149 }
144150
145- static void ClientBytesRecieved ( byte [ ] bytes )
151+ static void ClientBytesRecieved ( byte [ ] bytes , int offset , int count )
146152 {
147153 Interlocked . Increment ( ref totMsgCl ) ;
148154 // Console.WriteLine("udp client recieved");
@@ -202,7 +208,7 @@ private static void UdpTest2()
202208
203209 Console . ReadLine ( ) ;
204210 }
205- private static void ClienyRec ( byte [ ] bytes )
211+ private static void ClienyRec ( byte [ ] bytes , int offset , int count )
206212 {
207213 //Console.WriteLine(i);
208214 //s.Add(Thread.CurrentThread.ManagedThreadId);
@@ -221,24 +227,28 @@ private static void REc(int id, byte[] bytes)
221227 //----------TCP ----------------------------------------------------------------
222228 private static void TcpTest ( )
223229 {
224- server = new ByteProtocolTcpServer ( 2008 ) ;
225- List < ByteProtocolTcpClient > clients = new List < ByteProtocolTcpClient > ( ) ;
230+ server = new ByteMessageTcpServer ( 2008 ) ;
231+ List < ByteMessageTcpClient > clients = new List < ByteMessageTcpClient > ( ) ;
226232
227233
228234 int clAmount = 1 ;
235+ BufferManager . InitContigiousBuffers ( clAmount * 8 , 1280000 ) ;
236+
229237 bool V2 = true ;
230- server . V2 = V2 ;
238+ server . Lite = V2 ;
231239
232240 for ( int i = 0 ; i < clAmount ; i ++ )
233241 {
234242
235- var client = new ByteProtocolTcpClient ( ) ;
243+ var client = new ByteMessageTcpClient ( ) ;
236244 client . V2 = V2 ;
237245
238- client . ConnectAsyncAwaitable ( "127.0.0.1" , 2008 ) . Wait ( ) ;
246+ client . ConnectAsyncAwaitable ( "127.0.0.1" , 2008 ) ;
239247 Console . WriteLine ( server . Sessions . Count ) ;
240-
241- client . OnBytesRecieved += clientMsgRec ;
248+
249+ client . OnBytesRecieved += ( byte [ ] arg2 , int offset , int count ) => clientMsgRec2 ( client , arg2 , offset , count ) ;
250+ //client.OnBytesRecieved +=clientMsgRec;
251+ //client.OnConnected += () => Console.WriteLine("aaaaaaa");
242252 clients . Add ( client ) ;
243253 }
244254 //client.SendAsync(new byte[123]);
@@ -248,46 +258,66 @@ private static void TcpTest()
248258 Console . WriteLine ( server . Sessions . Count ) ;
249259 Console . ReadLine ( ) ;
250260 var msg = new byte [ 32 ] ;
261+ resp = msg ;
262+
251263 for ( int i = 0 ; i < msg . Length ; i ++ )
252264 {
253265 msg [ i ] = 11 ;
254266 }
255- clients [ 0 ] . SendAsync ( new byte [ 50000000 ] ) ;
256- //Thread.Sleep(300);
257- clients [ 0 ] . SendAsync ( new byte [ 50000000 ] ) ;
258267
268+
269+ const int numMsg = 100000000 ;
270+
259271 var t1 = new Thread ( ( ) =>
260- {
261- for ( int i = 0 ; i < 20000000 ; i ++ )
262272 {
263- foreach ( var client in clients )
264- {
265- client . SendAsync ( msg ) ;
266- if ( i == 200000 )
267- client . SendAsync ( new byte [ 50000000 ] ) ;
268-
269- }
270- }
271-
272- foreach ( var client in clients )
273- {
274- client . SendAsync ( new byte [ 502 ] ) ;
275- }
276-
277- } ) ;
273+ for ( int i = 0 ; i < numMsg ; i ++ )
274+ {
275+
276+ foreach ( var client in clients )
277+ {
278+ //msg = new byte[130000];
279+ //BufferManager.WriteInt32AsBytes(ref msg, 0, i);
280+
281+ client . SendAsync ( msg ) ;
282+
283+
284+ }
285+
286+ }
287+
288+ foreach ( var client in clients )
289+ {
290+
291+ client . SendAsync ( new byte [ 502 ] ) ;
292+ }
293+
294+ //Thread.Sleep(111);
295+
296+
297+ } ) ;
278298 t1 . Start ( ) ;
279299 sw2 . Start ( ) ;
280300
281- var t2 = new Thread ( ( ) => {
282- for ( int i = 0 ; i < 200000 ; i ++ )
283- {
284- server . BroadcastByteMsg ( msg ) ;
285- }
286- server . BroadcastByteMsg ( new byte [ 502 ] ) ;
287- } ) ;
288- // t2.Start();
301+
289302 sw . Start ( ) ;
290303
304+ //-------------------
305+ //Parallel.For(0, numMsg, i =>
306+ //{
307+ // foreach (var client in clients)
308+ // {
309+ // var msg1 = new byte[32];
310+ // ByteMessageSessionV2.FillHeader(ref msg1, 0, i);
311+ // client.SendAsync(msg1);
312+
313+ // }
314+
315+ //});
316+ //foreach (var client in clients)
317+ //{
318+ // client.SendAsync(new byte[502]);
319+ //}
320+ //-------------------
291321 t1 . Join ( ) ;
292322 Console . WriteLine ( sw2 . ElapsedMilliseconds ) ;
293323
@@ -296,97 +326,108 @@ private static void TcpTest()
296326
297327 Console . ReadLine ( ) ;
298328 GC . Collect ( ) ;
299- Console . WriteLine ( totMsgsw ) ;
300- Console . WriteLine ( totMsgCl ) ;
301- Console . ReadLine ( ) ;
329+ for ( int i = 0 ; i < 6 ; i ++ )
330+ {
331+ Console . WriteLine ( "Total on server: " + totMsgsw ) ;
332+ Console . WriteLine ( "Total on clients: " + totMsgCl ) ;
333+ Console . WriteLine ( "2-- " + sw2 . ElapsedMilliseconds ) ;
334+ Console . WriteLine ( "last was sw " + lastSW ) ;
335+
336+ Console . ReadLine ( ) ;
337+ if ( i == 2 )
338+ {
339+ Console . WriteLine ( "will pause server" ) ;
340+ pause = true ;
341+ }
342+ }
302343
344+ Console . ReadLine ( ) ;
303345
304- //client.SendAsync(new byte[502]);
305346
306- //OnMsgRecieved(Guid.NewGuid(), new byte[501]);
307- Thread . Sleep ( 3000 ) ;
308- sw . Start ( ) ;
309- for ( int i = 0 ; i < 100000 ; i ++ )
347+ Parallel . For ( 0 , clients . Count , i =>
310348 {
311349
312- server . BroadcastByteMsg ( new byte [ 500 ] ) ;
313- }
314- server . BroadcastByteMsg ( new byte [ 502 ] ) ;
350+ clients [ i ] . Disconnect ( ) ;
315351
352+ } ) ;
353+ Console . WriteLine ( "DC" ) ;
354+ Console . ReadLine ( ) ;
355+
356+ Console . Read ( ) ;
316357 }
317358
318- private static void clientMsgRec ( byte [ ] arg2 )
359+ private static void clientMsgRec2 ( ByteMessageTcpClient client , byte [ ] arg2 , int offset , int count )
319360 {
320-
321361
322- if ( arg2 . Length == 502 )
362+ clientMsgRec ( arg2 , offset , count ) ;
363+
364+ if ( pause )
365+ return ;
366+ //Task.Run(() =>client.SendAsync(resp));
367+ //Task.Run(() =>client.SendAsync(resp));
368+ //Task.Run(() =>client.SendAsync(resp));
369+ //client.SendAsync(resp);
370+ lastSW = false ;
371+ }
372+
373+ private static void clientMsgRec ( /*ByteProtocolTcpClient client,*/ byte [ ] arg2 , int offset , int count )
374+ {
375+ if ( count == 502 )
323376 {
324- //sw.Stop();
325- //Console.WriteLine("Server REc: " + arg2.Length);
326377 Console . WriteLine ( "Time client " + sw . ElapsedMilliseconds ) ;
327378 Console . WriteLine ( "tot msg client: " + totMsgCl ) ;
328379
329- sw . Reset ( ) ;
330- return ;
331- }
332- if ( arg2 . Length != 5000 )
333- {
334- // throw new Exception();
380+ //sw.Reset();
381+ //return;
335382 }
336- //for (int i = 0; i < arg2.Length; i++)
383+
384+ Interlocked . Increment ( ref totMsgCl ) ;
385+ //Console.WriteLine("Sending");
386+ //client.SendAsync(resp);
387+ //if (totMsgCl % 1000000 == 0)
337388 //{
338- // if (arg2[i] != 11)
339- // throw new Exception();
389+ // Console.WriteLine(totMsgCl);
390+ // Console.WriteLine("Time client " + sw.ElapsedMilliseconds);
391+
340392 //}
341- Interlocked . Increment ( ref totMsgCl ) ;
393+
342394 }
343395
344- public static byte [ ] response = new byte [ 32 ] ;
345- private static void SWOnMsgRecieved ( Guid arg1 , byte [ ] arg2 )
396+ private static void SWOnMsgRecieved ( Guid arg1 , byte [ ] arg2 , int offset , int count )
346397 {
347-
348- //Console.WriteLine(arg2.Length);
349398
350- if ( arg2 . Length == 502 )
399+ //server.SendBytesToClient(arg1, resp);
400+ // server.SendBytesToClient(arg1, resp);
401+ Interlocked . Increment ( ref totMsgsw ) ;
402+
403+
404+ if ( count == 502 )
351405 {
352- //sw.Stop();
353- //Console.WriteLine("Server REc: " + arg2.Length);
354406 Console . WriteLine ( "Time: " + sw2 . ElapsedMilliseconds ) ;
355407 Console . WriteLine ( "tot msg sw: " + totMsgsw ) ;
356- //sw2.Reset();
357- return ;
358- }
359- if ( arg2 . Length != 5000 )
360- {
361- // throw new Exception();
408+ server . SendBytesToClient ( arg1 , new byte [ 502 ] ) ;
409+
410+ //return;
362411 }
363- server . SendBytesToClient ( arg1 , arg2 ) ;
364- ///* cq.Enqueue*/Task.Run(() => {
365- //for (int i = 0; i < arg2.Length; i++)
412+ //server.SendBytesToClient(arg1, resp);
413+ lastSW = true ;
414+
415+ //if (BitConverter.ToInt32(arg2, offset) != prev + 1)
366416 //{
367- // if (arg2[i] != 11)
368- // throw new Exception( );
417+ // Console.WriteLine("--- Prev " + prev);
418+ // Console.WriteLine("---- Curr " + BitConverter.ToInt32(arg2, offset) );
369419 //}
370- Interlocked . Increment ( ref totMsgsw ) ;
371- // });
372- // are.Set();
420+ //prev = BitConverter.ToInt32(arg2, offset);
421+
422+ server . SendBytesToClient ( arg1 , resp ) ;
423+
424+
373425
374426
375- }
376427
377- private static void OncOn ( )
378- {
379- Console . WriteLine ( "client connected" ) ;
380- }
381428
382- private static void ClientRec ( byte [ ] bytes )
383- {
384- Console . WriteLine ( "client REc" ) ;
385429 }
386430
387- private static void AA ( int id , byte [ ] bytes )
388- {
389- Console . WriteLine ( "Server REc" ) ;
390- }
431+
391432 }
392433}
0 commit comments