private void Listen(object serverRaw) { if (!(serverRaw is UdpClient server)) { return; } while (_server != null) { try { var clientEp = new IPEndPoint(IPAddress.Any, _port); var clientRequestData = server.Receive(ref clientEp); var clientRequest = Encoding.ASCII.GetString(clientRequestData); var match = _raspberryRegex.Match(clientRequest); if (match.Success) { var r = new DiscoveredHost(match.Groups[1].Value, clientEp.Address); if (_raspberries.ContainsKey(clientEp.Address)) { SendString(_server, ResponseDataDuplicate, clientEp); continue; } _raspberries.TryAdd(clientEp.Address, r); SendString(_server, ResponseDataSuccess, clientEp); Log(API.LogType.Debug, $"We got a new client! {r}"); } else { Log(API.LogType.Debug, $"Someone tried to connect, but the message {clientRequest} is not that we are listening for"); } } catch (Exception e) { Log(API.LogType.Error, "Error while listening: {0}\n{1}", e.Message, e.StackTrace); } } }
protected bool Equals(DiscoveredHost other) { return(Equals(Address, other.Address)); }