예제 #1
0
    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()))
예제 #2
0
파일: server_api.py 프로젝트: 75509151/jprt
    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()))