Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
More fixes
  • Loading branch information
jkoritzinsky committed Feb 12, 2022
commit 4e9e8d297177ce57bc770125174c5757cbe70f59
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ static MsQuicApi()
{
if (NativeLibrary.TryGetExport(msQuicHandle, "MsQuicOpenVersion", out IntPtr msQuicOpenVersionAddress))
{
delegate* unmanaged[Cdecl]<uint, out NativeApi*, uint> msQuicOpenVersion =
(delegate* unmanaged[Cdecl]<uint, out NativeApi*, uint>)msQuicOpenVersionAddress;
uint status = msQuicOpenVersion(MsQuicVersion, out NativeApi* vtable);
NativeApi* vtable;
delegate* unmanaged[Cdecl]<uint, NativeApi**, uint> msQuicOpenVersion =
(delegate* unmanaged[Cdecl]<uint, NativeApi**, uint>)msQuicOpenVersionAddress;
uint status = msQuicOpenVersion(MsQuicVersion, &vtable);
if (MsQuicStatusHelper.SuccessfulStatusCode(status))
{
IsQuicSupported = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ internal struct NewConnectionInfo
internal delegate uint ListenerCallbackDelegate(
IntPtr listener,
IntPtr context,
ref ListenerEvent evt);
ListenerEvent* evt);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate uint ListenerOpenDelegate(
Expand Down Expand Up @@ -553,7 +553,7 @@ internal struct ConnectionEvent
internal delegate uint ConnectionCallbackDelegate(
IntPtr connection,
IntPtr context,
ref ConnectionEvent connectionEvent);
ConnectionEvent* connectionEvent);

// TODO: order is Open, Close, Shutdown, Start, SetConfiguration, SendResumptionTicket
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
Expand Down Expand Up @@ -708,7 +708,7 @@ internal struct SOCKADDR_INET
internal delegate uint StreamCallbackDelegate(
IntPtr stream,
IntPtr context,
ref StreamEvent streamEvent);
StreamEvent* streamEvent);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate uint StreamOpenDelegate(
Expand Down Expand Up @@ -1034,16 +1034,6 @@ internal uint ConfigurationLoadCredential(SafeMsQuicConfigurationHandle configur

return __retVal;
}
internal uint ListenerCallback(IntPtr listener, IntPtr context, ref ListenerEvent evt)
{
uint __retVal;
fixed (ListenerEvent* __evt_gen_native = &evt)
{
__retVal = ((delegate* unmanaged[Cdecl]<IntPtr, IntPtr, ListenerEvent*, uint>)_functionPointer)(listener, context, __evt_gen_native);
}

return __retVal;
}
internal uint ListenerOpen(SafeMsQuicRegistrationHandle registration, ListenerCallbackDelegate handler, IntPtr context, out SafeMsQuicListenerHandle listener)
{
IntPtr __handler_gen_native = default;
Expand Down Expand Up @@ -1146,16 +1136,6 @@ internal void ListenerStop(SafeMsQuicListenerHandle listener)
listener.DangerousRelease();
}
}
internal uint ConnectionCallback(IntPtr connection, IntPtr context, ref ConnectionEvent connectionEvent)
{
uint __retVal;
fixed (ConnectionEvent* __connectionEvent_gen_native = &connectionEvent)
{
__retVal = ((delegate* unmanaged[Cdecl]<IntPtr, IntPtr, ConnectionEvent*, uint>)_functionPointer)(connection, context, __connectionEvent_gen_native);
}

return __retVal;
}
internal uint ConnectionOpen(SafeMsQuicRegistrationHandle registration, ConnectionCallbackDelegate handler, IntPtr context, out SafeMsQuicConnectionHandle connection)
{
IntPtr __handler_gen_native = default;
Expand Down Expand Up @@ -1319,16 +1299,6 @@ internal void ConnectionShutdown(SafeMsQuicConnectionHandle connection, QUIC_CON
connection.DangerousRelease();
}
}
internal uint StreamCallback(IntPtr stream, IntPtr context, ref StreamEvent streamEvent)
{
uint __retVal;
fixed (StreamEvent* __streamEvent_gen_native = &streamEvent)
{
__retVal = ((delegate* unmanaged[Cdecl]<IntPtr, IntPtr, StreamEvent*, uint>)_functionPointer)(stream, context, __streamEvent_gen_native);
}

return __retVal;
}
internal uint StreamOpen(SafeMsQuicConnectionHandle connection, QUIC_STREAM_OPEN_FLAGS flags, StreamCallbackDelegate handler, IntPtr context, out SafeMsQuicStreamHandle stream)
{
IntPtr __handler_gen_native = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal sealed class MsQuicConnection : QuicConnectionProvider
private const uint DefaultResetValue = 0xffffffff; // Arbitrary value unlikely to conflict with application protocols.

// Delegate that wraps the static function that will be called when receiving an event.
private static readonly ConnectionCallbackDelegate s_connectionDelegate = new ConnectionCallbackDelegate(NativeCallbackHandler);
private static unsafe readonly ConnectionCallbackDelegate s_connectionDelegate = new ConnectionCallbackDelegate(NativeCallbackHandler);

// TODO: remove this.
// This is only used for client-initiated connections, and isn't needed even then once Connect() has been called.
Expand Down Expand Up @@ -748,10 +748,10 @@ internal void SetNegotiatedAlpn(IntPtr alpn, int alpnLength)
}
}

