Esempio n. 1
0
        public async ValueTask <QuicSecConfig> CreateSecurityConfig(X509Certificate2 certificate)
        {
            QuicSecConfig secConfig             = null;
            var           tcs                   = new TaskCompletionSource <object>();
            var           secConfigCreateStatus = MsQuicConstants.InternalError;

            var status = SecConfigCreateDelegate(
                _registrationContext,
                (uint)QUIC_SEC_CONFIG_FLAG.CERT_CONTEXT,
                certificate.Handle,
                null,
                IntPtr.Zero,
                SecCfgCreateCallbackHandler);

            MsQuicStatusException.ThrowIfFailed(status);

            void SecCfgCreateCallbackHandler(
                IntPtr context,
                uint status,
                IntPtr securityConfig)
            {
                secConfig             = new QuicSecConfig(this, securityConfig);
                secConfigCreateStatus = status;
                tcs.SetResult(null);
            }

            await tcs.Task;

            MsQuicStatusException.ThrowIfFailed(secConfigCreateStatus);

            return(secConfig);
        }
Esempio n. 2
0
 private void SetParam(
     QUIC_PARAM_CONN param,
     QuicBuffer buf)
 {
     MsQuicStatusException.ThrowIfFailed(_api.UnsafeSetParam(
                                             _nativeObjPtr,
                                             (uint)QUIC_PARAM_LEVEL.CONNECTION,
                                             (uint)param,
                                             buf));
 }
Esempio n. 3
0
 private void SetParam(
     QUIC_PARAM_SESSION param,
     MsQuicNativeMethods.QuicBuffer buf)
 {
     MsQuicStatusException.ThrowIfFailed(_registration.UnsafeSetParam(
                                             _nativeObjPtr,
                                             (uint)QUIC_PARAM_LEVEL.SESSION,
                                             (uint)param,
                                             buf));
 }
Esempio n. 4
0
        public void Shutdown(
            QUIC_CONNECTION_SHUTDOWN_FLAG Flags,
            ushort ErrorCode)
        {
            var status = _api.ConnectionShutdownDelegate(
                _nativeObjPtr,
                (uint)Flags,
                ErrorCode);

            MsQuicStatusException.ThrowIfFailed(status);
        }
Esempio n. 5
0
        internal IntPtr ListenerOpen(ListenerCallbackDelegate callback)
        {
            var status = _registration.ListenerOpenDelegate(
                _nativeObjPtr,
                callback,
                IntPtr.Zero,
                out var listenerPointer
                );

            MsQuicStatusException.ThrowIfFailed(status);

            return(listenerPointer);
        }
Esempio n. 6
0
        public MsQuicStream StreamOpen(
            QUIC_STREAM_OPEN_FLAG flags)
        {
            var streamPtr = IntPtr.Zero;
            var status    = _api.StreamOpenDelegate(
                _nativeObjPtr,
                (uint)flags,
                MsQuicStream.NativeCallbackHandler,
                IntPtr.Zero,
                out streamPtr);

            MsQuicStatusException.ThrowIfFailed(status);

            return(new MsQuicStream(_api, this, _context, flags, streamPtr));
        }
Esempio n. 7
0
        public ValueTask StartAsync(
            ushort family,
            string serverName,
            ushort serverPort)
        {
            var status = _api.ConnectionStartDelegate(
                _nativeObjPtr,
                family,
                serverName,
                serverPort);

            MsQuicStatusException.ThrowIfFailed(status);

            return(new ValueTask());
        }
Esempio n. 8
0
        public QuicSession SessionOpen(
            string alpn)
        {
            var sessionPtr = IntPtr.Zero;

            var status = SessionOpenDelegate(
                _registrationContext,
                Encoding.UTF8.GetBytes(alpn),
                IntPtr.Zero,
                ref sessionPtr);

            MsQuicStatusException.ThrowIfFailed(status);

            return(new QuicSession(this, sessionPtr));
        }
