public override void EndSend(IAsyncResult asyncResult) { AsyncSendRecordsResult asyncSendResult = (AsyncSendRecordsResult)asyncResult; if (!asyncSendResult.IsCompleted) { asyncSendResult.AsyncWaitHandle.WaitOne(); } if (asyncSendResult.CompletedWithException) { throw asyncSendResult.AsyncException; } }
private void StartSend(AsyncSendRecordsResult asyncSendResult) { lock (_sendLock) { if (_sending) { // Send in progress, add to queue if (asyncSendResult != null) { _sendQueue.Add(asyncSendResult); } return; } else if (asyncSendResult != null) { // No send in progress, insert to queue _sendQueue.Insert(0, asyncSendResult); } byte[] outputBuffer = new byte[0]; while (outputBuffer.Length == 0 && _sendQueue.Count > 0) { // Get first asyncSendResult in queue asyncSendResult = _sendQueue[0]; _sendQueue.Remove(asyncSendResult); // Get output buffer from record bytes outputBuffer = asyncSendResult.GetRecords(0); if (outputBuffer.Length == 0) { asyncSendResult.SetComplete(); asyncSendResult = null; } } if (asyncSendResult != null && outputBuffer.Length > 0) { // Start sending the record _innerStream.BeginWrite(outputBuffer, 0, outputBuffer.Length, new AsyncCallback(WriteCallback), asyncSendResult); _sending = true; } } }
public override IAsyncResult BeginSend(Record[] records, AsyncCallback requestCallback, Object state) { if (records == null) { throw new ArgumentNullException("records"); } // Make sure fragments are not too long AsyncSendRecordsResult asyncSendResult = new AsyncSendRecordsResult(records, requestCallback, state); foreach (Record record in records) { if (record.Fragment.Length > MAX_RECORD_SIZE) { asyncSendResult.SetComplete(new RecordTooLargeException("Trying to send a too large fragment: " + record.Fragment.Length)); return(asyncSendResult); } } // Start sending the records StartSend(asyncSendResult); return(asyncSendResult); }
private void WriteCallback(IAsyncResult asyncResult) { AsyncSendRecordsResult asyncSendResult = (AsyncSendRecordsResult)asyncResult.AsyncState; try { _innerStream.EndWrite(asyncResult); // Check if send request has more bytes to send byte[] outputBuffer = asyncSendResult.GetRecords(0); if (outputBuffer.Length > 0) { _innerStream.BeginWrite(outputBuffer, 0, outputBuffer.Length, new AsyncCallback(WriteCallback), asyncSendResult); return; } FinishSend(true); asyncSendResult.SetComplete(); } catch (Exception e) { FinishSend(false); asyncSendResult.SetComplete(e); } }
public override IAsyncResult BeginSend(Record[] records, AsyncCallback requestCallback, Object state) { if (records == null) { throw new ArgumentNullException("records"); } // Make sure fragments are not too long AsyncSendRecordsResult asyncSendResult = new AsyncSendRecordsResult(records, requestCallback, state); foreach (Record record in records) { if (record.Fragment.Length > MAX_RECORD_SIZE) { asyncSendResult.SetComplete(new RecordTooLargeException("Trying to send a too large fragment: " + record.Fragment.Length)); return asyncSendResult; } } // Start sending the records StartSend(asyncSendResult); return asyncSendResult; }