Debian Codegraph @

c/include/proton/proactor.h

raw
1 #ifndef PROTON_PROACTOR_H
2 #define PROTON_PROACTOR_H 1
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23 #include <proton/condition.h>
24 #include <proton/event.h>
25 #include <proton/import_export.h>
26 #include <proton/types.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /**
33 * @file
34 *
35 * @copybrief proactor
36 *
37 * @addtogroup proactor
38 * @{
39 *
40 * The proactor associates an abstract AMQP protocol @ref connection
41 * with a concrete IO @ref transport implementation for outgoing and
42 * incoming connections. pn_proactor_wait() returns @ref
43 * proactor_events to application threads for handling.
44 *
45 * The `pn_proactor_*` functions are thread-safe, but to handle @ref
46 * proactor_events you must also use the @ref core APIs, which are
47 * not. @ref core objects associated with different connections can be
48 * used concurrently, but objects associated with a single connection
49 * can only be used from their own thread.
50 *
51 * The proactor *serializes* @ref proactor_events for each connection
52 * - it never returns @ref proactor_events for the same connection
53 * concurrently in different threads. Event-handling code can safely
54 * use any @ref core object obtained from the current event. You can
55 * attach application data to @ref core objects (for example with
56 * pn_connection_attachments()).
57 *
58 * pn_connection_wake() allows any thread to "wake up" a
59 * connection. It causes pn_proactor_wait() to return a @ref
60 * PN_CONNECTION_WAKE event that is serialized with the connection's
61 * other @ref proactor_events. You can use this to implement
62 * communication between different connections, or from non-proactor
63 * threads.
64 *
65 * Serialization and pn_connection_wake() simplify building
66 * applications with a shared thread pool, which serialize work per
67 * connection. Many other variations are possible, but you are
68 * responsible for any additional synchronization needed.
69 */
70
71 /**
72 * Size of buffer that can hold the largest connection or listening address.
73 */
74 #define PN_MAX_ADDR 1060
75
76 /**
77 * Format a host:port address string for pn_proactor_connect() or pn_proactor_listen()
78 *
79 * @param[out] addr address is copied to this buffer, with trailing '\0'
80 * @param[in] size size of addr buffer
81 * @param[in] host network host name, DNS name or IP address
82 * @param[in] port network service name or decimal port number, e.g. "amqp" or "5672"
83 * @return the length of network address (excluding trailing '\0'), if >= size
84 * then the address was truncated
85 */
86 PNP_EXTERN int pn_proactor_addr(char *addr, size_t size, const char *host, const char *port);
87
88 /**
89 * Create a proactor. Must be freed with pn_proactor_free()
90 */
91 PNP_EXTERN pn_proactor_t *pn_proactor(void);
92
93 /**
94 * Free the proactor. Abort open connections/listeners, clean up all resources.
95 */
96 PNP_EXTERN void pn_proactor_free(pn_proactor_t *proactor);
97
98 /**
99 * Connect @p transport to @p addr and bind to @p connection.
100 * Errors are returned as @ref PN_TRANSPORT_CLOSED events by pn_proactor_wait().
101 *
102 * @note Thread-safe
103 *
104 * @param[in] proactor the proactor object
105 *
106 * @param[in] connection If NULL a new connection is created.
107 * @p proactor *takes ownership* of @p connection and will
108 * automatically call pn_connection_free() after the final @ref
109 * PN_TRANSPORT_CLOSED event is handled, or when pn_proactor_free() is
110 * called. You can prevent the automatic free with
111 * pn_proactor_release_connection()
112 *
113 * @param[in] transport If NULL a new transport is created.
114 * @p proactor *takes ownership* of @p transport, it will be freed even
115 * if pn_proactor_release_connection() is called.
116 *
117 * @param[in] addr the "host:port" network address, constructed by pn_proactor_addr()
118 * An empty host will connect to the local host via the default protocol (IPV6 or IPV4).
119 * An empty port will connect to the standard AMQP port (5672).
120 *
121 */
122 PNP_EXTERN void pn_proactor_connect2(pn_proactor_t *proactor, pn_connection_t *connection, pn_transport_t *transport, const char *addr);
123
124 /**
125 * **Deprecated** - Use ::pn_proactor_connect2()
126 */
127 PNP_EXTERN void pn_proactor_connect(pn_proactor_t *proactor, pn_connection_t *connection, const char *addr);
128
129 /**
130 * Start listening for incoming connections.
131 *
132 * pn_proactor_wait() will return a @ref PN_LISTENER_OPEN event when the
133 * listener is ready to accept connections, or a PN_LISTENER_CLOSE if the listen
134 * operation fails. If the listen failed, pn_listener_condition() will be set.
135 *
136 * When the listener is closed by pn_listener_close(), or because of an error, a
137 * PN_LISTENER_CLOSE event will be returned and pn_listener_condition() will be set
138 * for an error.
139 *
140 * @note Thread-safe
141 *
142 * @param[in] proactor the proactor object
143 *
144 * @param[in] listener @p proactor *takes ownership* of @p listener, and will
145 * automatically call pn_listener_free() after the final PN_LISTENER_CLOSE event
146 * is handled, or when pn_proactor_free() is called.
147 *
148 * @param[in] addr the "host:port" network address, constructed by pn_proactor_addr()
149 * An empty host will listen for all protocols (IPV6 and IPV4) on all local interfaces.
150 * An empty port will listen on the standard AMQP port (5672).
151 *
152 * @param[in] backlog of un-handled connection requests to allow before refusing
153 * connections. If @p addr resolves to multiple interface/protocol combinations,
154 * the backlog applies to each separately.
155 */
156 PNP_EXTERN void pn_proactor_listen(pn_proactor_t *proactor, pn_listener_t *listener, const char *addr, int backlog);
157
158 /**
159 * Disconnect all connections and listeners belonging to the proactor.
160 *
161 * @ref PN_LISTENER_CLOSE, @ref PN_TRANSPORT_CLOSED and other @ref proactor_events are
162 * generated as usual.
163 *
164 * A @ref PN_PROACTOR_INACTIVE event will be generated when all connections and
165 * listeners are disconnected and no timeout is pending. The event will also be
166 * generated if there are no listeners, connections or timeout when
167 * pn_proactor_disconnect() is called.
168 *
169 * Creating new connections and listeners after this call and before the
170 * PN_PROACTOR_INACTIVE event may prevent the proactor from becoming inactive.
171 * After the PN_PROACTOR_INACTIVE event, the proactor can be used normally.
172 *
173 * @note Thread-safe
174 *
175 * @param proactor the proactor
176 *
177 * @param condition if not NULL the condition data is copied to each
178 * disconnected transports and listener and is available in the close event.
179 */
180 PNP_EXTERN void pn_proactor_disconnect(pn_proactor_t *proactor, pn_condition_t *condition);
181
182 /**
183 * Wait until there are @ref proactor_events to handle.
184 *
185 * You must call pn_proactor_done() when you are finished with the batch, you
186 * must not use the batch pointer after calling pn_proactor_done().
187 *
188 * Normally it is most efficient to handle the entire batch in the calling
189 * thread and then call pn_proactor_done(), but see pn_proactor_done() for more options.
190 *
191 * pn_proactor_get() is a non-blocking version of this call.
192 *
193 * @note Thread-safe
194 *
195 * @return a non-empty batch of events that must be processed in sequence.
196 *
197 */
198 PNP_EXTERN pn_event_batch_t *pn_proactor_wait(pn_proactor_t *proactor);
199
200 /**
201 * Return @ref proactor_events if any are available immediately. If not, return NULL.
202 * If the return value is not NULL, the behavior is the same as pn_proactor_wait()
203 *
204 * @note Thread-safe
205 */
206 PNP_EXTERN pn_event_batch_t *pn_proactor_get(pn_proactor_t *proactor);
207
208 /**
209 * Remove the next event from the batch and return
210 * it. NULL means the batch is empty. The returned event pointer is
211 * valid until pn_event_batch_next() is called again on the same
212 * batch.
213 *
214 * @note there is deliberately no peek(), more() or other look-ahead on an event
215 * batch. We want to know exactly which events have been handled, next() only
216 * allows the user to get each event exactly once, in order.
217 */
218 PNP_EXTERN pn_event_t *pn_event_batch_next(pn_event_batch_t *batch);
219
220 /**
221 * Call when finished handling a batch of events.
222 *
223 * Must be called exactly once to match each call to pn_proactor_wait().
224 *
225 * @note Thread-safe: May be called from any thread provided the exactly once
226 * rule is respected.
227 */
228 PNP_EXTERN void pn_proactor_done(pn_proactor_t *proactor, pn_event_batch_t *events);
229
230 /**
231 * Return a @ref PN_PROACTOR_INTERRUPT event as soon as possible.
232 *
233 * At least one PN_PROACTOR_INTERRUPT event will be returned after this call.
234 * Interrupts can be "coalesced" - if several pn_proactor_interrupt() calls
235 * happen close together, there may be only one PN_PROACTOR_INTERRUPT event that
236 * occurs after all of them.
237 *
238 * @note Thread-safe and async-signal-safe: can be called in a signal handler.
239 * This is the only pn_proactor function that is async-signal-safe.
240 */
241 PNP_EXTERN void pn_proactor_interrupt(pn_proactor_t *proactor);
242
243 /**
244 * Return a @ref PN_PROACTOR_TIMEOUT after @p timeout milliseconds elapse. If no
245 * threads are blocked in pn_proactor_wait() when the timeout elapses, the event
246 * will be delivered to the next available thread.
247 *
248 * Calling pn_proactor_set_timeout() again before the PN_PROACTOR_TIMEOUT
249 * is delivered replaces the previous timeout value.
250 *
251 * @note Thread-safe
252 */
253 PNP_EXTERN void pn_proactor_set_timeout(pn_proactor_t *proactor, pn_millis_t timeout);
254
255 /**
256 * Cancel the pending timeout set by pn_proactor_set_timeout(). Does nothing
257 * if no timeout is set.
258 *
259 * @note Thread-safe
260 */
261 PNP_EXTERN void pn_proactor_cancel_timeout(pn_proactor_t *proactor);
262
263 /**
264 * Release ownership of @p connection, disassociate it from its proactor.
265 *
266 * The connection and related objects (@ref session "sessions", @ref link "links"
267 * and so on) remain intact, but the transport is closed and unbound. The
268 * proactor will not return any more events for this connection. The caller must
269 * call pn_connection_free(), either directly or indirectly by re-using @p
270 * connection in another call to pn_proactor_connect() or pn_proactor_listen().
271 *
272 * @note **Not thread-safe**. Call this function from a connection
273 * event handler.
274 *
275 * @note If @p connection does not belong to a proactor, this call does nothing.
276 *
277 * @note This has nothing to do with pn_connection_release().
278 */
279 PNP_EXTERN void pn_proactor_release_connection(pn_connection_t *connection);
280
281 /**
282 * Return a @ref PN_CONNECTION_WAKE event for @p connection as soon as possible.
283 *
284 * At least one wake event will be returned, serialized with other @ref proactor_events
285 * for the same connection. Wakes can be "coalesced" - if several
286 * pn_connection_wake() calls happen close together, there may be only one
287 * PN_CONNECTION_WAKE event that occurs after all of them.
288 *
289 * @note If @p connection does not belong to a proactor, this call does nothing.
290 *
291 * @note Thread-safe
292 */
293 PNP_EXTERN void pn_connection_wake(pn_connection_t *connection);
294
295 /**
296 * Return the proactor associated with a connection.
297 *
298 * @note **Not thread-safe**
299 *
300 * @return the proactor or NULL if the connection does not belong to a proactor.
301 */
302 PNP_EXTERN pn_proactor_t *pn_connection_proactor(pn_connection_t *connection);
303
304 /**
305 * Return the proactor associated with an event.
306 *
307 * @note **Not thread-safe**
308 *
309 * @return the proactor or NULL if the connection does not belong to a proactor.
310 */
311 PNP_EXTERN pn_proactor_t *pn_event_proactor(pn_event_t *event);
312
313 /**
314 * @deprecated Use ::pn_proactor_now_64()
315 *
316 * Get the real elapsed time since an arbitrary point in the past in milliseconds.
317 *
318 * This may be used as a portable way to get a process-local timestamp for the
319 * current time. It is monotonically increasing and will never go backwards.
320 *
321 * Note: this is not a suitable value for an AMQP timestamp to be sent as part
322 * of a message. Such a timestamp should use the real time in milliseconds
323 * since the epoch.
324 *
325 * @note Thread-safe
326 */
327 PNP_EXTERN pn_millis_t pn_proactor_now(void);
328
329 /**
330 * Get the real elapsed time since an arbitrary point in the past in milliseconds.
331 *
332 * This may be used as a portable way to get a process-local timestamp for the
333 * current time. It is monotonically increasing and will never go backwards.
334 *
335 * Note: this is not a suitable value for an AMQP timestamp to be sent as part
336 * of a message. Such a timestamp should use the real time in milliseconds
337 * since the epoch.
338 *
339 * @note Thread-safe
340 */
341 PNP_EXTERN int64_t pn_proactor_now_64(void);
342
343 /**
344 * Connect @p addr and bind to @p raw_connection.
345 *
346 * Errors are returned as @ref PN_RAW_CONNECTION_DISCONNECTED events by pn_proactor_wait().
347 *
348 * @note Thread-safe
349 *
350 * @param[in] proactor the proactor
351 *
352 * @param[in] raw_connection the application must create a raw connection with pn_raw_connection()
353 * this parameter cannot be null.
354 *
355 * @p proactor *takes ownership* of @p raw_connection and will
356 * automatically call pn_raw_connection_free() after the final @ref
357 * PN_RAW_CONNECTION_DISCONNECTED event is handled, or when pn_proactor_free() is
358 * called.
359 *
360 * @param[in] addr the "host:port" network address, constructed by pn_proactor_addr()
361 * An empty host will connect to the local host via the default protocol (IPV6 or IPV4).
362 * An empty port will connect to the standard AMQP port (5672).
363 */
364 PNP_EXTERN void pn_proactor_raw_connect(pn_proactor_t *proactor, pn_raw_connection_t *raw_connection, const char *addr);
365
366 /**
367 * @}
368 */
369
370 /**
371 * pn_proactor_wait() returns a subset of the event types defined by
372 * @ref pn_event_type_t. The PN_REACTOR_\*, PN_SELECTABLE_\*, and
373 * PN_\*_FINAL events are not returned.
374 *
375 * @addtogroup proactor_events
376 * @{
377 *
378 * Enumeration | Brief description, see @ref pn_event_type_t for more
379 * :-- | :--
380 * @ref PN_CONNECTION_INIT | @copybrief PN_CONNECTION_INIT
381 * @ref PN_CONNECTION_BOUND | @copybrief PN_CONNECTION_BOUND
382 * @ref PN_TIMER_TASK | @copybrief PN_TIMER_TASK
383 * @ref PN_CONNECTION_INIT | @copybrief PN_CONNECTION_INIT
384 * @ref PN_CONNECTION_BOUND | @copybrief PN_CONNECTION_BOUND
385 * @ref PN_CONNECTION_UNBOUND | @copybrief PN_CONNECTION_UNBOUND
386 * @ref PN_CONNECTION_LOCAL_OPEN | @copybrief PN_CONNECTION_LOCAL_OPEN
387 * @ref PN_CONNECTION_REMOTE_OPEN | @copybrief PN_CONNECTION_REMOTE_OPEN
388 * @ref PN_CONNECTION_LOCAL_CLOSE | @copybrief PN_CONNECTION_LOCAL_CLOSE
389 * @ref PN_CONNECTION_REMOTE_CLOSE | @copybrief PN_CONNECTION_REMOTE_CLOSE
390 * @ref PN_SESSION_INIT | @copybrief PN_SESSION_INIT
391 * @ref PN_SESSION_LOCAL_OPEN | @copybrief PN_SESSION_LOCAL_OPEN
392 * @ref PN_SESSION_REMOTE_OPEN | @copybrief PN_SESSION_REMOTE_OPEN
393 * @ref PN_SESSION_LOCAL_CLOSE | @copybrief PN_SESSION_LOCAL_CLOSE
394 * @ref PN_SESSION_REMOTE_CLOSE | @copybrief PN_SESSION_REMOTE_CLOSE
395 * @ref PN_LINK_INIT | @copybrief PN_LINK_INIT
396 * @ref PN_LINK_LOCAL_OPEN | @copybrief PN_LINK_LOCAL_OPEN
397 * @ref PN_LINK_REMOTE_OPEN | @copybrief PN_LINK_REMOTE_OPEN
398 * @ref PN_LINK_LOCAL_CLOSE | @copybrief PN_LINK_LOCAL_CLOSE
399 * @ref PN_LINK_REMOTE_CLOSE | @copybrief PN_LINK_REMOTE_CLOSE
400 * @ref PN_LINK_LOCAL_DETACH | @copybrief PN_LINK_LOCAL_DETACH
401 * @ref PN_LINK_REMOTE_DETACH | @copybrief PN_LINK_REMOTE_DETACH
402 * @ref PN_LINK_FLOW | @copybrief PN_LINK_FLOW
403 * @ref PN_DELIVERY | @copybrief PN_DELIVERY
404 * @ref PN_TRANSPORT | @copybrief PN_TRANSPORT
405 * @ref PN_TRANSPORT_AUTHENTICATED | @copybrief PN_TRANSPORT_AUTHENTICATED
406 * @ref PN_TRANSPORT_ERROR | @copybrief PN_TRANSPORT_ERROR
407 * @ref PN_TRANSPORT_HEAD_CLOSED | @copybrief PN_TRANSPORT_HEAD_CLOSED
408 * @ref PN_TRANSPORT_TAIL_CLOSED | @copybrief PN_TRANSPORT_TAIL_CLOSED
409 * @ref PN_TRANSPORT_CLOSED | The final event for a proactor connection, the transport is closed.
410 * @ref PN_LISTENER_OPEN | @copybrief PN_LISTENER_OPEN
411 * @ref PN_LISTENER_ACCEPT | @copybrief PN_LISTENER_ACCEPT
412 * @ref PN_LISTENER_CLOSE | @copybrief PN_LISTENER_CLOSE
413 * @ref PN_PROACTOR_INTERRUPT | @copybrief PN_PROACTOR_INTERRUPT
414 * @ref PN_PROACTOR_TIMEOUT | @copybrief PN_PROACTOR_TIMEOUT
415 * @ref PN_PROACTOR_INACTIVE | @copybrief PN_PROACTOR_INACTIVE
416 * @ref PN_CONNECTION_WAKE | @copybrief PN_CONNECTION_WAKE
417 * @ref PN_RAW_CONNECTION_CONNECTED | @copybrief PN_RAW_CONNECTION_CONNECTED
418 * @ref PN_RAW_CONNECTION_CLOSED_READ | @copybrief PN_RAW_CONNECTION_CLOSED_READ
419 * @ref PN_RAW_CONNECTION_CLOSED_WRITE | @copybrief PN_RAW_CONNECTION_CLOSED_WRITE
420 * @ref PN_RAW_CONNECTION_DISCONNECTED | @copybrief PN_RAW_CONNECTION_DISCONNECTED
421 * @ref PN_RAW_CONNECTION_NEED_READ_BUFFERS | @copybrief PN_RAW_CONNECTION_NEED_READ_BUFFERS
422 * @ref PN_RAW_CONNECTION_NEED_WRITE_BUFFERS | @copybrief PN_RAW_CONNECTION_NEED_WRITE_BUFFERS
423 * @ref PN_RAW_CONNECTION_READ | @copybrief PN_RAW_CONNECTION_READ
424 * @ref PN_RAW_CONNECTION_WRITTEN | @copybrief PN_RAW_CONNECTION_WRITTEN
425 * @ref PN_RAW_CONNECTION_WAKE | @copybrief PN_RAW_CONNECTION_WAKE
426 *
427 * @}
428 */
429
430 #ifdef __cplusplus
431 }
432 #endif
433
434 #endif /* proactor.h */