Respond to pending discovery requests by sending the TCP listening address and port

Returns the number of valid requests received.

The response messages includes the TCP listening address and port for remote clients to connect with TCP using connect. These connections are accepted by the server with accept_clients.

Property definitions

gamnit $ Server :: answer_discovery_requests
	# Respond to pending discovery requests by sending the TCP listening address and port
	#
	# Returns the number of valid requests received.
	#
	# The response messages includes the TCP listening address and port
	# for remote clients to connect with TCP using `connect`.
	# These connections are accepted by the server with `accept_clients`.
	fun answer_discovery_requests: Int
	do
		var count = 0
		loop
			var ptr = new Ref[nullable SocketAddress](null)
			var read = discovery_socket.recv_from(1024, ptr)

			# No sender means there is no discovery request
			var sender = ptr.item
			if sender == null then break

			var words = read.split(" ")
			if words.length != 2 or words[0] != discovery_request_message or words[1] != handshake_app_name then
				print "Server Warning: Rejected discovery request '{read}' from {sender.address}:{sender.port}"
				continue
			end

			var msg = "{discovery_response_message} {handshake_app_name} {self.port}"
			discovery_socket.send_to(sender.address, sender.port, msg)
			count += 1
		end
		return count
	end
lib/gamnit/network/server.nit:112,2--141,4