예제 #1
0
 /// <inheritdoc/>
 public IServerQuery Connect(IPEndPoint customLocalIPEndpoint, string serverAddressAndPort)
 {
     UdpClient = new UdpWrapper(customLocalIPEndpoint, SendTimeout, ReceiveTimeout);
     (string serverAddress, ushort port) = Helpers.ResolveIPAndPortFromString(serverAddressAndPort);
     PrepareAndConnect(serverAddress, port);
     return(this);
 }
예제 #2
0
        public void JoinMulticastGroup(UInt16 universe)
        {
            if (universe < Constants.Universe_MinValue || universe > Constants.Universe_MaxValue)
            {
                throw new ArgumentOutOfRangeException($"Universe must be between {Constants.Universe_MinValue} and {Constants.Universe_MaxValue} inclusive");
            }

            UdpWrapper.JoinMulticastGroup(MulticastAddressProvider.GetMulticastAddress(universe));
        }
예제 #3
0
 protected async Task SendInternal(IPAddress address, SacnPacket packet)
 {
     using (var owner = MemoryPool <byte> .Shared.Rent(packet.Length))
     {
         var bytes = owner.Memory.Slice(0, packet.Length);
         packet.Write(bytes.Span);
         await UdpWrapper.Send(new IPEndPoint(address, Constants.Port), bytes);
     }
 }
예제 #4
0
 internal void StartGame(UdpWrapper <GamePacket> connection)
 {
     if (_connection != null)
     {
         _connection.Dispose();
     }
     _connection = connection;
     _match      = new Match();
     _match.MatchFinishedEvent += OnMatchFinishedEvent;
 }
예제 #5
0
 private void OnMatchFinishedEvent(MatchResult result)
 {
     Program.Log(this).Information("Match finished: {0}", result);
     if (_connection != null)
     {
         _connection.Dispose();
         _connection = null;
     }
     _match = new Match();
     _match.MatchFinishedEvent += OnMatchFinishedEvent;
 }
        internal PlayerListCoordinator(PlayerListViewModel listViewModel, PlayerDataViewModel dataViewModel, Action <UdpWrapper <GamePacket> > onJoinGameEvent)
        {
            _playerView    = listViewModel;
            _playerDataBox = dataViewModel;
            {
                UdpClient multicastClient = new UdpClient(Config.MulticastEndPoint.Port);
                multicastClient.JoinMulticastGroup(Config.MulticastEndPoint.Address);
                multicastClient.MulticastLoopback = true;
                _multicaster = new UdpWrapper <Player>(multicastClient, Config.MulticastEndPoint, false);
            }
            _invitationManager = new InvitationManager();
            _invitationManager.JoinGameEvent += onJoinGameEvent;

            _playerDataBox.SetPublicEndPoint(_invitationManager.ListenerEndPoint);
        }
        public void CanLoopback()
        {
            IPEndPoint localEP = new IPEndPoint(IPAddress.Loopback, 34525);
            UdpClient  client  = new UdpClient(localEP);
            string     msg     = "testingCanLoopback";

            using (var wrapper = new UdpWrapper <string>(client, localEP, true))
            {
                wrapper.Send(msg);
                Thread.Sleep(10);
                Assert.True(wrapper.Pending);
                var recieved = wrapper.GetReceived();
                Assert.False(wrapper.Pending);
                Assert.Single(recieved);
                Assert.Equal(recieved.Single(), msg);
            }
        }
        public void CanMulticastLoopback()
        {
            IPEndPoint multicastEP = Config.MulticastEndPoint;
            string     msg         = "testingCanMulticastLoopBack";

            UdpClient client = new UdpClient(multicastEP.Port);

            client.JoinMulticastGroup(multicastEP.Address);
            client.MulticastLoopback = true;

            using (var wrapper = new UdpWrapper <string>(client, multicastEP, false))
            {
                wrapper.Send(msg);
                Thread.Sleep(10);
                Assert.True(wrapper.Pending);
                var recieved = wrapper.GetReceived();
                Assert.False(wrapper.Pending);
                Assert.Single(recieved);
                Assert.Equal(recieved.Single(), msg);
            }
        }
