Debian Codegraph @

c/include/proton/connection_driver.h

raw
1 #ifndef PROTON_CONNECTION_DRIVER_H
2 #define PROTON_CONNECTION_DRIVER_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 /**
24 * @file
25 *
26 * @copybrief connection_driver
27 *
28 * @addtogroup connection_driver
29 * @{
30 *
31 * Associate a @ref connection and @ref transport with AMQP byte
32 * streams from any source.
33 *
34 * - process AMQP-encoded bytes from some input byte stream
35 * - generate ::pn_event_t events for your application to handle
36 * - encode resulting AMQP output bytes for some output byte stream
37 *
38 * The `pn_connection_driver_*` functions provide a simplified API and
39 * extra logic to use ::pn_connection_t and ::pn_transport_t as a
40 * unit. You can also access them directly for features that do not
41 * have `pn_connection_driver_*` functions.
42 *
43 * The driver buffers events and data. You should run it until
44 * pn_connection_driver_finished() is true, to ensure all reading,
45 * writing, and event handling (including `ERROR` and `FINAL` events)
46 * is finished.
47 *
48 * ## Error handling
49 *
50 * The `pn_connection_driver_*` functions do not return an error
51 * code. IO errors are set on the transport condition and are returned
52 * as a `PN_TRANSPORT_ERROR`. The integration code can set errors
53 * using pn_connection_driver_errorf().
54 *
55 * ## IO patterns
56 *
57 * This API supports asynchronous, proactive, non-blocking and
58 * reactive IO. An integration does not have to follow the
59 * dispatch-read-write sequence above, but note that you should handle
60 * all available events before calling
61 * pn_connection_driver_read_buffer() and check that `size` is
62 * non-zero before starting a blocking or asynchronous read call. A
63 * `read` started while there are unprocessed `CLOSE` events in the
64 * buffer may never complete.
65 *
66 * AMQP is a full-duplex, asynchronous protocol. The "read" and
67 * "write" sides of an AMQP connection can close separately.
68 *
69 * ## Thread safety
70 *
71 * The @ref connection_driver types are not thread safe, but each
72 * connection and its associated types form an independent
73 * unit. Different connections can be processed concurrently by
74 * different threads.
75 */
76
77 #include <proton/import_export.h>
78 #include <proton/event.h>
79 #include <proton/types.h>
80
81 #include <stdarg.h>
82
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86
87 /**
88 * The elements needed to drive AMQP IO and events.
89 */
90 typedef struct pn_connection_driver_t {
91 pn_connection_t *connection;
92 pn_transport_t *transport;
93 pn_collector_t *collector;
94 } pn_connection_driver_t;
95
96 /**
97 * Set connection and transport to the provided values, or create a new
98 * @ref pn_connection_t or @ref pn_transport_t if either is NULL.
99 * The provided values belong to the connection driver and will be freed by
100 * pn_connection_driver_destroy().
101 *
102 * The transport is bound automatically after the PN_CONNECTION_INIT has been is
103 * handled by the application. It can be bound earlier with
104 * pn_connection_driver_bind().
105 *
106 * The following functions must be called before the transport is
107 * bound to have effect: pn_connection_set_username(), pn_connection_set_password(),
108 * pn_transport_set_server().
109 *
110 * @return PN_OUT_OF_MEMORY if any allocation fails.
111 */
112 PN_EXTERN int pn_connection_driver_init(pn_connection_driver_t*, pn_connection_t*, pn_transport_t*);
113
114 /**
115 * Force binding of the transport. This happens automatically after
116 * the PN_CONNECTION_INIT is processed.
117 *
118 * @return PN_STATE_ERR if the transport is already bound.
119 */
120 PN_EXTERN int pn_connection_driver_bind(pn_connection_driver_t *d);
121
122 /**
123 * Unbind, release and free the connection and transport. Set all pointers to
124 * NULL. Does not free the @ref pn_connection_driver_t struct itself.
125 */
126 PN_EXTERN void pn_connection_driver_destroy(pn_connection_driver_t *);
127
128 /**
129 * Disassociate the driver's connection from its transport and collector and
130 * sets d->connection = NULL. Returns the previous value, which must be freed
131 * by the caller.
132 *
133 * The transport and collector are still owned by the driver and will be freed by
134 * pn_connection_driver_destroy().
135 *
136 * @note This has nothing to do with pn_connection_release()
137 */
138 PN_EXTERN pn_connection_t *pn_connection_driver_release_connection(pn_connection_driver_t *d);
139
140 /**
141 * Try to get a read buffer with the specified size.
142 *
143 * This will try to grow the read buffer to the specified size and then it will return whatever size
144 * read buffer can be got.
145 *
146 * Copy data from your input byte source to buf.start, up to buf.size.
147 * Call pn_connection_driver_read_done() when reading is complete.
148 *
149 * buf.size==0 means reading is not possible: no buffer space or the read side is closed.
150 */
151 PN_EXTERN pn_rwbytes_t pn_connection_driver_read_buffer_sized(pn_connection_driver_t *, size_t n);
152
153 /**
154 * Get the read buffer.
155 *
156 * Copy data from your input byte source to buf.start, up to buf.size.
157 * Call pn_connection_driver_read_done() when reading is complete.
158 *
159 * buf.size==0 means reading is not possible: no buffer space or the read side is closed.
160 */
161 PN_EXTERN pn_rwbytes_t pn_connection_driver_read_buffer(pn_connection_driver_t *);
162
163 /**
164 * Process the first n bytes of data in pn_connection_driver_read_buffer() and
165 * reclaim the buffer space.
166 */
167 PN_EXTERN void pn_connection_driver_read_done(pn_connection_driver_t *, size_t n);
168
169 /**
170 * Close the read side. Call when the IO can no longer be read.
171 */
172 PN_EXTERN void pn_connection_driver_read_close(pn_connection_driver_t *);
173
174 /**
175 * True if read side is closed.
176 */
177 PN_EXTERN bool pn_connection_driver_read_closed(pn_connection_driver_t *);
178
179 /**
180 * Get the write buffer.
181 *
182 * Write data from buf.start to your IO destination, up to a max of buf.size.
183 * Call pn_connection_driver_write_done() when writing is complete.
184 *
185 * buf.size==0 means there is nothing to write.
186 */
187 PN_EXTERN pn_bytes_t pn_connection_driver_write_buffer(pn_connection_driver_t *);
188
189 /**
190 * Call when the first n bytes of pn_connection_driver_write_buffer() have been
191 * written to IO. Reclaims the buffer space and reset the write buffer.
192 */
193 PN_EXTERN pn_bytes_t pn_connection_driver_write_done(pn_connection_driver_t *, size_t n);
194
195 /**
196 * Close the write side. Call when IO can no longer be written to.
197 */
198 PN_EXTERN void pn_connection_driver_write_close(pn_connection_driver_t *);
199
200 /**
201 * True if write side is closed.
202 */
203 PN_EXTERN bool pn_connection_driver_write_closed(pn_connection_driver_t *);
204
205 /**
206 * Close both sides.
207 */
208 PN_EXTERN void pn_connection_driver_close(pn_connection_driver_t * c);
209
210 /**
211 * Get the next event to handle.
212 *
213 * @return pointer is valid till the next call of
214 * pn_connection_driver_next(). NULL if there are no more events available now,
215 * reading/writing may produce more.
216 */
217 PN_EXTERN pn_event_t* pn_connection_driver_next_event(pn_connection_driver_t *);
218
219 /**
220 * True if pn_connection_driver_next_event() will return a non-NULL event.
221 */
222 PN_EXTERN bool pn_connection_driver_has_event(pn_connection_driver_t *);
223
224 /**
225 * Return true if the the driver is closed for reading and writing and there are
226 * no more events.
227 *
228 * Call pn_connection_driver_destroy() to free all related memory.
229 */
230 PN_EXTERN bool pn_connection_driver_finished(pn_connection_driver_t *);
231
232 /**
233 * Set transport error.
234 *
235 * The name and formatted description are set on the transport condition, and
236 * returned as a PN_TRANSPORT_ERROR event from pn_connection_driver_next_event().
237 *
238 * You must call this *before* pn_connection_driver_read_close() or
239 * pn_connection_driver_write_close() to ensure the error is processed.
240 */
241 PN_EXTERN void pn_connection_driver_errorf(pn_connection_driver_t *d, const char *name, const char *fmt, ...);
242
243 /**
244 * Set transport error via a va_list, see pn_connection_driver_errorf()
245 */
246 PN_EXTERN void pn_connection_driver_verrorf(pn_connection_driver_t *d, const char *name, const char *fmt, va_list);
247
248 /**
249 * The write side of the transport is closed, it will no longer produce bytes to write to
250 * external IO. Synonym for PN_TRANSPORT_HEAD_CLOSED
251 */
252 #define PN_TRANSPORT_WRITE_CLOSED PN_TRANSPORT_HEAD_CLOSED
253
254 /**
255 * The read side of the transport is closed, it will no longer read bytes from external
256 * IO. Alias for PN_TRANSPORT_TAIL_CLOSED
257 */
258 #define PN_TRANSPORT_READ_CLOSED PN_TRANSPORT_TAIL_CLOSED
259
260 /**
261 * **Deprecated** - Use pn_transport_log().
262 */
263 PN_EXTERN void pn_connection_driver_log(pn_connection_driver_t *d, const char *msg);
264
265 /**
266 * **Deprecated** - Use pn_transport_logf().
267 */
268 PN_EXTERN void pn_connection_driver_logf(pn_connection_driver_t *d, const char *fmt, ...);
269
270 /**
271 * **Deprecated** - Use pn_transport_vlogf().
272 */
273 PN_EXTERN void pn_connection_driver_vlogf(pn_connection_driver_t *d, const char *fmt, va_list ap);
274
275 /**
276 * Associate a pn_connection_t with its pn_connection_driver_t.
277 *
278 * **NOTE**: this is only for use by IO integration writers. If you are using the standard
279 * pn_proactor_t you *must not* use this function.
280 *
281 * @return pointer to the pn_connection_driver_t* field in a pn_connection_t.
282 *
283 * Return type is pointer to a pointer so that the caller can (if desired) use
284 * atomic operations when loading and storing the value.
285 */
286 PN_EXTERN pn_connection_driver_t **pn_connection_driver_ptr(pn_connection_t *connection);
287
288 /**
289 * @}
290 */
291
292 #ifdef __cplusplus
293 }
294 #endif
295
296 #endif /* connection_driver.h */