internal static IAsyncResult BeginSend(SmtpConnection conn, AsyncCallback callback, object state) { MultiAsyncResult multiResult = new MultiAsyncResult(conn, callback, state); multiResult.Enter(); IAsyncResult writeResult = conn.BeginFlush(s_onWrite, multiResult); if (writeResult.CompletedSynchronously) { conn.EndFlush(writeResult); multiResult.Leave(); } SmtpReplyReader reader = conn.Reader.GetNextReplyReader(); multiResult.Enter(); //this actually does a read on the stream. IAsyncResult result = reader.BeginReadLine(s_onReadLine, multiResult); if (result.CompletedSynchronously) { LineInfo info = reader.EndReadLine(result); if (!(multiResult.Result is Exception)) { multiResult.Result = info; } multiResult.Leave(); } multiResult.CompleteSequence(); return(multiResult); }
internal IAsyncResult BeginReadLine(SmtpReplyReader caller, AsyncCallback callback, object state) { ReadLinesAsyncResult result = new ReadLinesAsyncResult(this, callback, state, true); result.Read(caller); return(result); }
internal static IAsyncResult BeginSend(SmtpConnection conn, AsyncCallback callback, object state) { MultiAsyncResult multiResult = new MultiAsyncResult(conn, callback, state); multiResult.Enter(); IAsyncResult writeResult = conn.BeginFlush(s_onWrite, multiResult); if (writeResult.CompletedSynchronously) { conn.EndFlush(writeResult); multiResult.Leave(); } SmtpReplyReader reader = conn.Reader.GetNextReplyReader(); multiResult.Enter(); IAsyncResult readLinesResult = reader.BeginReadLines(s_onReadLines, multiResult); if (readLinesResult.CompletedSynchronously) { LineInfo[] lines = conn.Reader.CurrentReader.EndReadLines(readLinesResult); if (!(multiResult.Result is Exception)) { multiResult.Result = lines; } multiResult.Leave(); } multiResult.CompleteSequence(); return(multiResult); }
private void Handshake() { _connection._responseReader = new SmtpReplyReaderFactory(_connection._networkStream); SmtpReplyReader reader = _connection.Reader.GetNextReplyReader(); IAsyncResult result = reader.BeginReadLine(s_handshakeCallback, this); if (!result.CompletedSynchronously) { return; } LineInfo info = reader.EndReadLine(result); if (info.StatusCode != SmtpStatusCode.ServiceReady) { throw new SmtpException(info.StatusCode, info.Line, true); } try { if (!SendEHello()) { return; } } catch { if (!SendHello()) { return; } } }
internal LineInfo ReadLine(SmtpReplyReader caller) { LineInfo[] info = ReadLines(caller, true); if (info != null && info.Length > 0) { return(info[0]); } return(new LineInfo()); }
internal static SmtpStatusCode Send(SmtpConnection conn, out string response) { conn.Flush(); SmtpReplyReader reader = conn.Reader.GetNextReplyReader(); LineInfo info = reader.ReadLine(); response = info.Line; reader.Close(); return(info.StatusCode); }
internal SmtpReplyReader GetNextReplyReader() { if (_currentReader != null) { _currentReader.Close(); } _readState = ReadState.Status0; _currentReader = new SmtpReplyReader(this); return(_currentReader); }
internal int Read(SmtpReplyReader caller, byte[] buffer, int offset, int count) { // if we've already found the delimitter, then return 0 indicating // end of stream. if (count == 0 || _currentReader != caller || _readState == ReadState.Done) { return(0); } int read = _bufferedStream.Read(buffer, offset, count); int actual = ProcessRead(buffer, offset, read, false); if (actual < read) { _bufferedStream.Push(buffer, offset + actual, read - actual); } return(actual); }
internal void Close(SmtpReplyReader caller) { if (_currentReader == caller) { if (_readState != ReadState.Done) { if (_byteBuffer == null) { _byteBuffer = new byte[SmtpReplyReaderFactory.DefaultBufferSize]; } while (0 != Read(caller, _byteBuffer, 0, _byteBuffer.Length)) { ; } } _currentReader = null; } }
internal void Read(SmtpReplyReader caller) { // if we've already found the delimitter, then return 0 indicating // end of stream. if (_parent._currentReader != caller || _parent._readState == ReadState.Done) { InvokeCallback(); return; } if (_parent._byteBuffer == null) { _parent._byteBuffer = new byte[SmtpReplyReaderFactory.DefaultBufferSize]; } System.Diagnostics.Debug.Assert(_parent._readState == ReadState.Status0); _builder = new StringBuilder(); _lines = new ArrayList(); Read(); }
internal LineInfo[] ReadLines(SmtpReplyReader caller, bool oneLine) { if (caller != _currentReader || _readState == ReadState.Done) { return(new LineInfo[0]); } if (_byteBuffer == null) { _byteBuffer = new byte[SmtpReplyReaderFactory.DefaultBufferSize]; } System.Diagnostics.Debug.Assert(_readState == ReadState.Status0); StringBuilder builder = new StringBuilder(); ArrayList lines = new ArrayList(); int statusRead = 0; for (int start = 0, read = 0; ;) { if (start == read) { read = _bufferedStream.Read(_byteBuffer, 0, _byteBuffer.Length); start = 0; } int actual = ProcessRead(_byteBuffer, start, read - start, true); if (statusRead < 4) { int left = Math.Min(4 - statusRead, actual); statusRead += left; start += left; actual -= left; if (actual == 0) { continue; } } builder.Append(Encoding.UTF8.GetString(_byteBuffer, start, actual)); start += actual; if (_readState == ReadState.Status0) { statusRead = 0; lines.Add(new LineInfo(_statusCode, builder.ToString(0, builder.Length - 2))); // return everything except CRLF if (oneLine) { _bufferedStream.Push(_byteBuffer, start, read - start); return((LineInfo[])lines.ToArray(typeof(LineInfo))); } builder = new StringBuilder(); } else if (_readState == ReadState.Done) { lines.Add(new LineInfo(_statusCode, builder.ToString(0, builder.Length - 2))); // return everything except CRLF _bufferedStream.Push(_byteBuffer, start, read - start); return((LineInfo[])lines.ToArray(typeof(LineInfo))); } } }
internal LineInfo[] ReadLines(SmtpReplyReader caller) { return(ReadLines(caller, false)); }