blob: 4d14a8b843944ac9fc77704ff13aca9b459e95f4 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00005 *
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.
10 *
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.
15 *
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
19 */
20
21/* ioqueue_common_abs.h
22 *
23 * This file contains private declarations for abstracting various
24 * event polling/dispatching mechanisms (e.g. select, poll, epoll)
25 * to the ioqueue.
26 */
27
28#include <pj/list.h>
29
30/*
31 * The select ioqueue relies on socket functions (pj_sock_xxx()) to return
32 * the correct error code.
33 */
34#if PJ_RETURN_OS_ERROR(100) != PJ_STATUS_FROM_OS(100)
35# error "Proper error reporting must be enabled for ioqueue to work!"
36#endif
37
38
39struct generic_operation
40{
41 PJ_DECL_LIST_MEMBER(struct generic_operation);
42 pj_ioqueue_operation_e op;
43};
44
45struct read_operation
46{
47 PJ_DECL_LIST_MEMBER(struct read_operation);
48 pj_ioqueue_operation_e op;
49
50 void *buf;
51 pj_size_t size;
52 unsigned flags;
53 pj_sockaddr_t *rmt_addr;
54 int *rmt_addrlen;
55};
56
57struct write_operation
58{
59 PJ_DECL_LIST_MEMBER(struct write_operation);
60 pj_ioqueue_operation_e op;
61
62 char *buf;
63 pj_size_t size;
64 pj_ssize_t written;
65 unsigned flags;
66 pj_sockaddr_in rmt_addr;
67 int rmt_addrlen;
68};
69
Benny Prijono9033e312005-11-21 02:08:39 +000070struct accept_operation
71{
72 PJ_DECL_LIST_MEMBER(struct accept_operation);
73 pj_ioqueue_operation_e op;
74
75 pj_sock_t *accept_fd;
76 pj_sockaddr_t *local_addr;
77 pj_sockaddr_t *rmt_addr;
78 int *addrlen;
79};
Benny Prijono9033e312005-11-21 02:08:39 +000080
81union operation_key
82{
83 struct generic_operation generic;
84 struct read_operation read;
85 struct write_operation write;
86#if PJ_HAS_TCP
87 struct accept_operation accept;
88#endif
89};
90
Benny Prijono5accbd02006-03-30 16:32:18 +000091#if PJ_IOQUEUE_HAS_SAFE_UNREG
92# define UNREG_FIELDS \
93 unsigned ref_count; \
94 pj_bool_t closing; \
95 pj_time_val free_time; \
96
97#else
98# define UNREG_FIELDS
99#endif
100
Benny Prijono9033e312005-11-21 02:08:39 +0000101#define DECLARE_COMMON_KEY \
102 PJ_DECL_LIST_MEMBER(struct pj_ioqueue_key_t); \
103 pj_ioqueue_t *ioqueue; \
104 pj_mutex_t *mutex; \
Benny Prijonoac52df42006-03-25 10:06:00 +0000105 pj_bool_t inside_callback; \
106 pj_bool_t destroy_requested; \
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000107 pj_bool_t allow_concurrent; \
Benny Prijono9033e312005-11-21 02:08:39 +0000108 pj_sock_t fd; \
109 int fd_type; \
110 void *user_data; \
111 pj_ioqueue_callback cb; \
112 int connecting; \
113 struct read_operation read_list; \
114 struct write_operation write_list; \
Benny Prijono5accbd02006-03-30 16:32:18 +0000115 struct accept_operation accept_list; \
116 UNREG_FIELDS
Benny Prijono9033e312005-11-21 02:08:39 +0000117
118
119#define DECLARE_COMMON_IOQUEUE \
120 pj_lock_t *lock; \
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000121 pj_bool_t auto_delete_lock; \
122 pj_bool_t default_concurrency;
Benny Prijono9033e312005-11-21 02:08:39 +0000123
124
125enum ioqueue_event_type
126{
127 NO_EVENT,
128 READABLE_EVENT,
129 WRITEABLE_EVENT,
130 EXCEPTION_EVENT,
131};
132
133static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue,
Benny Prijono63ab3562006-07-08 19:46:43 +0000134 pj_ioqueue_key_t *key,
Benny Prijono9033e312005-11-21 02:08:39 +0000135 enum ioqueue_event_type event_type );
136static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue,
Benny Prijono63ab3562006-07-08 19:46:43 +0000137 pj_ioqueue_key_t *key,
Benny Prijono9033e312005-11-21 02:08:39 +0000138 enum ioqueue_event_type event_type);
139