public static DiscoveryServerInfo ReadDiscoveryServerInfo(this NetIncomingMessage message) { var info = new DiscoveryServerInfo { Name = message.ReadString(), Players = message.ReadUInt16(), MaxPlayers = message.ReadUInt16() }; if (info.Name.Length > SharedConstants.MaxServerNameLength) { info.Name = info.Name.Substring(0, SharedConstants.MaxServerNameLength); } if (message.Position < message.LengthBits) { int version = message.ReadInt32(); // for future compatibility info.ServerVersion = message.ReadInt32(); int nameCount = message.ReadInt32(); for (int i = 0; i < nameCount; ++i) { info.PlayerNames.Add(message.ReadString()); } } return(info); }
private void DiscoverServerResponse() { while (true) { try { // Receive responses from servers IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, DiscoveryConstants.ServerPort); byte[] response = client.Receive(ref serverEndPoint); // Check that it is a response if (response.Length > 1 && (DiscoveryConstants.Responses)response[0] == DiscoveryConstants.Responses.Response) { // Try to parse out a ServerInfo DiscoveryServerInfo receivedServer = new DiscoveryServerInfo(); int responseIndex = receivedServer.FromBytes(response, 1); // If the ServerInfo parsed out, raise ServerDiscovered event. if (responseIndex > 0) { ServerDiscovered(serverEndPoint.Address, receivedServer); } } } catch (SocketException) { // Receive will throw an exception when the socket is closed, // aborting the thread does not unblock and stop the receive. return; } } }
public void StartService(DiscoveryServerInfo regServer) { StopService(); registeredServer = regServer; // Share a socket with both receiving requests and sending responses server = new UdpClient(DiscoveryConstants.ServerPort); // Single thread to listen for requests and send out responses discoverListener = new Thread(CheckDiscoverRequest); discoverListener.Start(); Discoverable = true; }
public void StartService(DiscoveryServerInfo regServer) { StopService(); registeredServer = regServer; // Share a socket with both receiving requests and sending responses server = new UdpClient(DiscoveryConstants.ServerPort); // Single thread to listen for requests and send out responses discoverListener = new Thread(CheckDiscoverRequest); discoverListener.Start(); Discoverable = true; }
public static void Write(this NetOutgoingMessage message, DiscoveryServerInfo serverInfo) { message.Write(serverInfo.Name); message.Write(serverInfo.Players); message.Write(serverInfo.MaxPlayers); // Write future compatible data message.Write(DiscoveryServerInfo.Version); message.Write(serverInfo.ServerVersion); message.Write(serverInfo.PlayerNames.Count); foreach (string name in serverInfo.PlayerNames) { message.Write(name); } }
private void DiscoveryClientService_ServerDiscovered(IPAddress serverIp, DiscoveryServerInfo server) { Application.Current.Dispatcher.Invoke(new Action(delegate() { // Fill a new instance of DiscoveredServer DiscoveredServer discServer = new DiscoveredServer(); discServer.Name = server.Name; discServer.Ip = serverIp; discServer.Port = server.Port; // Only add new servers, DiscoveredServer must be a struct for this contain to work. if (!DiscoveredServers.Contains(discServer)) { DiscoveredServers.Add(discServer); } }) ); }
public void StopService() { if (discoverListener != null) { discoverListener.Abort(); discoverListener = null; } if (server != null) { server.Close(); server = null; } requestHistory.Clear(); Discoverable = false; registeredServer = null; }
private void DiscoverServerResponse() { while (true) { try { // Receive responses from servers IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, DiscoveryConstants.ServerPort); byte[] response = client.Receive(ref serverEndPoint); // Check that it is a response if (response.Length > 1 && (DiscoveryConstants.Responses)response[0] == DiscoveryConstants.Responses.Response) { // Try to parse out a ServerInfo DiscoveryServerInfo receivedServer = new DiscoveryServerInfo(); int responseIndex = receivedServer.FromBytes(response, 1); // If the ServerInfo parsed out, raise ServerDiscovered event. if (responseIndex > 0) ServerDiscovered(serverEndPoint.Address, receivedServer); } } catch (SocketException) { // Receive will throw an exception when the socket is closed, // aborting the thread does not unblock and stop the receive. return; } } }
public void StopService() { if (discoverListener != null) { discoverListener.Abort(); discoverListener = null; } if (server != null) { server.Close(); server = null; } requestHistory.Clear(); Discoverable = false; registeredServer = null; }