def from_payload()

in zktraffic/base/client_message.py [0:0]


  def from_payload(cls, data, client, server):
    length, offset = read_number(data, 0)

    # Note: the C library doesn't include the length at the start
    if length >= cls.MAX_REQUEST_SIZE or length in (PING_XID, AUTH_XID, SET_WATCHES_XID):
      xid = length
      length = 0
    elif length == 0:
      length = 0
      return ConnectRequest.with_params(None, None, None, data, 0, length, client, server)
    elif length < 0:
      raise DeserializationError("Bad request length: %d" % (length))
    else:
      offs_start = offset  # if the xid is 0, it's a Connect so save the offset
      xid, offset = read_number(data, offset)
      if xid not in (PING_XID, AUTH_XID, SET_WATCHES_XID) and xid < 0:
        raise DeserializationError("Wrong XID: %d" % (xid))
      elif xid in ZK_VALID_PROTOCOL_VERSIONS:
        try:
          return ConnectRequest.with_params(None, None, None, data, offs_start, length, client, server)
        except DeserializationError:
          pass

    opcode, offset = read_opcode(data, offset)
    path, offset = read_path(data, offset) if has_path(opcode) else ("", offset)
    length, offset = read_number(data, offset) if length == 0 else (length, offset)
    watch, offset = read_bool(data, offset) if can_set_watch(opcode) else (False, offset)
    handler = ClientMessageType.get(opcode, cls)
    return handler.with_params(xid, path, watch, data, offset, length, client, server)