internal unsafe uint ListenerCallbackHandler( ref ListenerEvent evt) { try { switch (evt.Type) { case QUIC_LISTENER_EVENT.NEW_CONNECTION: { NewConnectionInfo connectionInfo = *(NewConnectionInfo *)evt.Data.NewConnection.Info; IPEndPoint localEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(*(SOCKADDR_INET *)connectionInfo.LocalAddress); IPEndPoint remoteEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(*(SOCKADDR_INET *)connectionInfo.RemoteAddress); MsQuicConnection msQuicConnection = new MsQuicConnection(localEndPoint, remoteEndPoint, evt.Data.NewConnection.Connection); _acceptConnectionQueue.Writer.TryWrite(msQuicConnection); } // Always pend the new connection to wait for the security config to be resolved // TODO this doesn't need to be async always return(MsQuicStatusCodes.Pending); default: return(MsQuicStatusCodes.InternalError); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(MsQuicStatusCodes.InternalError); } }
protected override IntPtr HookCallBack(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) // 大于等于0才是正确的消息 { // 虚拟键盘码转位WPF中的键盘值 Key key = KeyInterop.KeyFromVirtualKey(Marshal.ReadInt32(lParam)); string index = key.ToString(); // 按下事件 if (wParam == (IntPtr)WM.KEYDOWN || wParam == (IntPtr)WM.SYSKEYDOWN) { if (KeysStatus[index] == KeyAction.Down) { KeysStatus[index] = KeyAction.Pressed; ListenerEvent?.Invoke(this, new GlobalKeyEventArgs(key, KeyAction.Pressed)); } else if (KeysStatus[index] == KeyAction.Pressed) { // do nothing } else if (KeysStatus[index] == KeyAction.Up) { KeysStatus[index] = KeyAction.Down; ListenerEvent?.Invoke(this, new GlobalKeyEventArgs(key, KeyAction.Down)); } } // 抬起事件 if (wParam == (IntPtr)WM.KEYUP || wParam == (IntPtr)WM.SYSKEYUP) { KeysStatus[index] = KeyAction.Up; ListenerEvent?.Invoke(this, new GlobalKeyEventArgs(key, KeyAction.Up)); } } return(CallNextHookEx(nCode, wParam, lParam)); }
internal static uint NativeCallbackHandler( IntPtr listener, IntPtr context, ref ListenerEvent connectionEventStruct) { GCHandle handle = GCHandle.FromIntPtr(context); MsQuicListener quicListener = (MsQuicListener)handle.Target; return(quicListener.ListenerCallbackHandler(ref connectionEventStruct)); }
public void EventDispatch(string eventName) { //後ろから取らないとエラー for (int i = list.Count - 1; i >= 0; i--) { ListenerEvent listenerEvent = list[i]; if (listenerEvent.eventName == eventName) { listenerEvent.eventCallback(); } } }
public void RemoveEventListener(string eventName, ListenerEvent.EventCallback eventCallback) { for (int i = list.Count - 1; i >= 0; i--) { ListenerEvent listenerEvent = list[i]; if (listenerEvent.eventName == eventName && listenerEvent.eventCallback == eventCallback) { list.RemoveAt(i); listenerEvent.Destroy(); } } }
private static unsafe uint NativeCallbackHandler( IntPtr listener, IntPtr context, ref ListenerEvent evt) { if (evt.Type != QUIC_LISTENER_EVENT.NEW_CONNECTION) { return(MsQuicStatusCodes.InternalError); } GCHandle gcHandle = GCHandle.FromIntPtr(context); Debug.Assert(gcHandle.IsAllocated); Debug.Assert(gcHandle.Target is not null); var state = (State)gcHandle.Target; SafeMsQuicConnectionHandle?connectionHandle = null; try { 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); connectionHandle = new SafeMsQuicConnectionHandle(evt.Data.NewConnection.Connection); uint status = MsQuicApi.Api.ConnectionSetConfigurationDelegate(connectionHandle, state.ConnectionConfiguration); QuicExceptionHelpers.ThrowIfFailed(status, "ConnectionSetConfiguration failed."); var msQuicConnection = new MsQuicConnection(localEndPoint, remoteEndPoint, connectionHandle, state.RemoteCertificateRequired, state.RevocationMode, state.RemoteCertificateValidationCallback); msQuicConnection.SetNegotiatedAlpn(connectionInfo.NegotiatedAlpn, connectionInfo.NegotiatedAlpnLength); if (!state.AcceptConnectionQueue.Writer.TryWrite(msQuicConnection)) { // This handle will be cleaned up by MsQuic. connectionHandle.SetHandleAsInvalid(); msQuicConnection.Dispose(); return(MsQuicStatusCodes.InternalError); } return(MsQuicStatusCodes.Success); }
internal uint ListenerCallbackHandler( ref ListenerEvent evt) { switch (evt.Type) { case QUIC_LISTENER_EVENT.NEW_CONNECTION: { evt.Data.NewConnection.SecurityConfig = _secConfig.NativeObjPtr; var msQuicConnection = new MsQuicConnection(_api, _transportContext, evt.Data.NewConnection.Connection); _acceptConnectionQueue.Writer.TryWrite(msQuicConnection); } break; default: return(MsQuicConstants.InternalError); } return(MsQuicConstants.Success); }
internal unsafe uint ListenerCallbackHandler(ref ListenerEvent evt) { try { switch (evt.Type) { case QUIC_LISTENER_EVENT.NEW_CONNECTION: { ref NewConnectionInfo connectionInfo = ref *(NewConnectionInfo *)evt.Data.NewConnection.Info; IPEndPoint localEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(ref *(SOCKADDR_INET *)connectionInfo.LocalAddress); IPEndPoint remoteEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(ref *(SOCKADDR_INET *)connectionInfo.RemoteAddress); MsQuicConnection msQuicConnection = new MsQuicConnection(localEndPoint, remoteEndPoint, evt.Data.NewConnection.Connection, _options.IdleTimeout); msQuicConnection.SetNegotiatedAlpn(connectionInfo.NegotiatedAlpn, connectionInfo.NegotiatedAlpnLength); _acceptConnectionQueue.Writer.TryWrite(msQuicConnection); } // Always pend the new connection to wait for the security config to be resolved // TODO this doesn't need to be async always return(MsQuicStatusCodes.Pending);
private static unsafe uint NativeCallbackHandler( IntPtr listener, IntPtr context, ref 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) { return(MsQuicStatusCodes.InternalError); } SafeMsQuicConnectionHandle?connectionHandle = null; MsQuicConnection? msQuicConnection = null; try { 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); string targetHost = string.Empty; // compat with SslStream if (connectionInfo.ServerNameLength > 0 && connectionInfo.ServerName != IntPtr.Zero) { // TBD We should figure out what to do with international names. targetHost = Marshal.PtrToStringAnsi(connectionInfo.ServerName, connectionInfo.ServerNameLength); } SafeMsQuicConfigurationHandle?connectionConfiguration = state.ConnectionConfiguration; if (connectionConfiguration == null) { Debug.Assert(state.AuthenticationOptions.ServerCertificateSelectionCallback != null); try { // ServerCertificateSelectionCallback is synchronous. We will call it as needed when building configuration connectionConfiguration = SafeMsQuicConfigurationHandle.Create(state.ConnectionOptions, state.AuthenticationOptions, targetHost); } catch (Exception ex) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Error(state, $"[Listener#{state.GetHashCode()}] Exception occurred during creating configuration in connection callback: {ex}"); } } if (connectionConfiguration == null) { // We don't have safe handle yet so MsQuic will cleanup new connection. return(MsQuicStatusCodes.InternalError); } } connectionHandle = new SafeMsQuicConnectionHandle(evt.Data.NewConnection.Connection); uint status = MsQuicApi.Api.ConnectionSetConfigurationDelegate(connectionHandle, connectionConfiguration); if (MsQuicStatusHelper.SuccessfulStatusCode(status)) { msQuicConnection = new MsQuicConnection(localEndPoint, remoteEndPoint, state, connectionHandle, state.AuthenticationOptions.ClientCertificateRequired, state.AuthenticationOptions.CertificateRevocationCheckMode, state.AuthenticationOptions.RemoteCertificateValidationCallback); msQuicConnection.SetNegotiatedAlpn(connectionInfo.NegotiatedAlpn, connectionInfo.NegotiatedAlpnLength); if (!state.PendingConnections.TryAdd(connectionHandle.DangerousGetHandle(), msQuicConnection)) { msQuicConnection.Dispose(); } return(MsQuicStatusCodes.Success); } // If we fall-through here something wrong happened. }
internal void EventHandler(IntPtr EventPtr, IntPtr arg) { const uint topicTrigger = (uint)(V_EVENT.INCONSISTENT_TOPIC | V_EVENT.ALL_DATA_DISPOSED); const uint writerTrigger = (uint)(V_EVENT.OFFERED_DEADLINE_MISSED | V_EVENT.LIVELINESS_LOST | V_EVENT.OFFERED_INCOMPATIBLE_QOS | V_EVENT.PUBLICATION_MATCHED); const uint readerTrigger = (uint)(V_EVENT.SAMPLE_REJECTED | V_EVENT.LIVELINESS_CHANGED | V_EVENT.SAMPLE_LOST | V_EVENT.REQUESTED_DEADLINE_MISSED | V_EVENT.REQUESTED_INCOMPATIBLE_QOS | V_EVENT.SUBSCRIPTION_MATCHED); // The DATA_AVAILABLE and DATA_ON_READERS events are left out of the readerTrigger because // they don't have a v_readerStatus that has to be copied. Debug.Assert(EventPtr != IntPtr.Zero); v_listenerEvent listenerEvent = Marshal.PtrToStructure(EventPtr, listenerEventType) as v_listenerEvent; if (listenerEvent.kind == (uint)V_EVENT.TRIGGER) { // Nothing to deliver, so ignore. return; } ListenerEvent ev = new ListenerEvent(listenerEvent.kind); ev.Source = SacsSuperClass.fromUserData(listenerEvent.source) as Entity; if (ev.Source == null) { // Apparently the Source Entity has already been deleted. return; } if ((listenerEvent.kind & (uint)(V_EVENT.OBJECT_DESTROYED | V_EVENT.PREPARE_DELETE)) == 0) { ev.Target = SacsSuperClass.fromUserData(listenerEvent.userData) as Entity; if (listenerEvent.eventData != IntPtr.Zero) { if ((listenerEvent.kind & topicTrigger) != 0) { v_topicStatus vTopicStatus = (v_topicStatus)Marshal.PtrToStructure(listenerEvent.eventData, typeof(v_topicStatus)) as v_topicStatus; TopicStatus topicStatus = new TopicStatus(); vTopicStatusMarshaler.CopyOut(ref vTopicStatus, topicStatus); ev.Status = topicStatus; } else if ((listenerEvent.kind & writerTrigger) != 0) { v_writerStatus vWriterStatus = (v_writerStatus)Marshal.PtrToStructure(listenerEvent.eventData, typeof(v_writerStatus)) as v_writerStatus; WriterStatus writerStatus = new WriterStatus(); vWriterStatusMarshaler.CopyOut(ref vWriterStatus, writerStatus); ev.Status = writerStatus; } else if ((listenerEvent.kind & readerTrigger) != 0) { v_readerStatus vReaderStatus = (v_readerStatus)Marshal.PtrToStructure(listenerEvent.eventData, typeof(v_readerStatus)) as v_readerStatus; ReaderStatus readerStatus = new ReaderStatus(); vReaderStatusMarshaler.CopyOut(ref vReaderStatus, readerStatus); ev.Status = readerStatus; } else { v_status vStatus = (v_status)Marshal.PtrToStructure(listenerEvent.eventData, typeof(v_status)) as v_status; EntityStatus status = new EntityStatus(); vStatusMarshaler.CopyOut(ref vStatus, status); ev.Status = status; } } else { ev.Status = null; } } Events.Add(ev); }
public void AddEventListener(string eventName, ListenerEvent.EventCallback eventCallback) { ListenerEvent listenerEvent = new ListenerEvent(eventName, eventCallback); list.Add(listenerEvent); }
internal void AddListener(ListenerEvent listener) { listeners.Add(listener); }