gamnit: add more options to `accept_clients` & `broadcast`
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 28 May 2017 19:31:06 +0000 (15:31 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 30 May 2017 14:51:23 +0000 (10:51 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/tinks/src/server/server.nit
lib/gamnit/network/server.nit

index 6c63670..69f2bdb 100644 (file)
@@ -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
index 9e2e1f3..70cf002 100644 (file)
@@ -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