public void Start() { Server = TcpListener.Create(0); Server.Start(); var randomTcpPort = TcpListener.Create(0); randomTcpPort.Start(); var portNumber = ((IPEndPoint)randomTcpPort.LocalEndpoint).Port; randomTcpPort.Stop(); var token = _tokenSource.Token; DnsHostProcess = Process.Start(new ProcessStartInfo { FileName = "dns-sd.exe", Arguments = $"-R \"{GameHubConnectivity.DnsDiscoveryName}\" \"\" \"local\" {portNumber} {GameHubConnectivity.PortKeyDisplayName}={((IPEndPoint)Server.LocalEndpoint).Port}" }); GameHubConnectivity.Listen( HandleClientConnect, TimeSpan.FromMilliseconds(100), token, TaskCreationOptions.LongRunning); }
public void Subscribe() { _subscribeSoure = new CancellationTokenSource(); var token = _subscribeSoure.Token; GameHubConnectivity.Subscribe(Connection, MessageQueue, TimeSpan.FromMilliseconds(100), token); }
public void NotifyClient(GameHubConnectionClient client, GameHubMessage message) { try { if (client.Connection.Connected) { GameHubConnectivity.SendMessageToClient(client, message); } } catch { return; } }
public void Connect() { Task.Factory.StartNew(async() => { var notFound = true; while (notFound) { var host = await GameHubConnectivity.ResolveServerHost(); if (host.IPAddress == null) { continue; } var ipAddress = IPAddress.Parse(host.IPAddress); var ip = GameHubConnectivity.GetZeroconfigHostIp(host); InternalConnect(ipAddress, ip); notFound = false; } }); }
private void HandleClientConnect(CancellationToken token) { Task.Factory.StartNew(async() => { try { if (Server.Pending()) { var client = await Server.AcceptTcpClientAsync(); var gameHubClient = new GameHubConnectionClient { Connection = client }; lock (_clientsLock) { _clients.Add(gameHubClient); } GameHubConnectivity.Subscribe(client, MessageQueue, TimeSpan.FromMilliseconds(200), token); var message = new GameHubMessage { SenderGamePort = ((IPEndPoint)Server.LocalEndpoint).Port, Text = "Welcome to the GameHub Server. I hope you enjoy yourself", Status = EMessageType.InitialServerConnection }; NotifyClient(gameHubClient, message); } } catch { return; } }, token); }