private void ProcessSend(AsyncClientArgs client) { if (client.SocketError != SocketError.Success) { Log.Error("{0}: {1}", client.RemoteEndPoint, client.SocketError); } _clientPool.Release(client); }
private void ProcessSend(AsyncClientArgs args) { if (args.SocketError == SocketError.Success) { Log.Debug("{0} < {1}", args.AcceptSocket.RemoteEndPoint, args.UTF8Message); } else { Log.Error("{0} < {1}", args.AcceptSocket.RemoteEndPoint, args.SocketError); } CloseClientConnection(args); }
private void CloseClientConnection(AsyncClientArgs args) { try { args.AcceptSocket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { Log.Debug(ex.Message); } args.AcceptSocket.Close(); // освобождаем ресурс для следующего клиента _clientPool.Release(args); }
private void ProcessReceive(AsyncClientArgs args) { // если данные получены if (args.SocketError == SocketError.Success) { if (args.BytesTransferred > 0) { var message = args.UTF8Message; Log.Debug("{0} > {1}", args.AcceptSocket.RemoteEndPoint, message); // передаем сообщение логическому серверу и получаем ответ message = LogicServer.GetResponse(message); if (message == null) { CloseClientConnection(args); return; } args.UTF8Message = message; // посылаем сообщение обратно клиенту if (!args.AcceptSocket.SendAsync(args)) { ProcessSend(args); } } else { // клиент закончил передачу сообщения CloseClientConnection(args); } } else { Log.Error("{0} > {1}", args.AcceptSocket.RemoteEndPoint, args.SocketError); CloseClientConnection(args); } }
private void ProcessReceive(AsyncClientArgs client) { switch (client.SocketError) { case SocketError.Success: if (client.BytesTransferred > 0) { var request = client.UTF8Message; // send request to logic server and get the response var response = LogicServer.GetResponse(request); // send response to client, if it is not empty if (response != null) { client.UTF8Message = response; if (!client.AcceptSocket.SendToAsync(client)) { ProcessSend(client); } } Log.Debug("{0}: {1} -> {2}", client.RemoteEndPoint, request, response); } break; case SocketError.OperationAborted: _clientPool.Release(client); return; default: Log.Error("{0}: {1}", client.RemoteEndPoint, client.SocketError); _clientPool.Release(client); break; } StartReceiving(); }