Game server controller

Introduced properties

fun accept_clients(add_to_clients: nullable Bool): Array[RemoteClient]

gamnit :: Server :: accept_clients

Accept currently waiting clients and return them as an array
fun answer_discovery_requests: Int

gamnit :: Server :: answer_discovery_requests

Respond to pending discovery requests by sending the TCP listening address and port
fun broadcast(message: Serializable, except: nullable RemoteClient)

gamnit :: Server :: broadcast

Broadcast a message to all clients, then flush the connection
fun clients: Array[RemoteClient]

gamnit :: Server :: clients

All connected RemoteClient
protected fun clients=(clients: Array[RemoteClient])

gamnit :: Server :: clients=

All connected RemoteClient
init defaultinit(port: Int)

gamnit :: Server :: defaultinit

fun discovery_socket: UDPSocket

gamnit :: Server :: discovery_socket

UDP socket responding to discovery requests
protected fun discovery_socket=(discovery_socket: UDPSocket)

gamnit :: Server :: discovery_socket=

UDP socket responding to discovery requests
fun listening_socket: TCPServer

gamnit :: Server :: listening_socket

TCP socket accepting new connections
protected fun listening_socket=(listening_socket: TCPServer)

gamnit :: Server :: listening_socket=

TCP socket accepting new connections
fun port: Int

gamnit :: Server :: port

Port for the listening_socket
protected fun port=(port: Int)

gamnit :: Server :: port=

Port for the listening_socket

Redefined properties

redef type SELF: Server

gamnit $ Server :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun accept_clients(add_to_clients: nullable Bool): Array[RemoteClient]

gamnit :: Server :: accept_clients

Accept currently waiting clients and return them as an array
fun answer_discovery_requests: Int

gamnit :: Server :: answer_discovery_requests

Respond to pending discovery requests by sending the TCP listening address and port
fun broadcast(message: Serializable, except: nullable RemoteClient)

gamnit :: Server :: broadcast

Broadcast a message to all clients, then flush the connection
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun clients: Array[RemoteClient]

gamnit :: Server :: clients

All connected RemoteClient
protected fun clients=(clients: Array[RemoteClient])

gamnit :: Server :: clients=

All connected RemoteClient
init defaultinit(port: Int)

gamnit :: Server :: defaultinit

fun discovery_socket: UDPSocket

gamnit :: Server :: discovery_socket

UDP socket responding to discovery requests
protected fun discovery_socket=(discovery_socket: UDPSocket)

gamnit :: Server :: discovery_socket=

UDP socket responding to discovery requests
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun listening_socket: TCPServer

gamnit :: Server :: listening_socket

TCP socket accepting new connections
protected fun listening_socket=(listening_socket: TCPServer)

gamnit :: Server :: listening_socket=

TCP socket accepting new connections
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun port: Int

gamnit :: Server :: port

Port for the listening_socket
protected fun port=(port: Int)

gamnit :: Server :: port=

Port for the listening_socket
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram gamnit::Server Server core::Object Object gamnit::Server->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

gamnit $ Server
# Game server controller
class Server

	# Port for the `listening_socket`
	var port: Int

	# All connected `RemoteClient`
	var clients = new Array[RemoteClient]

	# TCP socket accepting new connections
	#
	# Opened on the first call to `accept_clients`.
	var listening_socket: TCPServer is lazy do
		var socket = new TCPServer(port)
		socket.listen 8
		socket.blocking = false
		return socket
	end

	# Accept currently waiting clients and return them as an array
	#
	# If `add_to_clients`, the default, the new clients are added to `clients`.
	# Otherwise, the return value of `accept_clients` may be added to `clients`
	# explicitly by the caller after an extra verification or sorting.
	fun accept_clients(add_to_clients: nullable Bool): Array[RemoteClient]
	do
		add_to_clients = add_to_clients or else true
		assert not listening_socket.closed

		var new_clients = new Array[RemoteClient]
		loop
			var client_socket = listening_socket.accept
			if client_socket == null then break

			var rc = new RemoteClient(client_socket)

			var handshake_success = rc.handshake
			if handshake_success then
				new_clients.add rc
				print "Server: Client at {client_socket.address} passed the handshake"
			else
				print_error "Server Error: Client at {client_socket.address} failed the handshake"
				client_socket.close
			end
		end

		if add_to_clients then clients.add_all new_clients

		return new_clients
	end

	# Broadcast a `message` to all `clients`, then flush the connection
	#
	# The client `except` is skipped and will not receive the `message`.
	fun broadcast(message: Serializable, except: nullable RemoteClient)
	do
		for client in clients do if client != except then
			client.writer.serialize(message)
			client.socket.flush
		end
	end

	# 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

	# UDP socket responding to discovery requests
	#
	# Usually opened on the first call to `answer_discovery_request`.
	var discovery_socket: UDPSocket is lazy do
		var s = new UDPSocket
		s.blocking = false
		s.bind(null, discovery_port)
		return s
	end
end
lib/gamnit/network/server.nit:50,1--152,3