[go: up one dir, main page]

Menu

[ebf9b6]: / src / flom_conns.h  Maximize  Restore  History

Download this file

393 lines (296 with data), 10.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright (c) 2013-2024, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLoM, Free Lock Manager
*
* FLoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2.0 as
* published by the Free Software Foundation.
*
* FLoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLoM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FLOM_CONNS_H
# define FLOM_CONNS_H
#include <config.h>
#ifdef HAVE_GLIB_H
# include <glib.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_POLL_H
# include <poll.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#include "flom_config.h"
#include "flom_conn.h"
#include "flom_msg.h"
#include "flom_tcp.h"
#include "flom_tls.h"
/* save old FLOM_TRACE_MODULE and set a new value */
#ifdef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE_SAVE FLOM_TRACE_MODULE
# undef FLOM_TRACE_MODULE
#else
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE */
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_CONNS
/**
* Default allocation size for @ref flom_conns_t objects
*/
#define FLOM_CONNS_DEFAULT_ALLOCATION 10
/**
* Expansion allocation step
*/
#define FLOM_CONNS_PERCENT_ALLOCATION 20
/**
* A structured object used to register connections
*/
struct flom_conns_s {
/**
* Array used for poll function (it must be re-generated before poll
* function)
*/
struct pollfd *poll_array;
/**
* Connection domain as specified when calling socket function
*/
int domain;
/**
* Array of connection data (it's an array of pointer for @ref
* flom_conn_t)
*/
GPtrArray *array;
/**
* Last Unique identifier: it can be used as a unique id for distinguishing
* any type of objects, for example the lockers
*/
flom_uid_t last_uid;
};
typedef struct flom_conns_s flom_conns_t;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* Check if n field of the object and the real size of the underlying
* array match
* @param conns IN connections object
* @return a boolean value: TRUE OK, FALSE KO
*/
int flom_conns_check_n(flom_conns_t *conns);
/**
* Initialize a new object
* @param conns IN/OUT object to be initialized
* @param domain IN socket domain for all the connections managed by this
* object
*/
void flom_conns_init(flom_conns_t *conns, int domain);
/**
* Add a new connection to the pool
* @param conns IN/OUT connection pool object
* @param conn IN connection object to add
*/
static inline void flom_conns_add_conn(
flom_conns_t *conns, flom_conn_t *conn) {
g_ptr_array_add(conns->array, conn);
}
/**
* Import a connection: the imported connection (@ref flom_conn_t)
* must not be freed by the caller because the import does not make a copy
* of the structure, it picks up the passed reference
* @param conns IN/OUT connections object
* @param fd IN file descriptor
* @param conn IN connection object
*/
void flom_conns_import(flom_conns_t *conns, int fd, flom_conn_t *conn);
/**
* Return the socket domain associated with all the connection
* @return socket domain
*/
static inline int flom_conns_get_domain(const flom_conns_t *conns) {
return conns->domain;
}
/**
* Set the socket domain of an already initialized connections object;
* pay attention this may invalidate the already stored addresses and
* should be used only when domain is not available at initialization
* time
* @param conns IN/OUT connections object
* @param domain IN the new domain of the connections object
*/
static inline void flom_conns_set_domain(flom_conns_t *conns,
int domain) {
conns->domain = domain;
}
/**
* Return the number of active connections managed by the object
* @param conns IN connections object
* @return the number of active connections
*/
static inline guint flom_conns_get_used(const flom_conns_t *conns) {
return conns->array->len;
}
/**
* Return the last unique identifier; 0 is a special value when no
* unique identifier has been already created
* @param conns IN connections object
* @return last unique id
*/
static inline flom_uid_t flom_conns_get_last_uid(
const flom_conns_t *conns) {
return conns->last_uid;
}
/**
* Generate a new unique id
* @param conns IN/OUT connections object
* @return last generated unique id
*/
static inline flom_uid_t flom_conns_get_new_uid(flom_conns_t *conns) {
return ++(conns->last_uid);
}
/**
* Return the file descriptor associated to a connection
* @param conns IN connections object
* @param id IN identificator (position in array) of the connection
* @return the associated file descriptor or @ref FLOM_NULL_FD if any error
* happens
*/
static inline int flom_conns_get_fd(const flom_conns_t *conns, guint id) {
if (id < conns->array->len)
return flom_tcp_get_sockfd(
flom_conn_get_tcp(
(flom_conn_t *)g_ptr_array_index(conns->array, id)));
else
return FLOM_NULL_FD;
}
/**
* Return the socket type related to a file descriptor
* @param conns IN connections object
* @param id IN identificator (position in array) of the connection
* @return SOCK_STREAM or SOCK_DGRAM or 0 (error condition)
*/
static inline int flom_conns_get_type(
const flom_conns_t *conns, guint id) {
if (id < conns->array->len)
return flom_tcp_get_socket_type(
flom_conn_get_tcp(
(flom_conn_t *)g_ptr_array_index(conns->array, id)));
else
return 0;
}
/**
* Return a reference (read-only pointer) to the struct containing
* connection data of a specific connection
* @param conns IN connections object
* @param id IN identificator (position in array) of the connection
* @return a reference to the asked structure or NULL
*/
static inline flom_conn_t *flom_conns_get_conn(
const flom_conns_t *conns, guint id) {
if (id < conns->array->len)
return ((flom_conn_t *)g_ptr_array_index(conns->array, id));
else
return NULL;
}
/**
* Build a new array for poll function and return it
* @param conns IN connections object
* @return the fds array to be used with poll function or NULL if an
* error happens
*/
struct pollfd *flom_conns_get_fds(flom_conns_t *conns);
/**
* Return the message associated to a connection
* @param conns IN connections object
* @param id IN identificator (position in array) of the connection
* @return the associated message or NULL if any error happens
*/
static inline struct flom_msg_s *flom_conns_get_msg(
flom_conns_t *conns, guint id) {
if (id < conns->array->len)
return flom_conn_get_msg(
(flom_conn_t *)g_ptr_array_index(conns->array, id));
else
return NULL;
}
/**
* Return the GMarkupParseContext associated to a connection
* @param conns IN connections object
* @param id IN identificator (position in array) of the connection
* @return the associated GMarkupParseContext or NULL if any error happens
*/
static inline GMarkupParseContext *flom_conns_get_parser(
flom_conns_t *conns, guint id) {
if (id < conns->array->len)
return flom_conn_get_parser(
(flom_conn_t *)g_ptr_array_index(conns->array, id));
else
return NULL;
}
/**
* Set events field for every connection in the object
* NOTE: it must be called after @ref flom_conns_get_fds because it
* resize the poll array if necessary
* @param conns IN/OUT connections object
* @param events IN new value for every events field
* @return a reason code
*/
int flom_conns_set_events(flom_conns_t *conns, short events);
/**
* Close a file descriptor and set it to @ref FLOM_NULL_FD; use
* @ref flom_conns_clean to remove the connections associated to closed
* file descriptors
* @param conns IN/OUT connections object
* @param id IN connection must be closed
* @return a reason code
*/
int flom_conns_close_fd(flom_conns_t *conns, guint id);
/**
* Mark as "transferred to another thread" a connection and detach it
* from this connections object
* @param conns IN/OUT connections object
* @param id IN connection must be marked
* @return a reason code
*/
int flom_conns_trns_fd(flom_conns_t *conns, guint id);
/**
* Remove connections with invalid (closed) file descriptor
* @param conns IN/OUT connections object
* @return a reason code
*/
int flom_conns_clean(flom_conns_t *conns);
/**
* Free all the memory allocated by connections object
* @param conns IN/OUT connections object
*/
void flom_conns_free(flom_conns_t *conns);
/**
* Trace the content of a connections object
* @param conns IN connections object to trace
*/
void flom_conns_trace(const flom_conns_t *conns);
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* restore old value of FLOM_TRACE_MODULE */
#ifdef FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE_SAVE */
#endif /* FLOM_CONNS_H */