private void ProcessRecvData(IAsyncResult result) { if (shutdownQueued) { return; //suppress any errors during shutdown } try { int recvLen = tcpStream.EndRead(result); if (recvLen <= 0) { throw new Exception("RecvLength less than or equal to zero."); } string recvText = System.Text.Encoding.ASCII.GetString(recvBuff, 0, recvLen); tcpStream.BeginRead(recvBuff, 0, recvBuff.Length, ProcessRecvData, null); Logger.Debug?.WriteLine("[<- RECV] " + recvText.Replace("\r\n", "")); connLog.Add("[<- RECV] " + recvText.Replace("\r\n", "")); //try executing the received text as a SMTP command TryExecCommand(recvText); } catch (Exception e) { Logger.Critical?.WriteLine("TCP read critical error, details: " + e.ToString()); shutdownQueued = true; ownerServer.QueueEndConnection(connId); } }
public RemoteConnection(TcpClient client, Dictionary <string, string> config, SMTPServer owner, int connectionId) { tcpClient = client; serverConfig = config; ownerServer = owner; connId = connectionId; //setting up defaults in config if (!config.ContainsKey("Domain")) { config.Add("Domain", "NoDomain"); } if (!config.ContainsKey("MessageMaxBytes")) { config.Add("MessageMaxBytes", "1000000"); } recvBuff = new byte[config.ContainsKey("BufferSize") ? int.Parse(config["BufferSize"]) : 4096]; try { tcpClient.ReceiveBufferSize = recvBuff.Length; tcpClient.SendBufferSize = recvBuff.Length; tcpStream = tcpClient.GetStream(); tcpStream.BeginRead(recvBuff, 0, recvBuff.Length, ProcessRecvData, null); SendResponse(new SMTPResult(SMTPResponseCode.ServerReady, config["Domain"] + " SEMPTY Server is ready for, welcome.")); } catch (Exception e) { Logger.Critical?.WriteLine("TCP critical error, cannot establish connection: " + e.ToString()); shutdownQueued = true; ownerServer.QueueEndConnection(connId); } }