private static uint NativeCallbackHandler(
private static unsafe uint NativeCallbackHandler(
IntPtr connection,
IntPtr context,
ref ConnectionEvent connectionEvent)
ConnectionEvent* connectionEvent)
{
GCHandle gcHandle = GCHandle.FromIntPtr(context);
Debug.Assert(gcHandle.IsAllocated);
Expand All @@ -760,27 +760,27 @@ private static uint NativeCallbackHandler(

if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(state, $"{state.TraceId} Connection received event {connectionEvent.Type}");
NetEventSource.Info(state, $"{state.TraceId} Connection received event {connectionEvent->Type}");
}

try
{
switch (connectionEvent.Type)
switch (connectionEvent->Type)
{
case QUIC_CONNECTION_EVENT_TYPE.CONNECTED:
return HandleEventConnected(state, ref connectionEvent);
return HandleEventConnected(state, ref *connectionEvent);
case QUIC_CONNECTION_EVENT_TYPE.SHUTDOWN_INITIATED_BY_TRANSPORT:
return HandleEventShutdownInitiatedByTransport(state, ref connectionEvent);
return HandleEventShutdownInitiatedByTransport(state, ref *connectionEvent);
case QUIC_CONNECTION_EVENT_TYPE.SHUTDOWN_INITIATED_BY_PEER:
return HandleEventShutdownInitiatedByPeer(state, ref connectionEvent);
return HandleEventShutdownInitiatedByPeer(state, ref *connectionEvent);
case QUIC_CONNECTION_EVENT_TYPE.SHUTDOWN_COMPLETE:
return HandleEventShutdownComplete(state, ref connectionEvent);
return HandleEventShutdownComplete(state, ref *connectionEvent);
case QUIC_CONNECTION_EVENT_TYPE.PEER_STREAM_STARTED:
return HandleEventNewStream(state, ref connectionEvent);
return HandleEventNewStream(state, ref *connectionEvent);
case QUIC_CONNECTION_EVENT_TYPE.STREAMS_AVAILABLE:
return HandleEventStreamsAvailable(state, ref connectionEvent);
return HandleEventStreamsAvailable(state, ref *connectionEvent);
case QUIC_CONNECTION_EVENT_TYPE.PEER_CERTIFICATE_RECEIVED:
return HandleEventPeerCertificateReceived(state, ref connectionEvent);
return HandleEventPeerCertificateReceived(state, ref *connectionEvent);
default:
return MsQuicStatusCodes.Success;
}
Expand All @@ -789,7 +789,7 @@ private static uint NativeCallbackHandler(
{
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Error(state, $"{state.TraceId} Exception occurred during handling {connectionEvent.Type} connection callback: {ex}");
NetEventSource.Error(state, $"{state.TraceId} Exception occurred during handling {connectionEvent->Type} connection callback: {ex}");
}

if (state.ConnectTcs != null)
Expand All @@ -801,7 +801,7 @@ private static uint NativeCallbackHandler(
}
else
{
Debug.Fail($"{state.TraceId} Exception occurred during handling {connectionEvent.Type} connection callback: {ex}");
Debug.Fail($"{state.TraceId} Exception occurred during handling {connectionEvent->Type} connection callback: {ex}");
}

// TODO: trigger an exception on any outstanding async calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace System.Net.Quic.Implementations.MsQuic
{
internal sealed class MsQuicListener : QuicListenerProvider, IDisposable
{
private static readonly ListenerCallbackDelegate s_listenerDelegate = new ListenerCallbackDelegate(NativeCallbackHandler);
private static unsafe readonly ListenerCallbackDelegate s_listenerDelegate = new ListenerCallbackDelegate(NativeCallbackHandler);

private readonly State _state;
private GCHandle _stateHandle;
Expand Down Expand Up @@ -223,13 +223,13 @@ private void Stop()
private static unsafe uint NativeCallbackHandler(
IntPtr listener,
IntPtr context,
ref ListenerEvent evt)
ListenerEvent* evt)
{
GCHandle gcHandle = GCHandle.FromIntPtr(context);
Debug.Assert(gcHandle.IsAllocated);
Debug.Assert(gcHandle.Target is not null);
var state = (State)gcHandle.Target;
if (evt.Type != QUIC_LISTENER_EVENT.NEW_CONNECTION)
if (evt->Type != QUIC_LISTENER_EVENT.NEW_CONNECTION)
{
return MsQuicStatusCodes.InternalError;
}
Expand All @@ -238,7 +238,7 @@ private static unsafe uint NativeCallbackHandler(
MsQuicConnection? msQuicConnection = null;
try
{
ref NewConnectionInfo connectionInfo = ref *evt.Data.NewConnection.Info;
ref NewConnectionInfo connectionInfo = ref *evt->Data.NewConnection.Info;

IPEndPoint localEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(ref *(SOCKADDR_INET*)connectionInfo.LocalAddress);
IPEndPoint remoteEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(ref *(SOCKADDR_INET*)connectionInfo.RemoteAddress);
Expand Down Expand Up @@ -274,7 +274,7 @@ private static unsafe uint NativeCallbackHandler(
}
}

connectionHandle = new SafeMsQuicConnectionHandle(evt.Data.NewConnection.Connection);
connectionHandle = new SafeMsQuicConnectionHandle(evt->Data.NewConnection.Connection);

uint status = MsQuicApi.Api.ConnectionSetConfigurationDelegate(connectionHandle, connectionConfiguration);
if (MsQuicStatusHelper.SuccessfulStatusCode(status))
Expand All @@ -296,7 +296,7 @@ private static unsafe uint NativeCallbackHandler(
{
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Error(state, $"[Listener#{state.GetHashCode()}] Exception occurred during handling {(QUIC_LISTENER_EVENT)evt.Type} connection callback: {ex}");
NetEventSource.Error(state, $"[Listener#{state.GetHashCode()}] Exception occurred during handling {(QUIC_LISTENER_EVENT)evt->Type} connection callback: {ex}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace System.Net.Quic.Implementations.MsQuic
internal sealed class MsQuicStream : QuicStreamProvider
{
// Delegate that wraps the static function that will be called when receiving an event.
internal static readonly StreamCallbackDelegate s_streamDelegate = new StreamCallbackDelegate(NativeCallbackHandler);
internal static unsafe readonly StreamCallbackDelegate s_streamDelegate = new StreamCallbackDelegate(NativeCallbackHandler);

// The state is passed to msquic and then it's passed back by msquic to the callback handler.
private readonly State _state = new State();
Expand Down Expand Up @@ -853,17 +853,17 @@ private void EnableReceive()
/// Callback calls for a single instance of a stream are serialized by msquic.
/// They happen on a msquic thread and shouldn't take too long to not to block msquic.
/// </summary>
private static uint NativeCallbackHandler(
private static unsafe uint NativeCallbackHandler(
IntPtr stream,
IntPtr context,
ref StreamEvent streamEvent)
StreamEvent* streamEvent)
{
GCHandle gcHandle = GCHandle.FromIntPtr(context);
Debug.Assert(gcHandle.IsAllocated);
Debug.Assert(gcHandle.Target is not null);
var state = (State)gcHandle.Target;

return HandleEvent(state, ref streamEvent);
return HandleEvent(state, ref *streamEvent);
}

private static uint HandleEvent(State state, ref StreamEvent evt)
Expand Down