示例#1
0
        // ------------------------- ReceiveCallback -------------------------
        // receive MailMessage in background thread.
        private void ReceiveCallback(IAsyncResult InResult)
        {
            try
            {
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                Pop3StateObject sockThreadState =
                    (Pop3StateObject)InResult.AsyncState;
                SocketExchange sockEx     = sockThreadState.SockEx;
                Socket         client     = sockEx.ConnectedSocket;
                SecureSocket   secureSock = sockEx.ConnectedSecureSocket;

                bool InlRcv = true;
                while (true)
                {
                    // receive from the socket.
                    int ReadCx;
                    if (InlRcv == true)
                    {
                        if (client != null)
                        {
                            ReadCx = client.EndReceive(InResult);
                        }
                        else
                        {
                            ReadCx = secureSock.EndReceive(InResult);
                        }
                    }
                    else
                    {
                        if (client != null)
                        {
                            ReadCx = client.Receive(
                                sockThreadState.buffer, 0, Pop3StateObject.BufferSize, SocketFlags.None);
                        }
                        else
                        {
                            ReadCx = secureSock.Receive(
                                sockThreadState.buffer, 0, Pop3StateObject.BufferSize, SocketFlags.None);
                        }
                    }
                    InlRcv = false;

                    // did not receive anything.  probably should leave.
                    if (ReadCx == 0)
                    {
                        break;
                    }

                    // There might be more data,
                    // so store the data received so far.
                    string block = Encoding.ASCII.GetString(sockThreadState.buffer, 0, ReadCx);
                    sockThreadState.sb.Append(block);

                    // is this the end of the message
                    if (Stringer.Tail(sockThreadState.sb, 5) == "\r\n.\r\n")
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw new
                      Pop3ReceiveException(
                          "ReceiveCallback error" +
                          e.ToString( ));
            }
            finally
            {
                m_manualEvent.Set( );
            }
        }