Beispiel #1
0
        // Token: 0x0600014F RID: 335 RVA: 0x00008054 File Offset: 0x00006254
        private void HandleCancellation(AsyncResult <ImapConnectionContext, ImapResultData> curOp)
        {
            ImapNetworkFacade.CountCommand(curOp, false);
            this.Log.Error("networkFacade operation cancelled.  Dropped connection.", new object[0]);
            Exception canceledException = AsyncOperationResult <ImapResultData> .CanceledException;

            curOp.ProcessCompleted(new ImapConnectionException(string.Empty, RetryPolicy.Backoff, canceledException));
        }
Beispiel #2
0
        // Token: 0x06000134 RID: 308 RVA: 0x00006C24 File Offset: 0x00004E24
        public IAsyncResult BeginCommand(ImapCommand command, bool processResponse, ImapConnectionContext imapConnectionContext, AsyncCallback callback, object callbackState)
        {
            base.CheckDisposed();
            byte[] array = command.ToBytes();
            this.currentResponse.Reset(command);
            this.currentCommand = command;
            AsyncResult <ImapConnectionContext, ImapResultData> asyncResult = new AsyncResult <ImapConnectionContext, ImapResultData>(this, imapConnectionContext, callback, callbackState);

            asyncResult.State.TimeSent = ExDateTime.MinValue;
            if (this.cancellationRequested)
            {
                asyncResult.SetCompletedSynchronously();
                this.HandleCancellation(asyncResult);
                return(asyncResult);
            }
            if (this.isNetworkConnectionShutdown)
            {
                asyncResult.SetCompletedSynchronously();
                ImapNetworkFacade.HandleError(ImapNetworkFacade.GetConnectionClosedException(), asyncResult);
                return(asyncResult);
            }
            this.totalBytesSent += (long)array.Length;
            if (this.totalBytesSent > this.connectionParameters.MaxBytesToTransfer)
            {
                asyncResult.SetCompletedSynchronously();
                ImapNetworkFacade.HandleError(ImapNetworkFacade.MaxBytesSentExceeded(), asyncResult);
                return(asyncResult);
            }
            if (processResponse && this.totalBytesReceived > this.connectionParameters.MaxBytesToTransfer)
            {
                asyncResult.State.Log.Debug("Not sending {0}, since we've exceeded our received-bytes threshold.", new object[]
                {
                    this.currentCommand.ToPiiCleanString()
                });
                asyncResult.SetCompletedSynchronously();
                ImapNetworkFacade.HandleError(ImapNetworkFacade.MaxBytesReceivedExceeded(), asyncResult);
                return(asyncResult);
            }
            asyncResult.State.Log.Info("IMAP Send command: [{0}]", new object[]
            {
                this.currentCommand.ToPiiCleanString()
            });
            DownloadCompleteEventArgs eventArgs = new DownloadCompleteEventArgs(0L, (long)array.Length);

            imapConnectionContext.ActivatePerfDownloadEvent(imapConnectionContext, eventArgs);
            if (processResponse)
            {
                asyncResult.PendingAsyncResult = this.networkConnection.BeginWrite(array, 0, array.Length, new AsyncCallback(this.OnEndWriteCommandOrLiteralBeginReadResponse), asyncResult);
            }
            else
            {
                asyncResult.PendingAsyncResult = this.networkConnection.BeginWrite(array, 0, array.Length, new AsyncCallback(this.OnEndSendCommandIgnoreResponse), asyncResult);
                asyncResult.SetCompletedSynchronously();
                this.currentResultData.Clear();
                asyncResult.ProcessCompleted(this.currentResultData);
            }
            return(asyncResult);
        }
Beispiel #3
0
        // Token: 0x06000146 RID: 326 RVA: 0x000074DC File Offset: 0x000056DC
        private void OnEndConnectNegotiateTlsAsClientInternalReadResponse(IAsyncResult asyncResult)
        {
            lock (this.syncRoot)
            {
                if (this.ShouldCancelCallback())
                {
                    NetworkConnection networkConnection = this.ExtractNetworkConnectionFrom(asyncResult);
                    object            obj2;
                    networkConnection.EndNegotiateTlsAsClient(asyncResult, out obj2);
                }
                else
                {
                    base.CheckDisposed();
                    AsyncResult <ImapConnectionContext, ImapResultData> asyncResult2 = (AsyncResult <ImapConnectionContext, ImapResultData>)asyncResult.AsyncState;
                    ImapConnectionContext state = asyncResult2.State;
                    object obj2;
                    this.networkConnection.EndNegotiateTlsAsClient(asyncResult, out obj2);
                    if (obj2 != null)
                    {
                        ImapNetworkFacade.HandleError(obj2, asyncResult2);
                    }
                    else if (this.cancellationRequested)
                    {
                        this.HandleCancellation(asyncResult2);
                    }
                    else
                    {
                        ExTraceGlobals.FaultInjectionTracer.TraceTest(3211144509U);
                        switch (state.ImapSecurityMechanism)
                        {
                        case ImapSecurityMechanism.Ssl:
                            this.currentResponse.Reset(null);
                            asyncResult2.PendingAsyncResult = this.BeginNetworkRead(asyncResult2, new AsyncCallback(this.OnReadAndDiscardLine));
                            break;

                        case ImapSecurityMechanism.Tls:
                            asyncResult2.ProcessCompleted();
                            break;

                        default:
                            throw new InvalidOperationException("Invalid security mechanism");
                        }
                    }
                }
            }
        }
