Esempio n. 1
0
 def tcp_from_server(self, data):
     command = protocol.command(data)
     if command == protocol.SNAPSHOT:
         snapshot_data = data[1:]
         self.apply_snapshot_data(snapshot_data)
     elif command == protocol.EVENT:
         self.manage_events(data[1:])
Esempio n. 2
0
 def data_received(self, data):
     # print("data_received", data, "from", self.transport.get_extra_info("peername"))
     command = protocol.command(data)
     if command == protocol.CONNECT:
         player_id = self.server.add_client(self.transport)
         if player_id:
             print("yes", player_id)
             self.transport.write(
                 protocol.connected_struct.pack(protocol.CONNECTED,
                                                player_id,
                                                self.server.max_n_players))
         else:
             self.transport.write(b"NO")
     elif command == protocol.DISCONNECT:
         id_ = int.from_bytes(data[1:2], "big")
         player = self.server.clients.get(id_)
         if player:
             self.transport.write(b"OK")
             self.transport.write_eof()
             self.server.remove_client(id_)
         else:
             self.transport.write(b"NO")
     elif command == protocol.SEND_GAME_PORT:
         command, player_id, port, rudp_port = protocol.sendgameport_struct.unpack(
             data)
         print("game - rudp port", player_id, port, rudp_port)
         self.server.set_game_address(player_id, port, rudp_port)
         for player_id in self.server.engine.players:
             event_manager.add_player_joined(player_id)
     elif command == protocol.REQUEST_SNAPSHOT:
         command, player_id = protocol.request_snapshot_struct.unpack(data)
         self.server.send_requested_snapshot(player_id)
Esempio n. 3
0
 def udp_from_server(self, socket):
     data = socket.recv(1024)
     command = protocol.command(data)
     if command == protocol.SNAPSHOT:
         # print("snapshot payload", data)
         snapshot_data = data[1:]
         response = protocol.snapshotack_struct.pack(protocol.SNAPSHOT_ACK, self.client.id)
         socket.send(response)
         self.apply_snapshot_data(snapshot_data)
Esempio n. 4
0
 def datagram_received(self, data, addr):
     command = protocol.command(data)
     # print("{} bytes received from {}".format(len(data), addr))
     if command == protocol.SNAPSHOT_ACK:
         command, id = protocol.snapshotack_struct.unpack(data)
         self.server.ack_client(id)
     elif command == protocol.INPUT:
         command, id = protocol.command_id_struct.unpack_from(data)
         self.server.apply_input(id, data)
Esempio n. 5
0
 def manage_server_connection(self, socket):
     data = socket.recv(1024)
     command = protocol.command(data)
     if data == b'OK':
         self._disconnected()
         event.event_manager.clear()
     elif command == protocol.CONNECTED:
         self._connected(data)
     else:
         self.server_callback(data)
Esempio n. 6
0
 def rudp_from_server(self, message, address, mailbox):
     command = protocol.command(message)
     if command == protocol.EVENT:
         event_data = message[1:]
         self.manage_events(event_data)