Debian Codegraph @

c/include/proton/link.h

raw
1 #ifndef PROTON_LINK_H
2 #define PROTON_LINK_H 1
3
4 /*
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 */
24
25 #include <proton/import_export.h>
26 #include <proton/type_compat.h>
27 #include <proton/condition.h>
28 #include <proton/terminus.h>
29 #include <proton/types.h>
30 #include <proton/object.h>
31 #include <stddef.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /**
38 * @file
39 *
40 * @copybrief link
41 *
42 * @addtogroup link
43 * @{
44 */
45
46 /**
47 * Construct a new sender on a session.
48 *
49 * Each sending link between two AMQP containers must be uniquely
50 * named. Note that this uniqueness cannot be enforced at the API
51 * level, so some consideration should be taken in choosing link
52 * names.
53 *
54 * @param[in] session the session object
55 * @param[in] name the name of the link
56 * @return a newly constructed sender link or NULL on error
57 */
58 PN_EXTERN pn_link_t *pn_sender(pn_session_t *session, const char *name);
59
60 /**
61 * Construct a new receiver on a session.
62 *
63 * Each receiving link between two AMQP containers must be uniquely
64 * named. Note that this uniqueness cannot be enforced at the API
65 * level, so some consideration should be taken in choosing link
66 * names.
67 *
68 * @param[in] session the session object
69 * @param[in] name the name of the link
70 * @return a newly constructed receiver link or NULL on error
71 */
72 PN_EXTERN pn_link_t *pn_receiver(pn_session_t *session, const char *name);
73
74 /**
75 * Free a link object.
76 *
77 * When a link object is freed, all ::pn_delivery_t objects associated
78 * with the session are also freed. Freeing a link will settle any
79 * unsettled deliveries on the link.
80 *
81 * @param[in] link a link object to free (or NULL)
82 */
83 PN_EXTERN void pn_link_free(pn_link_t *link);
84
85 /**
86 * Get the application context that is associated with a link object.
87 *
88 * The application context for a link may be set using
89 * ::pn_link_set_context.
90 *
91 * @param[in] link the link whose context is to be returned.
92 * @return the application context for the link object
93 */
94 PN_EXTERN void *pn_link_get_context(pn_link_t *link);
95
96 /**
97 * Set a new application context for a link object.
98 *
99 * The application context for a link object may be retrieved using
100 * ::pn_link_get_context.
101 *
102 * @param[in] link the link object
103 * @param[in] context the application context
104 */
105 PN_EXTERN void pn_link_set_context(pn_link_t *link, void *context);
106
107 /**
108 * Get the attachments that are associated with a link object.
109 *
110 * @param[in] link the link whose attachments are to be returned.
111 * @return the attachments for the link object
112 */
113 PN_EXTERN pn_record_t *pn_link_attachments(pn_link_t *link);
114
115 /**
116 * Get the name of a link.
117 *
118 * @param[in] link a link object
119 * @return the name of the link
120 */
121 PN_EXTERN const char *pn_link_name(pn_link_t *link);
122
123 /**
124 * Test if a link is a sender.
125 *
126 * @param[in] link a link object
127 * @return true if and only if the link is a sender
128 */
129 PN_EXTERN bool pn_link_is_sender(pn_link_t *link);
130
131 /**
132 * Test if a link is a receiver.
133 *
134 * @param[in] link a link object
135 * @return true if and only if the link is a receiver
136 */
137 PN_EXTERN bool pn_link_is_receiver(pn_link_t *link);
138
139 /**
140 * Get the endpoint state flags for a link.
141 *
142 * @param[in] link the link
143 * @return the link's state flags
144 */
145 PN_EXTERN pn_state_t pn_link_state(pn_link_t *link);
146
147 /**
148 * **Deprecated** - Use ::pn_link_condition().
149 *
150 * Get additional error information associated with the link.
151 *
152 * Whenever a link operation fails (i.e. returns an error code),
153 * additional error details can be obtained using this function. The
154 * error object that is returned may also be used to clear the error
155 * condition.
156 *
157 * The pointer returned by this operation is valid until the
158 * link object is freed.
159 *
160 * @param[in] link the link object
161 * @return the link's error object
162 */
163 /* PN_DEPRECATED("Use pn_link_condition") */
164 PN_EXTERN pn_error_t *pn_link_error(pn_link_t *link);
165
166 /**
167 * Get the local condition associated with a link endpoint.
168 *
169 * The ::pn_condition_t object retrieved may be modified prior to
170 * closing a link in order to indicate a particular condition
171 * exists when the link closes. This is normally used to
172 * communicate error conditions to the remote peer, however it may
173 * also be used in non error cases. See ::pn_condition_t for more
174 * details.
175 *
176 * The pointer returned by this operation is valid until the link
177 * object is freed.
178 *
179 * Deprecation notice: note that this will always return an empty
180 * error object
181 *
182 * @param[in] link the link object
183 * @return the link's local condition object
184 */
185 PN_EXTERN pn_condition_t *pn_link_condition(pn_link_t *link);
186
187 /**
188 * Get the remote condition associated with a link endpoint.
189 *
190 * The ::pn_condition_t object retrieved may be examined in order to
191 * determine whether the remote peer was indicating some sort of
192 * exceptional condition when the remote link endpoint was
193 * closed. The ::pn_condition_t object returned may not be modified.
194 *
195 * The pointer returned by this operation is valid until the
196 * link object is freed.
197 *
198 * @param[in] link the link object
199 * @return the link's remote condition object
200 */
201 PN_EXTERN pn_condition_t *pn_link_remote_condition(pn_link_t *link);
202
203 /**
204 * Get the parent session for a link object.
205 *
206 * This operation retrieves the parent ::pn_session_t object that
207 * contains the given ::pn_link_t object.
208 *
209 * @param[in] link the link object
210 * @return the parent session object
211 */
212 PN_EXTERN pn_session_t *pn_link_session(pn_link_t *link);
213
214 /**
215 * Retrieve the first link that matches the given state mask.
216 *
217 * Examines the state of each link owned by the connection and returns
218 * the first link that matches the given state mask. If state contains
219 * both local and remote flags, then an exact match against those
220 * flags is performed. If state contains only local or only remote
221 * flags, then a match occurs if any of the local or remote flags are
222 * set respectively. state==0 matches all links.
223 *
224 * @param[in] connection to be searched for matching Links
225 * @param[in] state mask to match
226 * @return the first link owned by the connection that matches the
227 * mask, else NULL if no links match
228 */
229 PN_EXTERN pn_link_t *pn_link_head(pn_connection_t *connection, pn_state_t state);
230
231 /**
232 * Retrieve the next link that matches the given state mask.
233 *
234 * When used with pn_link_head, the application can access all links
235 * on the connection that match the given state. See pn_link_head for
236 * description of match behavior.
237 *
238 * @param[in] link the previous link obtained from pn_link_head or
239 * pn_link_next
240 * @param[in] state mask to match
241 * @return the next session owned by the connection that matches the
242 * mask, else NULL if no sessions match
243 */
244 PN_EXTERN pn_link_t *pn_link_next(pn_link_t *link, pn_state_t state);
245
246 /**
247 * Open a link.
248 *
249 * Once this operation has completed, the PN_LOCAL_ACTIVE state flag
250 * will be set.
251 *
252 * @param[in] link a link object
253 */
254 PN_EXTERN void pn_link_open(pn_link_t *link);
255
256 /**
257 * Close a link.
258 *
259 * Once this operation has completed, the PN_LOCAL_CLOSED state flag
260 * will be set. This may be called without calling
261 * ::pn_link_open, in this case it is equivalent to calling
262 * ::pn_link_open followed by ::pn_link_close.
263 *
264 * @param[in] link a link object
265 */
266 PN_EXTERN void pn_link_close(pn_link_t *link);
267
268 /**
269 * Detach a link.
270 *
271 * @param[in] link a link object
272 */
273 PN_EXTERN void pn_link_detach(pn_link_t *link);
274
275 /**
276 * Access the locally defined source definition for a link.
277 *
278 * The pointer returned by this operation is valid until the link
279 * object is freed.
280 *
281 * @param[in] link a link object
282 * @return a pointer to a source terminus
283 */
284 PN_EXTERN pn_terminus_t *pn_link_source(pn_link_t *link);
285
286 /**
287 * Access the locally defined target definition for a link.
288 *
289 * The pointer returned by this operation is valid until the link
290 * object is freed.
291 *
292 * @param[in] link a link object
293 * @return a pointer to a target terminus
294 */
295 PN_EXTERN pn_terminus_t *pn_link_target(pn_link_t *link);
296
297 /**
298 * Access the remotely defined source definition for a link.
299 *
300 * The pointer returned by this operation is valid until the link
301 * object is freed. The remotely defined terminus will be empty until
302 * the link is remotely opened as indicated by the PN_REMOTE_ACTIVE
303 * flag.
304 *
305 * @param[in] link a link object
306 * @return a pointer to the remotely defined source terminus
307 */
308 PN_EXTERN pn_terminus_t *pn_link_remote_source(pn_link_t *link);
309
310 /**
311 * Access the remotely defined target definition for a link.
312 *
313 * The pointer returned by this operation is valid until the link
314 * object is freed. The remotely defined terminus will be empty until
315 * the link is remotely opened as indicated by the PN_REMOTE_ACTIVE
316 * flag.
317 *
318 * @param[in] link a link object
319 * @return a pointer to the remotely defined target terminus
320 */
321 PN_EXTERN pn_terminus_t *pn_link_remote_target(pn_link_t *link);
322
323 /**
324 * Get the current delivery for a link.
325 *
326 * Each link maintains a sequence of deliveries in the order they were
327 * created, along with a pointer to the *current* delivery. All
328 * send/recv operations on a link take place on the *current*
329 * delivery. If a link has no current delivery, the current delivery
330 * is automatically initialized to the next delivery created on the
331 * link. Once initialized, the current delivery remains the same until
332 * it is changed through use of ::pn_link_advance or until it is
333 * settled via ::pn_delivery_settle.
334 *
335 * @param[in] link a link object
336 * @return the current delivery for the link, or NULL if there is none
337 */
338 PN_EXTERN pn_delivery_t *pn_link_current(pn_link_t *link);
339
340 /**
341 * Advance the current delivery of a link to the next delivery on the
342 * link.
343 *
344 * For sending links this operation is used to finish sending message
345 * data for the current outgoing delivery and move on to the next
346 * outgoing delivery (if any).
347 *
348 * For receiving links, this operation is used to finish accessing
349 * message data from the current incoming delivery and move on to the
350 * next incoming delivery (if any).
351 *
352 * Each link maintains a sequence of deliveries in the order they were
353 * created, along with a pointer to the *current* delivery. The
354 * pn_link_advance operation will modify the *current* delivery on the
355 * link to point to the next delivery in the sequence. If there is no
356 * next delivery in the sequence, the current delivery will be set to
357 * NULL. This operation will return true if invoking it caused the
358 * value of the current delivery to change, even if it was set to
359 * NULL.
360 *
361 * @param[in] link a link object
362 * @return true if the current delivery was changed
363 */
364 PN_EXTERN bool pn_link_advance(pn_link_t *link);
365
366 /**
367 * Get the credit balance for a link.
368 *
369 * Links use a credit based flow control scheme. Every receiver
370 * maintains a credit balance that corresponds to the number of
371 * deliveries that the receiver can accept at any given moment. As
372 * more capacity becomes available at the receiver (see
373 * ::pn_link_flow), it adds credit to this balance and communicates
374 * the new balance to the sender. Whenever a delivery is
375 * sent/received, the credit balance maintained by the link is
376 * decremented by one. Once the credit balance at the sender reaches
377 * zero, the sender must pause sending until more credit is obtained
378 * from the receiver.
379 *
380 * Note that a sending link may still be used to send deliveries even
381 * if pn_link_credit reaches zero, however those deliveries will end
382 * up being buffered by the link until enough credit is obtained from
383 * the receiver to send them over the wire. In this case the balance
384 * reported by ::pn_link_credit will go negative.
385 *
386 * @param[in] link a link object
387 * @return the credit balance for the link
388 */
389 PN_EXTERN int pn_link_credit(pn_link_t *link);
390
391 /**
392 * Get the number of queued deliveries for a link.
393 *
394 * Links may queue deliveries for a number of reasons, for example
395 * there may be insufficient credit to send them to the receiver (see
396 * ::pn_link_credit), or they simply may not have yet had a chance to
397 * be written to the wire. This operation will return the number of
398 * queued deliveries on a link.
399 *
400 * @param[in] link a link object
401 * @return the number of queued deliveries for the link
402 */
403 PN_EXTERN int pn_link_queued(pn_link_t *link);
404
405 /**
406 * Get the remote view of the credit for a link.
407 *
408 * The remote view of the credit for a link differs from local view of
409 * credit for a link by the number of queued deliveries. In other
410 * words ::pn_link_remote_credit is defined to be ::pn_link_credit -
411 * ::pn_link_queued.
412 *
413 * @param[in] link a link object
414 * @return the remote view of the credit for a link
415 */
416 PN_EXTERN int pn_link_remote_credit(pn_link_t *link);
417
418 /**
419 * Get the drain flag for a link.
420 *
421 * If a link is in drain mode, then the sending endpoint of a link
422 * must immediately use up all available credit on the link. If this
423 * is not possible, the excess credit must be returned by invoking
424 * ::pn_link_drained. Only the receiving endpoint can set the drain
425 * mode. See ::pn_link_set_drain for details.
426 *
427 * @param[in] link a link object
428 * @return true if and only if the link is in drain mode
429 */
430 PN_EXTERN bool pn_link_get_drain(pn_link_t *link);
431
432 /**
433 * Drain excess credit for a link.
434 *
435 * When a link is in drain mode, the sender must use all excess credit
436 * immediately, and release any excess credit back to the receiver if
437 * there are no deliveries available to send.
438 *
439 * When invoked on a sending link that is in drain mode, this
440 * operation will release all excess credit back to the receiver and
441 * return the number of credits released back to the sender. If the
442 * link is not in drain mode, this operation is a noop.
443 *
444 * When invoked on a receiving link, this operation will return and
445 * reset the number of credits the sender has released back to the
446 * receiver.
447 *
448 * @param[in] link a link object
449 * @return the number of credits drained
450 */
451 PN_EXTERN int pn_link_drained(pn_link_t *link);
452
453 /**
454 * Get the available deliveries hint for a link.
455 *
456 * The available count for a link provides a hint as to the number of
457 * deliveries that might be able to be sent if sufficient credit were
458 * issued by the receiving link endpoint. See ::pn_link_offered for
459 * more details.
460 *
461 * @param[in] link a link object
462 * @return the available deliveries hint
463 */
464 PN_EXTERN int pn_link_available(pn_link_t *link);
465
466 /**
467 * Describes the permitted/expected settlement behaviours of a sending
468 * link.
469 *
470 * The sender settle mode describes the permitted and expected
471 * behaviour of a sending link with respect to settling of deliveries.
472 * See ::pn_delivery_settle for more details.
473 */
474 typedef enum {
475 PN_SND_UNSETTLED = 0, /**< The sender will send all deliveries
476 initially unsettled. */
477 PN_SND_SETTLED = 1, /**< The sender will send all deliveries settled
478 to the receiver. */
479 PN_SND_MIXED = 2 /**< The sender may send a mixture of settled and
480 unsettled deliveries. */
481 } pn_snd_settle_mode_t;
482
483 /**
484 * Describes the permitted/expected settlement behaviours of a
485 * receiving link.
486 *
487 * The receiver settle mode describes the permitted and expected
488 * behaviour of a receiving link with respect to settling of
489 * deliveries. See ::pn_delivery_settle for more details.
490 */
491 typedef enum {
492 PN_RCV_FIRST = 0, /**< The receiver will settle deliveries
493 regardless of what the sender does. */
494 PN_RCV_SECOND = 1 /**< The receiver will only settle deliveries
495 after the sender settles. */
496 } pn_rcv_settle_mode_t;
497
498 /**
499 * Get the local sender settle mode for a link.
500 *
501 * @param[in] link a link object
502 * @return the local sender settle mode
503 */
504 PN_EXTERN pn_snd_settle_mode_t pn_link_snd_settle_mode(pn_link_t *link);
505
506 /**
507 * Get the local receiver settle mode for a link.
508 *
509 * @param[in] link a link object
510 * @return the local receiver settle mode
511 */
512 PN_EXTERN pn_rcv_settle_mode_t pn_link_rcv_settle_mode(pn_link_t *link);
513
514 /**
515 * Set the local sender settle mode for a link.
516 *
517 * @param[in] link a link object
518 * @param[in] mode the sender settle mode
519 */
520 PN_EXTERN void pn_link_set_snd_settle_mode(pn_link_t *link, pn_snd_settle_mode_t mode);
521
522 /**
523 * Set the local receiver settle mode for a link.
524 *
525 * @param[in] link a link object
526 * @param[in] mode the receiver settle mode
527 */
528 PN_EXTERN void pn_link_set_rcv_settle_mode(pn_link_t *link, pn_rcv_settle_mode_t mode);
529
530 /**
531 * Get the remote sender settle mode for a link.
532 *
533 * @param[in] link a link object
534 * @return the remote sender settle mode
535 */
536 PN_EXTERN pn_snd_settle_mode_t pn_link_remote_snd_settle_mode(pn_link_t *link);
537
538 /**
539 * Get the remote receiver settle mode for a link.
540 *
541 * @param[in] link a link object
542 * @return the remote receiver settle mode
543 */
544 PN_EXTERN pn_rcv_settle_mode_t pn_link_remote_rcv_settle_mode(pn_link_t *link);
545
546 /**
547 * Get the number of unsettled deliveries for a link.
548 *
549 * @param[in] link a link object
550 * @return the number of unsettled deliveries
551 */
552 PN_EXTERN int pn_link_unsettled(pn_link_t *link);
553
554 /**
555 * Get the first unsettled delivery for a link.
556 *
557 " @param[in] link a link object
558 * @return a pointer to the first unsettled delivery on the link
559 */
560 PN_EXTERN pn_delivery_t *pn_unsettled_head(pn_link_t *link);
561
562 /**
563 * Get the next unsettled delivery on a link.
564 *
565 * @param[in] delivery a delivery object
566 * @return the next unsettled delivery on the link
567 */
568 PN_EXTERN pn_delivery_t *pn_unsettled_next(pn_delivery_t *delivery);
569
570 /**
571 * Signal the availability of deliveries for a link.
572 *
573 * @param[in] sender a sender link object
574 * @param[in] credit the number of deliveries potentially available
575 * for transfer
576 */
577 PN_EXTERN void pn_link_offered(pn_link_t *sender, int credit);
578
579 /**
580 * Send message data for the current delivery on a link.
581 *
582 * @param[in] sender a sender link object
583 * @param[in] bytes the start of the message data
584 * @param[in] n the number of bytes of message data
585 * @return the number of bytes sent, or an error code
586 */
587 PN_EXTERN ssize_t pn_link_send(pn_link_t *sender, const char *bytes, size_t n);
588
589 /**
590 * Grant credit for incoming deliveries on a receiver.
591 *
592 * @param[in] receiver a receiving link object
593 * @param[in] credit the amount to increment the link credit
594 */
595 PN_EXTERN void pn_link_flow(pn_link_t *receiver, int credit);
596
597 /**
598 * Grant credit for incoming deliveries on a receiver, and set drain
599 * mode to true.
600 *
601 * Use ::pn_link_set_drain to set the drain mode explicitly.
602 *
603 * @param[in] receiver a receiving link object
604 * @param[in] credit the amount to increment the link credit
605 */
606 PN_EXTERN void pn_link_drain(pn_link_t *receiver, int credit);
607
608 /**
609 * Set the drain mode on a link.
610 *
611 * @param[in] receiver a receiving link object
612 * @param[in] drain the drain mode
613 */
614 PN_EXTERN void pn_link_set_drain(pn_link_t *receiver, bool drain);
615
616 /**
617 * Receive message data for the current delivery on a link.
618 *
619 * Use ::pn_delivery_pending on the current delivery to figure out how
620 * much buffer space is needed.
621 *
622 * Note that the link API can be used to stream large messages across
623 * the network, so just because there is no data to read does not
624 * imply the message is complete. To ensure the entirety of the
625 * message data has been read, either invoke ::pn_link_recv until
626 * PN_EOS is returned, or verify that
627 *
628 * (!pn_delivery_partial(d) && !pn_delivery_aborted(d) && pn_delivery_pending(d)==0)
629 *
630 * @param[in] receiver a receiving link object
631 * @param[in] bytes a pointer to an empty buffer
632 * @param[in] n the buffer capacity
633 * @return The number of bytes received, or an error code:
634 * - ::PN_EOS: The message has been completely received
635 * - 0: No data available now.
636 * If pn_delivery_partial() there will be further ::PN_DELIVERY events with more data.
637 * - ::PN_STATE_ERR: The link has no current delivery
638 * - ::PN_ABORTED: See pn_delivery_aborted()
639 */
640 PN_EXTERN ssize_t pn_link_recv(pn_link_t *receiver, char *bytes, size_t n);
641
642 /**
643 * Check if a link is currently draining.
644 *
645 * A link is defined to be draining when drain mode is set to true,
646 * and the sender still has excess credit.
647 *
648 * @param[in] receiver a receiving link object
649 * @return true if the link is currently draining, false otherwise
650 */
651 PN_EXTERN bool pn_link_draining(pn_link_t *receiver);
652
653 /**
654 * **Unsettled API** - Get the maximum message size for a link.
655 *
656 * A zero maximum message size means the size is unlimited.
657 *
658 * @param[in] link a link object
659 * @return the maximum message size for a link.
660 */
661 PN_EXTERN uint64_t pn_link_max_message_size(pn_link_t *link);
662
663 /**
664 * **Unsettled API** - Set the maximum message size for a link.
665 *
666 * A zero maximum message size means the size is unlimited.
667 *
668 * @param[in] link a link object
669 * @param[in] size the maximum message size for the link
670 */
671 PN_EXTERN void pn_link_set_max_message_size(pn_link_t *link, uint64_t size);
672
673 /**
674 * **Unsettled API** - Get the remote view of the maximum message size for a link.
675 *
676 * A zero maximum message size means the size is unlimited.
677 *
678 * @param[in] link a link object
679 * @return the remote view of the maximum message size for a link
680 */
681 PN_EXTERN uint64_t pn_link_remote_max_message_size(pn_link_t *link);
682
683 /**
684 * Access/modify the AMQP properties data for a link object.
685 *
686 * This operation will return a pointer to a ::pn_data_t object that is valid
687 * until the link object is freed. Any data contained by the ::pn_data_t object
688 * will be sent as the AMQP properties for the link object when the link is
689 * opened by calling ::pn_link_open. Note that this MUST take the form of a
690 * symbol keyed map to be valid.
691 *
692 * The ::pn_data_t pointer returned is valid until the link object is freed.
693 *
694 * @param[in] link the link object
695 * @return a pointer to a pn_data_t representing the link properties
696 */
697 PN_EXTERN pn_data_t *pn_link_properties(pn_link_t *link);
698
699 /**
700 * Access the AMQP link properties supplied by the remote link endpoint.
701 *
702 * This operation will return a pointer to a ::pn_data_t object that
703 * is valid until the link object is freed. This data object
704 * will be empty until the remote link is opened as indicated by
705 * the ::PN_REMOTE_ACTIVE flag.
706 *
707 * @param[in] link the link object
708 * @return the remote link properties
709 */
710 PN_EXTERN pn_data_t *pn_link_remote_properties(pn_link_t *link);
711
712 /**
713 * @}
714 */
715
716 #ifdef __cplusplus
717 }
718 #endif
719
720 #endif /* link.h */
721