22
33namespace CustomNetworkLib
44{
5+ using CustomNetworkLib . Utils ;
56 using System ;
67 using System . Collections . Concurrent ;
78 using System . Collections . Generic ;
@@ -22,26 +23,36 @@ public class AsyncTcpServer
2223 public ClientAccepted OnClientAccepted ;
2324 public BytesRecieved OnBytesRecieved ;
2425
25- public int MaxIndexedMemoryPerClient = 128000 ;
26+ public BufferProvider BufferManager { get ; private set ; }
27+
28+ public int MaxClients { get ; private set ; } = 10000 ;
29+ public int ClientSendBufsize = 128000 ;
30+ public int ClientReceiveBufsize = 128000 ;
31+ public int MaxIndexedMemoryPerClient = 1280000 ;
2632 public int SoocketReceiveBufferSize = 2080000000 ;
27- public bool DropOnBackPressure = false ;
33+ public bool DropOnBackPressure = false ;
2834 public bool NaggleNoDelay = false ;
2935
3036 protected Socket ServerSocket ;
3137 protected int ServerPort { get ; }
3238 public ConcurrentDictionary < Guid , IAsyncSession > Sessions { get ; } = new ConcurrentDictionary < Guid , IAsyncSession > ( ) ;
33- public AsyncTcpServer ( int port = 20008 )
39+ public bool Stopping { get ; private set ; }
40+
41+ public AsyncTcpServer ( int port = 20008 , int maxClients = 100 )
3442 {
3543 ServerPort = port ;
44+ MaxClients = maxClients ;
3645 }
3746
3847 public void StartServer ( )
3948 {
49+ BufferManager = new BufferProvider ( MaxClients , ClientSendBufsize , MaxClients , ClientReceiveBufsize ) ;
50+
4051 ServerSocket = new Socket ( SocketType . Stream , ProtocolType . Tcp ) ;
4152 ServerSocket . NoDelay = NaggleNoDelay ;
4253 ServerSocket . ReceiveBufferSize = SoocketReceiveBufferSize ;
4354 ServerSocket . Bind ( new IPEndPoint ( IPAddress . Any , ServerPort ) ) ;
44- ServerSocket . Listen ( 10000 ) ;
55+ ServerSocket . Listen ( MaxClients ) ;
4556
4657
4758 SocketAsyncEventArgs e = new SocketAsyncEventArgs ( ) ;
@@ -55,51 +66,60 @@ public void StartServer()
5566
5667 private void Accepted ( object sender , SocketAsyncEventArgs acceptedArg )
5768 {
58- if ( acceptedArg . SocketError != SocketError . Success )
59- {
60- HandleError ( acceptedArg . SocketError , "While Accepting Client an error occured" ) ;
69+ if ( Stopping )
6170 return ;
62- }
6371 SocketAsyncEventArgs nextClient = new SocketAsyncEventArgs ( ) ;
6472 nextClient . Completed += Accepted ;
6573
6674 if ( ! ServerSocket . AcceptAsync ( nextClient ) )
6775 {
68- ThreadPool . QueueUserWorkItem ( ( s ) => Accepted ( null , nextClient ) ) ;
76+ ThreadPool . UnsafeQueueUserWorkItem ( ( s ) => Accepted ( null , nextClient ) , null ) ;
6977 }
7078
7179 if ( acceptedArg . SocketError != SocketError . Success )
7280 {
73- Console . WriteLine ( Enum . GetName ( typeof ( SocketError ) , acceptedArg . SocketError ) ) ;
81+ HandleError ( acceptedArg . SocketError , "While Accepting Client an Error Occured :" ) ;
7482 return ;
7583 }
7684
77- int port = ( ( IPEndPoint ) acceptedArg . AcceptSocket . RemoteEndPoint ) . Port ;
78- Guid clientGuid = Guid . NewGuid ( ) ;
85+ if ( ! HandleConnectionRequest ( acceptedArg . AcceptSocket ) )
86+ {
87+ return ;
88+ }
7989
80- var session = CreateSession ( acceptedArg , clientGuid ) ;
81- Sessions . TryAdd ( clientGuid , session ) ;
90+ Guid clientGuid = Guid . NewGuid ( ) ;
91+ var session = CreateSession ( acceptedArg , clientGuid , BufferManager ) ;
8292
83- session . OnBytesRecieved += ( byte [ ] bytes , int offset , int count ) => { HandleBytesRecieved ( clientGuid , bytes , offset , count ) ; } ;
93+ session . OnBytesRecieved += ( Guid sessionId , byte [ ] bytes , int offset , int count ) => { HandleBytesRecieved ( sessionId , bytes , offset , count ) ; } ;
8494 session . OnSessionClosed += ( Guid sessionId ) => { HandleDeadSession ( sessionId ) ; } ;
8595
8696 session . StartSession ( ) ;
87- Console . WriteLine ( "Accepted with port: " + ( ( IPEndPoint ) acceptedArg . AcceptSocket . RemoteEndPoint ) . Port ) ;
97+ Sessions . TryAdd ( clientGuid , session ) ;
98+
99+ string msg = "Accepted with port: " + ( ( IPEndPoint ) acceptedArg . AcceptSocket . RemoteEndPoint ) . Port +
100+ " Ip: " + ( ( IPEndPoint ) acceptedArg . AcceptSocket . RemoteEndPoint ) . Address . ToString ( ) ;
101+
102+ MiniLogger . Log ( MiniLogger . LogLevel . Info , msg ) ;
88103
89104 HandleClientAccepted ( clientGuid , acceptedArg ) ;
90105 }
91106
107+ protected virtual bool HandleConnectionRequest ( Socket acceptSocket )
108+ {
109+ return true ;
110+ }
92111
93- protected virtual IAsyncSession CreateSession ( SocketAsyncEventArgs e , Guid sessionId )
112+ protected virtual IAsyncSession CreateSession ( SocketAsyncEventArgs e , Guid sessionId , BufferProvider bufferManager )
94113 {
95- var session = new TcpSession ( e , sessionId ) ;
96- session . MaxIndexedMemory = MaxIndexedMemoryPerClient ;
97- session . DropOnCongestion = DropOnBackPressure ;
114+ var session = new TcpSession ( e , sessionId , bufferManager ) ;
115+ session . socketSendBufferSize = ClientSendBufsize ;
116+ session . socketRecieveBufferSize = ClientReceiveBufsize ;
117+ session . maxIndexedMemory = MaxIndexedMemoryPerClient ;
118+ session . dropOnCongestion = DropOnBackPressure ;
98119 return session ;
99120 }
100121
101122
102- //#region Send
103123 public void SendBytesToAllClients ( byte [ ] bytes )
104124 {
105125 Parallel . ForEach ( Sessions , session =>
@@ -129,12 +149,13 @@ protected virtual void HandleDeadSession(Guid sessionId)
129149 Sessions . TryRemove ( sessionId , out _ ) ;
130150 }
131151
132- protected virtual void HandleError ( SocketError error , object context )
152+ protected virtual void HandleError ( SocketError error , string context )
133153 {
134-
154+ MiniLogger . Log ( MiniLogger . LogLevel . Error , context + Enum . GetName ( typeof ( SocketError ) , error ) ) ;
135155 }
136- public void StopServer ( )
156+ public virtual void StopServer ( )
137157 {
158+ Stopping = true ;
138159 ServerSocket . Close ( ) ;
139160 ServerSocket . Dispose ( ) ;
140161
0 commit comments