use of org.apache.qpid.server.protocol.v1_0.codec.ValueWriter in project qpid-broker-j by apache.
the class LinkStoreUtils method objectToAmqpBytes.
public static byte[] objectToAmqpBytes(final Object object) {
ValueWriter valueWriter = DESCRIBED_TYPE_REGISTRY.getValueWriter(object);
int encodedSize = valueWriter.getEncodedSize();
try (QpidByteBuffer qpidByteBuffer = QpidByteBuffer.allocate(encodedSize)) {
valueWriter.writeToBuffer(qpidByteBuffer);
return qpidByteBuffer.array();
}
}
use of org.apache.qpid.server.protocol.v1_0.codec.ValueWriter in project qpid-broker-j by apache.
the class AMQPConnection_1_0Impl method sendFrame.
@Override
public int sendFrame(final int channel, final FrameBody body, final QpidByteBuffer payload) {
if (!_closedForOutput) {
ValueWriter<FrameBody> writer = _describedTypeRegistry.getValueWriter(body);
if (payload == null) {
send(AMQFrame.createAMQFrame(channel, body));
return 0;
} else {
int size = writer.getEncodedSize();
int maxPayloadSize = _maxFrameSize - (size + 9);
long payloadLength = (long) payload.remaining();
if (payloadLength <= maxPayloadSize) {
send(AMQFrame.createAMQFrame(channel, body, payload));
return (int) payloadLength;
} else {
((Transfer) body).setMore(Boolean.TRUE);
writer = _describedTypeRegistry.getValueWriter(body);
size = writer.getEncodedSize();
maxPayloadSize = _maxFrameSize - (size + 9);
try (QpidByteBuffer payloadDup = payload.view(0, maxPayloadSize)) {
payload.position(payload.position() + maxPayloadSize);
send(AMQFrame.createAMQFrame(channel, body, payloadDup));
}
return maxPayloadSize;
}
}
} else {
return -1;
}
}
use of org.apache.qpid.server.protocol.v1_0.codec.ValueWriter in project qpid-broker-j by apache.
the class AbstractLinkEndpoint method handleOversizedUnsettledMapIfNecessary.
private Attach handleOversizedUnsettledMapIfNecessary(final Attach attachToSend) {
final AMQPDescribedTypeRegistry describedTypeRegistry = getSession().getConnection().getDescribedTypeRegistry();
final ValueWriter<Attach> valueWriter = describedTypeRegistry.getValueWriter(attachToSend);
if (valueWriter.getEncodedSize() + 8 > getSession().getConnection().getMaxFrameSize()) {
_localIncompleteUnsettled = true;
attachToSend.setIncompleteUnsettled(true);
final int targetSize = getSession().getConnection().getMaxFrameSize();
int lowIndex = 0;
Map<Binary, DeliveryState> localUnsettledMap = attachToSend.getUnsettled();
if (localUnsettledMap == null) {
localUnsettledMap = Collections.emptyMap();
}
int highIndex = localUnsettledMap.size();
int currentIndex = (highIndex - lowIndex) / 2;
int oldIndex;
HashMap<Binary, DeliveryState> unsettledMap = null;
int totalSize;
do {
HashMap<Binary, DeliveryState> partialUnsettledMap = new HashMap<>(currentIndex);
final Iterator<Map.Entry<Binary, DeliveryState>> iterator = localUnsettledMap.entrySet().iterator();
for (int i = 0; i < currentIndex; ++i) {
final Map.Entry<Binary, DeliveryState> entry = iterator.next();
partialUnsettledMap.put(entry.getKey(), entry.getValue());
}
attachToSend.setUnsettled(partialUnsettledMap);
totalSize = describedTypeRegistry.getValueWriter(attachToSend).getEncodedSize() + FRAME_HEADER_SIZE;
if (totalSize > targetSize) {
highIndex = currentIndex;
} else if (totalSize < targetSize) {
lowIndex = currentIndex;
unsettledMap = partialUnsettledMap;
} else {
lowIndex = highIndex = currentIndex;
unsettledMap = partialUnsettledMap;
}
oldIndex = currentIndex;
currentIndex = lowIndex + (highIndex - lowIndex) / 2;
} while (oldIndex != currentIndex);
if (unsettledMap == null || unsettledMap.isEmpty()) {
final End endWithError = new End();
endWithError.setError(new Error(AmqpError.FRAME_SIZE_TOO_SMALL, "Cannot fit a single unsettled delivery into Attach frame."));
getSession().end(endWithError);
}
attachToSend.setUnsettled(unsettledMap);
} else {
_localIncompleteUnsettled = false;
}
return attachToSend;
}
Aggregations