Beispiel #4
0
        // Token: 0x0600012F RID: 303 RVA: 0x00006AF8 File Offset: 0x00004CF8
        public IAsyncResult BeginConnect(ImapConnectionContext imapConnectionContext, AsyncCallback callback, object callbackState)
        {
            base.CheckDisposed();
            this.ResetStateForConnectIfNecessary();
            this.socket = SocketFactory.CreateTcpStreamSocket();
            AsyncResult <ImapConnectionContext, ImapResultData> asyncResult = new AsyncResult <ImapConnectionContext, ImapResultData>(this, imapConnectionContext, callback, callbackState);

            try
            {
                asyncResult.PendingAsyncResult = this.socket.BeginConnect(this.serverParameters.Server, this.serverParameters.Port, new AsyncCallback(this.OnEndConnectInternalReadResponse), asyncResult);
            }
            catch (SocketException errorCode)
            {
                this.socket = null;
                ImapNetworkFacade.HandleError(errorCode, asyncResult);
            }
            return(asyncResult);
        }
Beispiel #5
0
 // Token: 0x06000149 RID: 329 RVA: 0x00007998 File Offset: 0x00005B98
 private void OnEndWriteCommandOrLiteralBeginReadResponse(IAsyncResult asyncResult)
 {
     lock (this.syncRoot)
     {
         if (this.ShouldCancelCallback())
         {
             NetworkConnection networkConnection = this.ExtractNetworkConnectionFrom(asyncResult);
             object            obj2;
             networkConnection.EndWrite(asyncResult, out obj2);
         }
         else
         {
             base.CheckDisposed();
             AsyncResult <ImapConnectionContext, ImapResultData> asyncResult2 = (AsyncResult <ImapConnectionContext, ImapResultData>)asyncResult.AsyncState;
             object obj2;
             this.networkConnection.EndWrite(asyncResult, out obj2);
             if (obj2 != null)
             {
                 ImapNetworkFacade.HandleError(obj2, asyncResult2);
             }
             else if (this.cancellationRequested)
             {
                 this.HandleCancellation(asyncResult2);
             }
             else
             {
                 ExTraceGlobals.FaultInjectionTracer.TraceTest(2942709053U);
                 asyncResult2.State.Log.Debug("Command/literal sent, begin reading response.", new object[0]);
                 this.currentResponse.Reset(this.currentCommand);
                 this.currentResultData.Clear();
                 asyncResult2.State.TimeSent     = ExDateTime.UtcNow;
                 asyncResult2.PendingAsyncResult = this.BeginNetworkRead(asyncResult2, new AsyncCallback(this.OnReadMoreResponse));
             }
         }
     }
 }
Beispiel #6
0
 // Token: 0x06000148 RID: 328 RVA: 0x0000786C File Offset: 0x00005A6C
 private void OnEndWriteLiteral(IAsyncResult asyncResult)
 {
     lock (this.syncRoot)
     {
         if (this.ShouldCancelCallback())
         {
             NetworkConnection networkConnection = this.ExtractNetworkConnectionFrom(asyncResult);
             object            obj2;
             networkConnection.EndWrite(asyncResult, out obj2);
         }
         else
         {
             base.CheckDisposed();
             AsyncResult <ImapConnectionContext, ImapResultData> asyncResult2 = (AsyncResult <ImapConnectionContext, ImapResultData>)asyncResult.AsyncState;
             object obj2;
             this.networkConnection.EndWrite(asyncResult, out obj2);
             if (obj2 != null)
             {
                 ImapNetworkFacade.HandleError(obj2, asyncResult2);
             }
             else if (this.cancellationRequested)
             {
                 this.HandleCancellation(asyncResult2);
             }
             else
             {
                 ExTraceGlobals.FaultInjectionTracer.TraceTest(3748015421U);
                 this.totalBytesSent += (long)ImapNetworkFacade.BytesCrLf.Length;
                 DownloadCompleteEventArgs eventArgs = new DownloadCompleteEventArgs(0L, (long)ImapNetworkFacade.BytesCrLf.Length);
                 asyncResult2.State.ActivatePerfDownloadEvent(asyncResult2.State, eventArgs);
                 asyncResult2.State.Log.Debug("Literal sent, sending CRLF to complete it.", new object[0]);
                 asyncResult2.PendingAsyncResult = this.networkConnection.BeginWrite(ImapNetworkFacade.BytesCrLf, 0, ImapNetworkFacade.BytesCrLf.Length, new AsyncCallback(this.OnEndWriteCommandOrLiteralBeginReadResponse), asyncResult2);
             }
         }
     }
 }
