|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic.Internal |
| 8 | +{ |
| 9 | + internal class MsQuicApi : IDisposable |
| 10 | + { |
| 11 | + private bool _disposed = false; |
| 12 | + |
| 13 | + private IntPtr _registrationContext; |
| 14 | + |
| 15 | + internal unsafe MsQuicApi() |
| 16 | + { |
| 17 | + var status = (uint)MsQuicNativeMethods.MsQuicOpen(version: 1, out var registration); |
| 18 | + MsQuicStatusException.ThrowIfFailed(status); |
| 19 | + |
| 20 | + NativeRegistration = *registration; |
| 21 | + |
| 22 | + RegistrationOpenDelegate = |
| 23 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.RegistrationOpenDelegate>( |
| 24 | + NativeRegistration.RegistrationOpen); |
| 25 | + RegistrationCloseDelegate = |
| 26 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.RegistrationCloseDelegate>( |
| 27 | + NativeRegistration.RegistrationClose); |
| 28 | + |
| 29 | + SecConfigCreateDelegate = |
| 30 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SecConfigCreateDelegate>( |
| 31 | + NativeRegistration.SecConfigCreate); |
| 32 | + SecConfigDeleteDelegate = |
| 33 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SecConfigDeleteDelegate>( |
| 34 | + NativeRegistration.SecConfigDelete); |
| 35 | + |
| 36 | + SessionOpenDelegate = |
| 37 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SessionOpenDelegate>( |
| 38 | + NativeRegistration.SessionOpen); |
| 39 | + SessionCloseDelegate = |
| 40 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SessionCloseDelegate>( |
| 41 | + NativeRegistration.SessionClose); |
| 42 | + SessionShutdownDelegate = |
| 43 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SessionShutdownDelegate>( |
| 44 | + NativeRegistration.SessionShutdown); |
| 45 | + |
| 46 | + ListenerOpenDelegate = |
| 47 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ListenerOpenDelegate>( |
| 48 | + NativeRegistration.ListenerOpen); |
| 49 | + ListenerCloseDelegate = |
| 50 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ListenerCloseDelegate>( |
| 51 | + NativeRegistration.ListenerClose); |
| 52 | + ListenerStartDelegate = |
| 53 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ListenerStartDelegate>( |
| 54 | + NativeRegistration.ListenerStart); |
| 55 | + ListenerStopDelegate = |
| 56 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ListenerStopDelegate>( |
| 57 | + NativeRegistration.ListenerStop); |
| 58 | + |
| 59 | + ConnectionOpenDelegate = |
| 60 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ConnectionOpenDelegate>( |
| 61 | + NativeRegistration.ConnectionOpen); |
| 62 | + ConnectionCloseDelegate = |
| 63 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ConnectionCloseDelegate>( |
| 64 | + NativeRegistration.ConnectionClose); |
| 65 | + ConnectionShutdownDelegate = |
| 66 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ConnectionShutdownDelegate>( |
| 67 | + NativeRegistration.ConnectionShutdown); |
| 68 | + ConnectionStartDelegate = |
| 69 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.ConnectionStartDelegate>( |
| 70 | + NativeRegistration.ConnectionStart); |
| 71 | + |
| 72 | + StreamOpenDelegate = |
| 73 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.StreamOpenDelegate>( |
| 74 | + NativeRegistration.StreamOpen); |
| 75 | + StreamCloseDelegate = |
| 76 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.StreamCloseDelegate>( |
| 77 | + NativeRegistration.StreamClose); |
| 78 | + StreamStartDelegate = |
| 79 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.StreamStartDelegate>( |
| 80 | + NativeRegistration.StreamStart); |
| 81 | + StreamShutdownDelegate = |
| 82 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.StreamShutdownDelegate>( |
| 83 | + NativeRegistration.StreamShutdown); |
| 84 | + StreamSendDelegate = |
| 85 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.StreamSendDelegate>( |
| 86 | + NativeRegistration.StreamSend); |
| 87 | + |
| 88 | + SetContextDelegate = |
| 89 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SetContextDelegate>( |
| 90 | + NativeRegistration.SetContext); |
| 91 | + GetContextDelegate = |
| 92 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.GetContextDelegate>( |
| 93 | + NativeRegistration.GetContext); |
| 94 | + SetCallbackHandlerDelegate = |
| 95 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SetCallbackHandlerDelegate>( |
| 96 | + NativeRegistration.SetCallbackHandler); |
| 97 | + |
| 98 | + SetParamDelegate = |
| 99 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.SetParamDelegate>( |
| 100 | + NativeRegistration.SetParam); |
| 101 | + GetParamDelegate = |
| 102 | + Marshal.GetDelegateForFunctionPointer<MsQuicNativeMethods.GetParamDelegate>( |
| 103 | + NativeRegistration.GetParam); |
| 104 | + } |
| 105 | + |
| 106 | + internal MsQuicNativeMethods.NativeApi NativeRegistration { get; private set; } |
| 107 | + |
| 108 | + internal MsQuicNativeMethods.RegistrationOpenDelegate RegistrationOpenDelegate { get; private set; } |
| 109 | + internal MsQuicNativeMethods.RegistrationCloseDelegate RegistrationCloseDelegate { get; private set; } |
| 110 | + |
| 111 | + internal MsQuicNativeMethods.SecConfigCreateDelegate SecConfigCreateDelegate { get; private set; } |
| 112 | + internal MsQuicNativeMethods.SecConfigCreateCompleteDelegate SecConfigCreateCompleteDelegate { get; private set; } |
| 113 | + internal MsQuicNativeMethods.SecConfigDeleteDelegate SecConfigDeleteDelegate { get; private set; } |
| 114 | + |
| 115 | + internal MsQuicNativeMethods.SessionOpenDelegate SessionOpenDelegate { get; private set; } |
| 116 | + internal MsQuicNativeMethods.SessionCloseDelegate SessionCloseDelegate { get; private set; } |
| 117 | + internal MsQuicNativeMethods.SessionShutdownDelegate SessionShutdownDelegate { get; private set; } |
| 118 | + |
| 119 | + internal MsQuicNativeMethods.ListenerOpenDelegate ListenerOpenDelegate { get; private set; } |
| 120 | + internal MsQuicNativeMethods.ListenerCloseDelegate ListenerCloseDelegate { get; private set; } |
| 121 | + internal MsQuicNativeMethods.ListenerStartDelegate ListenerStartDelegate { get; private set; } |
| 122 | + internal MsQuicNativeMethods.ListenerStopDelegate ListenerStopDelegate { get; private set; } |
| 123 | + |
| 124 | + internal MsQuicNativeMethods.ConnectionOpenDelegate ConnectionOpenDelegate { get; private set; } |
| 125 | + internal MsQuicNativeMethods.ConnectionCloseDelegate ConnectionCloseDelegate { get; private set; } |
| 126 | + internal MsQuicNativeMethods.ConnectionShutdownDelegate ConnectionShutdownDelegate { get; private set; } |
| 127 | + internal MsQuicNativeMethods.ConnectionStartDelegate ConnectionStartDelegate { get; private set; } |
| 128 | + |
| 129 | + internal MsQuicNativeMethods.StreamOpenDelegate StreamOpenDelegate { get; private set; } |
| 130 | + internal MsQuicNativeMethods.StreamCloseDelegate StreamCloseDelegate { get; private set; } |
| 131 | + internal MsQuicNativeMethods.StreamStartDelegate StreamStartDelegate { get; private set; } |
| 132 | + internal MsQuicNativeMethods.StreamShutdownDelegate StreamShutdownDelegate { get; private set; } |
| 133 | + internal MsQuicNativeMethods.StreamSendDelegate StreamSendDelegate { get; private set; } |
| 134 | + internal MsQuicNativeMethods.StreamReceiveCompleteDelegate StreamReceiveComplete { get; private set; } |
| 135 | + |
| 136 | + internal MsQuicNativeMethods.SetContextDelegate SetContextDelegate { get; private set; } |
| 137 | + internal MsQuicNativeMethods.GetContextDelegate GetContextDelegate { get; private set; } |
| 138 | + internal MsQuicNativeMethods.SetCallbackHandlerDelegate SetCallbackHandlerDelegate { get; private set; } |
| 139 | + |
| 140 | + internal MsQuicNativeMethods.SetParamDelegate SetParamDelegate { get; private set; } |
| 141 | + internal MsQuicNativeMethods.GetParamDelegate GetParamDelegate { get; private set; } |
| 142 | + |
| 143 | + internal void RegistrationOpen(byte[] name) |
| 144 | + { |
| 145 | + MsQuicStatusException.ThrowIfFailed(RegistrationOpenDelegate(name, out var ctx)); |
| 146 | + _registrationContext = ctx; |
| 147 | + } |
| 148 | + |
| 149 | + internal unsafe uint UnsafeSetParam( |
| 150 | + IntPtr Handle, |
| 151 | + uint Level, |
| 152 | + uint Param, |
| 153 | + MsQuicNativeMethods.QuicBuffer Buffer) |
| 154 | + { |
| 155 | + return SetParamDelegate( |
| 156 | + Handle, |
| 157 | + Level, |
| 158 | + Param, |
| 159 | + Buffer.Length, |
| 160 | + Buffer.Buffer); |
| 161 | + } |
| 162 | + |
| 163 | + public void Dispose() |
| 164 | + { |
| 165 | + Dispose(disposing: true); |
| 166 | + GC.SuppressFinalize(this); |
| 167 | + } |
| 168 | + |
| 169 | + ~MsQuicApi() |
| 170 | + { |
| 171 | + Dispose(disposing: false); |
| 172 | + } |
| 173 | + |
| 174 | + private void Dispose(bool disposing) |
| 175 | + { |
| 176 | + if (_disposed) |
| 177 | + { |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + RegistrationCloseDelegate?.Invoke(_registrationContext); |
| 182 | + |
| 183 | + _disposed = true; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments