private static async Task BeginListeningAsync(CancellationToken token) { var client = new UdpClient(BroadcastEndpoint.Port); client.JoinMulticastGroup(BroadcastEndpoint.Address); token.Register(() => client.Close()); try { token.ThrowIfCancellationRequested(); while (true) { var result = await client.ReceiveAsync(); var data = Encoding.UTF8.GetString(result.Buffer); if (data.StartsWith(new Message().Header(), StringComparison.Ordinal)) { /*if (ServerFound != null) * { * var details = new ServerDetails * { * Hostname = result.RemoteEndPoint.Address.ToString(), * Port = int.Parse(data.Substring(Header.Length)) * }; * LoggingService.LogInfo("Found TunezServer at {0}", details.FullAddress); * ServerFound(this, details); * }*/ UnityEngine.Debug.Log("MF: " + data); ServerFoundEvent.serverFoundDel sfd = new ServerFoundEvent.serverFoundDel(TEST); ServerFoundEvent.OnServerFound += sfd; string[] split = data.Split('/'); ServerFoundEvent.RegisterFoundServer(new ServerDetails() { IP = split[3].ToString(), Port = int.Parse(split[4].ToString()), DiscoveryMethod = DiscoveryMethod.Multicast }); ServerFoundEvent.OnServerFound -= sfd; } } } catch (ObjectDisposedException) { // This will happen when we cancel the task. We can ignore this. } catch (SocketException) { token.ThrowIfCancellationRequested(); // Ignore this } catch (Exception ex) { token.ThrowIfCancellationRequested(); UnityEngine.Debug.Log("Ignoring bad UDP " + ex); } }
private static async Task BeginListeningAsync(CancellationToken token) { var client = new UdpClient(BroadcastEndpoint.Port); token.Register(() => client.Close()); try { token.ThrowIfCancellationRequested(); while (true) { var result = await client.ReceiveAsync(); var data = Encoding.UTF8.GetString(result.Buffer); if (data.StartsWith(new Message().Header(), StringComparison.Ordinal)) { UnityEngine.Debug.Log("BF: " + data); //ServerFoundEvent.serverFoundDel sfd = new ServerFoundEvent.serverFoundDel(TEST); //ServerFoundEvent.OnServerFound += sfd; string[] split = data.Split('/'); ServerFoundEvent.RegisterFoundServer(new ServerDetails() { IP = split[3].ToString(), Port = int.Parse(split[4].ToString()), DiscoveryMethod = DiscoveryMethod.Broadcast }); //ServerFoundEvent.OnServerFound -= sfd; } } } catch (ObjectDisposedException) { // This will happen when we cancel the task. We can ignore this. } catch (SocketException) { token.ThrowIfCancellationRequested(); // Ignore this } catch (Exception ex) { token.ThrowIfCancellationRequested(); UnityEngine.Debug.Log("Ignoring bad UDP " + ex); } }