Beispiel #7
0
 // Token: 0x0600014A RID: 330 RVA: 0x00007AA4 File Offset: 0x00005CA4
 private void OnReadMoreResponse(IAsyncResult asyncResult)
 {
     lock (this.syncRoot)
     {
         if (this.ShouldCancelCallback())
         {
             NetworkConnection networkConnection = this.ExtractNetworkConnectionFrom(asyncResult);
             byte[]            data;
             int    offset;
             int    num;
             object obj2;
             networkConnection.EndRead(asyncResult, out data, out offset, out num, out obj2);
         }
         else
         {
             base.CheckDisposed();
             AsyncResult <ImapConnectionContext, ImapResultData> asyncResult2 = (AsyncResult <ImapConnectionContext, ImapResultData>)asyncResult.AsyncState;
             ImapResponse imapResponse = this.currentResponse;
             byte[]       data;
             int          offset;
             int          num;
             object       obj2;
             this.networkConnection.EndRead(asyncResult, out data, out offset, out num, out obj2);
             if (obj2 != null)
             {
                 ImapNetworkFacade.HandleError(obj2, asyncResult2);
             }
             else if (this.cancellationRequested)
             {
                 this.HandleCancellation(asyncResult2);
             }
             else
             {
                 ExTraceGlobals.FaultInjectionTracer.TraceTest(2674273597U);
                 this.totalBytesReceived += (long)num;
                 DownloadCompleteEventArgs eventArgs = new DownloadCompleteEventArgs((long)num, 0L);
                 asyncResult2.State.ActivatePerfDownloadEvent(asyncResult2.State, eventArgs);
                 if (this.totalBytesReceived > this.connectionParameters.MaxBytesToTransfer)
                 {
                     ImapNetworkFacade.HandleError(ImapNetworkFacade.MaxBytesReceivedExceeded(), asyncResult2);
                 }
                 else
                 {
                     int num2 = imapResponse.AddData(data, offset, num);
                     int num3 = num - num2;
                     this.Log.Assert(num3 >= 0, "The unconsumed bytes must be non-negative.", new object[0]);
                     if (num3 > 0)
                     {
                         this.networkConnection.PutBackReceivedBytes(num3);
                     }
                     if (imapResponse.IsComplete)
                     {
                         ImapNetworkFacade.CountCommand(asyncResult2, true);
                         asyncResult2.State.Log.Debug("Command complete: [{0}].  Status = {1}", new object[]
                         {
                             this.currentCommand.ToPiiCleanString(),
                             imapResponse.Status
                         });
                         this.currentResultData.Status = imapResponse.Status;
                         if (asyncResult.CompletedSynchronously)
                         {
                             asyncResult2.SetCompletedSynchronously();
                         }
                         if (imapResponse.Status == ImapStatus.No || imapResponse.Status == ImapStatus.Bad || imapResponse.Status == ImapStatus.Bye)
                         {
                             this.LogFailureDetails(this.currentCommand, imapResponse);
                             asyncResult2.ProcessCompleted(this.currentResultData);
                         }
                         else if (imapResponse.Status != ImapStatus.Ok)
                         {
                             this.LogFailureDetails(this.currentCommand, imapResponse);
                             asyncResult2.ProcessCompleted(this.BuildAndLogUnknownCommandFailureException(asyncResult2.State));
                         }
                         else
                         {
                             if (!imapResponse.TryParseIntoResult(this.currentCommand, ref this.currentResultData))
                             {
                                 if (this.currentResultData.FailureException == null)
                                 {
                                     if (this.currentResultData.MessageStream != null)
                                     {
                                         this.totalMessagesReceived += 1UL;
                                     }
                                     asyncResult2.ProcessCompleted(this.currentResultData, this.BuildAndLogUnknownParseFailureException(asyncResult2.State));
                                     return;
                                 }
                                 ImapUtilities.LogExceptionDetails(asyncResult2.State.Log, this.currentCommand, this.currentResultData.FailureException);
                                 this.LogFailureDetails(this.currentCommand, this.currentResponse);
                             }
                             else
                             {
                                 asyncResult2.State.Log.Debug("Parsed server response succesfully.", new object[0]);
                             }
                             asyncResult2.ProcessCompleted(this.currentResultData);
                         }
                     }
                     else if (imapResponse.IsWaitingForLiteral)
                     {
                         Exception ex = null;
                         Stream    commandParameterStream = this.currentCommand.GetCommandParameterStream(this.serverParameters.Server, imapResponse.GetLastResponseLine(), out ex);
                         if (ex != null)
                         {
                             if (commandParameterStream != null)
                             {
                                 commandParameterStream.Close();
                             }
                             ImapNetworkFacade.CountCommand(asyncResult2, false);
                             asyncResult2.ProcessCompleted(ex);
                         }
                         else if (commandParameterStream == null)
                         {
                             ImapNetworkFacade.CountCommand(asyncResult2, false);
                             asyncResult2.ProcessCompleted(this.BuildAndLogUnexpectedLiteralRequestException(asyncResult2.State));
                         }
                         else
                         {
                             this.totalBytesSent += commandParameterStream.Length;
                             if (this.totalBytesSent > this.connectionParameters.MaxBytesToTransfer)
                             {
                                 ImapNetworkFacade.HandleError(ImapNetworkFacade.MaxBytesSentExceeded(), asyncResult2);
                             }
                             else
                             {
                                 eventArgs = new DownloadCompleteEventArgs(0L, commandParameterStream.Length);
                                 asyncResult2.State.ActivatePerfDownloadEvent(asyncResult2.State, eventArgs);
                                 asyncResult2.State.Log.Debug("Begin writing literal stream.", new object[0]);
                                 asyncResult2.PendingAsyncResult = this.networkConnection.BeginWrite(commandParameterStream, new AsyncCallback(this.OnEndWriteLiteral), asyncResult2);
                             }
                         }
                     }
                     else if (imapResponse.TotalLiteralBytesExpected > 0 && this.totalBytesReceived + (long)imapResponse.LiteralBytesRemaining > this.connectionParameters.MaxBytesToTransfer)
                     {
                         this.totalBytesReceived += (long)imapResponse.LiteralBytesRemaining;
                         ImapNetworkFacade.HandleError(ImapNetworkFacade.MaxBytesReceivedExceeded(), asyncResult2);
                     }
                     else
                     {
                         asyncResult2.PendingAsyncResult = this.BeginNetworkRead(asyncResult2, new AsyncCallback(this.OnReadMoreResponse));
                     }
                 }
             }
         }
     }
 }
