Exemple #1
0
    /// <summary>
    /// Initializes a new instance of an inbound <see cref="QuicStream" />.
    /// </summary>
    /// <param name="connectionState">Connection state</param>
    /// <param name="connectionHandle"><see cref="QuicConnection"/> safe handle, used to increment/decrement reference count with each associated stream.</param>
    /// <param name="handle">Native handle.</param>
    /// <param name="flags">Related data from the PEER_STREAM_STARTED connection event.</param>
    /// <param name="defaultErrorCode">Error code used when the stream needs to abort read or write side of the stream internally.</param>
    internal unsafe QuicStream(QuicConnection.State connectionState, MsQuicContextSafeHandle connectionHandle, QUIC_HANDLE *handle, QUIC_STREAM_OPEN_FLAGS flags, long defaultErrorCode)
    {
        _connectionState = connectionState;
        GCHandle context = GCHandle.Alloc(this, GCHandleType.Weak);

        try
        {
            delegate * unmanaged[Cdecl] < QUIC_HANDLE *, void *, QUIC_STREAM_EVENT *, int > nativeCallback = &NativeCallback;
            MsQuicApi.Api.ApiTable->SetCallbackHandler(
                handle,
                nativeCallback,
                (void *)GCHandle.ToIntPtr(context));
            _handle = new MsQuicContextSafeHandle(handle, context, MsQuicApi.Api.ApiTable->StreamClose, SafeHandleType.Stream, connectionHandle);
        }
        catch
        {
            context.Free();
            throw;
        }

        _defaultErrorCode = defaultErrorCode;

        _canRead  = true;
        _canWrite = !flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL);
        if (!_canWrite)
        {
            _sendTcs.TrySetResult(final: true);
        }
        _id   = (long)GetMsQuicParameter <ulong>(_handle, QUIC_PARAM_STREAM_ID);
        _type = flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL) ? QuicStreamType.Unidirectional : QuicStreamType.Bidirectional;
        _startedTcs.TrySetResult();
    }
Exemple #2
0
    /// <summary>
    /// Initializes a new instance of an outbound <see cref="QuicStream" />.
    /// </summary>
    /// <param name="connectionState">Connection state</param>
    /// <param name="connectionHandle"><see cref="QuicConnection"/> safe handle, used to increment/decrement reference count with each associated stream.</param>
    /// <param name="type">The type of the stream to open.</param>
    /// <param name="defaultErrorCode">Error code used when the stream needs to abort read or write side of the stream internally.</param>
    internal unsafe QuicStream(QuicConnection.State connectionState, MsQuicContextSafeHandle connectionHandle, QuicStreamType type, long defaultErrorCode)
    {
        _connectionState = connectionState;
        GCHandle context = GCHandle.Alloc(this, GCHandleType.Weak);

        try
        {
            QUIC_HANDLE *handle;
            ThrowHelper.ThrowIfMsQuicError(MsQuicApi.Api.ApiTable->StreamOpen(
                                               connectionHandle.QuicHandle,
                                               type == QuicStreamType.Unidirectional ? QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL : QUIC_STREAM_OPEN_FLAGS.NONE,
                                               &NativeCallback,
                                               (void *)GCHandle.ToIntPtr(context),
                                               &handle),
                                           "StreamOpen failed");
            _handle = new MsQuicContextSafeHandle(handle, context, MsQuicApi.Api.ApiTable->StreamClose, SafeHandleType.Stream, connectionHandle);
        }
        catch
        {
            context.Free();
            throw;
        }

        _defaultErrorCode = defaultErrorCode;

        _canRead  = type == QuicStreamType.Bidirectional;
        _canWrite = true;
        if (!_canRead)
        {
            _receiveTcs.TrySetResult(final: true);
        }
        _type = type;
    }
Exemple #3
0
 private byte GetStreamType(QuicStreamType type)
 {
     if (_isClient)
     {
         if (type == QuicStreamType.Bidirectional)
         {
             return(0);
         }
         else
         {
             return(2);
         }
     }
     else
     {
         if (type == QuicStreamType.Bidirectional)
         {
             return(1);
         }
         else
         {
             return(3);
         }
     }
 }
Exemple #4
0
        public QuicStream CreateStream(QuicStreamType type, long desiredStreamId)
        {
            var streamType = desiredStreamId & 0x03;

            if (streamType != GetStreamType(type))
            {
                throw new InvalidOperationException($"The ID 0x{desiredStreamId:X} is not a valid {type} stream.");
            }
            return(new QuicStream(this, desiredStreamId));
        }
Exemple #5
0
        private long NextStreamId(QuicStreamType type)
        {
            long streamId;

            if (type == QuicStreamType.Bidirectional)
            {
                streamId = Interlocked.Increment(ref _bidiStreamCount) - 1;
            }
            else
            {
                streamId = Interlocked.Increment(ref _uniStreamCount) - 1;
            }

            return((streamId << 2) | GetStreamType(type));
        }
Exemple #6
0
        public QuicStream CreateStream(QuicStreamType type)
        {
            var nextId = NextStreamId(type);

            return(CreateStream(type, nextId));
        }