private void StartWriting(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest) { // We loop to this method from the callback. // If the last chunk was just completed from async callback (count < 0), we complete user request. if (count >= 0) { byte[] outBuffer = null; do { int chunkBytes = Math.Min(count, NegoState.MaxWriteDataSize); int encryptedBytes; try { encryptedBytes = _negoState.EncryptData(buffer, offset, chunkBytes, ref outBuffer); } catch (Exception e) { throw new IOException(SR.net_io_encrypt, e); } if (asyncRequest != null) { // prepare for the next request asyncRequest.SetNextRequest(buffer, offset + chunkBytes, count - chunkBytes, null); IAsyncResult ar = InnerStreamAPM.BeginWrite(outBuffer, 0, encryptedBytes, s_writeCallback, asyncRequest); if (!ar.CompletedSynchronously) { return; } InnerStreamAPM.EndWrite(ar); } else { InnerStream.Write(outBuffer, 0, encryptedBytes); } offset += chunkBytes; count -= chunkBytes; } while (count != 0); } if (asyncRequest != null) { asyncRequest.CompleteUser(); } }
// // private IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Async)) { #endif _negoState.CheckThrow(true); if (!_negoState.CanGetSecureStream) { return(InnerStreamAPM.BeginWrite(buffer, offset, count, asyncCallback, asyncState)); } BufferAsyncResult bufferResult = new BufferAsyncResult(this, buffer, offset, count, true, asyncState, asyncCallback); AsyncProtocolRequest asyncRequest = new AsyncProtocolRequest(bufferResult); ProcessWrite(buffer, offset, count, asyncRequest); return(bufferResult); #if DEBUG } #endif }