예제 #9
0
        protected override async Task <bool> PostServerInfoAsync(GameServer discordGameServer)
        {
            if (discordGameServer == null)
            {
                return(false);
            }

            ServerQuery serverQuery = null;

            try
            {
                using var udpClient = new UdpWrapper();
                serverQuery         = new ServerQuery(udpClient, null);
                serverQuery.Connect(discordGameServer.ServerIP.ToString());
                ServerInfo serverInfo = await serverQuery.GetServerInfoAsync().ConfigureAwait(false);

                List <Player> players = (await serverQuery.GetPlayersAsync().ConfigureAwait(false)).Where(p => !p.Name.IsEmptyOrWhiteSpace()).ToList();
                if (serverInfo == null || players == null)
                {
                    return(false);
                }
                SocketGuild  guild   = discordClient?.GetGuild(discordGameServer.GuildID);
                ITextChannel channel = guild?.GetTextChannel(discordGameServer.ChannelID);
                if (guild == null || channel == null)
                {
                    return(false);
                }

                string onlinePlayers = players.Count > serverInfo.MaxPlayers
                    ? $"{serverInfo.MaxPlayers}(+{players.Count - serverInfo.MaxPlayers})/{serverInfo.MaxPlayers}"
                    : $"{players.Count}/{serverInfo.MaxPlayers}";

                var builder = new EmbedBuilder()
                              .WithColor(new Color(21, 26, 35))
                              .WithTitle($"{serverInfo.Game} Server ({discordGameServer.ServerIP.Address}:{serverInfo.Port})")
                              .WithDescription(serverInfo.Name)
                              .AddField("Online Players", onlinePlayers)
                              .AddField("Current Map", serverInfo.Map);

                if (players != null && players.Count > 0)
                {
                    _ = builder.AddField("Currently connected players:", string.Join(", ", players.Select(x => x.Name).Where(name => !name.IsEmpty()).OrderBy(x => x)).TruncateTo(1023));
                }

                //Discord removed support for protocols other than http or https so this currently makes no sense. Leaving it here, in case they re-enable it
                //string connectLink = $"steam://connect/{discordGameServer.ServerIP.Address}:{serverInfo.Port}";
                //_ = builder.AddField("Connect using this link", connectLink);

                if (discordGameServer.GameVersion.IsEmpty())
                {
                    discordGameServer.GameVersion = serverInfo.Version;
                    _ = dbContext.GameServers.Update(discordGameServer);
                    _ = await dbContext.SaveChangesAsync().ConfigureAwait(false);
                }
                else
                {
                    if (serverInfo.Version != discordGameServer.GameVersion)
                    {
                        discordGameServer.GameVersion       = serverInfo.Version;
                        discordGameServer.LastVersionUpdate = DateTime.Now;
                        _ = dbContext.GameServers.Update(discordGameServer);
                        _ = await dbContext.SaveChangesAsync().ConfigureAwait(false);
                    }
                }


                string lastServerUpdate = "";
                if (discordGameServer.LastVersionUpdate.HasValue)
                {
                    lastServerUpdate = $" (Last update: {discordGameServer.LastVersionUpdate.Value})";
                }

                _ = builder.AddField("Server version", $"{serverInfo.Version}{lastServerUpdate}");
                _ = builder.WithFooter($"Last check: {DateTime.Now}");

                string chart = await GenerateHistoryChartAsync(discordGameServer, serverInfo.Players, serverInfo.MaxPlayers).ConfigureAwait(false);

                if (!chart.IsEmptyOrWhiteSpace())
                {
                    _ = builder.AddField("Player Count History", chart);
                }

                if (discordGameServer.MessageID.HasValue)
                {
                    if (await channel.GetMessageAsync(discordGameServer.MessageID.Value).ConfigureAwait(false) is IUserMessage existingMessage && existingMessage != null)
                    {
                        await existingMessage.ModifyAsync(x => x.Embed = builder.Build()).ConfigureAwait(false);
                    }
                    else
                    {
                        logger.LogWarning($"Error getting updates for server {discordGameServer.ServerIP}. Original message was removed.");
                        await RemoveServerAsync(discordGameServer.ServerIP, discordGameServer.GuildID).ConfigureAwait(false);

                        _ = await channel.SendMessageAsync($"Error getting updates for server {discordGameServer.ServerIP}. Original message was removed. Please use the proper remove command to remove the gameserver").ConfigureAwait(false);

                        return(false);
                    }
                }
                else
                {
                    discordGameServer.MessageID = (await(channel?.SendMessageAsync("", false, builder.Build())).ConfigureAwait(false)).Id;
                    _ = dbContext.GameServers.Update(discordGameServer);
                    _ = await dbContext.SaveChangesAsync().ConfigureAwait(false);
                }
            }
예제 #10
0
 /// <summary>
 /// Creates a new instance of ServerQuery with the given Local IPEndpoint.
 /// </summary>
 /// <param name="customLocalIPEndpoint">Desired local IPEndpoint to bound.</param>
 /// <param name="serverAddress">IPAddress or HostName of the server that queries will be sent.</param>
 /// <param name="port">Port of the server that queries will be sent.</param>
 public ServerQuery(IPEndPoint customLocalIPEndpoint, string serverAddress, ushort port)
 {
     UdpClient = new UdpWrapper(customLocalIPEndpoint, SendTimeout, ReceiveTimeout);
     PrepareAndConnect(serverAddress, port);
 }
예제 #11
0
 public void Dispose()
 {
     UdpWrapper?.Dispose();
 }