mainloop.h 8.26 KB
Newer Older
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
8 9 10 11 12 13 14 15 16 17 18 19 20
    (at your option) any later version.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef BELLE_SIP_MAINLOOP_H
#define BELLE_SIP_MAINLOOP_H
jehan's avatar
jehan committed
21

Simon Morlat's avatar
Simon Morlat committed
22

23
#define BELLE_SIP_EVENT_READ 1
24 25
#define BELLE_SIP_EVENT_WRITE (1<<1)
#define BELLE_SIP_EVENT_ERROR (1<<2)
Simon Morlat's avatar
Simon Morlat committed
26
#define BELLE_SIP_EVENT_TIMEOUT (1<<3)
27 28 29

typedef struct belle_sip_source belle_sip_source_t;

Ghislain MARY's avatar
Ghislain MARY committed
30 31
BELLESIP_EXPORT int belle_sip_source_set_events(belle_sip_source_t* source, int event_mask);
BELLESIP_EXPORT belle_sip_socket_t belle_sip_source_get_socket(const belle_sip_source_t* source);
32

Simon Morlat's avatar
Simon Morlat committed
33 34 35
/**
 * Callback function prototype for main loop notifications.
 * Return value is important:
Simon Morlat's avatar
Simon Morlat committed
36
 * BELLE_SIP_STOP => source is removed from main loop.
37 38
 * BELLE_SIP_CONTINUE => source is kept, timeout is restarted if any according to last expiry time
 * BELLE_SIP_CONTINUE_WITHOUT_CATCHUP => source is kept, timeout is restarted if any according to current time
Simon Morlat's avatar
Simon Morlat committed
39 40
**/
typedef int (*belle_sip_source_func_t)(void *user_data, unsigned int events);
41

42 43 44 45 46
/*
 * Call back fonction invoked when source is removed from main loop
 */
typedef void (*belle_sip_source_remove_callback_t)(belle_sip_source_t *);

Simon Morlat's avatar
Simon Morlat committed
47 48
typedef void (*belle_sip_callback_t)(void *user_data);

49 50
typedef struct belle_sip_main_loop belle_sip_main_loop_t;

51 52 53
#define BELLE_SIP_CONTINUE_WITHOUT_CATCHUP 2
#define BELLE_SIP_CONTINUE	1
#define BELLE_SIP_STOP		0
54

55
BELLE_SIP_BEGIN_DECLS
56

Ghislain MARY's avatar
Ghislain MARY committed
57
BELLESIP_EXPORT void belle_sip_main_loop_add_source(belle_sip_main_loop_t *ml, belle_sip_source_t *source);
58

Ghislain MARY's avatar
Ghislain MARY committed
59
BELLESIP_EXPORT void belle_sip_main_loop_remove_source(belle_sip_main_loop_t *ml, belle_sip_source_t *source);
60

Simon Morlat's avatar
Simon Morlat committed
61 62 63
/**
 * Creates a mainloop.
**/
Ghislain MARY's avatar
Ghislain MARY committed
64
BELLESIP_EXPORT belle_sip_main_loop_t *belle_sip_main_loop_new(void);
Simon Morlat's avatar
Simon Morlat committed
65 66 67 68 69 70 71 72 73

/**
 * Adds a timeout into the main loop
 * @param ml
 * @param func a callback function to be called to notify timeout expiration
 * @param data a pointer to be passed to the callback
 * @param timeout_value_ms duration of the timeout.
 * @returns timeout id
**/
Ghislain MARY's avatar
Ghislain MARY committed
74
BELLESIP_EXPORT unsigned long belle_sip_main_loop_add_timeout(belle_sip_main_loop_t *ml, belle_sip_source_func_t func, void *data, unsigned int timeout_value_ms);
Simon Morlat's avatar
Simon Morlat committed
75

