Under the similar structure, the service is started HandleAcceptConnected start processing, HandleDataReceived asynchronous processing, now the service starts with cumulative time, gradually rise, memory, help to look at, such as online ~
The following is a source:
///& lt; Summary>
///start the server
///& lt;/summary>
///& lt; Returns> Asynchronous TCP server & lt;/returns>
Public SocketModuleAsyncServer Start ()
{
if (! Set the)
{
Set the=true;
_serverSocket. IOControl (IOControlCode KeepAliveValues, KeepAliveTime, null);
_serverSocket. SetSocketOption (SocketOptionLevel. Socket, SocketOptionName KeepAlive, true);
_serverSocket. Bind (new IPEndPoint (this Address, enclosing Port));
_serverSocket. Listen (_maxClient);
_serverSocket. BeginAccept (new AsyncCallback (HandleAcceptConnected), _serverSocket);
}
Return this.
}
///& lt; Summary>
///processing client connection - & gt; A client is connected
///& lt;/summary>
///& lt; Param name="ar" & gt;
Private void HandleAcceptConnected IAsyncResult (ar)
{
If (set)
{
The Socket server=null;
The Socket client=null;
Try
{
Server=(Socket) ar. AsyncState;
The client=server. EndAccept (ar);
}
The catch (Exception ex)
{
Server. BeginAccept (new AsyncCallback (HandleAcceptConnected), ar. AsyncState);
}
If (client!=null & amp; & Client. Connected)
{
//check if the maximum allowed number of client
If (_clientCount & gt; _maxClient)
{
//TODO triggering event
RaiseServerException (" ");
}
The else
{
Try
{
The Session Session=new Session (the client);
If (session!=null)
{
RaiseClientConnected (session);//trigger client connection
Session. RecvDataBuffer=new byte [client ReceiveBufferSize];
//start asynchronous receive data from the client's
Client. BeginReceive (session. RecvDataBuffer, 0, session. RecvDataBuffer. Length, SocketFlags. None, new AsyncCallback (HandleDataReceived), session).
_clientCount + +;
}
}
The catch (Exception)
{
}
}
Try
{
//accept the next client request
Server. BeginAccept (new AsyncCallback (HandleAcceptConnected), ar. AsyncState);
}
The catch (Exception)
{
Throw;
}
}
}
}
///& lt; Summary>
///processing client data - & gt; Receive a client message
///& lt;/summary>
///& lt; Param name="ar" & gt;
Private void HandleDataReceived IAsyncResult (ar)
{
If (set)
{
The Session Session=(Session) ar. AsyncState;
The Socket client=session. ClientSocket;
The string content=string. The Empty;
Try
{
If (client==null | |! Client. Connected)
{
CloseSession (session);
RaiseNetError (session);//trigger network error event
return;
}
Int recv=client. EndReceive (ar);
If (recv==0)
{
CloseSession (session);
RaiseNetError (session);//trigger network error event
return;
}
//business processing
}
The catch (SocketException)
{
RaiseNetError (session);//trigger network error event
}
The finally
{
If (session RecvDataBuffer!=null)
{
Try
{
//continue to receive data from to the client's
Client. BeginReceive (session. RecvDataBuffer, 0, session. RecvDataBuffer. Length, SocketFlags. None, new AsyncCallback (HandleDataReceived), session).
}
The catch (Exception)
{
CloseSession (session);
}
}
}
}
}
CodePudding user response:
Don't want to say, say more, again someone said the ans new lessonhttps://github.com/rsocket/rsocket-net/tree/master/RSocket.Core/Transports
This code yourself see,
https://github.com/mgravell/Pipelines.Sockets.Unofficial
This can also see
Things are all the things, to say the noun "pipeline, asynchronous, memory pool, span
CodePudding user response:
Direct error prompt,CodePudding user response:
OutOfMemoryCodePudding user response:
Do the connection state judgment, it would have been much betterCodePudding user response:
This code is written, or from the Internet to find? Logically, incredibly still want to perform in the catch important code?