def _new_connection_handler(self, connection, address, port): """ info: will setup a TCPHost connection :param connection: socket connection :param address: str :param port: int :return: None """ try: new_connection = TCPHost(connection, address, port, self._connection_password, self._encryption, self._object_packer, self._our_information) try: self._lock.acquire() # TCPHost connection init has no error # TCPHost is ready to be placed int the new connections queue self._new_connections.append(new_connection) finally: self._lock.release() except Exception as error: log.warning("_new_connection_handler error {} {}: {}".format( self._host, self._port, error))
def _connection_finder(self): """ info: will receive and new connection and start a new daemon thread to handle it :return: None """ while self._open: try: # get new raw tcp connection connection, address = self._socket.accept() address, port = address[0], address[1] # check if new connection is in white list if self._address_white_list is not None and address not in self._address_white_list: raise TCPError("{} is not on white list".format(address)) # check if new connection is in not in black list if address in self._address_black_list: raise TCPError("{} is on black list".format(address)) # hand new connection over to new connection handler Thread(target=self._new_connection_handler, args=(connection, address, port), daemon=True).start() except Exception as error: log.warning("_connection_finder error {} {}: {}".format( self._host, self._port, error))
def _thread_task(func, *args, **kwargs): try: return func(*args, **kwargs) except Exception as error: if hasattr(func, "__name__"): log.warning("_thread_task {} error: {}".format(func.__name__, error)) else: log.warning("_thread_task error: {}".format(error))
def close_sound(self, sound_id): """ info: will close sound :param sound_id: int :return: None """ if sound_id in self._active_sound_ids: self._active_sound_ids.remove(sound_id) else: log.warning("Sound ID \"{}\" all ready closed".format(sound_id))
def close(self): """ info: will close tcp receiver connection :return: None """ if self._open: self._open = False try: self._socket.shutdown(0) except OSError as e: log.warning("tcp shutdown error: {}".format(e)) self._socket.close()
def close(self): """ info: will close the connection :return: None """ if not hasattr(self, "_open"): self._open = False elif self._open: self._open = False try: self._connection.shutdown(0) except OSError as e: log.warning("tcp shutdown error: {}".format(e)) self._connection.close()
def pop_frame_stack(self): """ info: will remove TAMFrame from stack :return: TAMFrame or None """ if len(self._frame_stack) != 0: frame = self._frame_stack.pop() frame._frame_done(self, self._loop_data, self._other_handlers, {other_handler: self._other_handlers[other_handler].get_loop_data() for other_handler in self._other_handlers}) log.debug("pop_frame_stack: popped {}".format(frame.__class__.__name__)) return frame log.warning("pop_frame_stack: no frame to pop")
def run_tcp_connection(connection, io=None): """ info: will run a tcp connection :param connection: TCPConnection :param io: IO or None: None will uses default IO :return: None """ if io is None: io = tam_identifier.IO snapshot = io.get_snapshot() try: tcp.TCPObjectWrapper( connection, io, object_packer.ObjectPackerJson( (tam_colors.RGBA, tam_colors.Color, tam_surface.TAMSurface)))() except tcp.TCPError as error: log.warning("run_tcp_connection error: {}".format(error)) except KeyboardInterrupt: log.critical("Caught KeyboardInterrupt") finally: io.apply_snapshot(snapshot) io.clear()
def _update_loop(self): """ info: will update frame and call draw :return: """ surface = TAMSurface(0, 0, " ", BLACK, BLACK) frame_skip = 0 clock = timer.Timer() other_keys = {} other_surfaces = {} log_keys = [] try: while self.is_running() and self._error is None and len(self._frame_stack) != 0: if self._fps_on: self._ups_ticker.tick() # get frame and fps frame = self._frame_stack[-1] frame_time = 1 / frame.get_fps() # check if new handlers have come for receiver_name in self._receivers: new_handler = self._receivers[receiver_name].get_handler() if new_handler is not None: if new_handler.get_full_name() not in self._other_handlers: self.thread_task(new_handler.__call__) log.debug("new handler accepted: {}".format(new_handler.get_full_name())) self._other_handlers[new_handler.get_full_name()] = new_handler other_keys[new_handler.get_full_name()] = [] other_surfaces[new_handler.get_full_name()] = TAMSurface(0, 0, " ", BLACK, BLACK) else: # new handler cant join it has the same name as another handler log.warning("new handler can't join: {}".format(new_handler.get_full_name())) self.thread_task(new_handler.done) self._remove_dead_handlers(other_keys, other_surfaces) # get other handler keys and update dimensions for other_handler in self._other_handlers: other_keys[other_handler] = self._other_handlers[other_handler].pump_keys() keys = self.pump_keys() # update log if self._enable_loop_log: if self._loop_log_key in keys: self._log_on = not self._log_on if self._log_on: keys = list(keys) while self._loop_log_key in keys: keys.remove(self._loop_log_key) keys = tuple(keys) log_keys = keys keys = [] # update fps if self._enable_loop_fps and self._fps_key in keys: self._fps_on = not self._fps_on while self._fps_key in keys: keys.remove(self._fps_key) # update frame.update(self, keys, self.get_loop_data(), self._other_handlers, other_keys, {other_handler: self._other_handlers[other_handler].get_loop_data() for other_handler in self._other_handlers}) # check if still running and for errors if self.is_running() and self._error is None: self._remove_dead_handlers(other_keys, other_surfaces) if frame_skip == 0: frame.make_surface_ready(surface, *self.get_dimensions()) for other_handler in self._other_handlers: frame.make_surface_ready(other_surfaces[other_handler], *self._other_handlers[other_handler].get_dimensions()) frame.draw(surface, self.get_loop_data(), other_surfaces, {other_handler: self._other_handlers[other_handler].get_loop_data() for other_handler in self._other_handlers}) if self._fps_on: self._fps_ticker.tick() self._draw_fps(surface) if self._log_on: # draw log to screen self._io.draw(self._update_log(log_keys)) else: # draw to scree self._io.draw(surface) for other_handler in self._other_handlers: self.thread_task(self._other_handlers[other_handler].get_io().draw, other_surfaces[other_handler]) _, run_time = clock.offset_sleep(max(frame_time - frame_skip, 0)) if run_time >= frame_time + frame_time/10 and frame_skip == 0: frame_skip = run_time - frame_time else: frame_skip = 0 except BaseException as error: self._error = error finally: self.done()
def _fire_event(self, event_type, data=None): if len(self._event_queue) <= 1000: self._event_queue.append((event_type, data)) else: log.warning("Lost Event : ({}, {})".format(event_type, data))