Debian Codegraph @

c/include/proton/selectable.h

raw
1 #ifndef PROTON_SELECTABLE_H
2 #define PROTON_SELECTABLE_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/object.h>
27 #include <proton/event.h>
28 #include <proton/type_compat.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /**
35 * @cond INTERNAL
36 */
37
38 /**
39 * A ::pn_socket_t provides an abstract handle to an IO stream. The
40 * pipe version is uni-directional. The network socket version is
41 * bi-directional. Both are non-blocking.
42 *
43 * pn_socket_t handles from pn_pipe() may only be used with
44 * pn_read(), pn_write(), pn_close() and pn_selector_select().
45 *
46 * pn_socket_t handles from pn_listen(), pn_accept() and
47 * pn_connect() must perform further IO using Proton functions.
48 * Mixing Proton io.h functions with native IO functions on the same
49 * handles will result in undefined behavior.
50 *
51 * pn_socket_t handles may only be used with a single pn_io_t during
52 * their lifetime.
53 */
54 #if defined(_WIN32) && ! defined(__CYGWIN__)
55 #ifdef _WIN64
56 typedef unsigned __int64 pn_socket_t;
57 #else
58 typedef unsigned int pn_socket_t;
59 #endif
60 #define PN_INVALID_SOCKET (pn_socket_t)(~0)
61 #else
62 typedef int pn_socket_t;
63 #define PN_INVALID_SOCKET (-1)
64 #endif
65
66 /**
67 * A selectable object provides an interface that can be used to
68 * incorporate proton's I/O into third party event loops.
69 *
70 * Every selectable is associated with exactly one file descriptor.
71 * Selectables may be interested in three kinds of events, read
72 * events, write events, and timer events.
73 *
74 * When a read, write, or timer event occurs, the selectable must be
75 * notified by calling ::pn_selectable_readable(),
76 * ::pn_selectable_writable(), and ::pn_selectable_expired() as
77 * appropriate.
78 *
79 * Once a selectable reaches a terminal state (see
80 * ::pn_selectable_is_terminal()), it will never be interested in
81 * events of any kind. When this occurs it should be removed from the
82 * external event loop and discarded using ::pn_selectable_free().
83 */
84 typedef struct pn_selectable_t pn_selectable_t;
85
86 PNX_EXTERN pn_selectable_t *pn_selectable(void);
87
88 PNX_EXTERN void pn_selectable_on_readable(pn_selectable_t *sel, void (*readable)(pn_selectable_t *));
89 PNX_EXTERN void pn_selectable_on_writable(pn_selectable_t *sel, void (*writable)(pn_selectable_t *));
90 PNX_EXTERN void pn_selectable_on_expired(pn_selectable_t *sel, void (*expired)(pn_selectable_t *));
91 PNX_EXTERN void pn_selectable_on_error(pn_selectable_t *sel, void (*error)(pn_selectable_t *));
92 PNX_EXTERN void pn_selectable_on_release(pn_selectable_t *sel, void (*release)(pn_selectable_t *));
93 PNX_EXTERN void pn_selectable_on_finalize(pn_selectable_t *sel, void (*finalize)(pn_selectable_t *));
94
95 PNX_EXTERN pn_record_t *pn_selectable_attachments(pn_selectable_t *sel);
96
97 /**
98 * Get the file descriptor associated with a selectable.
99 *
100 * @param[in] selectable a selectable object
101 * @return the file descriptor associated with the selectable
102 */
103 PNX_EXTERN pn_socket_t pn_selectable_get_fd(pn_selectable_t *selectable);
104
105 /**
106 * Set the file descriptor associated with a selectable.
107 *
108 * @param[in] selectable a selectable object
109 * @param[in] fd the file descriptor
110 */
111 PNX_EXTERN void pn_selectable_set_fd(pn_selectable_t *selectable, pn_socket_t fd);
112
113 /**
114 * Check if a selectable is interested in readable events.
115 *
116 * @param[in] selectable a selectable object
117 * @return true iff the selectable is interested in read events
118 */
119 PNX_EXTERN bool pn_selectable_is_reading(pn_selectable_t *selectable);
120
121 PNX_EXTERN void pn_selectable_set_reading(pn_selectable_t *sel, bool reading);
122
123 /**
124 * Check if a selectable is interested in writable events.
125 *
126 * @param[in] selectable a selectable object
127 * @return true iff the selectable is interested in writable events
128 */
129 PNX_EXTERN bool pn_selectable_is_writing(pn_selectable_t *selectable);
130
131 PNX_EXTERN void pn_selectable_set_writing(pn_selectable_t *sel, bool writing);
132
133 /**
134 * Get the next deadline for a selectable.
135 *
136 * A selectable with a deadline is interested in being notified when
137 * that deadline expires. Zero indicates there is currently no
138 * deadline.
139 *
140 * @param[in] selectable a selectable object
141 * @return the next deadline or zero
142 */
143 PNX_EXTERN pn_timestamp_t pn_selectable_get_deadline(pn_selectable_t *selectable);
144
145 PNX_EXTERN void pn_selectable_set_deadline(pn_selectable_t *sel, pn_timestamp_t deadline);
146
147 /**
148 * Notify a selectable that the file descriptor is readable.
149 *
150 * @param[in] selectable a selectable object
151 */
152 PNX_EXTERN void pn_selectable_readable(pn_selectable_t *selectable);
153
154 /**
155 * Notify a selectable that the file descriptor is writable.
156 *
157 * @param[in] selectable a selectable object
158 */
159 PNX_EXTERN void pn_selectable_writable(pn_selectable_t *selectable);
160
161 /**
162 * Notify a selectable that there is an error on the file descriptor.
163 *
164 * @param[in] selectable a selectable object
165 */
166 PNX_EXTERN void pn_selectable_error(pn_selectable_t *selectable);
167
168 /**
169 * Notify a selectable that its deadline has expired.
170 *
171 * @param[in] selectable a selectable object
172 */
173 PNX_EXTERN void pn_selectable_expired(pn_selectable_t *selectable);
174
175 /**
176 * Check if a selectable is registered.
177 *
178 * This flag is set via ::pn_selectable_set_registered() and can be
179 * used for tracking whether a given selectable has been registered
180 * with an external event loop.
181 *
182 * @param[in] selectable
183 * @return true if the selectable is registered
184 */
185 PNX_EXTERN bool pn_selectable_is_registered(pn_selectable_t *selectable);
186
187 /**
188 * Set the registered flag for a selectable.
189 *
190 * See ::pn_selectable_is_registered() for details.
191 *
192 * @param[in] selectable a selectable object
193 * @param[in] registered the registered flag
194 */
195 PNX_EXTERN void pn_selectable_set_registered(pn_selectable_t *selectable, bool registered);
196
197 /**
198 * Check if a selectable is in the terminal state.
199 *
200 * A selectable that is in the terminal state will never be interested
201 * in being notified of events of any kind ever again. Once a
202 * selectable reaches this state it should be removed from any
203 * external I/O loops and freed in order to reclaim any resources
204 * associated with it.
205 *
206 * @param[in] selectable a selectable object
207 * @return true if the selectable is in the terminal state, false otherwise
208 */
209 PNX_EXTERN bool pn_selectable_is_terminal(pn_selectable_t *selectable);
210
211 /**
212 * Terminate a selectable.
213 *
214 * @param[in] selectable a selectable object
215 */
216 PNX_EXTERN void pn_selectable_terminate(pn_selectable_t *selectable);
217
218 PNX_EXTERN void pn_selectable_release(pn_selectable_t *selectable);
219
220 /**
221 * Free a selectable object.
222 *
223 * @param[in] selectable a selectable object (or NULL)
224 */
225 PNX_EXTERN void pn_selectable_free(pn_selectable_t *selectable);
226
227 /**
228 * Configure a selectable with a set of callbacks that emit readable,
229 * writable, and expired events into the supplied collector.
230 *
231 * @param[in] selectable a selectable object
232 * @param[in] collector a collector object
233 */
234 PNX_EXTERN void pn_selectable_collect(pn_selectable_t *selectable, pn_collector_t *collector);
235
236 /**
237 * @endcond
238 */
239
240 #ifdef __cplusplus
241 }
242 #endif
243
244 #endif /* selectable.h */