{
UnsignedInteger deliveryId = tpSession.getOutgoingDeliveryId();
TransportDelivery tpDelivery = new TransportDelivery(deliveryId, delivery, tpLink);
delivery.setTransportDelivery(tpDelivery);
final Transfer transfer = new Transfer();
transfer.setDeliveryId(deliveryId);
transfer.setDeliveryTag(new Binary(delivery.getTag()));
transfer.setHandle(tpLink.getLocalHandle());
if(delivery.isSettled())
{
transfer.setSettled(Boolean.TRUE);
}
else
{
tpSession.addUnsettledOutgoing(deliveryId, delivery);
}
if(snd.current() == delivery)
{
transfer.setMore(true);
}
transfer.setMessageFormat(UnsignedInteger.ZERO);
// TODO - large frames
ByteBuffer payload = delivery.getData() == null ? null :
ByteBuffer.wrap(delivery.getData(), delivery.getDataOffset(),
delivery.getDataLength());
writeFrame(tpSession.getLocalChannel(), transfer, payload,
new PartialTransfer(transfer));
tpSession.incrementOutgoingId();
tpSession.decrementRemoteIncomingWindow();
if(payload == null || !payload.hasRemaining())
{
session.incrementOutgoingBytes(-delivery.pending());
delivery.setData(null);
delivery.setDataLength(0);
if (!transfer.getMore()) {
delivery.setDone();
tpLink.setDeliveryCount(tpLink.getDeliveryCount().add(UnsignedInteger.ONE));
tpLink.setLinkCredit(tpLink.getLinkCredit().subtract(UnsignedInteger.ONE));
tpSession.incrementOutgoingDeliveryId();
session.incrementOutgoingDeliveries(-1);