Exemplo n.º 1
0
        public async void startHosting()
        {
            isHosting = true;
            UdpClient client = new UdpClient();

            client.EnableBroadcast = true;
            IPEndPoint targetIp = new IPEndPoint(IPAddress.Broadcast, 2504);

            do
            {
                string lobbyDesignation = (BindingContext as LobbyViewModel).lobbyName;
                string lobbyIp          = "";
                foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up)
                    {
                        foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                lobbyIp = ip.Address.ToString();
                            }
                        }
                    }
                }
                int lobbyConnectedPlayer = 0;
                foreach (Player player in (BindingContext as LobbyViewModel).lobbyPlayers)
                {
                    if (player.name != null)
                    {
                        lobbyConnectedPlayer++;
                    }
                }
                int lobbyMaximumPlayers = (BindingContext as LobbyViewModel).lobbyMaximumPlayers;

                var data = new HostingLobbyInformation
                {
                    designation       = lobbyDesignation,
                    hostIp            = lobbyIp,
                    conntectedPlayers = lobbyConnectedPlayer,
                    maximumPlayers    = lobbyMaximumPlayers,
                    isHosting         = isHosting,
                };

                byte[] bytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(data));
                client.Send(bytes, bytes.Length, targetIp);
                await Task.Run(() => { Thread.Sleep(10000); });
            }while (isHosting);

            client.Close();
        }
Exemplo n.º 2
0
        private void OnReceive(IAsyncResult ar)
        {
            DateTime dateTime = (DateTime)ar.AsyncState;

            if (DateTime.Now.Subtract(dateTime).TotalMilliseconds > SEARCH_INTERVAL ||
                udp.Client == null)
            {
                return;
            }

            byte[] bytes = udp.EndReceive(ar, ref ip);
            HostingLobbyInformation hostingLobbyInformation = JsonConvert.DeserializeObject <HostingLobbyInformation>(Encoding.ASCII.GetString(bytes));

            if (hostingLobbyInformation.isHosting)
            {
                var hostingLobby = (from hl in searchLobbyViewModel.hostingLobbies where hl.hostIp == hostingLobbyInformation.hostIp select hl).FirstOrDefault();
                if (hostingLobby == null)
                {
                    searchLobbyViewModel.hostingLobbies.Add(hostingLobbyInformation);
                }
                else if (!hostingLobbyInformation.Equals(hostingLobby))
                {
                    searchLobbyViewModel.hostingLobbies.Remove(hostingLobby);
                    searchLobbyViewModel.hostingLobbies.Add(hostingLobbyInformation);
                }
            }
            else
            {
                var hostingLobby = (from hl in searchLobbyViewModel.hostingLobbies where hl.hostIp == hostingLobbyInformation.hostIp select hl).FirstOrDefault();
                if (hostingLobby != null)
                {
                    searchLobbyViewModel.hostingLobbies.Remove(hostingLobby);
                }
            }

            udp.BeginReceive(OnReceive, dateTime);
        }