Beispiel #8
0
 // Token: 0x06000147 RID: 327 RVA: 0x000075E4 File Offset: 0x000057E4
 private void OnReadAndDiscardLine(IAsyncResult asyncResult)
 {
     lock (this.syncRoot)
     {
         if (this.ShouldCancelCallback())
         {
             NetworkConnection networkConnection = this.ExtractNetworkConnectionFrom(asyncResult);
             byte[]            data;
             int    offset;
             int    num;
             object obj2;
             networkConnection.EndRead(asyncResult, out data, out offset, out num, out obj2);
         }
         else
         {
             base.CheckDisposed();
             AsyncResult <ImapConnectionContext, ImapResultData> asyncResult2 = (AsyncResult <ImapConnectionContext, ImapResultData>)asyncResult.AsyncState;
             ImapResponse imapResponse = this.currentResponse;
             byte[]       data;
             int          offset;
             int          num;
             object       obj2;
             this.networkConnection.EndRead(asyncResult, out data, out offset, out num, out obj2);
             if (obj2 != null)
             {
                 ImapNetworkFacade.HandleError(obj2, asyncResult2);
             }
             else if (this.cancellationRequested)
             {
                 this.HandleCancellation(asyncResult2);
             }
             else
             {
                 ExTraceGlobals.FaultInjectionTracer.TraceTest(4284886333U);
                 this.totalBytesReceived += (long)num;
                 DownloadCompleteEventArgs eventArgs = new DownloadCompleteEventArgs((long)num, 0L);
                 asyncResult2.State.ActivatePerfDownloadEvent(asyncResult2.State, eventArgs);
                 if (this.totalBytesReceived > this.connectionParameters.MaxBytesToTransfer)
                 {
                     ImapNetworkFacade.HandleError(ImapNetworkFacade.MaxBytesReceivedExceeded(), asyncResult2);
                 }
                 else
                 {
                     int num2 = imapResponse.AddData(data, offset, num);
                     int num3 = num - num2;
                     this.Log.Assert(num3 >= 0, "The unconsumed bytes must be non-negative.", new object[0]);
                     if (num3 > 0)
                     {
                         this.networkConnection.PutBackReceivedBytes(num3);
                     }
                     if (imapResponse.IsComplete)
                     {
                         this.currentResultData.Clear();
                         this.currentResultData.Status = imapResponse.Status;
                         if (asyncResult.CompletedSynchronously)
                         {
                             asyncResult2.SetCompletedSynchronously();
                         }
                         asyncResult2.State.Log.Assert(this.currentCommand == null, "this.currentCommand is expected to be null", new object[0]);
                         if (imapResponse.Status == ImapStatus.No || imapResponse.Status == ImapStatus.Bad || imapResponse.Status == ImapStatus.Bye)
                         {
                             this.LogFailureDetails("Connecting", imapResponse);
                             asyncResult2.ProcessCompleted(this.currentResultData);
                         }
                         else
                         {
                             asyncResult2.State.Log.Assert(imapResponse.ResponseLines != null && imapResponse.ResponseLines.Count > 0, "ResponseLines was null or had no content", new object[0]);
                             if (imapResponse.Status != ImapStatus.Ok)
                             {
                                 asyncResult2.ProcessCompleted(this.BuildAndLogUnknownCommandFailureException(asyncResult2.State, "Connecting"));
                             }
                             else
                             {
                                 asyncResult2.ProcessCompleted(this.currentResultData);
                             }
                         }
                     }
                     else
                     {
                         asyncResult2.PendingAsyncResult = this.BeginNetworkRead(asyncResult2, new AsyncCallback(this.OnReadAndDiscardLine));
                     }
                 }
             }
         }
     }
 }
