コード例 #1
0
def main():
    out = StarLog(level=1, time=datetime.datetime.now, star="* Thread-0")
    out.comment("initialising server")

    # set up a shared matchmaking pool
    pool = MatchmakingPool()

    # listen for connections incoming on PORT:
    connections = Connection.iter_listen(HOST, PORT)
    out.comment(f"listening on port {PORT}...")
    for connection, address in connections:
        # repeatedly accept a new connection, and hand off to a new thread
        out.comment("new client connected: ", address)

        out.comment("starting a new thread to handle this client...")
        handler = threading.Thread(target=servant, args=(connection, pool))
        handler.daemon = True  # so that this thread exits when main exits
        handler.start()
コード例 #2
0
def main():
    out = StarLog(level=1 + DEBUG, timefn=lambda: f"Thread-0 {datetime.now()}")
    out.comment("initialising server", depth=-1)

    # set up a shared matchmaking pool
    pool = MatchmakingPool(num_players=NUM_PLAYERS,
                           special_channels=SPECIAL_CHANNELS)

    # listen for connections incoming on PORT:
    try:
        # Host of "" allows all incoming connections on the chosen port
        connections = Connection.iter_listen(host="", port=DEFAULT_SERVER_PORT)
        out.comment(f"listening on port {DEFAULT_SERVER_PORT}...")
        for connection, address in connections:
            # repeatedly accept a new connection, and hand off to a new thread
            out.comment("new client connected: ", address)
            out.comment("starting a new thread to handle this client...")
            handler = threading.Thread(target=servant, args=(connection, pool))
            handler.daemon = True  # so that new thread exits when main exits
            handler.start()
    except KeyboardInterrupt:
        print()  # end line
        out.comment("bye!")