/** {@inheritDoc} */ @Override public <T> T receive(ClientOperation op, long reqId, Function<BinaryInputStream, T> payloadReader) throws ClientConnectionException, ClientAuthorizationException { final int MIN_RES_SIZE = 8 + 4; // minimal response size: long (8 bytes) ID + int (4 bytes) status int resSize = new BinaryHeapInputStream(read(4)).readInt(); if (resSize < 0) throw new ClientProtocolError(String.format("Invalid response size: %s", resSize)); if (resSize == 0) return null; BinaryInputStream resIn = new BinaryHeapInputStream(read(MIN_RES_SIZE)); long resId = resIn.readLong(); if (resId != reqId) throw new ClientProtocolError(String.format("Unexpected response ID [%s], [%s] was expected", resId, reqId)); int status = resIn.readInt(); if (status != 0) { resIn = new BinaryHeapInputStream(read(resSize - MIN_RES_SIZE)); String err = new BinaryReaderExImpl(null, resIn, null, true).readString(); switch (status) { case ClientStatus.SECURITY_VIOLATION: throw new ClientAuthorizationException(); default: throw new ClientServerError(err, status, reqId); } } if (resSize <= MIN_RES_SIZE || payloadReader == null) return null; BinaryInputStream payload = new BinaryHeapInputStream(read(resSize - MIN_RES_SIZE)); return payloadReader.apply(payload); }
/** {@inheritDoc} */ @Override public <T> T receive(ClientOperation op, long reqId, Function<BinaryInputStream, T> payloadReader) throws ClientConnectionException, ClientAuthorizationException { final int MIN_RES_SIZE = 8 + 4; // minimal response size: long (8 bytes) ID + int (4 bytes) status int resSize = new BinaryHeapInputStream(read(4)).readInt(); if (resSize < 0) throw new ClientProtocolError(String.format("Invalid response size: %s", resSize)); if (resSize == 0) return null; BinaryInputStream resIn = new BinaryHeapInputStream(read(MIN_RES_SIZE)); long resId = resIn.readLong(); if (resId != reqId) throw new ClientProtocolError(String.format("Unexpected response ID [%s], [%s] was expected", resId, reqId)); int status = resIn.readInt(); if (status != 0) { resIn = new BinaryHeapInputStream(read(resSize - MIN_RES_SIZE)); String err = new BinaryReaderExImpl(null, resIn, null, true).readString(); switch (status) { case ClientStatus.SECURITY_VIOLATION: throw new ClientAuthorizationException(); default: throw new ClientServerError(err, status, reqId); } } if (resSize <= MIN_RES_SIZE || payloadReader == null) return null; BinaryInputStream payload = new BinaryHeapInputStream(read(resSize - MIN_RES_SIZE)); return payloadReader.apply(payload); }