Esempio n. 9
0
        public async ValueTask <MsQuicConnection> ConnectionOpenAsync(IPEndPoint endpoint, MsQuicTransportContext context)
        {
            var status = _registration.ConnectionOpenDelegate(
                _nativeObjPtr,
                MsQuicConnection.NativeCallbackHandler,
                IntPtr.Zero,
                out var connectionPtr);

            MsQuicStatusException.ThrowIfFailed(status);

            var msQuicConnection = new MsQuicConnection(_registration, context, connectionPtr);

            await msQuicConnection.StartAsync((ushort)endpoint.AddressFamily, endpoint.Address.ToString(), (ushort)endpoint.Port);

            return(msQuicConnection);
        }
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            _api.RegistrationOpen(Encoding.ASCII.GetBytes(_transportContext.Options.RegistrationName));

            _secConfig = await _api.CreateSecurityConfig(_transportContext.Options.Certificate);

            _session = _api.SessionOpen(_transportContext.Options.Alpn);
            _log.LogDebug(0, "Started session");

            _nativeObjPtr = _session.ListenerOpen(NativeCallbackHandler);

            SetCallbackHandler();

            _session.SetIdleTimeout(_transportContext.Options.IdleTimeout);
            _session.SetPeerBiDirectionalStreamCount(_transportContext.Options.MaxBidirectionalStreamCount);
            _session.SetPeerUnidirectionalStreamCount(_transportContext.Options.MaxBidirectionalStreamCount);

            var address = MsQuicNativeMethods.Convert(EndPoint as IPEndPoint);

            MsQuicStatusException.ThrowIfFailed(_api.ListenerStartDelegate(
                                                    _nativeObjPtr,
                                                    ref address));
        }
Esempio n. 11
0
        internal unsafe MsQuicApi()
        {
            var status = (uint)MsQuicNativeMethods.MsQuicOpen(version: 1, out var registration);

            MsQuicStatusException.ThrowIfFailed(status);

            NativeRegistration = *registration;

            RegistrationOpenDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.RegistrationOpenDelegate>(
                    NativeRegistration.RegistrationOpen);
            RegistrationCloseDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.RegistrationCloseDelegate>(
                    NativeRegistration.RegistrationClose);

            SecConfigCreateDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SecConfigCreateDelegate>(
                    NativeRegistration.SecConfigCreate);
            SecConfigDeleteDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SecConfigDeleteDelegate>(
                    NativeRegistration.SecConfigDelete);

            SessionOpenDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SessionOpenDelegate>(
                    NativeRegistration.SessionOpen);
            SessionCloseDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SessionCloseDelegate>(
                    NativeRegistration.SessionClose);
            SessionShutdownDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SessionShutdownDelegate>(
                    NativeRegistration.SessionShutdown);

            ListenerOpenDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ListenerOpenDelegate>(
                    NativeRegistration.ListenerOpen);
            ListenerCloseDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ListenerCloseDelegate>(
                    NativeRegistration.ListenerClose);
            ListenerStartDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ListenerStartDelegate>(
                    NativeRegistration.ListenerStart);
            ListenerStopDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ListenerStopDelegate>(
                    NativeRegistration.ListenerStop);

            ConnectionOpenDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ConnectionOpenDelegate>(
                    NativeRegistration.ConnectionOpen);
            ConnectionCloseDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ConnectionCloseDelegate>(
                    NativeRegistration.ConnectionClose);
            ConnectionShutdownDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ConnectionShutdownDelegate>(
                    NativeRegistration.ConnectionShutdown);
            ConnectionStartDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.ConnectionStartDelegate>(
                    NativeRegistration.ConnectionStart);

            StreamOpenDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.StreamOpenDelegate>(
                    NativeRegistration.StreamOpen);
            StreamCloseDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.StreamCloseDelegate>(
                    NativeRegistration.StreamClose);
            StreamStartDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.StreamStartDelegate>(
                    NativeRegistration.StreamStart);
            StreamShutdownDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.StreamShutdownDelegate>(
                    NativeRegistration.StreamShutdown);
            StreamSendDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.StreamSendDelegate>(
                    NativeRegistration.StreamSend);

            SetContextDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SetContextDelegate>(
                    NativeRegistration.SetContext);
            GetContextDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.GetContextDelegate>(
                    NativeRegistration.GetContext);
            SetCallbackHandlerDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SetCallbackHandlerDelegate>(
                    NativeRegistration.SetCallbackHandler);

            SetParamDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.SetParamDelegate>(
                    NativeRegistration.SetParam);
            GetParamDelegate =
                Marshal.GetDelegateForFunctionPointer <MsQuicNativeMethods.GetParamDelegate>(
                    NativeRegistration.GetParam);
        }