상속: AaltoTLS.AsyncGenericResult
예제 #1
0
        public override void EndSend(IAsyncResult asyncResult)
        {
            AsyncSendRecordsResult asyncSendResult = (AsyncSendRecordsResult)asyncResult;

            if (!asyncSendResult.IsCompleted)
            {
                asyncSendResult.AsyncWaitHandle.WaitOne();
            }
            if (asyncSendResult.CompletedWithException)
            {
                throw asyncSendResult.AsyncException;
            }
        }
예제 #2
0
        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;
                }
            }
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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;
        }
예제 #6
0
        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;
                }
            }
        }