jehan's avatar
jehan committed
76 77
/**
 * Adds a timeout into the main loop
Simon Morlat's avatar
Simon Morlat committed
78 79
 * The caller of this function is responsible for freeing (with belle_sip_object_unref()) the returned belle_sip_source_t object when it is no longer
 * needed.
jehan's avatar
jehan committed
80 81 82 83 84 85 86
 * @param ml
 * @param func a callback function to be called to notify timeout expiration
 * @param data a pointer to be passed to the callback
 * @param timeout_value_ms duration of the timeout.
 * @param timer_name name of the timer, can be null
 * @returns timeout belle_sip_source_t  with ref count = 1
**/
Ghislain MARY's avatar
Ghislain MARY committed
87
BELLESIP_EXPORT belle_sip_source_t* belle_sip_main_loop_create_timeout(belle_sip_main_loop_t *ml
Simon Morlat's avatar
Simon Morlat committed
88 89 90 91
							, belle_sip_source_func_t func
							, void *data
							, unsigned int timeout_value_ms
							,const char* timer_name);
jehan's avatar
jehan committed
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/**
 * Adds a timeout into the main loop
 * The caller of this function is responsible for freeing (with belle_sip_object_unref()) the returned belle_sip_source_t object when it is no longer
 * needed.
 * @param ml
 * @param func a callback function to be called to notify timeout expiration
 * @param data a pointer to be passed to the callback
 * @param timeout_value_ms duration of the timeout.
 * @param timer_name name of the timer, can be null
 * @param function called when source is removed, can be null
 * @returns timeout belle_sip_source_t  with ref count = 1
 **/
BELLESIP_EXPORT belle_sip_source_t* belle_sip_main_loop_create_timeout_with_remove_cb (belle_sip_main_loop_t *ml
																					   , belle_sip_source_func_t func
																					   , void *data
																					   , unsigned int timeout_value_ms
																					   , const char* timer_name
																					   , belle_sip_source_remove_callback_t remove_func);


Simon Morlat's avatar
Simon Morlat committed
113 114 115
/**
 * Schedule an arbitrary task at next main loop iteration.
**/
Ghislain MARY's avatar
Ghislain MARY committed
116
BELLESIP_EXPORT void belle_sip_main_loop_do_later(belle_sip_main_loop_t *ml, belle_sip_callback_t func, void *data);
Simon Morlat's avatar
Simon Morlat committed
117 118 119 120 121 122

/**
 * Creates a timeout source, similarly to belle_sip_main_loop_add_timeout().
 * However in this case the timeout must be entered manually using belle_sip_main_loop_add_source().
 * Its pointer can be used to remove it from the source (that is cancelling it).
**/
Ghislain MARY's avatar
Ghislain MARY committed
123
BELLESIP_EXPORT belle_sip_source_t * belle_sip_timeout_source_new(belle_sip_source_func_t func, void *data, unsigned int timeout_value_ms);
Simon Morlat's avatar
Simon Morlat committed
124

Ghislain MARY's avatar
Ghislain MARY committed
125
BELLESIP_EXPORT void belle_sip_source_set_timeout(belle_sip_source_t *s, unsigned int value_ms);
126 127 128 129 130
/**
 * Cancel a source. Will be removed at next iterate. It is not freed.
 **/
BELLESIP_EXPORT void belle_sip_source_cancel(belle_sip_source_t * src);

Simon Morlat's avatar
Simon Morlat committed
131

Ghislain MARY's avatar
Ghislain MARY committed
132
BELLESIP_EXPORT unsigned int belle_sip_source_get_timeout(const belle_sip_source_t *s);
133

Ghislain MARY's avatar
Ghislain MARY committed
134
BELLESIP_EXPORT belle_sip_source_t * belle_sip_socket_source_new(belle_sip_source_func_t func, void *data, belle_sip_socket_t fd, unsigned int events, unsigned int timeout_value_ms);
135 136 137 138 139 140
/*
 * register a callback invoked once source is removed from mainloop
 */
