Пример #1
0
        public byte[] EndReceive(IAsyncResult asyncResult)
        {
            AsyncReceiveDataResult asyncReceiveResult = (AsyncReceiveDataResult)asyncResult;

            if (!asyncReceiveResult.IsCompleted)
            {
                asyncReceiveResult.AsyncWaitHandle.WaitOne();
            }

            if (asyncReceiveResult.CompletedWithException)
            {
                throw asyncReceiveResult.AsyncException;
            }
            return(asyncReceiveResult.AsyncResult);
        }
Пример #2
0
        private void ReceiveDataCallback(IAsyncResult asyncResult)
        {
            AsyncReceiveDataResult asyncReceiveDataResult = (AsyncReceiveDataResult)asyncResult.AsyncState;

            try {
                Record[]     records   = _recordStream.EndReceive(asyncResult);
                MemoryStream memStream = new MemoryStream();
                foreach (Record record in records)
                {
                    _recordHandler.ProcessInputRecord(record);

                    switch (record.Type)
                    {
                    case RecordType.ChangeCipherSpec:
                        // FIXME: get the handshake result from somewhere
                        //ProcessChangeCipherSpecRecord(record, asyncHandshakeResult);
                        break;

                    case RecordType.Alert:
                        ProcessAlertRecord(record, asyncReceiveDataResult);
                        break;

                    case RecordType.Handshake:
                        // FIXME: get the handshake result from somewhere
                        //ProcessHandshakeRecord(record, asyncHandshakeResult);
                        break;

                    case RecordType.Data:
                        memStream.Write(record.Fragment, 0, record.Fragment.Length);
                        break;

                    default:
                        ProcessUnknownRecord(record, asyncReceiveDataResult);
                        break;
                    }
                }
                asyncReceiveDataResult.SetComplete(memStream.ToArray());
            } catch (AlertException ae) {
                ProcessSendFatalAlert(new Alert(ae.AlertDescription, _handshakeSession.NegotiatedVersion));
                asyncReceiveDataResult.SetComplete(new Exception("Connection closed because of local alert", ae));
            } catch (IOException) {
                asyncReceiveDataResult.SetComplete(new EndOfStreamException("Connection closed unexpectedly"));
            } catch (Exception e) {
                ProcessSendFatalAlert(new Alert(AlertDescription.InternalError, _handshakeSession.NegotiatedVersion));
                asyncReceiveDataResult.SetComplete(new Exception("Connection closed because of local error", e));
            }
        }
Пример #3
0
        public IAsyncResult BeginReceive(AsyncCallback asyncCallback, Object asyncState)
        {
            AsyncReceiveDataResult asyncReceiveResult = new AsyncReceiveDataResult(asyncCallback, asyncState);

            lock (_handshakeLock) {
                if (!_isAuthenticated)
                {
                    Exception e = new InvalidOperationException("Trying to receive on an unauthenticated session");
                    asyncReceiveResult.SetComplete(e);
                    return(asyncReceiveResult);
                }
                if (_isHandshaking)
                {
                    // Queue requests to receive data during renegotiation
                    // TODO: implement this when renegotiation is implemented
                    Exception e = new InvalidOperationException("Receiving data during renegotiation not implemented");
                    asyncReceiveResult.SetComplete(e);
                    return(asyncReceiveResult);
                }
            }

            _recordStream.BeginReceive(new AsyncCallback(ReceiveDataCallback), asyncReceiveResult);
            return(asyncReceiveResult);
        }