示例#1
0
        private static void AsyncCallback_Context(object state)
        {
            DbAsyncResult ar = (DbAsyncResult)state;

            if (ar._callback != null)
            {
                ar._callback(ar);
            }
        }
示例#2
0
        private void ExecuteCallback(object asyncResult)
        {
            DbAsyncResult state = (DbAsyncResult)asyncResult;

            if (state._callback != null)
            {
                if (state._execContext != null)
                {
                    ExecutionContext.Run(state._execContext, _contextCallback, state);
                }
                else
                {
                    state._callback(this);
                }
            }
        }
 internal void SetActiveConnectionAndResult(DbAsyncResult result, SqlConnection activeConnection)
 {
     this._cachedAsyncCloseCount = activeConnection.CloseCount;
     this._cachedAsyncResult = result;
     if (((activeConnection != null) && !activeConnection.Parser.MARSOn) && activeConnection.AsycCommandInProgress)
     {
         throw SQL.MARSUnspportedOnConnection();
     }
     this._cachedAsyncConnection = activeConnection;
     this._cachedAsyncConnection.AsycCommandInProgress = true;
 }
 internal void ResetAsyncState()
 {
     this._cachedAsyncCloseCount = -1;
     this._cachedAsyncResult = null;
     if (this._cachedAsyncConnection != null)
     {
         this._cachedAsyncConnection.AsycCommandInProgress = false;
         this._cachedAsyncConnection = null;
     }
     this._cachedAsyncReader = null;
     this._cachedRunBehavior = RunBehavior.ReturnImmediately;
     this._cachedSetOptions = null;
 }
 public IAsyncResult BeginExecuteXmlReader(AsyncCallback callback, object stateObject)
 {
     IAsyncResult result2;
     SqlConnection.ExecutePermission.Demand();
     this._pendingCancel = false;
     this.ValidateAsyncCommand();
     SqlStatistics statistics = null;
     try
     {
         statistics = SqlStatistics.StartTimer(this.Statistics);
         ExecutionContext execContext = (callback == null) ? null : ExecutionContext.Capture();
         DbAsyncResult result = new DbAsyncResult(this, "EndExecuteXmlReader", callback, stateObject, execContext);
         try
         {
             this.RunExecuteReader(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, true, "BeginExecuteXmlReader", result);
         }
         catch (Exception exception4)
         {
             if (!ADP.IsCatchableOrSecurityExceptionType(exception4))
             {
                 throw;
             }
             this.PutStateObject();
             throw;
         }
         SNIHandle target = null;
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
             target = SqlInternalConnection.GetBestEffortCleanupTarget(this._activeConnection);
             this.cachedAsyncState.SetActiveConnectionAndResult(result, this._activeConnection);
             this._stateObj.ReadSni(result, this._stateObj);
         }
         catch (OutOfMemoryException exception3)
         {
             this._activeConnection.Abort(exception3);
             throw;
         }
         catch (StackOverflowException exception2)
         {
             this._activeConnection.Abort(exception2);
             throw;
         }
         catch (ThreadAbortException exception)
         {
             this._activeConnection.Abort(exception);
             SqlInternalConnection.BestEffortCleanup(target);
             throw;
         }
         catch (Exception)
         {
             if (this._cachedAsyncState != null)
             {
                 this._cachedAsyncState.ResetAsyncState();
             }
             this.PutStateObject();
             throw;
         }
         result2 = result;
     }
     finally
     {
         SqlStatistics.StopTimer(statistics);
     }
     return result2;
 }
 private void VerifyEndExecuteState(DbAsyncResult dbAsyncResult, string endMethod)
 {
     if (dbAsyncResult == null)
     {
         throw ADP.ArgumentNull("asyncResult");
     }
     if (dbAsyncResult.EndMethodName != endMethod)
     {
         throw ADP.MismatchedAsyncResult(dbAsyncResult.EndMethodName, endMethod);
     }
     if (!this.cachedAsyncState.IsActiveConnectionValid(this._activeConnection))
     {
         throw ADP.CommandAsyncOperationCompleted();
     }
     dbAsyncResult.CompareExchangeOwner(this, endMethod);
 }
 internal SqlDataReader RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, bool returnStream, string method, DbAsyncResult result)
 {
     SqlDataReader reader;
     bool async = null != result;
     this._rowsAffected = -1;
     if ((CommandBehavior.SingleRow & cmdBehavior) != CommandBehavior.Default)
     {
         cmdBehavior |= CommandBehavior.SingleResult;
     }
     this.ValidateCommand(method, null != result);
     this.CheckNotificationStateAndAutoEnlist();
     SNIHandle target = null;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         target = SqlInternalConnection.GetBestEffortCleanupTarget(this._activeConnection);
         SqlStatistics statistics = this.Statistics;
         if (statistics != null)
         {
             if (((!this.IsDirty && this.IsPrepared) && !this._hiddenPrepare) || (this.IsPrepared && (this._execType == EXECTYPE.PREPAREPENDING)))
             {
                 statistics.SafeIncrement(ref statistics._preparedExecs);
             }
             else
             {
                 statistics.SafeIncrement(ref statistics._unpreparedExecs);
             }
         }
         if (this._activeConnection.IsContextConnection)
         {
             return this.RunExecuteReaderSmi(cmdBehavior, runBehavior, returnStream);
         }
         reader = this.RunExecuteReaderTds(cmdBehavior, runBehavior, returnStream, async);
     }
     catch (OutOfMemoryException exception3)
     {
         this._activeConnection.Abort(exception3);
         throw;
     }
     catch (StackOverflowException exception2)
     {
         this._activeConnection.Abort(exception2);
         throw;
     }
     catch (ThreadAbortException exception)
     {
         this._activeConnection.Abort(exception);
         SqlInternalConnection.BestEffortCleanup(target);
         throw;
     }
     return reader;
 }
 private int InternalExecuteNonQuery(DbAsyncResult result, string methodName, bool sendToPipe)
 {
     int num;
     bool async = null != result;
     SqlStatistics statistics = this.Statistics;
     this._rowsAffected = -1;
     SNIHandle target = null;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         target = SqlInternalConnection.GetBestEffortCleanupTarget(this._activeConnection);
         this.ValidateCommand(methodName, null != result);
         this.CheckNotificationStateAndAutoEnlist();
         if (this._activeConnection.IsContextConnection)
         {
             if (statistics != null)
             {
                 statistics.SafeIncrement(ref statistics._unpreparedExecs);
             }
             this.RunExecuteNonQuerySmi(sendToPipe);
         }
         else if ((!this.BatchRPCMode && (System.Data.CommandType.Text == this.CommandType)) && (this.GetParameterCount(this._parameters) == 0))
         {
             if (statistics != null)
             {
                 if (!this.IsDirty && this.IsPrepared)
                 {
                     statistics.SafeIncrement(ref statistics._preparedExecs);
                 }
                 else
                 {
                     statistics.SafeIncrement(ref statistics._unpreparedExecs);
                 }
             }
             this.RunExecuteNonQueryTds(methodName, async);
         }
         else
         {
             Bid.Trace("<sc.SqlCommand.ExecuteNonQuery|INFO> %d#, Command executed as RPC.\n", this.ObjectID);
             SqlDataReader reader = this.RunExecuteReader(CommandBehavior.Default, RunBehavior.UntilDone, false, methodName, result);
             if (reader != null)
             {
                 reader.Close();
             }
         }
         num = this._rowsAffected;
     }
     catch (OutOfMemoryException exception3)
     {
         this._activeConnection.Abort(exception3);
         throw;
     }
     catch (StackOverflowException exception2)
     {
         this._activeConnection.Abort(exception2);
         throw;
     }
     catch (ThreadAbortException exception)
     {
         this._activeConnection.Abort(exception);
         SqlInternalConnection.BestEffortCleanup(target);
         throw;
     }
     return num;
 }
 private IAsyncResult InternalBeginExecuteReader(AsyncCallback callback, object stateObject, CommandBehavior behavior)
 {
     ExecutionContext execContext = (callback == null) ? null : ExecutionContext.Capture();
     DbAsyncResult result = new DbAsyncResult(this, "EndExecuteReader", callback, stateObject, execContext);
     this.ValidateAsyncCommand();
     try
     {
         this.RunExecuteReader(behavior, RunBehavior.ReturnImmediately, true, "BeginExecuteReader", result);
     }
     catch (Exception exception4)
     {
         if (!ADP.IsCatchableOrSecurityExceptionType(exception4))
         {
             throw;
         }
         this.PutStateObject();
         throw;
     }
     SNIHandle target = null;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         target = SqlInternalConnection.GetBestEffortCleanupTarget(this._activeConnection);
         this.cachedAsyncState.SetActiveConnectionAndResult(result, this._activeConnection);
         this._stateObj.ReadSni(result, this._stateObj);
     }
     catch (OutOfMemoryException exception3)
     {
         this._activeConnection.Abort(exception3);
         throw;
     }
     catch (StackOverflowException exception2)
     {
         this._activeConnection.Abort(exception2);
         throw;
     }
     catch (ThreadAbortException exception)
     {
         this._activeConnection.Abort(exception);
         SqlInternalConnection.BestEffortCleanup(target);
         throw;
     }
     catch (Exception)
     {
         if (this._cachedAsyncState != null)
         {
             this._cachedAsyncState.ResetAsyncState();
         }
         this.PutStateObject();
         throw;
     }
     return result;
 }
 internal void ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
 {
     if ((this._parser.State != TdsParserState.Broken) && (this._parser.State != TdsParserState.Closed))
     {
         IntPtr zero = IntPtr.Zero;
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
             uint num;
             if (!this._parser.AsyncOn)
             {
                 num = SNINativeMethodWrapper.SNIReadSync(stateObj.Handle, ref zero, TdsParserStaticMethods.GetTimeoutMilliseconds(stateObj.TimeoutTime));
                 if (num == 0)
                 {
                     stateObj.ProcessSniPacket(zero, 0);
                 }
                 else
                 {
                     this.ReadSniError(stateObj, num);
                 }
             }
             else
             {
                 stateObj._asyncResult = asyncResult;
                 RuntimeHelpers.PrepareConstrainedRegions();
                 try
                 {
                 }
                 finally
                 {
                     stateObj.IncrementPendingCallbacks();
                     num = SNINativeMethodWrapper.SNIReadAsync(stateObj.Handle, ref zero);
                     if ((num != 0) && (0x3e5 != num))
                     {
                         stateObj.DecrementPendingCallbacks(false);
                     }
                 }
                 if (num == 0)
                 {
                     stateObj._asyncResult.SetCompletedSynchronously();
                     stateObj.ReadAsyncCallback(ADP.PtrZero, zero, 0);
                 }
                 else if (0x3e5 != num)
                 {
                     this.ReadSniError(stateObj, num);
                 }
             }
         }
         finally
         {
             if (zero != IntPtr.Zero)
             {
                 SNINativeMethodWrapper.SNIPacketRelease(zero);
             }
         }
     }
 }
 internal void ReadNetworkPacket()
 {
     this._inBytesUsed = 0;
     if (this.Parser.AsyncOn && (this._cachedAsyncResult == null))
     {
         this._cachedAsyncResult = new DbAsyncResult(this, string.Empty, null, null, null);
     }
     this.ReadSni(this._cachedAsyncResult, this);
     if (this.Parser.AsyncOn)
     {
         this.ReadSniSyncOverAsync();
     }
     this.SniReadStatisticsAndTracing();
     if (Bid.AdvancedOn)
     {
         Bid.TraceBin("<sc.TdsParser.ReadNetworkPacket|INFO|ADV> Packet read", this._inBuff, (ushort) this._inBytesRead);
     }
 }
 private void WriteSni()
 {
     uint num;
     if (this._sniPacket == null)
     {
         this._sniPacket = new SNIPacket(this.Handle);
     }
     else
     {
         SNINativeMethodWrapper.SNIPacketReset(this.Handle, SNINativeMethodWrapper.IOType.WRITE, this._sniPacket, SNINativeMethodWrapper.ConsumerNumber.SNI_Consumer_SNI);
     }
     SNINativeMethodWrapper.SNIPacketSetData(this._sniPacket, this._outBuff, this._outBytesUsed);
     if (this._parser.AsyncOn)
     {
         if (this._cachedAsyncResult == null)
         {
             this._cachedAsyncResult = new DbAsyncResult(this._parser, string.Empty, null, null, null);
         }
         this._asyncResult = this._cachedAsyncResult;
         num = this.SNIWriteAsync(this.Handle, this._sniPacket, this._cachedAsyncResult);
     }
     else
     {
         num = SNINativeMethodWrapper.SNIWriteSync(this.Handle, this._sniPacket);
         if (num != 0)
         {
             Bid.Trace("<sc.TdsParser.WritePacket|Info> write sync returned error code %d\n", (int) num);
             this._parser.Errors.Add(this._parser.ProcessSNIError(this));
             this.ThrowExceptionAndWarning();
         }
         if (this._bulkCopyOpperationInProgress && (TdsParserStaticMethods.GetTimeoutMilliseconds(this.TimeoutTime) == 0))
         {
             this._parser.Errors.Add(new SqlError(-2, 0, 11, this._parser.Server, SQLMessage.Timeout(), "", 0));
             this.SendAttention();
             this._parser.ProcessPendingAck(this);
             this._parser.ThrowExceptionAndWarning();
         }
     }
     if ((this._parser.State == TdsParserState.OpenNotLoggedIn) && (this._parser.EncryptionOptions == EncryptionOptions.LOGIN))
     {
         this._parser.RemoveEncryption();
         this._parser.EncryptionOptions = EncryptionOptions.OFF;
         this._sniPacket.Dispose();
         this._sniPacket = new SNIPacket(this.Handle);
     }
     this.SniWriteStatisticsAndTracing();
     this.ResetBuffer();
 }
 private uint SNIWriteAsync(SNIHandle handle, SNIPacket packet, DbAsyncResult asyncResult)
 {
     uint num;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
     }
     finally
     {
         this.IncrementPendingCallbacks();
         num = SNINativeMethodWrapper.SNIWriteAsync(handle, packet);
         if ((num == 0) || (num != 0x3e5))
         {
             this.DecrementPendingCallbacks(false);
         }
     }
     if (num != 0)
     {
         if (num != 0x3e5)
         {
             Bid.Trace("<sc.TdsParser.WritePacket|Info> write async returned error code %d\n", (int) num);
             this._parser.Errors.Add(this._parser.ProcessSNIError(this));
             this.ThrowExceptionAndWarning();
             return num;
         }
         if (num != 0x3e5)
         {
             return num;
         }
         try
         {
             ((IAsyncResult) asyncResult).AsyncWaitHandle.WaitOne();
             if (this._error != null)
             {
                 this._parser.Errors.Add(this._error);
                 this._error = null;
                 Bid.Trace("<sc.TdsParser.WritePacket|Info> write async returned error code %d\n", (int) num);
                 this.ThrowExceptionAndWarning();
             }
         }
         finally
         {
             asyncResult.Reset();
         }
     }
     return num;
 }
 internal void SendAttention()
 {
     if (!this._attentionSent && ((this._parser.State != TdsParserState.Closed) && (this._parser.State != TdsParserState.Broken)))
     {
         uint num;
         SNIPacket packet = new SNIPacket(this.Handle);
         if (this._parser.AsyncOn)
         {
             this._sniAsyncAttnPacket = packet;
             if (this._asyncAttentionResult == null)
             {
                 this._asyncAttentionResult = new DbAsyncResult(this._parser, string.Empty, null, null, null);
             }
         }
         else
         {
             this._sniAsyncAttnPacket = null;
         }
         SNINativeMethodWrapper.SNIPacketSetData(packet, SQL.AttentionHeader, 8);
         if (this._parser.AsyncOn)
         {
             num = this.SNIWriteAsync(this.Handle, packet, this._asyncAttentionResult);
             Bid.Trace("<sc.TdsParser.SendAttention|Info> Send Attention ASync .\n");
         }
         else
         {
             num = SNINativeMethodWrapper.SNIWriteSync(this.Handle, packet);
             Bid.Trace("<sc.TdsParser.SendAttention|Info> Send Attention Sync.\n");
             if (num != 0)
             {
                 Bid.Trace("<sc.TdsParser.SendAttention|Info> SNIWriteSync returned error code %d\n", (int) num);
                 this._parser.Errors.Add(this._parser.ProcessSNIError(this));
                 this._parser.ThrowExceptionAndWarning();
             }
         }
         this.SetTimeoutSeconds(5);
         this._attentionSent = true;
         if (Bid.AdvancedOn)
         {
             Bid.TraceBin("<sc.TdsParser.WritePacket|INFO|ADV>  Packet sent", this._outBuff, (ushort) this._outBytesUsed);
         }
         Bid.Trace("<sc.TdsParser.SendAttention|Info> Attention sent to the server.\n");
     }
 }