public override uint SendAsync(SNIPacket packet, bool disposePacketAfterSendAsync, SNIAsyncCallback callback = null) { SNIAsyncCallback cb = callback ?? _sendCallback; packet.WriteToStreamAsync(_stream, cb, SNIProviders.NP_PROV, disposePacketAfterSendAsync); return(TdsEnums.SNI_SUCCESS_IO_PENDING); }
/// <summary> /// Send a packet asynchronously /// </summary> /// <param name="packet">SNI packet</param> /// <param name="callback">Completion callback</param> /// <returns>SNI error code</returns> public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { SNIAsyncCallback cb = callback ?? _sendCallback; lock (this) { packet.WriteToStreamAsync(_stream, cb, SNIProviders.TCP_PROV); } return(TdsEnums.SNI_SUCCESS_IO_PENDING); }
/// <summary> /// Send a packet asynchronously /// </summary> /// <param name="packet">SNI packet</param> /// <param name="callback">Completion callback</param> /// <returns>SNI error code</returns> public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { Task writeTask = packet.WriteToStreamAsync(_stream); writeTask.ContinueWith((t) => { SNIAsyncCallback cb = callback ?? _sendCallback; uint status = TdsEnums.SNI_SUCCESS; if (t.IsFaulted) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, t.Exception); status = TdsEnums.SNI_ERROR; } cb(packet, status); } ); return(TdsEnums.SNI_SUCCESS_IO_PENDING); }
/// <summary> /// Send a packet asynchronously /// </summary> /// <param name="packet">SNI packet</param> /// <param name="callback">Completion callback</param> /// <returns>SNI error code</returns> public sealed override bool SendAsync(SNIPacket packet, SNIAsyncCallback callback, bool forceCallback, out SNIError sniError) { try { Task writeTask; using (_debugLock.Acquire(this)) { writeTask = packet.WriteToStreamAsync(_stream); } if (writeTask.IsCompleted && !forceCallback) { if (writeTask.IsFaulted) { sniError = new SNIError(ProviderNumber, 0, writeTask.Exception); } else { sniError = null; } return(true); } else { writeTask.ContinueWith(SendAsyncContinuation, Tuple.Create(this, packet, callback ?? _sendCallback), CancellationToken.None, TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default); sniError = null; return(false); } } catch (ObjectDisposedException ode) { sniError = new SNIError(ProviderNumber, 0, ode); } catch (SocketException se) { sniError = new SNIError(ProviderNumber, 0, se); } catch (IOException ioe) { sniError = new SNIError(ProviderNumber, 0, ioe); } // Fallthrough: We caught an error Debug.Assert(sniError != null, "Should have either set an error or returned early"); if (forceCallback) { Task.Factory.StartNew(SendAsyncErrorContinuation, Tuple.Create(packet, callback ?? _sendCallback, sniError), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); sniError = null; return(false); } else { return(true); } }