private async Task ProccessMessage() { TcpMessage requestMessage = await MessageTransport.ReadAsync(this._connectionInfo); if (requestMessage == null) { throw new Exception("Сообщение имеет некорректный формат"); } Command = requestMessage.Header.Command; bool isNeedExeLog = ServerCommands.IsNeedExeLog(Command); Stopwatch stopwatch = null; if (isNeedExeLog) { stopwatch = new Stopwatch(); stopwatch.Start(); } using (CommandContext commandContext = new CommandContext(this, requestMessage, _bufferManager)) { this._commandContext = commandContext; await CommandDispatcher.Instance.ExecuteAsync(commandContext); this._commandContext = null; } if (isNeedExeLog) { stopwatch.Stop(); ServerLog.AddFBLog(ExecuteFolder, string.Format("| {0,8} | {1,8} | {2}", Command, stopwatch.ElapsedMilliseconds, ClientAddress)); } }
private void LogProcessError(Exception ex) { if (Glvar.RunAsServise) { ServerLog.AddFBLog(CommunicationErrorFolder, string.Format("| {0,8} | {1}\r\n{2}", this.Command, this.ClientAddress, ex)); } else { Console.WriteLine(ex.ToString()); Logger.ErrorException("ProccessMessage error", ex); } }
public async Task Start() { this._clientSocket.ReceiveBufferSize = this._listener.Configuration.ClientSocketReceiveBufferSize; this._clientSocket.SendBufferSize = this._listener.Configuration.ClientSocketSendBufferSize; this._listener.AddConnection(this); try { await ProccessMessage(); } catch (IOException ex) { SocketException socketException = ex.InnerException as SocketException; if (socketException != null) { string errText = string.Format("SocketException. {0}, {1}", socketException.SocketErrorCode, socketException); if (Glvar.RunAsServise) { ServerLog.AddFBLog(SocketErrorFolder, string.Format("| {0,8} | {1}\r\n{2}", this.Command, this.ClientAddress, errText)); } else { Console.WriteLine(errText); Logger.ErrorException("ProccessMessage io error", ex); } } else { this.LogProcessError(ex); } } catch (Exception ex) { this.LogProcessError(ex); } finally { this._listener.RemoveConnection(this); Dispose(); } }