Exemplo n.º 1
0
        internal static IAsyncResult BeginSend(SmtpConnection conn, AsyncCallback callback, object state)
        {
            MultiAsyncResult result = new MultiAsyncResult(conn, callback, state);

            result.Enter();
            IAsyncResult result2 = conn.BeginFlush(onWrite, result);

            if (result2.CompletedSynchronously)
            {
                conn.EndFlush(result2);
                result.Leave();
            }
            SmtpReplyReader nextReplyReader = conn.Reader.GetNextReplyReader();

            result.Enter();
            IAsyncResult result3 = nextReplyReader.BeginReadLine(onReadLine, result);

            if (result3.CompletedSynchronously)
            {
                LineInfo info = nextReplyReader.EndReadLine(result3);
                if (!(result.Result is Exception))
                {
                    result.Result = info;
                }
                result.Leave();
            }
            result.CompleteSequence();
            return(result);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
 private static void HandshakeCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState !;
         try
         {
             try
             {
                 LineInfo info = SmtpReplyReader.EndReadLine(result);
                 if (info.StatusCode != SmtpStatusCode.ServiceReady)
                 {
                     thisPtr.InvokeCallback(new SmtpException(info.StatusCode, info.Line, true));
                     return;
                 }
                 if (!thisPtr.SendEHello())
                 {
                     return;
                 }
             }
             catch (SmtpException)
             {
                 if (!thisPtr.SendHello())
                 {
                     return;
                 }
             }
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
 private void Handshake()
 {
     this.connection.responseReader = new SmtpReplyReaderFactory(this.connection.pooledStream.NetworkStream);
     this.connection.pooledStream.UpdateLifetime();
     if (((SmtpPooledStream)this.connection.pooledStream).previouslyUsed)
     {
         this.connection.isConnected = true;
         base.InvokeCallback();
     }
     else
     {
         SmtpReplyReader nextReplyReader = this.connection.Reader.GetNextReplyReader();
         IAsyncResult    result          = nextReplyReader.BeginReadLine(handshakeCallback, this);
         if (result.CompletedSynchronously)
         {
             LineInfo info = nextReplyReader.EndReadLine(result);
             if (info.StatusCode != SmtpStatusCode.ServiceReady)
             {
                 throw new SmtpException(info.StatusCode, info.Line, true);
             }
             try
             {
                 if (!this.SendEHello())
                 {
                 }
             }
             catch
             {
                 if (!this.SendHello())
                 {
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
            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;
                    }
                }
            }
Exemplo n.º 7
0
        internal IAsyncResult BeginReadLine(SmtpReplyReader caller, AsyncCallback?callback, object?state)
        {
            ReadLinesAsyncResult result = new ReadLinesAsyncResult(this, callback, state, true);

            result.Read(caller);
            return(result);
        }
Exemplo n.º 8
0
 internal LineInfo ReadLine(SmtpReplyReader caller)
 {
     LineInfo[] infoArray = this.ReadLines(caller, true);
     if ((infoArray != null) && (infoArray.Length > 0))
     {
         return(infoArray[0]);
     }
     return(new LineInfo());
 }
 internal LineInfo ReadLine(SmtpReplyReader caller)
 {
     LineInfo[] info = ReadLines(caller, true);
     if (info != null && info.Length > 0)
     {
         return(info[0]);
     }
     return(new LineInfo());
 }
Exemplo n.º 10
0
 internal SmtpReplyReader GetNextReplyReader()
 {
     if (this.currentReader != null)
     {
         this.currentReader.Close();
     }
     this.readState     = ReadState.Status0;
     this.currentReader = new SmtpReplyReader(this);
     return(this.currentReader);
 }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        internal int Read(SmtpReplyReader caller, byte[] buffer, int offset, int count)
        {
            if (((count == 0) || (this.currentReader != caller)) || (this.readState == ReadState.Done))
            {
                return(0);
            }
            int read = this.bufferedStream.Read(buffer, offset, count);
            int num2 = this.ProcessRead(buffer, offset, read, false);

            if (num2 < read)
            {
                this.bufferedStream.Push(buffer, offset + num2, read - num2);
            }
            return(num2);
        }
Exemplo n.º 13
0
            void Handshake()
            {
                connection.responseReader = new SmtpReplyReaderFactory(connection.pooledStream.NetworkStream);


                //if we've already used this stream, then we've already done the handshake

                //set connectionlease
                connection.pooledStream.UpdateLifetime();

                if (((SmtpPooledStream)connection.pooledStream).previouslyUsed == true)
                {
                    connection.isConnected = true;
                    InvokeCallback();
                    return;
                }


                SmtpReplyReader reader = connection.Reader.GetNextReplyReader();
                IAsyncResult    result = reader.BeginReadLine(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;
                    }
                }
            }
Exemplo n.º 14
0
        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;
            }
        }
Exemplo n.º 15
0
        internal void Close(SmtpReplyReader caller)
        {
            if (_currentReader == caller)
            {
                if (_readState != ReadState.Done)
                {
                    _byteBuffer ??= new byte[SmtpReplyReaderFactory.DefaultBufferSize];

                    while (0 != Read(caller, _byteBuffer, 0, _byteBuffer.Length))
                    {
                        ;
                    }
                }

                _currentReader = null;
            }
        }
Exemplo n.º 16
0
 internal void Close(SmtpReplyReader caller)
 {
     if (this.currentReader == caller)
     {
         if (this.readState != ReadState.Done)
         {
             if (this.byteBuffer == null)
             {
                 this.byteBuffer = new byte[0x100];
             }
             while (this.Read(caller, this.byteBuffer, 0, this.byteBuffer.Length) != 0)
             {
             }
         }
         this.currentReader = null;
     }
 }
        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;
            }
        }
