def invite(self, head, body, hashkey = -1): self.hashkey = hashkey sock = self.__get_socket() if not sock: log.warning("ub_client: connect to %s failed", self.name) return None rhead = None rbuf = None for i in range(self.retry): try: req = head.tobin() + body sock.settimeout(self.wrtmout) sock.sendall(req) sock.settimeout(self.rdtmout) rhead = nshead() rbuf = self.__recv(sock, rhead.size) if len(rbuf) == 0: raise socket.error("connection reset by peer") rhead.frombin(rbuf) rbuf = self.__recv(sock, rhead.body_len) if len(rbuf) != rhead.body_len: raise socket.error("recv body failed") break except socket.error, why: log.warning("socket error (%s), retry[%d]", why, i) sock.close() self.__put_socket(None) sock = self.__get_socket() if not sock: log.warning("ub_client: connect to %s failed", self.name) return None
def thread_work(): head = nshead() pack = mcpack.dumps({"cmd":"test"}) for i in range(COUNT): head.log_id = 1234560000 + i head.body_len = len(pack) resp = cli.invite(head, pack) if not resp: log.warning("ub_client invite error") time.sleep(1) (h, body) = resp print mcpack.loads(body)
def __init__(self, server): self.server = server self.nshead = nshead()