public override async Task <ServerInformation> GetServerInfo(Server server, System.Threading.CancellationToken?token) { var endpoint = GetServerEndpoint(server); var infoTask = ServerQuery.Info(endpoint, ServerQuery.ServerType.Source); if (token.HasValue) { infoTask = infoTask.WithCancellation(token.Value); } var info = await infoTask as SourceQueryInfo; return(new ServerInformation() { Name = info.Name, Map = info.Map, Players = info.Players, MaxPlayers = info.MaxPlayers }); }
static async Task MainAsync(string[] args) { Console.WriteLine("Fetching info.\n"); var status = await ServerQuery.Info(IPAddress.Parse("127.0.0.1"), 25565, ServerQuery.ServerType.Minecraft) as MinecraftQueryInfo; Console.WriteLine("Info has been fetched:"); Console.WriteLine("---------------------------------"); // Too lazy too type all individual properties. IList <PropertyInfo> props = new List <PropertyInfo>(typeof(MinecraftQueryInfo).GetProperties()); foreach (var prop in props) { if (prop.Name == "Players") { Console.WriteLine("Players: "); foreach (var player in (List <string>)prop.GetValue(status)) { Console.WriteLine("- " + player); } } else { Console.WriteLine(prop.Name + ": " + prop.GetValue(status)); } } Console.WriteLine("---------------------------------"); Console.WriteLine("\nTesting RCON:"); var rcon = new RCON(IPAddress.Parse("127.0.0.1"), 25575, "kJGSDO7242hSA*D0sad0"); var cmd = await rcon.SendCommandAsync("help"); Console.Write(cmd + "\n\nCommand: "); while (true) { var command = Console.ReadLine(); Console.Write(await rcon.SendCommandAsync(command) + "\n\nCommand: "); } }
public override async Task <ServerInfoBase> GetServerInfoAsync() { ServerInfoBase result; try { var steamResult = await ServerQuery.Info(_endpoint, ServerQuery.ServerType.Source) as SourceQueryInfo; result = ConvertInfoResponse(steamResult); } catch (SocketException e) { _logger.LogError("Socket Exception while trying to query server {ip}:{port}", _endpoint.Address, _endpoint.Port); result = new SteamServerInfo() { MaxPlayers = 0, CurrentPlayers = 0 }; } return(result); }
/// <summary> /// Example program for CoreRCON. /// </summary> internal static void Main(string[] args) { var task = Task.Run(async() => { var ip = "192.168.1.8"; // "162.248.93.211"; // var endpoint = new IPEndPoint( IPAddress.Parse(ip), 27015 ); var rcon = new RCON(endpoint, "rcon"); var log = new LogReceiver(0, endpoint); var players = await ServerQuery.Players(endpoint); var info = await ServerQuery.Info(endpoint, ServerQuery.ServerType.Source) as SourceQueryInfo; Console.WriteLine($"Connected to server with {players.Length} players. Map is {info.Map} in game {info.Game} running on {info.Environment}"); // Tell the server to send logs here await rcon.SendCommandAsync($"logaddress_add 192.168.1.8:{log.ResolvedPort}"); rcon.OnDisconnected += () => { Console.WriteLine("Server closed connection!"); }; var status = await rcon.SendCommandAsync <Status>("status"); Console.WriteLine($"Got status, hostname is: {status.Hostname}"); // Listen for chat messages log.Listen <ChatMessage>(chat => { Console.WriteLine($"Chat message: {chat.Player.Name} said {chat.Message} on channel {chat.Channel}"); }); // Listen for kills log.Listen <KillFeed>(kill => { Console.WriteLine($"Player {kill.Killer.Name} ({kill.Killer.Team}) killed {kill.Killed.Name} ({kill.Killed.Team}) with {kill.Weapon}"); }); log.Listen <PlayerConnected>(connection => { Console.WriteLine($"Player {connection.Player.Name} connected with host {connection.Host}"); }); log.Listen <PlayerDisconnected>(dis => { Console.WriteLine($"Player {dis.Player.Name} disconnected"); }); log.Listen <NameChange>(name => { Console.WriteLine($"{name.Player.Name} changed name to {name.NewName}"); }); // Never stop await Task.Delay(-1); }); // .Wait() puts exceptions into an AggregateException, while .GetResult() doesn't task.GetAwaiter().GetResult(); }