def handle_client_command_packet(sock): try: data, address = sock.recvfrom(2048) command, data = Codec.decode_command(data) socket_handlers.SocketHandlers.server_command_handlers[ sock].update(address, command, data) except socket.error, e: print e.strerror
def broadcast_command_packet(command, data, socket_type): for address in Sockets.client_sockets.keys(): if Sockets.client_sockets[address].__name__ == socket_type: try: Sockets.SERVER_CMD_S.sendto( Codec.encode_command(command, data), address) except socket.error: Sockets.remove_client_socket(address)
def broadcast_media_packet(packet, socket_type): encoded_packet = None for sock_addr_pair in Sockets.client_sockets.keys(): if sock_addr_pair in Sockets.client_sockets.keys() and \ Sockets.client_sockets[sock_addr_pair].__name__ == socket_type: if not encoded_packet: encoded_packet = Codec.encode(packet) try: # print "packet size: " + socket_type + ": " + str(len(encoded_packet)) sock_addr_pair[0].sendall(encoded_packet) except socket.error: Sockets.remove_client_socket(sock_addr_pair[1])
def update(self, address, command, data): if command == constants.COMMAND_REGISTER: self.register_client(address) elif command == constants.COMMAND_INPUT_BUTTON: Controller.set_button_input(data) elif command == constants.COMMAND_INPUT_L3R3: Controller.set_l3r3_input(data) elif command == constants.COMMAND_INPUT_TOUCH: Controller.set_touch_input(data) elif command == constants.COMMAND_INPUT_JOYSTICK: Controller.set_joystick_input(data) elif command == constants.COMMAND_PING: sockets.Sockets.SERVER_CMD_S.sendto( Codec.encode_command(constants.COMMAND_PONG), address)
def recv(self, sock, buffer_id): if buffer_id not in self.buffers.keys(): self.buffers[buffer_id] = "" while Codec.end_delimiter not in self.buffers[buffer_id]: data = sock.recv( 100000 ) # The buffer will only be this big on uncompressed loopback most likely if not data: self.buffers = {} raise socket.error self.buffers[buffer_id] += data index = self.buffers[buffer_id].find(Codec.end_delimiter) packet = self.buffers[buffer_id][0:index] self.buffers[buffer_id] = self.buffers[buffer_id][index + len(Codec. end_delimiter):] return Codec.decode(packet)
def set_joystick_input(cls, data): cls.joystick_buffer = Codec.decode_input(data)
def set_touch_input(cls, data): cls.touch_buffer = Codec.decode_input(data)
def set_l3r3_input(cls, data): cls.l3r3_buffer = Codec.decode_input(data)
def set_button_input(cls, data): cls.button_buffer = Codec.decode_input(data)