Beispiel #1
0
        public void TestBroadcastAndReceive()
        {
            var udp = new UDPNetworkService (1667);
            var inst = new NetworkGameInstance ()
            {
                UniqueId = "Unique1",
                Name = "TestName",
                SenderAddress = "undefined",
                Version = "1.0"
            };

            Task<NetworkGameInstance> discoverTask = udp.DiscoverGameInstanceAsync ();
            Task<bool> sendResultTask = udp.BroadcastGameInstanceAsync (inst);

            Task.WaitAll (new Task[] { discoverTask, sendResultTask });
            bool sendTest = sendResultTask.Result;
            NetworkGameInstance instTest = discoverTask.Result;

            Assert.IsTrue(sendTest, "Sending failed");
            Assert.IsNotNull (instTest, "Failed to receive instance.");
            Assert.AreEqual (instTest.UniqueId, inst.UniqueId, "Unique ID of received instance incorrect.");
            Assert.AreEqual (instTest.Name, inst.Name, "Name of received instance incorrect.");
            Assert.AreEqual (instTest.Version, inst.Version, "Version of received instance incorrect.");
            Assert.IsNotEmpty (instTest.SenderAddress, "Sender ID not set.");
        }
        public GameNetworkInstanceDiscoveryController(NetworkGameInstance gameInstance)
            : base(UITableViewStyle.Grouped, null)
        {
            this.model = ServiceContainer.Resolve<NetworkBrowserModel> ();
            this.model.CurrentGameInstance = gameInstance;

            this.Root = new RootElement (String.Empty) {
                new Section ("Discovered game instances") {
                },
            };
        }
Beispiel #3
0
		/// <summary>
		/// Broadcasts the current game information so that other instances can find players.
		/// </summary>
		/// <param name="gameInstance">the game information</param>
		/// <returns><c>true</c>, if message was sent, <c>false</c> otherwise.</returns>
		public async Task<bool> BroadcastGameInstanceAsync(NetworkGameInstance gameInstance, CancellationToken ct = default(CancellationToken))
		{
			int bytesSent = 0;
			var json = gameInstance.ToJsonString ();
			using (var udpClient = new UdpClient ())
			{ 
				udpClient.EnableBroadcast = true;
				var recipient = new IPEndPoint (IPAddress.Broadcast, this.Port);
				var data = Encoding.UTF8.GetBytes (json);
				bytesSent = await udpClient.SendAsync (data, data.Length, recipient);
				udpClient.Close ();
			}

			return bytesSent == json.Length;
		}
 public Task<bool> BroadcastGameInstanceAsync(NetworkGameInstance gameInstance, CancellationToken ct)
 {
     return Task.FromResult (true);
 }