Exemplo n.º 19
0
 private static void OnReadLine(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         MultiAsyncResult multiResult = (MultiAsyncResult)result.AsyncState !;
         try
         {
             SmtpConnection conn = (SmtpConnection)multiResult.Context;
             LineInfo       info = SmtpReplyReader.EndReadLine(result);
             if (!(multiResult.Result is Exception))
             {
                 multiResult.Result = info;
             }
             multiResult.Leave();
         }
         catch (Exception e)
         {
             multiResult.Leave(e);
         }
     }
 }
Exemplo n.º 20
0
 internal void Read(SmtpReplyReader caller)
 {
     if ((this.parent.currentReader != caller) || (this.parent.readState == SmtpReplyReaderFactory.ReadState.Done))
     {
         base.InvokeCallback();
     }
     else
     {
         if (this.parent.byteBuffer == null)
         {
             this.parent.byteBuffer = new byte[0x100];
         }
         if (this.parent.charBuffer == null)
         {
             this.parent.charBuffer = new char[0x100];
         }
         this.builder = new StringBuilder();
         this.lines   = new ArrayList();
         this.Read();
     }
 }
            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();
            }
Exemplo n.º 22
0
        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;
        }
Exemplo n.º 23
0
 internal LineInfo ReadLine(SmtpReplyReader caller)
 {
     LineInfo[] info = ReadLines(caller, true);
     if (info != null && info.Length > 0)
     {
         return info[0];
     }
     return new LineInfo();
 }
Exemplo n.º 24
0
 internal LineInfo[] ReadLines(SmtpReplyReader caller)
 {
     return ReadLines(caller, false);
 }
Exemplo n.º 25
0
        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));
                }
            }
        }
Exemplo n.º 26
0
            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();
            }
Exemplo n.º 27
0
 internal IAsyncResult BeginReadLine(SmtpReplyReader caller, AsyncCallback callback, object state)
 {
     ReadLinesAsyncResult result = new ReadLinesAsyncResult(this, callback, state, true);
     result.Read(caller);
     return result;
 }
Exemplo n.º 28
0
        internal LineInfo[] ReadLines(SmtpReplyReader caller, bool oneLine)
        {
            if ((caller != this.currentReader) || (this.readState == ReadState.Done))
            {
                return(new LineInfo[0]);
            }
            if (this.byteBuffer == null)
            {
                this.byteBuffer = new byte[0x100];
            }
            if (this.charBuffer == null)
            {
                this.charBuffer = new char[0x100];
            }
            StringBuilder builder = new StringBuilder();
            ArrayList     list    = new ArrayList();
            int           num     = 0;
            int           offset  = 0;
            int           num3    = 0;

Label_005C:
            if (offset == num3)
            {
                num3   = this.bufferedStream.Read(this.byteBuffer, 0, this.byteBuffer.Length);
                offset = 0;
            }
            int num4 = this.ProcessRead(this.byteBuffer, offset, num3 - offset, true);

            if (num < 4)
            {
                int num5 = Math.Min(4 - num, num4);
                num    += num5;
                offset += num5;
                num4   -= num5;
                if (num4 == 0)
                {
                    goto Label_005C;
                }
            }
            for (int i = offset; i < (offset + num4); i++)
            {
                this.charBuffer[i] = (char)this.byteBuffer[i];
            }
            builder.Append(this.charBuffer, offset, num4);
            offset += num4;
            if (this.readState == ReadState.Status0)
            {
                num = 0;
                list.Add(new LineInfo(this.statusCode, builder.ToString(0, builder.Length - 2)));
                if (oneLine)
                {
                    this.bufferedStream.Push(this.byteBuffer, offset, num3 - offset);
                    return((LineInfo[])list.ToArray(typeof(LineInfo)));
                }
                builder = new StringBuilder();
                goto Label_005C;
            }
            if (this.readState != ReadState.Done)
            {
                goto Label_005C;
            }
            list.Add(new LineInfo(this.statusCode, builder.ToString(0, builder.Length - 2)));
            this.bufferedStream.Push(this.byteBuffer, offset, num3 - offset);
            return((LineInfo[])list.ToArray(typeof(LineInfo)));
        }
Exemplo n.º 29
0
 internal LineInfo[] ReadLines(SmtpReplyReader caller)
 {
     return(this.ReadLines(caller, false));
 }
        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)));
                }
            }
        }
Exemplo n.º 31
0
        internal SmtpReplyReader GetNextReplyReader()
        {
            if (_currentReader != null)
            {
                _currentReader.Close();
            }

            _readState = ReadState.Status0;
            _currentReader = new SmtpReplyReader(this);
            return _currentReader;
        }