/// <inheritdoc /> public Task StartLanDiscovery(CancellationToken cancellationToken, LandDiscoveryDelegate callback = null) { return(Task.CompletedTask); }
/// <inheritdoc /> public async Task StartLanDiscovery(CancellationToken cancellationToken, LandDiscoveryDelegate callback = null) { if (callback == null) { return; } Stopwatch sw = new Stopwatch(); sw.Start(); RaknetConnection connection = new RaknetConnection(); connection.RemoteEndpoint = null; connection.AutoConnect = false; connection.Start(); while (!connection.FoundServer) { if ((!sw.IsRunning || sw.ElapsedMilliseconds > 100)) { sw.Restart(); connection.SendUnconnectedPingInternal(null); } if (cancellationToken.IsCancellationRequested || connection.FoundServer) { break; } await Task.Delay(200); } sw.Stop(); var remoteEndPoint = connection.RemoteEndpoint; var remoteServerName = connection.RemoteServerName; var ping = sw.ElapsedMilliseconds; connection.Session?.Close(); connection.Stop(); var motd = new BedrockMotd(remoteServerName); await callback.Invoke( new LanDiscoveryResult( remoteEndPoint, new ServerPingResponse(connection.FoundServer, ping), new ServerQueryResponse( connection.FoundServer, new ServerQueryStatus() { Address = remoteEndPoint.Address.ToString(), Delay = sw.ElapsedMilliseconds, Port = (ushort)remoteEndPoint.Port, Query = new ServerQuery() { Players = new Players() { Max = motd.MaxPlayers, Online = motd.Players }, Version = new API.Services.Version() { Protocol = motd.ProtocolVersion, Name = motd.ClientVersion, Compatibility = CompatibilityResult.Compatible }, Description = new Description() { Text = motd.MOTD }, Modinfo = null, Favicon = null }, WaitingOnPing = false, Success = true, EndPoint = remoteEndPoint }))); }