BELLESIP_EXPORT void belle_sip_source_set_remove_cb(belle_sip_source_t *s, belle_sip_source_remove_callback_t func);

BELLESIP_EXPORT unsigned long belle_sip_source_get_id(const belle_sip_source_t *s);
141

142 143
BELLESIP_EXPORT void * belle_sip_source_get_user_data(const belle_sip_source_t *s);
BELLESIP_EXPORT void belle_sip_source_set_user_data(belle_sip_source_t *s, void *user_data);
144

Ghislain MARY's avatar
Ghislain MARY committed
145
BELLESIP_EXPORT belle_sip_source_t *belle_sip_main_loop_find_source(belle_sip_main_loop_t *ml, unsigned long id);
146

Simon Morlat's avatar
Simon Morlat committed
147 148 149
/**
 * Executes the main loop forever (or until belle_sip_main_loop_quit() is called)
**/
Ghislain MARY's avatar
Ghislain MARY committed
150
BELLESIP_EXPORT void belle_sip_main_loop_run(belle_sip_main_loop_t *ml);
Simon Morlat's avatar
Simon Morlat committed
151 152 153 154

/**
 * Executes the main loop for the time specified in milliseconds.
**/
155
BELLESIP_EXPORT void belle_sip_main_loop_sleep(belle_sip_main_loop_t *ml, int milliseconds);
Simon Morlat's avatar
Simon Morlat committed
156 157 158 159

/**
 * Break out the main loop.
**/
160
BELLESIP_EXPORT int belle_sip_main_loop_quit(belle_sip_main_loop_t *ml);
Simon Morlat's avatar
Simon Morlat committed
161 162 163 164

/**
 * Cancel (removes) a source. It is not freed.
**/
Ghislain MARY's avatar
Ghislain MARY committed
165
BELLESIP_EXPORT void belle_sip_main_loop_cancel_source(belle_sip_main_loop_t *ml, unsigned long id);
166

167
BELLE_SIP_END_DECLS
168 169 170
#ifndef BELLE_SIP_USE_STL
#define BELLE_SIP_USE_STL 1
#endif
171
#if __cplusplus >= 201103L && BELLE_SIP_USE_STL
172 173 174 175 176 177 178
#include <functional>
typedef std::function<int (unsigned int)> belle_sip_source_cpp_func_t;
BELLESIP_EXPORT inline int belle_sip_source_cpp_func(belle_sip_source_cpp_func_t* user_data, unsigned int events)
{
	int result = (*user_data)(events);
	return result;
}
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
BELLESIP_EXPORT inline void belle_sip_source_on_remove(belle_sip_source_t* source)
{
	delete static_cast<belle_sip_source_cpp_func_t *>(belle_sip_source_get_user_data(source));
	belle_sip_source_set_user_data(source,NULL);
}

/*purpose of this function is to simplify c++ timer integration.
 * ex:
 * std::string helloworld("Hello world):
 * belle_sip_source_cpp_func_t *func = new belle_sip_source_cpp_func_t([helloworld](unsigned int events) {
 *						std::cout << helloworld << std::endl;
 *						return BELLE_SIP_STOP;
 *					});
 *create timer
 *mTimer = belle_sip_main_loop_create_cpp_timeout( mainloop
 *												, func
 *												, 1000
 *												,"timer for c++");
 *
 */

BELLESIP_EXPORT inline belle_sip_source_t * belle_sip_main_loop_create_cpp_timeout(belle_sip_main_loop_t *ml
																				 , belle_sip_source_cpp_func_t *func
																				 , unsigned int timeout_value_ms
																				 , const char* timer_name)
{
	belle_sip_source_t* source = belle_sip_main_loop_create_timeout(  ml
																	, (belle_sip_source_func_t)belle_sip_source_cpp_func
																	, func
																	, timeout_value_ms
																	, timer_name);
	belle_sip_source_set_remove_cb(source,belle_sip_source_on_remove);
	return source;
}

214
#endif
215

216
#endif