Delivery
Engine

## Modules

| Disposition

## Typedefs

typedef pn_bytes_t | pn_delivery_tag_t
| An AMQP delivery tag. More...
typedef struct pn_delivery_t | pn_delivery_t
| An AMQP Delivery object. More...

## Functions

PN_EXTERN pn_delivery_tag_t | pn_dtag (const char *bytes, size_t size)
| Construct a delivery tag. More...
PN_EXTERN pn_delivery_t * | pn_delivery (pn_link_t *link, pn_delivery_tag_t tag)
| Create a delivery on a link. More...
PN_EXTERN void * | pn_delivery_get_context (pn_delivery_t *delivery)
PN_EXTERN void | pn_delivery_set_context (pn_delivery_t *delivery, void *context)
PN_EXTERN pn_record_t * | pn_delivery_attachments (pn_delivery_t *delivery)
| Get the attachments that are associated with a delivery object. More...
PN_EXTERN pn_delivery_tag_t | pn_delivery_tag (pn_delivery_t *delivery)
| Get the tag for a delivery object. More...
PN_EXTERN pn_link_t * | pn_delivery_link (pn_delivery_t *delivery)
| Get the parent link for a delivery object. More...
PN_EXTERN pn_disposition_t * | pn_delivery_local (pn_delivery_t *delivery)
| Get the local disposition for a delivery. More...
PN_EXTERN uint64_t | pn_delivery_local_state (pn_delivery_t *delivery)
| Get the local disposition state for a delivery. More...
PN_EXTERN pn_disposition_t * | pn_delivery_remote (pn_delivery_t *delivery)
| Get the remote disposition for a delivery. More...
PN_EXTERN uint64_t | pn_delivery_remote_state (pn_delivery_t *delivery)
| Get the remote disposition state for a delivery. More...
PN_EXTERN bool | pn_delivery_settled (pn_delivery_t *delivery)
| Check if a delivery is remotely settled. More...
PN_EXTERN size_t | pn_delivery_pending (pn_delivery_t *delivery)
| Get the amount of pending message data for a delivery. More...
PN_EXTERN bool | pn_delivery_partial (pn_delivery_t *delivery)
| Check if a delivery only has partial message data. More...
PN_EXTERN bool | pn_delivery_writable (pn_delivery_t *delivery)
| Check if a delivery is writable. More...
PN_EXTERN bool | pn_delivery_readable (pn_delivery_t *delivery)
| Check if a delivery is readable. More...
PN_EXTERN bool | pn_delivery_updated (pn_delivery_t *delivery)
| Check if a delivery is updated. More...
PN_EXTERN void | pn_delivery_update (pn_delivery_t *delivery, uint64_t state)
| Update the disposition of a delivery. More...
PN_EXTERN void | pn_delivery_clear (pn_delivery_t *delivery)
| Clear the updated flag for a delivery. More...
PN_EXTERN bool | pn_delivery_current (pn_delivery_t *delivery)
| Return true if delivery is the current delivery for its link. More...
PN_EXTERN void | pn_delivery_settle (pn_delivery_t *delivery)
| Settle a delivery. More...
PN_EXTERN void | pn_delivery_dump (pn_delivery_t *delivery)
| Utility function for printing details of a delivery. More...
PN_EXTERN bool | pn_delivery_buffered (pn_delivery_t *delivery)
| Check if a delivery is buffered. More...
PN_EXTERN pn_delivery_t * | pn_work_head (pn_connection_t *connection)
| Extracts the first delivery on the connection that has pending operations. More...
PN_EXTERN pn_delivery_t * | pn_work_next (pn_delivery_t *delivery)
| Get the next delivery on the connection that needs has pending operations. More...

## Detailed Description

## Typedef Documentation

## pn_delivery_t

typedef struct pn_delivery_t pn_delivery_t

An AMQP Delivery object.

A pn_delivery_t object encapsulates all of the endpoint state associated with an AMQP Delivery. Every delivery exists within the context of a pn_link_t object.

The AMQP model for settlement is based on the lifecycle of a delivery at an endpoint. At each end of a link, a delivery is created, it exists for some period of time, and finally it is forgotten, aka settled. Note that because this lifecycle happens independently at both the sender and the receiver, there are actually four events of interest in the combined lifecycle of a given delivery:

- created at sender
- created at receiver
- settled at sender
- settled at receiver

Because the sender and receiver are operating concurrently, these events can occur in a variety of different orders, and the order of these events impacts the types of failures that may occur when transferring a delivery. Eliminating scenarios where the receiver creates the delivery first, we have the following possible sequences of interest:

## Sender presettles (aka at-most-once):

1. created at sender
2. settled at sender
3. created at receiver
4. settled at receiver

In this configuration the sender settles (i.e. forgets about) the delivery before it even reaches the receiver, and if anything should happen to the delivery in-flight, there is no way to recover, hence the "at most once" semantics.

## Receiver settles first (aka at-least-once):

