blob: a8061d64fbd12574e5fc6fb9ab8237daa1641650 [file] [log] [blame]
Benny Prijonoe0312a72005-11-18 00:16:43 +00001/* $Id$ */
Benny Prijonoe7224612005-11-13 19:40:44 +00002/*
Benny Prijonoe0312a72005-11-18 00:16:43 +00003 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
Benny Prijonoe7224612005-11-13 19:40:44 +00004 *
Benny Prijonoe0312a72005-11-18 00:16:43 +00005 * This program is free software; you can redistribute it and/or modify
6 * 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 * (at your option) any later version.
Benny Prijonoe7224612005-11-13 19:40:44 +00009 *
Benny Prijonoe0312a72005-11-18 00:16:43 +000010 * This program is distributed in the hope that it will be useful,
Benny Prijonoe7224612005-11-13 19:40:44 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Benny Prijonoe0312a72005-11-18 00:16:43 +000012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Benny Prijonoe7224612005-11-13 19:40:44 +000018 */
19#ifndef __PJPP_PROACTOR_HPP__
20#define __PJPP_PROACTOR_HPP__
21
22#include <pj/ioqueue.h>
23#include <pj++/pool.hpp>
24#include <pj++/sock.hpp>
25#include <pj++/timer.hpp>
26#include <pj/errno.h>
27
28class Pj_Proactor;
29class Pj_Event_Handler;
30
31
32//////////////////////////////////////////////////////////////////////////////
33// Asynchronous operation key.
34//
35// Applications may inheric this class to put their application
36// specific data.
37//
38class Pj_Async_Op : public pj_ioqueue_op_key_t
39{
40public:
41 //
42 // Construct with null handler.
43 // App must call set_handler() before use.
44 //
45 Pj_Async_Op()
46 : handler_(NULL)
47 {
48 pj_ioqueue_op_key_init(this, sizeof(*this));
49 }
50
51 //
52 // Constructor.
53 //
54 explicit Pj_Async_Op(Pj_Event_Handler *handler)
55 : handler_(handler)
56 {
57 pj_ioqueue_op_key_init(this, sizeof(*this));
58 }
59
60 //
61 // Set handler.
62 //
63 void set_handler(Pj_Event_Handler *handler)
64 {
65 handler_ = handler;
66 }
67
68 //
69 // Check whether operation is still pending for this key.
70 //
71 bool is_pending();
72
73 //
74 // Cancel the operation.
75 //
76 bool cancel(pj_ssize_t bytes_status=-PJ_ECANCELLED);
77
78protected:
79 Pj_Event_Handler *handler_;
80};
81
82
83//////////////////////////////////////////////////////////////////////////////
84// Event handler.
85//
86// Applications should inherit this class to receive various event
87// notifications.
88//
89// Applications should implement get_socket_handle().
90//
91class Pj_Event_Handler : public Pj_Object
92{
93 friend class Pj_Proactor;
94public:
95 //
96 // Default constructor.
97 //
98 Pj_Event_Handler()
99 : key_(NULL)
100 {
101 pj_memset(&timer_, 0, sizeof(timer_));
102 timer_.user_data = this;
103 timer_.cb = &timer_callback;
104 }
105
106 //
107 // Destroy.
108 //
109 virtual ~Pj_Event_Handler()
110 {
111 unregister();
112 }
113
114 //
115 // Unregister this handler from the ioqueue.
116 //
117 void unregister()
118 {
119 if (key_) {
120 pj_ioqueue_unregister(key_);
121 key_ = NULL;
122 }
123 }
124
125 //
126 // Get socket handle associated with this.
127 //
128 virtual pj_sock_t get_socket_handle()
129 {
130 return PJ_INVALID_SOCKET;
131 }
132
133 //
134 // Start async receive.
135 //
136 pj_status_t recv( Pj_Async_Op *op_key,
137 void *buf, pj_ssize_t *len,
138 unsigned flags)
139 {
140 return pj_ioqueue_recv( key_, op_key,
141 buf, len, flags);
142 }
143
144 //
145 // Start async recvfrom()
146 //
147 pj_status_t recvfrom( Pj_Async_Op *op_key,
148 void *buf, pj_ssize_t *len, unsigned flags,
149 Pj_Inet_Addr *addr)
150 {
151 addr->addrlen_ = sizeof(Pj_Inet_Addr);
152 return pj_ioqueue_recvfrom( key_, op_key, buf, len, flags,
153 addr, &addr->addrlen_ );
154 }
155
156 //
157 // Start async send()
158 //
159 pj_status_t send( Pj_Async_Op *op_key,
160 const void *data, pj_ssize_t *len,
161 unsigned flags)
162 {
163 return pj_ioqueue_send( key_, op_key, data, len, flags);
164 }
165
166 //
167 // Start async sendto()
168 //
169 pj_status_t sendto( Pj_Async_Op *op_key,
170 const void *data, pj_ssize_t *len, unsigned flags,
171 const Pj_Inet_Addr &addr)
172 {
173 return pj_ioqueue_sendto(key_, op_key, data, len, flags,
174 &addr, sizeof(addr));
175 }
176
177#if PJ_HAS_TCP
178 //
179 // Start async connect()
180 //
181 pj_status_t connect(const Pj_Inet_Addr &addr)
182 {
183 return pj_ioqueue_connect(key_, &addr, sizeof(addr));
184 }
185
186 //
187 // Start async accept().
188 //
189 pj_status_t accept( Pj_Async_Op *op_key,
190 Pj_Socket *sock,
191 Pj_Inet_Addr *local = NULL,
192 Pj_Inet_Addr *remote = NULL)
193 {
194 int *addrlen = local ? &local->addrlen_ : NULL;
195 return pj_ioqueue_accept( key_, op_key, &sock->sock_,
196 local, remote, addrlen );
197 }
198
199#endif
200
201protected:
202 //////////////////
203 // Overridables
204 //////////////////
205
206 //
207 // Timeout callback.
208 //
209 virtual void on_timeout(int data)
210 {
211 }
212
213 //
214 // On read complete callback.
215 //
216 virtual void on_read_complete( Pj_Async_Op *op_key,
217 pj_ssize_t bytes_read)
218 {
219 }
220
221 //
222 // On write complete callback.
223 //
224 virtual void on_write_complete( Pj_Async_Op *op_key,
225 pj_ssize_t bytes_sent)
226 {
227 }
228
229#if PJ_HAS_TCP
230 //
231 // On connect complete callback.
232 //
233 virtual void on_connect_complete(pj_status_t status)
234 {
235 }
236
237 //
238 // On new connection callback.
239 //
240 virtual void on_accept_complete( Pj_Async_Op *op_key,
241 pj_sock_t new_sock,
242 pj_status_t status)
243 {
244 }
245
246#endif
247
248
249private:
250 pj_ioqueue_key_t *key_;
251 pj_timer_entry timer_;
252
253 friend class Pj_Proactor;
254 friend class Pj_Async_Op;
255
256 //
257 // Static timer callback.
258 //
259 static void timer_callback( pj_timer_heap_t *timer_heap,
260 struct pj_timer_entry *entry)
261 {
262 Pj_Event_Handler *handler =
263 (Pj_Event_Handler*) entry->user_data;
264
265 handler->on_timeout(entry->id);
266 }
267};
268
269inline bool Pj_Async_Op::is_pending()
270{
271 return pj_ioqueue_is_pending(handler_->key_, this) != 0;
272}
273
274inline bool Pj_Async_Op::cancel(pj_ssize_t bytes_status)
275{
276 return pj_ioqueue_post_completion(handler_->key_, this,
277 bytes_status) == PJ_SUCCESS;
278}
279
280//////////////////////////////////////////////////////////////////////////////
281// Proactor
282//
283class Pj_Proactor : public Pj_Object
284{
285public:
286 //
287 // Default constructor, initializes to NULL.
288 //
289 Pj_Proactor()
290 : ioq_(NULL), th_(NULL)
291 {
292 cb_.on_read_complete = &read_complete_cb;
293 cb_.on_write_complete = &write_complete_cb;
294 cb_.on_accept_complete = &accept_complete_cb;
295 cb_.on_connect_complete = &connect_complete_cb;
296 }
297
298 //
299 // Construct proactor.
300 //
301 Pj_Proactor( Pj_Pool *pool, pj_size_t max_fd,
302 pj_size_t max_timer_entries )
303 : ioq_(NULL), th_(NULL)
304 {
305 cb_.on_read_complete = &read_complete_cb;
306 cb_.on_write_complete = &write_complete_cb;
307 cb_.on_accept_complete = &accept_complete_cb;
308 cb_.on_connect_complete = &connect_complete_cb;
309
310 create(pool, max_fd, max_timer_entries);
311 }
312
313 //
314 // Destructor.
315 //
316 ~Pj_Proactor()
317 {
318 destroy();
319 }
320
321 //
322 // Create proactor.
323 //
324 pj_status_t create( Pj_Pool *pool, pj_size_t max_fd,
325 pj_size_t timer_entry_count)
326 {
327 pj_status_t status;
328
329 destroy();
330
331 status = pj_ioqueue_create(pool->pool_(), max_fd, &ioq_);
332 if (status != PJ_SUCCESS)
333 return status;
334
335 status = pj_timer_heap_create(pool->pool_(),
336 timer_entry_count, &th_);
337 if (status != PJ_SUCCESS) {
338 pj_ioqueue_destroy(ioq_);
339 ioq_ = NULL;
340 return NULL;
341 }
342
343 return status;
344 }
345
346 //
347 // Destroy proactor.
348 //
349 void destroy()
350 {
351 if (ioq_) {
352 pj_ioqueue_destroy(ioq_);
353 ioq_ = NULL;
354 }
355 if (th_) {
356 pj_timer_heap_destroy(th_);
357 th_ = NULL;
358 }
359 }
360
361 //
362 // Register handler.
363 // This will call handler->get_socket_handle()
364 //
365 pj_status_t register_socket_handler(Pj_Pool *pool,
366 Pj_Event_Handler *handler)
367 {
368 return pj_ioqueue_register_sock( pool->pool_(), ioq_,
369 handler->get_socket_handle(),
370 handler, &cb_, &handler->key_ );
371 }
372
373 //
374 // Unregister handler.
375 //
376 static void unregister_handler(Pj_Event_Handler *handler)
377 {
378 if (handler->key_) {
379 pj_ioqueue_unregister( handler->key_ );
380 handler->key_ = NULL;
381 }
382 }
383
384 //
385 // Scheduler timer.
386 //
387 bool schedule_timer( Pj_Event_Handler *handler,
388 const Pj_Time_Val &delay,
389 int id=-1)
390 {
391 return schedule_timer(th_, handler, delay, id);
392 }
393
394 //
395 // Cancel timer.
396 //
397 bool cancel_timer(Pj_Event_Handler *handler)
398 {
399 return pj_timer_heap_cancel(th_, &handler->timer_) == 1;
400 }
401
402 //
403 // Handle events.
404 //
405 int handle_events(Pj_Time_Val *max_timeout)
406 {
407 Pj_Time_Val timeout(0, 0);
408 int timer_count;
409
410 timer_count = pj_timer_heap_poll( th_, &timeout );
411
412 if (timeout.get_sec() < 0)
413 timeout.sec = PJ_MAXINT32;
414
415 /* If caller specifies maximum time to wait, then compare the value
416 * with the timeout to wait from timer, and use the minimum value.
417 */
418 if (max_timeout && timeout >= *max_timeout) {
419 timeout = *max_timeout;
420 }
421
422 /* Poll events in ioqueue. */
423 int ioqueue_count;
424
425 ioqueue_count = pj_ioqueue_poll(ioq_, &timeout);
426 if (ioqueue_count < 0)
427 return ioqueue_count;
428
429 return ioqueue_count + timer_count;
430 }
431
432 //
433 // Get the internal ioqueue object.
434 //
435 pj_ioqueue_t *get_io_queue()
436 {
437 return ioq_;
438 }
439
440 //
441 // Get the internal timer heap object.
442 //
443 pj_timer_heap_t *get_timer_heap()
444 {
445 return th_;
446 }
447
448private:
449 pj_ioqueue_t *ioq_;
450 pj_timer_heap_t *th_;
451 pj_ioqueue_callback cb_;
452
453 static bool schedule_timer( pj_timer_heap_t *timer,
454 Pj_Event_Handler *handler,
455 const Pj_Time_Val &delay,
456 int id=-1)
457 {
458 handler->timer_.id = id;
459 return pj_timer_heap_schedule(timer, &handler->timer_, &delay) == 0;
460 }
461
462
463 //
464 // Static read completion callback.
465 //
466 static void read_complete_cb( pj_ioqueue_key_t *key,
467 pj_ioqueue_op_key_t *op_key,
468 pj_ssize_t bytes_read)
469 {
470 Pj_Event_Handler *handler =
471 (Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
472
473 handler->on_read_complete((Pj_Async_Op*)op_key, bytes_read);
474 }
475
476 //
477 // Static write completion callback.
478 //
479 static void write_complete_cb(pj_ioqueue_key_t *key,
480 pj_ioqueue_op_key_t *op_key,
481 pj_ssize_t bytes_sent)
482 {
483 Pj_Event_Handler *handler =
484 (Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
485
486 handler->on_write_complete((Pj_Async_Op*)op_key, bytes_sent);
487 }
488
489 //
490 // Static accept completion callback.
491 //
492 static void accept_complete_cb(pj_ioqueue_key_t *key,
493 pj_ioqueue_op_key_t *op_key,
494 pj_sock_t new_sock,
495 pj_status_t status)
496 {
497 Pj_Event_Handler *handler =
498 (Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
499
500 handler->on_accept_complete((Pj_Async_Op*)op_key, new_sock, status);
501 }
502
503 //
504 // Static connect completion callback.
505 //
506 static void connect_complete_cb(pj_ioqueue_key_t *key,
507 pj_status_t status)
508 {
509 Pj_Event_Handler *handler =
510 (Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
511
512 handler->on_connect_complete(status);
513 }
514
515};
516
517#endif /* __PJPP_PROACTOR_HPP__ */
518