def on_recv(self): """ recieve request and return reply. """ try: protocol = Protocol() head_size = protocol.get_head_size() head = self.recvall(head_size) if len(head) != head_size: raise CommunicateException("Connection closed by peer") _type, size, codec = protocol.parse_head(head) if size > 0 and size < MAX_MESSAGE_LENGTH: body = self.recvall(size) # raise CommunicateException try: body = codec.decode(body[:-4]) except Exception as ex: e = "Decode Request Message Body Error: %s" % ex log.error(e) raise ProtocolException(e) else: raise CommunicateException("size error: " + str(size)) if _type == MSG_TYPE_REPLY: log.info("recv : %s" % body) else: log.error("Unknown Message Ignoring...") return body except Exception as e: log.warning("on_recv: %s" % str(traceback.format_exc()))
def on_recv(self): """ recieve request and return reply. """ try: protocol = Protocol() head_size = protocol.get_head_size() head = self.recvall(head_size) if len(head) != head_size: raise CommunicateException("Connection closed by peer") _type, size, codec = protocol.parse_head(head) if size > 0 and size < MAX_MESSAGE_LENGTH: body = self.recvall(size) # raise CommunicateException try: print("recv raw: %s" % body) body = codec.decode(body[:-4]) log.info("recv: %s" % body) except Exception as ex: e = "Decode Request Message Body Error: %s" % ex log.error(e) raise ProtocolException(e) else: raise CommunicateException("size error: " + str(size)) if _type == MSG_TYPE_REQUEST: # break up the request req_id, func_name, params = body["id"], body["func"], body["params"] log.info("in %s(%s)" % (func_name, params)) self.reply_cli(req_id, func_name, params) log.info("out %s(%s)" % (func_name, params)) except Exception as e: log.warning("on_recv: %s" % str(traceback.format_exc()))
def _on_recv_body(self, protocol): head_size = protocol.get_head_size() head = self.recvall(head_size) if len(head) != head_size: raise CommunicateException("Connection closed by peer") _type, size, codec = protocol.parse_head(head) if size > 0 and size < MAX_MESSAGE_LENGTH: # print "request size:", size body = self.recvall(size) # raise CommunicateException # print "request body", body try: body = codec.decode(body[:-4]) except Exception as ex: e = "Decode Request Message Body Error: %s" % ex log.error(e) raise ProtocolException(e) else: raise CommunicateException("size error: " + str(size)) return _type, body
def recvall(self, size): """ recieve all. """ try: s = size buf = b"" while True: b = self.cli.recv(s) buf = buf + b s = s - len(b) if s == 0 or not b: return buf except Exception as ex: raise CommunicateException("RecvALL Error:%s" % ex)