| 1 |
#ifndef PROTON_RAW_CONNECTION_H |
| 2 |
#define PROTON_RAW_CONNECTION_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 |
* @addtogroup raw_connection |
| 36 |
* @{ |
| 37 |
*/ |
| 38 |
|
| 39 |
/** |
| 40 |
* A descriptor used to represent a single raw buffer in memory. |
| 41 |
* |
| 42 |
* @note The intent of the offset is to allow the actual bytes being read/written to be at a variable |
| 43 |
* location relative to the head of the buffer because of other data or structures that are important to the application |
| 44 |
* associated with the data to be written but not themselves read/written to the connection. |
| 45 |
* |
| 46 |
* @note For read buffers: When read buffers are returned to the application size will be the number of bytes read. |
| 47 |
* Read operations will not change the context, bytes or capacity members of the structure. |
| 48 |
* |
| 49 |
* @note For write buffers: When write buffers are returned to the application all of the struct members will be returned |
| 50 |
* unaltered. Also write operations will not modify the bytes of the buffer passed in at all. In principle this means that |
| 51 |
* the write buffer can be used for multiple writes at the same time as long as the actual buffer is unmodified by the |
| 52 |
* application at any time the buffer is being used by any raw connection. |
| 53 |
*/ |
| 54 |
typedef struct pn_raw_buffer_t { |
| 55 |
uintptr_t context; /**< Used to associate arbitrary application data with this raw buffer */ |
| 56 |
char *bytes; /**< Pointer to the start of the raw buffer, if this is null then no buffer is represented */ |
| 57 |
uint32_t capacity; /**< Count of available bytes starting at @ref bytes */ |
| 58 |
uint32_t size; /**< Number of bytes read or to be written starting at @ref offset */ |
| 59 |
uint32_t offset; /**< First byte in the buffer to be read or written */ |
| 60 |
} pn_raw_buffer_t; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Create a new raw connection for use with the @ref proactor. |
| 65 |
* See @ref pn_proactor_raw_connect and @ref pn_listener_raw_accept. |
| 66 |
* |
| 67 |
* @return A newly allocated pn_raw_connection_t or NULL if there wasn't sufficient memory. |
| 68 |
* |
| 69 |
* @note This is the only pn_raw_connection_t function that allocates memory. So an application that |
| 70 |
* wants good control of out of memory conditions should check the return value for NULL. |
| 71 |
* |
| 72 |
* @note It would be a good practice is to create a raw connection and attach application |
| 73 |
* specific context to it before giving it to the proactor. |
| 74 |
* |
| 75 |
* @note There is no way to free a pn_raw_connection_t as once passed to the proactor the proactor owns |
| 76 |
* it and controls its lifecycle. |
| 77 |
*/ |
| 78 |
PNP_EXTERN pn_raw_connection_t *pn_raw_connection(void); |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the local address of a raw connection. Return `NULL` if not available. |
| 82 |
* Pointer is invalid after the transport closes (@ref PN_RAW_CONNECTION_DISCONNECTED event is handled) |
| 83 |
*/ |
| 84 |
PNP_EXTERN const struct pn_netaddr_t *pn_raw_connection_local_addr(pn_raw_connection_t *connection); |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the local address of a raw connection. Return `NULL` if not available. |
| 88 |
* Pointer is invalid after the transport closes (@ref PN_RAW_CONNECTION_DISCONNECTED event is handled) |
| 89 |
*/ |
| 90 |
PNP_EXTERN const struct pn_netaddr_t *pn_raw_connection_remote_addr(pn_raw_connection_t *connection); |
| 91 |
|
| 92 |
/** |
| 93 |
* Close a raw connection. |
| 94 |
* This will flush any buffers to be written; close the underlying socket and release all buffers held by the |
| 95 |
* raw connection. |
| 96 |
* |
| 97 |
* It will cause @ref PN_RAW_CONNECTION_READ and @ref PN_RAW_CONNECTION_WRITTEN to be emitted so |
| 98 |
* the application can clean up buffers given to the raw connection. After that a |
| 99 |
* @ref PN_RAW_CONNECTION_DISCONNECTED event will be emitted to allow the application to clean up |
| 100 |
* any other state held by the raw connection. |
| 101 |
* |
| 102 |
*/ |
| 103 |
PNP_EXTERN void pn_raw_connection_close(pn_raw_connection_t *connection); |
| 104 |
|
| 105 |
/** |
| 106 |
* Shutdown a raw connection for reading. |
| 107 |
* This will close the underlying socket for reading and release all empty read buffers held by the raw connection. |
| 108 |
* |
| 109 |
* It will cause @ref PN_RAW_CONNECTION_READ to be emitted so the application can clean up buffers given to the raw |
| 110 |
* connection. Note that these buffers may still also contain data read from the socket but not yet consumed by the |
| 111 |
* application. |
| 112 |
* |
| 113 |
* If @ref pn_raw_connection_write_close() has already been called then @ref PN_RAW_CONNECTION_DISCONNECTED will then |
| 114 |
* also be emitted. |
| 115 |
* |
| 116 |
* In order to fully close a raw connection the application will need to either call @ref pn_raw_connection_close() |
| 117 |
* or @ref pn_raw_connection_write_close() after it calls @ref pn_raw_connection_read_close(). |
| 118 |
*/ |
| 119 |
PNP_EXTERN void pn_raw_connection_read_close(pn_raw_connection_t *connection); |
| 120 |
|
| 121 |
/** |
| 122 |
* Shutdown a raw connection for writing. |
| 123 |
* This will flush any buffers to be written to the socket; close the underlying socket for writing and release all |
| 124 |
* write buffers held by the raw connection. |
| 125 |
* |
| 126 |
* It will cause @ref PN_RAW_CONNECTION_WRITTEN to be emitted so the application can clean up write buffers given to |
| 127 |
* the raw connection. |
| 128 |
* |
| 129 |
* If @ref pn_raw_connection_read_close() has already been called then @ref PN_RAW_CONNECTION_DISCONNECTED will then |
| 130 |
* also be emitted. |
| 131 |
* |
| 132 |
* In order to fully close a raw connection the application will need to either call @ref pn_raw_connection_close() |
| 133 |
* or @ref pn_raw_connection_read_close() after it calls @ref pn_raw_connection_write_close(). |
| 134 |
*/ |
| 135 |
PNP_EXTERN void pn_raw_connection_write_close(pn_raw_connection_t *connection); |
| 136 |
|
| 137 |
/** |
| 138 |
* Query the raw connection for how many more read buffers it can be given |
| 139 |
*/ |
| 140 |
PNP_EXTERN size_t pn_raw_connection_read_buffers_capacity(pn_raw_connection_t *connection); |
| 141 |
|
| 142 |
/** |
| 143 |
* Query the raw connection for how many more write buffers it can be given |
| 144 |
*/ |
| 145 |
PNP_EXTERN size_t pn_raw_connection_write_buffers_capacity(pn_raw_connection_t *connection); |
| 146 |
|
| 147 |
/** |
| 148 |
* Give the raw connection buffers to use for reading from the underlying socket. |
| 149 |
* If the raw socket has no read buffers then the application will never receive |
| 150 |
* a @ref PN_RAW_CONNECTION_READ event. |
| 151 |
* |
| 152 |
* A @ref PN_RAW_CONNECTION_NEED_READ_BUFFERS event will be generated immediately after |
| 153 |
* the @ref PN_RAW_CONNECTION_CONNECTED event if there are no read buffers. It will also be |
| 154 |
* generated whenever the raw connection runs out of read buffers. In both these cases the |
| 155 |
* event will not be generated again until @ref pn_raw_connection_give_read_buffers is called. |
| 156 |
* |
| 157 |
* @return the number of buffers actually given to the raw connection. This will only be different |
| 158 |
* from the number supplied if the connection has no more space to record more buffers. In this case |
| 159 |
* the buffers taken will be the earlier buffers in the array supplied, the elements 0 to the |
| 160 |
* returned value-1. |
| 161 |
* |
| 162 |
* @note The buffers given to the raw connection are owned by it until the application |
| 163 |
* receives the buffers back with a @ref pn_raw_connection_take_read_buffers call. They must |
| 164 |
* not be accessed at all (written or even read) from calling @ref pn_raw_connection_give_read_buffers |
| 165 |
* until receiving them back. |
| 166 |
* |
| 167 |
* @note The application should not assume that the @ref PN_RAW_CONNECTION_NEED_READ_BUFFERS |
| 168 |
* event signifies that the connection is readable. |
| 169 |
*/ |
| 170 |
PNP_EXTERN size_t pn_raw_connection_give_read_buffers(pn_raw_connection_t *connection, pn_raw_buffer_t const *buffers, size_t num); |
| 171 |
|
| 172 |
/** |
| 173 |
* Fetch buffers with bytes read from the raw socket |
| 174 |
* |
| 175 |
* The buffers will be placed in the buffers array in the order in which they were read. So the first buffer in the array will contain the |
| 176 |
* first bytes read; the second buffer the next bytes etc. |
| 177 |
* |
| 178 |
* @param[out] buffers pointer to an array of @ref pn_raw_buffer_t structures which will be filled in with the read buffer information |
| 179 |
* @param[in] num the number of buffers allocated in the passed in array of buffers |
| 180 |
* @return the number of buffers being returned, if there are no read bytes then this will be 0. As many buffers will be returned |
| 181 |
* as can be given the number that are passed in. So if the number returned is less than the number passed in there are no more buffers |
| 182 |
* read. But if the number is the same there may be more read buffers to take. |
| 183 |
* |
| 184 |
* @note After the application receives @ref PN_RAW_CONNECTION_READ there should be bytes read from the socket and |
| 185 |
* hence this call should return buffers. It is safe to carry on calling @ref pn_raw_connection_take_read_buffers |
| 186 |
* until it returns 0. |
| 187 |
*/ |
| 188 |
PNP_EXTERN size_t pn_raw_connection_take_read_buffers(pn_raw_connection_t *connection, pn_raw_buffer_t *buffers, size_t num); |
| 189 |
|
| 190 |
/** |
| 191 |
* Give the raw connection buffers to write to the underlying socket. |
| 192 |
* |
| 193 |
* The buffers will be written to the connection in the order that they are passed in. That is the first buffer in the array of buffers |
| 194 |
* passed will be the first buffer written to the connection; the second buffer passed in will be the second written etc. |
| 195 |
* |
| 196 |
* A @ref PN_RAW_CONNECTION_WRITTEN event will be generated once the buffers have been written to the socket |
| 197 |
* until this point the buffers must not be accessed at all (written or even read). |
| 198 |
* |
| 199 |
* A @ref PN_RAW_CONNECTION_NEED_WRITE_BUFFERS event will be generated immediately after |
| 200 |
* the @ref PN_RAW_CONNECTION_CONNECTED event if there are no write buffers. It will also be |
| 201 |
* generated whenever the raw connection finishes writing all the write buffers. In both these cases the |
| 202 |
* event will not be generated again until @ref pn_raw_connection_write_buffers is called. |
| 203 |
* |
| 204 |
* @return the number of buffers actually recorded by the raw connection to write. This will only be different |
| 205 |
* from the number supplied if the connection has no more space to record more buffers. In this case |
| 206 |
* the buffers recorded will be the earlier buffers in the array supplied, the elements 0 to the |
| 207 |
* returned value-1. |
| 208 |
* |
| 209 |
*/ |
| 210 |
PNP_EXTERN size_t pn_raw_connection_write_buffers(pn_raw_connection_t *connection, pn_raw_buffer_t const *buffers, size_t num); |
| 211 |
|
| 212 |
/** |
| 213 |
* Return a buffer chain with buffers that have all been written to the raw socket |
| 214 |
* |
| 215 |
* @param[out] buffers pointer to an array of @ref pn_raw_buffer_t structures which will be filled in with the written buffer information |
| 216 |
* @param[in] num the number of buffers allocated in the passed in array of buffers |
| 217 |
* @return the number of buffers being returned, if there is are no written buffers to return then this will be 0. As many buffers will be returned |
| 218 |
* as can be given the number that are passed in. So if the number returned is less than the number passed in there are no more buffers |
| 219 |
* written. But if the number is the same there may be more written buffers to take. |
| 220 |
* |
| 221 |
* @note After the application receives @ref PN_RAW_CONNECTION_WRITTEN there should be bytes written to the socket and |
| 222 |
* hence this call should return buffers. It is safe to carry on calling @ref pn_raw_connection_take_written_buffers |
| 223 |
* until it returns 0. |
| 224 |
* |
| 225 |
* @note The buffers will be returned in the same order as they were originally passed in. |
| 226 |
*/ |
| 227 |
PNP_EXTERN size_t pn_raw_connection_take_written_buffers(pn_raw_connection_t *connection, pn_raw_buffer_t *buffers, size_t num); |
| 228 |
|
| 229 |
/** |
| 230 |
* Is @p connection closed for read? |
| 231 |
* |
| 232 |
* @return true if the raw connection is closed for read. |
| 233 |
*/ |
| 234 |
PNP_EXTERN bool pn_raw_connection_is_read_closed(pn_raw_connection_t *connection); |
| 235 |
|
| 236 |
/** |
| 237 |
* Is @p connection closed for write? |
| 238 |
* |
| 239 |
* @return true if the raw connection is closed for write. |
| 240 |
*/ |
| 241 |
PNP_EXTERN bool pn_raw_connection_is_write_closed(pn_raw_connection_t *connection); |
| 242 |
|
| 243 |
/** |
| 244 |
* Return a @ref PN_RAW_CONNECTION_WAKE event for @p connection as soon as possible. |
| 245 |
* |
| 246 |
* At least one wake event will be returned, serialized with other @ref proactor_events |
| 247 |
* for the same raw connection. Wakes can be "coalesced" - if several |
| 248 |
* @ref pn_raw_connection_wake() calls happen close together, there may be only one |
| 249 |
* @ref PN_RAW_CONNECTION_WAKE event that occurs after all of them. |
| 250 |
* |
| 251 |
* @note Thread-safe |
| 252 |
*/ |
| 253 |
PNP_EXTERN void pn_raw_connection_wake(pn_raw_connection_t *connection); |
| 254 |
|
| 255 |
/** |
| 256 |
* Get additional information about a raw connection error. |
| 257 |
* There is a raw connection error if the @ref PN_RAW_CONNECTION_DISCONNECTED event |
| 258 |
* is received and the pn_condition_t associated is non null (@see pn_condition_is_set). |
| 259 |
* |
| 260 |
* The value returned is only valid until the end of handler for the |
| 261 |
* @ref PN_RAW_CONNECTION_DISCONNECTED event. |
| 262 |
*/ |
| 263 |
PNP_EXTERN pn_condition_t *pn_raw_connection_condition(pn_raw_connection_t *connection); |
| 264 |
|
| 265 |
/** |
| 266 |
* Get the application context associated with this raw connection. |
| 267 |
* |
| 268 |
* The application context for a raw connection may be set using |
| 269 |
* ::pn_raw_connection_set_context. |
| 270 |
* |
| 271 |
* @param[in] connection the raw connection whose context is to be returned. |
| 272 |
* @return the application context for the raw connection |
| 273 |
*/ |
| 274 |
PNP_EXTERN void *pn_raw_connection_get_context(pn_raw_connection_t *connection); |
| 275 |
|
| 276 |
/** |
| 277 |
* Set a new application context for a raw connection. |
| 278 |
* |
| 279 |
* The application context for a raw connection may be retrieved |
| 280 |
* using ::pn_raw_connection_get_context. |
| 281 |
* |
| 282 |
* @param[in] connection the raw connection object |
| 283 |
* @param[in] context the application context |
| 284 |
*/ |
| 285 |
PNP_EXTERN void pn_raw_connection_set_context(pn_raw_connection_t *connection, void *context); |
| 286 |
|
| 287 |
/** |
| 288 |
* Get the attachments that are associated with a raw connection. |
| 289 |
*/ |
| 290 |
PNP_EXTERN pn_record_t *pn_raw_connection_attachments(pn_raw_connection_t *connection); |
| 291 |
|
| 292 |
/** |
| 293 |
* Return the raw connection associated with an event. |
| 294 |
* |
| 295 |
* @return NULL if the event is not associated with a raw connection. |
| 296 |
*/ |
| 297 |
PNP_EXTERN pn_raw_connection_t *pn_event_raw_connection(pn_event_t *event); |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* @} |
| 302 |
*/ |
| 303 |
|
| 304 |
#ifdef __cplusplus |
| 305 |
} |
| 306 |
#endif |
| 307 |
|
| 308 |
#endif /* raw_connection.h */ |