示例#1
0
 private void OnConnectionReceived(object sender, ListenerConnectEventArgs e)
 {
     if (ConnectionReceived != null)
     {
         ConnectionReceived.Invoke(sender, e);
     }
 }
示例#2
0
        private void LogListener_Connect(object sender, ListenerConnectEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler <ListenerConnectEventArgs>(LogListener_Connect), sender, e);
                return;
            }

            if (e.Failed)
            {
                Log($"Could not start listening on debug port {e.Port}.", LogLevel.Error, e.Exception.Message + "\r\n" + e.Exception.StackTrace);
            }
            else
            {
                Log($"Started listening on debug port {e.Port}.");
            }
        }
示例#3
0
        private async Task UdpListener(IPAddress bindIp, int port)
        {
            using (var uc = new UdpClient(bindIp.AddressFamily))
            {
                try
                {
                    uc.Client.Bind(new IPEndPoint(bindIp, port));
                }
                catch (SocketException x)
                {
                    Connect?.Invoke(this, ListenerConnectEventArgs.Failure(port, x));
                    return;
                }

                Connect?.Invoke(this, ListenerConnectEventArgs.Success(Port));

                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        var udpResult = await uc.ReceiveAsync()
                                        .WithCancellation(token)
                                        .ConfigureAwait(false);

                        LogDataRecieved?.Invoke(this, new LogDataRecievedEventArgs()
                        {
                            Data   = udpResult.Buffer,
                            Source = udpResult.RemoteEndPoint
                        });
                    }
                    catch (OperationCanceledException) { }
                    catch (Exception x)
                    {
                        Debug.Write($"LogListener {x.GetType().Name}: {x.Message}\n\n{x.StackTrace}");
                    }
                }
            }
        }