From: Alexis Laferrière Date: Sun, 28 May 2017 19:31:06 +0000 (-0400) Subject: gamnit: add more options to `accept_clients` & `broadcast` X-Git-Url: http://nitlanguage.org gamnit: add more options to `accept_clients` & `broadcast` Signed-off-by: Alexis Laferrière --- diff --git a/contrib/tinks/src/server/server.nit b/contrib/tinks/src/server/server.nit index 6c63670..69f2bdb 100644 --- a/contrib/tinks/src/server/server.nit +++ b/contrib/tinks/src/server/server.nit @@ -68,8 +68,6 @@ redef class Server client.writer.serialize game client.writer.serialize client.player client.socket.flush - - clients.add client end if dedicated and clients.is_empty then diff --git a/lib/gamnit/network/server.nit b/lib/gamnit/network/server.nit index 9e2e1f3..70cf002 100644 --- a/lib/gamnit/network/server.nit +++ b/lib/gamnit/network/server.nit @@ -67,8 +67,13 @@ class Server end # Accept currently waiting clients and return them as an array - fun accept_clients: Array[RemoteClient] + # + # 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] @@ -87,13 +92,18 @@ class Server 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 - fun broadcast(message: Serializable) + # + # The client `except` is skipped and will not receive the `message`. + fun broadcast(message: Serializable, except: nullable RemoteClient) do - for client in clients do + for client in clients do if client != except then client.writer.serialize(message) client.socket.flush end