private async void UDPRecv(Task <UdpReceiveResult> task) { if (_udpClient == null) { return; } Console.WriteLine($"Networking: Broadcast result from {task.Result.RemoteEndPoint}"); var receivedData = Encoding.ASCII.GetString(task.Result.Buffer); if (receivedData.StartsWith("TC")) { var clientID = receivedData.Substring(2); var found = AvailableEndpoints.FirstOrDefault(x => x.ClientID == clientID); if (found != null) { found.LastActvity = DateTime.Now; } else { AvailableEndpoints.Add(new TacControlEndpoint { Address = task.Result.RemoteEndPoint, ClientID = clientID, LastActvity = DateTime.Now }); } } _udpClient.ReceiveAsync().ContinueWith(UDPRecv, TaskScheduler.FromCurrentSynchronizationContext()); }
public void StopUDPSearch() { //IPEndPoint end = _beaconTarget; //_udpClient.EndReceive(null, ref end); if (_beaconTimer != null) { _beaconTimer.Dispose(); _beaconTimer = null; } if (_udpClient != null) { _udpClient.Close(); _udpClient = null; } AvailableEndpoints.Clear(); }
public void StartUDPSearch() { if (_udpClient != null) { return; } _udpClient = new UdpClient(0);//(8082, AddressFamily.InterNetworkV6 _udpClient.EnableBroadcast = true; _beaconTimer = new Timer((x) => { SendUDPBeacon(); }, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromSeconds(10)); _udpClient.ReceiveAsync().ContinueWith(UDPRecv, TaskScheduler.FromCurrentSynchronizationContext()); foreach (var tacControlEndpoint in AppConfig.Instance.GetEntry <IEnumerable <TacControlEndpoint> >("Networking.DirectEndpoints")) { AvailableEndpoints.Add(tacControlEndpoint); } }