1. created at sender
2. created at receiver
3. settled at receiver
4. settled at sender

In this configuration the receiver settles the delivery first, and the sender settles once it sees the receiver has settled. Should anything happen to the delivery in-flight, the sender can resend, however the receiver may have already forgotten the delivery and so it could interpret the resend as a new delivery, hence the "at least once" semantics.

## Receiver settles second (aka exactly-once):

1. created at sender
2. created at receiver
3. settled at sender
4. settled at receiver

In this configuration the receiver settles only once it has seen that the sender has settled. This provides the sender the option to retransmit, and the receiver has the option to recognize (and discard) duplicates, allowing for exactly once semantics.

Note that in the last scenario the sender needs some way to know when it is safe to settle. This is where delivery state comes in. In addition to these lifecycle related events surrounding deliveries there is also the notion of a delivery state that can change over the lifetime of a delivery, e.g. it might start out as nothing, transition to PN_RECEIVED and then transition to PN_ACCEPTED. In the first two scenarios the delivery state isn't required, however in final scenario the sender would typically trigger settlement based on seeing the delivery state transition to a terminal state like PN_ACCEPTED or PN_REJECTED.

In practice settlement is controlled by application policy, so there may well be more options here, e.g. a sender might not settle strictly based on what has happened at the receiver, it might also choose to impose some time limit and settle after that period has expired, or it could simply have a sliding window of the last N deliveries and settle the oldest whenever a new one comes along.

## pn_delivery_tag_t

typedef pn_bytes_t pn_delivery_tag_t

An AMQP delivery tag.

## Function Documentation

## pn_delivery()

PN_EXTERN pn_delivery_t* pn_delivery | ( | pn_link_t * | link,
|  | pn_delivery_tag_t | tag
| ) |  |

Create a delivery on a link.

Every delivery object within a link must be supplied with a unique tag. Links maintain a sequence of delivery object in the order that they are created.

Parameters

  [in] | link | a link object
  [in] | tag | the delivery tag

Returns
  a newly created delivery, or NULL if there was an error

## pn_delivery_attachments()

PN_EXTERN pn_record_t* pn_delivery_attachments | ( | pn_delivery_t * | delivery | ) |

Get the attachments that are associated with a delivery object.

Parameters

  [in] | delivery | the delivery whose attachments are to be returned.

Returns
  the attachments for the delivery object

## pn_delivery_buffered()

PN_EXTERN bool pn_delivery_buffered | ( | pn_delivery_t * | delivery | ) |

Check if a delivery is buffered.

A delivery that is buffered has not yet been written to the wire.

Note that returning false does not imply that a delivery was definitely written to the wire. If false is returned, it is not known whether the delivery was actually written to the wire or not.

Parameters

  [in] | delivery | a delivery object

Returns
  true if the delivery is buffered

## pn_delivery_clear()

PN_EXTERN void pn_delivery_clear | ( | pn_delivery_t * | delivery | ) |

Clear the updated flag for a delivery.

See pn_delivery_updated.

Parameters

  [in] | delivery | a delivery object

## pn_delivery_current()

PN_EXTERN bool pn_delivery_current | ( | pn_delivery_t * | delivery | ) |

Return true if delivery is the current delivery for its link.

Parameters

  [in] | delivery | a delivery object

Returns
  true if delivery is the current delivery for its link.

## pn_delivery_dump()

PN_EXTERN void pn_delivery_dump | ( | pn_delivery_t * | delivery | ) |

Utility function for printing details of a delivery.

Parameters

  [in] | delivery | a delivery object

## pn_delivery_get_context()

PN_EXTERN void* pn_delivery_get_context | ( | pn_delivery_t * | delivery | ) |

Deprecated:
  Get the application context that is associated with a delivery object.

The application context for a delivery may be set using pn_delivery_set_context.

Parameters

  [in] | delivery | the delivery whose context is to be returned.

Returns
  the application context for the delivery object

## pn_delivery_link()

PN_EXTERN pn_link_t* pn_delivery_link | ( | pn_delivery_t * | delivery | ) |

Get the parent link for a delivery object.

Parameters

  [in] | delivery | a delivery object

Returns
  the parent link

## pn_delivery_local()

PN_EXTERN pn_disposition_t* pn_delivery_local | ( | pn_delivery_t * | delivery | ) |

Get the local disposition for a delivery.

The pointer returned by this object is valid until the delivery is settled.

Parameters

  [in] | delivery | a delivery object

Returns
  a pointer to the local disposition

## pn_delivery_local_state()

PN_EXTERN uint64_t pn_delivery_local_state | ( | pn_delivery_t * | delivery | ) |

Get the local disposition state for a delivery.

Parameters

  [in] | delivery | a delivery object

Returns
  the local disposition state

## pn_delivery_partial()

PN_EXTERN bool pn_delivery_partial | ( | pn_delivery_t * | delivery | ) |

Check if a delivery only has partial message data.

Parameters

  [in] | delivery | a delivery object

