void DequeueAndFlush(ByteBuffer currentBuffer, AsyncEventArgsCallback callback) { // Dequeue does a checkout of the buffer from its slot. // the callback for the [....] path only enqueues the buffer. // The WriteAsync callback needs to enqueue and also complete. this.currentByteBuffer = null; ByteBuffer dequeued = this.buffers.Dequeue(); Fx.Assert(dequeued == currentBuffer, "Buffer queue in an inconsistent state."); WriteFlushAsyncEventArgs writeflushState = (WriteFlushAsyncEventArgs)currentBuffer.FlushAsyncArgs; if (writeflushState == null) { writeflushState = new WriteFlushAsyncEventArgs(); currentBuffer.FlushAsyncArgs = writeflushState; } writeflushState.Set(callback, null, this); if (currentBuffer.FlushAsync() == AsyncCompletionResult.Completed) { this.buffers.Enqueue(currentBuffer); writeflushState.Complete(true); } }
protected void SetAsyncState(AsyncEventArgsCallback callback, object state) { if (callback == null) { throw Fx.Exception.ArgumentNull("callback"); } this.State = OperationState.PendingCompletion; this.asyncState = state; this.callback = callback; }
internal AsyncCompletionResult GetBufferAsync(GetBufferAsyncEventArgs getBufferState) { Fx.Assert(getBufferState != null, "GetBufferAsyncEventArgs cannot be null."); int count = getBufferState.Arguments.Count; Fx.Assert(count >= 0 && count <= bufferLength, String.Empty); int finalOffset = 0; int bufferOffset = this.offset; if (bufferOffset + count <= bufferLength) { finalOffset = bufferOffset; } else { if (onGetFlushComplete == null) { onGetFlushComplete = new AsyncEventArgsCallback(GetBufferFlushComplete); } if (flushBufferState == null) { this.flushBufferState = new AsyncEventArgs <object>(); } this.flushBufferState.Set(onGetFlushComplete, getBufferState, this); if (FlushBufferAsync(this.flushBufferState) == AsyncCompletionResult.Completed) { finalOffset = 0; this.flushBufferState.Complete(true); } else { return(AsyncCompletionResult.Queued); } } #if DEBUG Fx.Assert(finalOffset + count <= bufferLength, ""); for (int i = 0; i < count; i++) { buffer[finalOffset + i] = (byte)'<'; } #endif //return the buffer and finalOffset; getBufferState.Result = getBufferState.Result ?? new GetBufferEventResult(); getBufferState.Result.Buffer = this.buffer; getBufferState.Result.Offset = finalOffset; return(AsyncCompletionResult.Completed); }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { this.EnsureOpened(); this.IncrementAsyncWriteCount(); Fx.Assert(this.writeState == null || this.writeState.Arguments == null || this.writeState.Arguments.Count <= 0, "All data has not been written yet."); if (onWriteCallback == null) { onWriteCallback = new AsyncEventArgsCallback(OnWriteCallback); onAsyncFlushComplete = new AsyncEventArgsCallback(OnAsyncFlushComplete); } if (this.writeState == null) { this.writeState = new WriteAsyncState(); this.writeArgs = new WriteAsyncArgs(); } else { // Since writeState!= null, check if the stream has an // exception as the async path has already been invoked. this.ThrowOnException(); } this.writeArgs.Set(buffer, offset, count, callback, state); this.writeState.Set(onWriteCallback, this.writeArgs, this); if (this.WriteAsync(this.writeState) == AsyncCompletionResult.Completed) { this.writeState.Complete(true); if (callback != null) { callback(this.writeState.CompletedSynchronouslyAsyncResult); } return(this.writeState.CompletedSynchronouslyAsyncResult); } return(this.writeState.PendingAsyncResult); }
AsyncCompletionResult WriteAsync() { if (this.writerAsyncState == null) { this.writerAsyncArgs = new XmlWriteBase64AsyncArguments(); this.writerAsyncState = new AsyncEventArgs<XmlWriteBase64AsyncArguments>(); } if (onWriteComplete == null) { onWriteComplete = new AsyncEventArgsCallback(OnWriteComplete); } this.writerAsyncArgs.Buffer = this.block; this.writerAsyncArgs.Offset = 0; this.writerAsyncArgs.Count = this.bytesRead; this.writerAsyncState.Set(onWriteComplete, this.writerAsyncArgs, this); if (this.writer.WriteBase64Async(this.writerAsyncState) == AsyncCompletionResult.Completed) { this.HandleWriteComplete(); this.writerAsyncState.Complete(true); return AsyncCompletionResult.Completed; } return AsyncCompletionResult.Queued; }
bool HandleWriteBase64Text(bool isAsyncCallback) { Fx.Assert(this.count > 0 && this.actualByteCount >= 3, "HandleWriteBase64Text cannot be invoked with less than 3 bytes."); if (!writer.isXmlnsAttribute) { if (!isAsyncCallback) { if (this.nodeWriterAsyncState == null) { this.nodeWriterAsyncState = new AsyncEventArgs<XmlNodeWriterWriteBase64TextArgs>(); this.nodeWriterArgs = new XmlNodeWriterWriteBase64TextArgs(); } if (onWriteComplete == null) { onWriteComplete = new AsyncEventArgsCallback(OnWriteComplete); } this.writer.StartContent(); this.nodeWriterArgs.TrailBuffer = this.writer.trailBytes; this.nodeWriterArgs.TrailCount = this.writer.trailByteCount; this.nodeWriterArgs.Buffer = this.buffer; this.nodeWriterArgs.Offset = this.offset; this.nodeWriterArgs.Count = this.actualByteCount - this.writer.trailByteCount; this.nodeWriterAsyncState.Set(onWriteComplete, this.nodeWriterArgs, this); if (this.writer.writer.WriteBase64TextAsync(this.nodeWriterAsyncState) != AsyncCompletionResult.Completed) { return false; } this.nodeWriterAsyncState.Complete(true); } this.writer.EndContent(); } this.writer.trailByteCount = (this.totalByteCount - this.actualByteCount); if (this.writer.trailByteCount > 0) { int trailOffset = offset + count - this.writer.trailByteCount; for (int i = 0; i < this.writer.trailByteCount; i++) this.writer.trailBytes[i] = this.buffer[trailOffset++]; } return true; }
public virtual void Set(AsyncEventArgsCallback callback, TArgument arguments, object state) { base.SetAsyncState(callback, state); this.Arguments = arguments; }