Beispiel #9
0
        // Token: 0x06000141 RID: 321 RVA: 0x00006FEC File Offset: 0x000051EC
        private static void HandleError(object errorCode, AsyncResult <ImapConnectionContext, ImapResultData> curOp)
        {
            ImapNetworkFacade.CountCommand(curOp, false);
            if (errorCode is ConnectionsPermanentException || errorCode is ConnectionsTransientException)
            {
                curOp.State.Log.Error("Exception while communicating: {0}.", new object[]
                {
                    errorCode
                });
                curOp.ProcessCompleted((Exception)errorCode);
                return;
            }
            ImapNetworkFacade imapNetworkFacade = (ImapNetworkFacade)curOp.State.NetworkFacade;

            if (imapNetworkFacade.networkConnection != null)
            {
                imapNetworkFacade.networkConnection.Shutdown();
                imapNetworkFacade.isNetworkConnectionShutdown = true;
            }
            if (errorCode is SocketError)
            {
                switch ((SocketError)errorCode)
                {
                case SocketError.Shutdown:
                    curOp.State.Log.Error("Network connection shut down: {0}.", new object[]
                    {
                        errorCode
                    });
                    curOp.ProcessCompleted(ImapNetworkFacade.BuildConnectionShutdownException());
                    return;

                case SocketError.TimedOut:
                    curOp.State.Log.Error("Operation timed out, shutting down network connection: {0}.", new object[]
                    {
                        errorCode
                    });
                    curOp.ProcessCompleted(ImapNetworkFacade.BuildTimeoutException());
                    return;
                }
                curOp.State.Log.Error("HandleError unhandled SocketError={0}, shutting down network connection.", new object[]
                {
                    ((SocketError)errorCode).ToString()
                });
                curOp.ProcessCompleted(ImapNetworkFacade.BuildUnknownNetworkException());
                return;
            }
            if (errorCode is SecurityStatus)
            {
                curOp.State.Log.Error("ImapNetworkFacade.HandleError (SecurityStatus={0}), shutting down network connection.", new object[]
                {
                    errorCode
                });
                LocalizedString     value          = CXStrings.TlsFailureOccurredError(((SecurityStatus)errorCode).ToString());
                TlsFailureException innerException = new TlsFailureException(value);
                curOp.ProcessCompleted(new ImapConnectionException(CXStrings.ImapSecurityStatusError, RetryPolicy.Backoff, innerException));
                return;
            }
            curOp.State.Log.Error("HandleError unknown error type (error={0}), shutting down network connection.", new object[]
            {
                errorCode
            });
            curOp.ProcessCompleted(ImapNetworkFacade.BuildUnknownNetworkException());
        }