Returns
  true if the delivery only contains part of a message, false otherwise

## pn_delivery_pending()

PN_EXTERN size_t pn_delivery_pending | ( | pn_delivery_t * | delivery | ) |

Get the amount of pending message data for a delivery.

Parameters

  [in] | delivery | a delivery object

Returns
  the amount of pending message data in bytes

## pn_delivery_readable()

PN_EXTERN bool pn_delivery_readable | ( | pn_delivery_t * | delivery | ) |

Check if a delivery is readable.

A delivery is considered readable if it is the current delivery on an incoming link.

Parameters

  [in] | delivery | a delivery object

Returns
  true if the delivery is readable, false otherwise

## pn_delivery_remote()

PN_EXTERN pn_disposition_t* pn_delivery_remote | ( | pn_delivery_t * | delivery | ) |

Get the remote disposition for a delivery.

The pointer returned by this object is valid until the delivery is settled.

Parameters

  [in] | delivery | a delivery object

Returns
  a pointer to the remote disposition

## pn_delivery_remote_state()

PN_EXTERN uint64_t pn_delivery_remote_state | ( | pn_delivery_t * | delivery | ) |

Get the remote disposition state for a delivery.

Parameters

  [in] | delivery | a delivery object

Returns
  the remote disposition state

## pn_delivery_set_context()

PN_EXTERN void pn_delivery_set_context | ( | pn_delivery_t * | delivery,
|  | void * | context
| ) |  |

Deprecated:
  Set a new application context for a delivery object.

The application context for a delivery object may be retrieved using pn_delivery_get_context.

Parameters

  [in] | delivery | the delivery object
  [in] | context | the application context

## pn_delivery_settle()

PN_EXTERN void pn_delivery_settle | ( | pn_delivery_t * | delivery | ) |

Settle a delivery.

A settled delivery can never be used again.

NOTE: if pn_delivery_current(delivery) is true before the call then pn_link_advance(pn_delivery_link(deliver)) is called automatically.

Parameters

  [in] | delivery | a delivery object

## pn_delivery_settled()

PN_EXTERN bool pn_delivery_settled | ( | pn_delivery_t * | delivery | ) |

Check if a delivery is remotely settled.

Parameters

  [in] | delivery | a delivery object

Returns
  true if the delivery is settled at the remote endpoint, false otherwise

## pn_delivery_tag()

PN_EXTERN pn_delivery_tag_t pn_delivery_tag | ( | pn_delivery_t * | delivery | ) |

Get the tag for a delivery object.

Parameters

  [in] | delivery | a delivery object

Returns
  the delivery tag

## pn_delivery_update()

PN_EXTERN void pn_delivery_update | ( | pn_delivery_t * | delivery,
|  | uint64_t | state
| ) |  |

Update the disposition of a delivery.

When update is invoked the updated disposition of the delivery will be communicated to the peer.

Parameters

  [in] | delivery | a delivery object
  [in] | state | the updated delivery state

## pn_delivery_updated()

PN_EXTERN bool pn_delivery_updated | ( | pn_delivery_t * | delivery | ) |

Check if a delivery is updated.

A delivery is considered updated whenever the peer communicates a new disposition for the delivery. Once a delivery becomes updated, it will remain so until pn_delivery_clear is called.

Parameters

  [in] | delivery | a delivery object

Returns
  true if the delivery is updated, false otherwise

## pn_delivery_writable()

PN_EXTERN bool pn_delivery_writable | ( | pn_delivery_t * | delivery | ) |

Check if a delivery is writable.

A delivery is considered writable if it is the current delivery on an outgoing link, and the link has positive credit.

Parameters

  [in] | delivery | a delivery object

Returns
  true if the delivery is writable, false otherwise

## pn_dtag()

PN_EXTERN pn_delivery_tag_t pn_dtag | ( | const char * | bytes,
|  | size_t | size
| ) |  |

Construct a delivery tag.

Parameters

  [in] | bytes | a pointer to the beginning of the tag
  [in] | size | the size of the tag

Returns
  the delivery tag

## pn_work_head()

PN_EXTERN pn_delivery_t* pn_work_head | ( | pn_connection_t * | connection | ) |

Extracts the first delivery on the connection that has pending operations.

Retrieves the first delivery on the Connection that has pending operations. A readable delivery indicates message data is waiting to be read. A writable delivery indicates that message data may be sent. An updated delivery indicates that the delivery's disposition has changed. A delivery will never be both readable and writible, but it may be both readable and updated or both writiable and updated.

Parameters

  [in] | connection | the connection

Returns
  the first delivery object that needs to be serviced, else NULL if none

## pn_work_next()

PN_EXTERN pn_delivery_t* pn_work_next | ( | pn_delivery_t * | delivery | ) |

Get the next delivery on the connection that needs has pending operations.

Parameters

  [in] | delivery | the previous delivery retrieved from either pn_work_head or pn_work_next

Returns
  the next delivery that has pending operations, else NULL if none