stream: add a concurrency option to services, limiting parallel callbacks
[strongswan.git] / src / libstrongswan / networking / streams / stream_service.h
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup stream_service stream_service
18 * @{ @ingroup streams
19 */
20
21 #ifndef STREAM_SERVICE_H_
22 #define STREAM_SERVICE_H_
23
24 typedef struct stream_service_t stream_service_t;
25
26 #include <library.h>
27 #include <processing/jobs/job.h>
28 #include <networking/streams/stream.h>
29
30 /**
31 * Constructor function prototype for stream_service_t.
32 *
33 * @param uri URI to create a stream for
34 * @param backlog size of the backlog queue, as passed to listen()
35 * @return stream instance, NULL on error
36 */
37 typedef stream_service_t*(*stream_service_constructor_t)(char *uri, int backlog);
38
39 /**
40 * Service callback routine for accepting client connections.
41 *
42 * The passed stream_service gets closed/destroyed by the callback caller.
43 *
44 * @param data user data, as passed during registration
45 * @param stream accept()ed client connection
46 */
47 typedef void (*stream_service_cb_t)(void *data, stream_t *stream);
48
49 /**
50 * A service accepting client connection streams.
51 */
52 struct stream_service_t {
53
54 /**
55 * Start accepting client connections on this stream service.
56 *
57 * To stop accepting connections, pass a NULL callback function.
58 *
59 * @param cb callback function to call for accepted client streams
60 * @param data data to pass to callback function
61 * @param prio job priority to run callback with
62 * @param cncrncy maximum number of parallel callback invocations
63 */
64 void (*on_accept)(stream_service_t *this,
65 stream_service_cb_t cb, void *data,
66 job_priority_t prio, u_int cncrncy);
67
68 /**
69 * Destroy a stream_service_t.
70 */
71 void (*destroy)(stream_service_t *this);
72 };
73
74 /**
75 * Create a service from a file descriptor.
76 *
77 * The file descriptor MUST be a socket.
78 *
79 * @param fd file descriptor to wrap into a stream_service_t
80 * @return stream_service instance
81 */
82 stream_service_t *stream_service_create_from_fd(int fd);
83
84 /**
85 * Create a service instance for UNIX sockets.
86 *
87 * @param uri UNIX socket specific URI, must start with "unix://"
88 * @param backlog size of the backlog queue, as passed to listen()
89 * @return stream_service instance, NULL on failure
90 */
91 stream_service_t *stream_service_create_unix(char *uri, int backlog);
92
93 /**
94 * Create a service instance for TCP sockets.
95 *
96 * @param uri TCP socket specific URI, must start with "tcp://"
97 * @param backlog size of the backlog queue, as passed to listen()
98 * @return stream_service instance, NULL on failure
99 */
100 stream_service_t *stream_service_create_tcp(char *uri, int backlog);
101
102 #endif /** STREAM_SERVICE_H_ @}*/