1
/* $Id: proactor.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJPP_PROACTOR_HPP__
21
#define __PJPP_PROACTOR_HPP__
23
#include <pj/ioqueue.h>
24
#include <pj++/pool.hpp>
25
#include <pj++/sock.hpp>
26
#include <pj++/timer.hpp>
30
class Pj_Event_Handler;
33
//////////////////////////////////////////////////////////////////////////////
34
// Asynchronous operation key.
36
// Applications may inheric this class to put their application
39
class Pj_Async_Op : public pj_ioqueue_op_key_t
43
// Construct with null handler.
44
// App must call set_handler() before use.
49
pj_ioqueue_op_key_init(this, sizeof(*this));
55
explicit Pj_Async_Op(Pj_Event_Handler *handler)
58
pj_ioqueue_op_key_init(this, sizeof(*this));
64
void set_handler(Pj_Event_Handler *handler)
70
// Check whether operation is still pending for this key.
75
// Cancel the operation.
77
bool cancel(pj_ssize_t bytes_status=-PJ_ECANCELLED);
80
Pj_Event_Handler *handler_;
84
//////////////////////////////////////////////////////////////////////////////
87
// Applications should inherit this class to receive various event
90
// Applications should implement get_socket_handle().
92
class Pj_Event_Handler : public Pj_Object
94
friend class Pj_Proactor;
97
// Default constructor.
102
pj_memset(&timer_, 0, sizeof(timer_));
103
timer_.user_data = this;
104
timer_.cb = &timer_callback;
110
virtual ~Pj_Event_Handler()
116
// Unregister this handler from the ioqueue.
121
pj_ioqueue_unregister(key_);
127
// Get socket handle associated with this.
129
virtual pj_sock_t get_socket_handle()
131
return PJ_INVALID_SOCKET;
135
// Start async receive.
137
pj_status_t recv( Pj_Async_Op *op_key,
138
void *buf, pj_ssize_t *len,
141
return pj_ioqueue_recv( key_, op_key,
146
// Start async recvfrom()
148
pj_status_t recvfrom( Pj_Async_Op *op_key,
149
void *buf, pj_ssize_t *len, unsigned flags,
152
addr->addrlen_ = sizeof(Pj_Inet_Addr);
153
return pj_ioqueue_recvfrom( key_, op_key, buf, len, flags,
154
addr, &addr->addrlen_ );
158
// Start async send()
160
pj_status_t send( Pj_Async_Op *op_key,
161
const void *data, pj_ssize_t *len,
164
return pj_ioqueue_send( key_, op_key, data, len, flags);
168
// Start async sendto()
170
pj_status_t sendto( Pj_Async_Op *op_key,
171
const void *data, pj_ssize_t *len, unsigned flags,
172
const Pj_Inet_Addr &addr)
174
return pj_ioqueue_sendto(key_, op_key, data, len, flags,
175
&addr, sizeof(addr));
180
// Start async connect()
182
pj_status_t connect(const Pj_Inet_Addr &addr)
184
return pj_ioqueue_connect(key_, &addr, sizeof(addr));
188
// Start async accept().
190
pj_status_t accept( Pj_Async_Op *op_key,
192
Pj_Inet_Addr *local = NULL,
193
Pj_Inet_Addr *remote = NULL)
195
int *addrlen = local ? &local->addrlen_ : NULL;
196
return pj_ioqueue_accept( key_, op_key, &sock->sock_,
197
local, remote, addrlen );
210
virtual void on_timeout(int)
215
// On read complete callback.
217
virtual void on_read_complete( Pj_Async_Op*, pj_ssize_t)
222
// On write complete callback.
224
virtual void on_write_complete( Pj_Async_Op *, pj_ssize_t)
230
// On connect complete callback.
232
virtual void on_connect_complete(pj_status_t)
237
// On new connection callback.
239
virtual void on_accept_complete( Pj_Async_Op*, pj_sock_t, pj_status_t)
247
pj_ioqueue_key_t *key_;
248
pj_timer_entry timer_;
250
friend class Pj_Proactor;
251
friend class Pj_Async_Op;
254
// Static timer callback.
256
static void timer_callback( pj_timer_heap_t*,
257
struct pj_timer_entry *entry)
259
Pj_Event_Handler *handler =
260
(Pj_Event_Handler*) entry->user_data;
262
handler->on_timeout(entry->id);
266
inline bool Pj_Async_Op::is_pending()
268
return pj_ioqueue_is_pending(handler_->key_, this) != 0;
271
inline bool Pj_Async_Op::cancel(pj_ssize_t bytes_status)
273
return pj_ioqueue_post_completion(handler_->key_, this,
274
bytes_status) == PJ_SUCCESS;
277
//////////////////////////////////////////////////////////////////////////////
280
class Pj_Proactor : public Pj_Object
284
// Default constructor, initializes to NULL.
287
: ioq_(NULL), th_(NULL)
289
cb_.on_read_complete = &read_complete_cb;
290
cb_.on_write_complete = &write_complete_cb;
291
cb_.on_accept_complete = &accept_complete_cb;
292
cb_.on_connect_complete = &connect_complete_cb;
296
// Construct proactor.
298
Pj_Proactor( Pj_Pool *pool, pj_size_t max_fd,
299
pj_size_t max_timer_entries )
300
: ioq_(NULL), th_(NULL)
302
cb_.on_read_complete = &read_complete_cb;
303
cb_.on_write_complete = &write_complete_cb;
304
cb_.on_accept_complete = &accept_complete_cb;
305
cb_.on_connect_complete = &connect_complete_cb;
307
create(pool, max_fd, max_timer_entries);
321
pj_status_t create( Pj_Pool *pool, pj_size_t max_fd,
322
pj_size_t timer_entry_count)
328
status = pj_ioqueue_create(pool->pool_(), max_fd, &ioq_);
329
if (status != PJ_SUCCESS)
332
status = pj_timer_heap_create(pool->pool_(),
333
timer_entry_count, &th_);
334
if (status != PJ_SUCCESS) {
335
pj_ioqueue_destroy(ioq_);
349
pj_ioqueue_destroy(ioq_);
353
pj_timer_heap_destroy(th_);
360
// This will call handler->get_socket_handle()
362
pj_status_t register_socket_handler(Pj_Pool *pool,
363
Pj_Event_Handler *handler)
365
return pj_ioqueue_register_sock( pool->pool_(), ioq_,
366
handler->get_socket_handle(),
367
handler, &cb_, &handler->key_ );
371
// Unregister handler.
373
static void unregister_handler(Pj_Event_Handler *handler)
376
pj_ioqueue_unregister( handler->key_ );
377
handler->key_ = NULL;
384
bool schedule_timer( Pj_Event_Handler *handler,
385
const Pj_Time_Val &delay,
388
return schedule_timer(th_, handler, delay, id);
394
bool cancel_timer(Pj_Event_Handler *handler)
396
return pj_timer_heap_cancel(th_, &handler->timer_) == 1;
402
int handle_events(Pj_Time_Val *max_timeout)
404
Pj_Time_Val timeout(0, 0);
407
timer_count = pj_timer_heap_poll( th_, &timeout );
409
if (timeout.get_sec() < 0)
410
timeout.sec = PJ_MAXINT32;
412
/* If caller specifies maximum time to wait, then compare the value
413
* with the timeout to wait from timer, and use the minimum value.
415
if (max_timeout && timeout >= *max_timeout) {
416
timeout = *max_timeout;
419
/* Poll events in ioqueue. */
422
ioqueue_count = pj_ioqueue_poll(ioq_, &timeout);
423
if (ioqueue_count < 0)
424
return ioqueue_count;
426
return ioqueue_count + timer_count;
430
// Get the internal ioqueue object.
432
pj_ioqueue_t *get_io_queue()
438
// Get the internal timer heap object.
440
pj_timer_heap_t *get_timer_heap()
447
pj_timer_heap_t *th_;
448
pj_ioqueue_callback cb_;
450
static bool schedule_timer( pj_timer_heap_t *timer,
451
Pj_Event_Handler *handler,
452
const Pj_Time_Val &delay,
455
handler->timer_.id = id;
456
return pj_timer_heap_schedule(timer, &handler->timer_, &delay) == 0;
461
// Static read completion callback.
463
static void read_complete_cb( pj_ioqueue_key_t *key,
464
pj_ioqueue_op_key_t *op_key,
465
pj_ssize_t bytes_read)
467
Pj_Event_Handler *handler =
468
(Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
470
handler->on_read_complete((Pj_Async_Op*)op_key, bytes_read);
474
// Static write completion callback.
476
static void write_complete_cb(pj_ioqueue_key_t *key,
477
pj_ioqueue_op_key_t *op_key,
478
pj_ssize_t bytes_sent)
480
Pj_Event_Handler *handler =
481
(Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
483
handler->on_write_complete((Pj_Async_Op*)op_key, bytes_sent);
487
// Static accept completion callback.
489
static void accept_complete_cb(pj_ioqueue_key_t *key,
490
pj_ioqueue_op_key_t *op_key,
494
Pj_Event_Handler *handler =
495
(Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
497
handler->on_accept_complete((Pj_Async_Op*)op_key, new_sock, status);
501
// Static connect completion callback.
503
static void connect_complete_cb(pj_ioqueue_key_t *key,
506
Pj_Event_Handler *handler =
507
(Pj_Event_Handler*) pj_ioqueue_get_user_data(key);
509
handler->on_connect_complete(status);
514
#endif /* __PJPP_PROACTOR_HPP__ */