def tcp_readable(self): rdata = self.reader.read() self.__decrypt.input(rdata) while self.__decrypt.can_continue_parse(): try: self.__decrypt.parse() except proto_utils.ProtoError: self.delete_handler(self.fileno) return while 1: pkt_info = self.__decrypt.get_pkt() if not pkt_info: break session_id, action, message = pkt_info if action not in proto_utils.ACTS: continue self.__session_id = session_id if self.__session_id and self.__session_id != session_id: self.delete_handler(self.fileno) return if action == proto_utils.ACT_PONG: continue if action == proto_utils.ACT_PING: self.send_msg(session_id, self.__address, proto_utils.ACT_PONG, proto_utils.rand_bytes()) continue self.dispatcher.handle_msg_from_tunnel(self.fileno, session_id, self.__address, action, message) '''''' self.__update_time = time.time()
def udp_readable(self, message, address): result = self.__decrypt.parse(message) if not result: return session_id, action, byte_data = result if action not in proto_utils.ACTS: return # 丢弃PING和PONG的数据包 if action == proto_utils.ACT_PING: self.send_msg(session_id, address, proto_utils.ACT_PONG, proto_utils.rand_bytes()) return if action == proto_utils.ACT_PONG: return self.dispatcher.handle_msg_from_tunnel(self.fileno, session_id, address, action, byte_data)
def udp_timeout(self): t = time.time() v = t - self.__update_time if v > self.__conn_timeout: logging.print_general("udp_timeout", self.__server_address) self.delete_handler(self.fileno) return if self.__enable_heartbeat: if t >= self.__heartbeat_timeout: self.send_msg_to_tunnel(self.dispatcher.session_id, proto_utils.ACT_PING, proto_utils.rand_bytes()) '''''' self.set_timeout(self.fileno, self.__LOOP_TIMEOUT)
def __handle_heartbeat_timeout(self): t = time.time() if t - self.__update_time >= self.__heartbeat_timeout: self.send_msg_to_tunnel(self.dispatcher.session_id, proto_utils.ACT_PING, proto_utils.rand_bytes()) return