private string m_UserName = ""; // Holds loggedIn UserName. #endregion Fields #region Constructors /// <summary> /// Default constructor. /// </summary> /// <param name="clientSocket">Referance to socket.</param> /// <param name="server">Referance to FTP server.</param> /// <param name="sessionID">Session ID which is assigned to this session.</param> /// <param name="logWriter">Log writer.</param> public FTP_Session(Socket clientSocket,FTP_Server server,string sessionID,_LogWriter logWriter) { m_pSocket = new BufferedSocket(clientSocket); m_pServer = server; m_SessionID = sessionID; m_pLogWriter = logWriter; m_SessionStartTime = DateTime.Now; m_LastDataTime = DateTime.Now; m_pSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.NoDelay,1); // Start session proccessing StartSession(); }
/// <summary> /// Reads specified count of data from Socket. /// </summary> /// <param name="socket"></param> /// <param name="count">Number of bytes to read.</param> /// <param name="storeStrm"></param> /// <param name="storeToStream">If true stores readed data to stream, otherwise just junks specified amount of data.</param> /// <param name="cmdIdleTimeOut"></param> /// <returns></returns> public static ReadReplyCode ReadData(BufferedSocket socket, long count, Stream storeStrm, bool storeToStream, int cmdIdleTimeOut) { ReadReplyCode replyCode = ReadReplyCode.Ok; try{ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, cmdIdleTimeOut); long readedCount = 0; while (readedCount < count) { byte[] b = new byte[4000]; // Ensure that we don't get more data than needed if ((count - readedCount) < 4000) { b = new byte[count - readedCount]; } int countRecieved = socket.Receive(b); if (countRecieved > 0) { readedCount += countRecieved; if (storeToStream) { storeStrm.Write(b, 0, countRecieved); } } // Client disconnected else { throw new Exception("Client disconnected"); } } } catch (Exception x) { replyCode = ReadReplyCode.UnKnownError; if (x is SocketException) { SocketException xS = (SocketException)x; if (xS.ErrorCode == 10060) { replyCode = ReadReplyCode.TimeOut; } } } return(replyCode); }
private string m_UserName = ""; // Holds loggedIn UserName. #endregion Fields #region Constructors /// <summary> /// Default constructor. /// </summary> /// <param name="clientSocket">Referance to socket.</param> /// <param name="server">Referance to IMAP server.</param> /// <param name="logWriter">Log writer.</param> internal IMAP_Session(Socket clientSocket,IMAP_Server server,_LogWriter logWriter) { m_pSocket = new BufferedSocket(clientSocket); m_pServer = server; m_pLogWriter = logWriter; m_SessionID = Guid.NewGuid().ToString(); m_SessionStartTime = DateTime.Now; m_LastDataTime = DateTime.Now; m_pSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.NoDelay,1); // Start session proccessing StartSession(); }
private string m_UserName = ""; // Holds loggedIn UserName. #endregion Fields #region Constructors /// <summary> /// Default constructor. /// </summary> /// <param name="clientSocket">Referance to socket.</param> /// <param name="server">Referance to SMTP server.</param> /// <param name="logWriter">Log writer.</param> internal SMTP_Session(Socket clientSocket,SMTP_Server server,_LogWriter logWriter) { m_pSocket = new BufferedSocket(clientSocket); m_pServer = server; m_pLogWriter = logWriter; m_pMsgStream = new MemoryStream(); m_SessionID = Guid.NewGuid().ToString(); m_BodyType = BodyType.x7_bit; m_Forward_path = new Hashtable(); m_CmdValidator = new SMTP_Cmd_Validator(); m_SessionStart = DateTime.Now; m_LastDataTime = DateTime.Now; m_pSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.NoDelay,1); // Start session proccessing StartSession(); }
private string m_UserName = ""; // Holds loggedIn UserName. #endregion Fields #region Constructors /// <summary> /// Default constructor. /// </summary> /// <param name="clientSocket">Referance to socket.</param> /// <param name="server">Referance to IMAP server.</param> /// <param name="logWriter">Log writer.</param> internal IMAP_Session(Socket clientSocket,IMAP_Server server,SocketLogger logWriter) { m_pSocket = new BufferedSocket(clientSocket); m_pServer = server; m_SessionID = Guid.NewGuid().ToString(); m_SessionStartTime = DateTime.Now; m_LastDataTime = DateTime.Now; if(m_pServer.LogCommands){ m_pSocket.Logger = logWriter; m_pSocket.Logger.SessionID = m_SessionID; } m_pSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.NoDelay,1); m_pSocket.Activity += new EventHandler(OnSocketActivity); // Start session proccessing StartSession(); }
/// <summary> /// Reads reply from socket. /// </summary> /// <param name="socket"></param> /// <param name="replyData">Data that has been readen from socket.</param> /// <param name="maxLength">Maximum Length of data which may read.</param> /// <param name="cmdIdleTimeOut">Command idle time out in milliseconds.</param> /// <param name="terminator">Terminator string which terminates reading. eg '\r\n'.</param> /// <param name="removeFromEnd">Removes following string from reply.NOTE: removes only if ReadReplyCode is Ok.</param> /// <returns>Return reply code.</returns> public static ReadReplyCode ReadData(BufferedSocket socket,out MemoryStream replyData,int maxLength,int cmdIdleTimeOut,string terminator,string removeFromEnd) { ReadReplyCode replyCode = ReadReplyCode.Ok; replyData = null; try{ socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,cmdIdleTimeOut); replyData = new MemoryStream(); _FixedStack stack = new _FixedStack(terminator); int nextReadWriteLen = 1; while(nextReadWriteLen > 0){ //Read byte(s) byte[] b = new byte[nextReadWriteLen]; int countRecieved = socket.Receive(b); if(countRecieved > 0){ // Write byte(s) to buffer, if length isn't exceeded. if(replyCode != ReadReplyCode.LengthExceeded){ replyData.Write(b,0,countRecieved); } // Write to stack(terminator checker) nextReadWriteLen = stack.Push(b,countRecieved); //---- Check if maximum length is exceeded ---------------------------------// if(replyCode != ReadReplyCode.LengthExceeded && replyData.Length > maxLength){ replyCode = ReadReplyCode.LengthExceeded; } //--------------------------------------------------------------------------// } // Client disconnected else{ throw new Exception("Client disconnected"); } } // If reply is ok then remove chars if any specified by 'removeFromEnd'. if(replyCode == ReadReplyCode.Ok && removeFromEnd.Length > 0){ replyData.SetLength(replyData.Length - removeFromEnd.Length); } } catch(Exception x){ replyCode = ReadReplyCode.UnKnownError; if(x is SocketException){ SocketException xS = (SocketException)x; if(xS.ErrorCode == 10060){ replyCode = ReadReplyCode.TimeOut; } } } return replyCode; }
/// <summary> /// Reads specified count of data from Socket. /// </summary> /// <param name="socket"></param> /// <param name="count">Number of bytes to read.</param> /// <param name="storeStrm"></param> /// <param name="storeToStream">If true stores readed data to stream, otherwise just junks specified amount of data.</param> /// <param name="cmdIdleTimeOut"></param> /// <returns></returns> public static ReadReplyCode ReadData(BufferedSocket socket,long count,Stream storeStrm,bool storeToStream,int cmdIdleTimeOut) { ReadReplyCode replyCode = ReadReplyCode.Ok; try{ socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,cmdIdleTimeOut); long readedCount = 0; while(readedCount < count){ byte[] b = new byte[4000]; // Ensure that we don't get more data than needed if((count - readedCount) < 4000){ b = new byte[count - readedCount]; } int countRecieved = socket.Receive(b); if(countRecieved > 0){ readedCount += countRecieved; if(storeToStream){ storeStrm.Write(b,0,countRecieved); } } // Client disconnected else{ throw new Exception("Client disconnected"); } } } catch(Exception x){ replyCode = ReadReplyCode.UnKnownError; if(x is SocketException){ SocketException xS = (SocketException)x; if(xS.ErrorCode == 10060){ replyCode = ReadReplyCode.TimeOut; } } } return replyCode; }
/* * /// <summary> * /// Reads specified count of data from Socket. * /// </summary> * /// <param name="socket"></param> * /// <param name="count">Number of bytes to read.</param> * /// <param name="storeStrm"></param> * /// <param name="storeToStream">If true stores readed data to stream, otherwise just junks data.</param> * /// <param name="cmdIdleTimeOut"></param> * /// <returns></returns> * public static ReadReplyCode ReadData(Socket socket,long count,Stream storeStrm,bool storeToStream,int cmdIdleTimeOut) * { * ReadReplyCode replyCode = ReadReplyCode.Ok; * * try{ * socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,cmdIdleTimeOut); * * long readedCount = 0; * while(readedCount < count){ * byte[] b = new byte[4000]; * // Ensure that we don't get more data than needed !!! * if((count - readedCount) < 4000){ * b = new byte[count - readedCount]; * } * * int countRecieved = socket.Receive(b); * if(countRecieved > 0){ * readedCount += countRecieved; * * if(storeToStream){ * storeStrm.Write(b,0,countRecieved); * } * } * // Client disconnected * else{ * throw new Exception("Client disconnected"); * } * } * } * catch(Exception x){Console.WriteLine(x.Message); * replyCode = ReadReplyCode.UnKnownError; * } * * return replyCode; * } */ #endregion #region method ReadData /// <summary> /// Reads reply from socket. /// </summary> /// <param name="socket"></param> /// <param name="replyData">Data that has been readen from socket.</param> /// <param name="maxLength">Maximum Length of data which may read.</param> /// <param name="cmdIdleTimeOut">Command idle time out in milliseconds.</param> /// <param name="terminator">Terminator string which terminates reading. eg '\r\n'.</param> /// <param name="removeFromEnd">Removes following string from reply.NOTE: removes only if ReadReplyCode is Ok.</param> /// <returns>Return reply code.</returns> public static ReadReplyCode ReadData(BufferedSocket socket, out MemoryStream replyData, int maxLength, int cmdIdleTimeOut, string terminator, string removeFromEnd) { ReadReplyCode replyCode = ReadReplyCode.Ok; replyData = null; try{ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, cmdIdleTimeOut); replyData = new MemoryStream(); _FixedStack stack = new _FixedStack(terminator); int nextReadWriteLen = 1; while (nextReadWriteLen > 0) { //Read byte(s) byte[] b = new byte[nextReadWriteLen]; int countRecieved = socket.Receive(b); if (countRecieved > 0) { // Write byte(s) to buffer, if length isn't exceeded. if (replyCode != ReadReplyCode.LengthExceeded) { replyData.Write(b, 0, countRecieved); } // Write to stack(terminator checker) nextReadWriteLen = stack.Push(b, countRecieved); //---- Check if maximum length is exceeded ---------------------------------// if (replyCode != ReadReplyCode.LengthExceeded && replyData.Length > maxLength) { replyCode = ReadReplyCode.LengthExceeded; } //--------------------------------------------------------------------------// } // Client disconnected else { throw new Exception("Client disconnected"); } } // If reply is ok then remove chars if any specified by 'removeFromEnd'. if (replyCode == ReadReplyCode.Ok && removeFromEnd.Length > 0) { replyData.SetLength(replyData.Length - removeFromEnd.Length); } } catch (Exception x) { replyCode = ReadReplyCode.UnKnownError; if (x is SocketException) { SocketException xS = (SocketException)x; if (xS.ErrorCode == 10060) { replyCode = ReadReplyCode.TimeOut; } } } return(replyCode); }