Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * 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. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * 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 |
| 18 | */ |
| 19 | |
| 20 | /* ioqueue_common_abs.h |
| 21 | * |
| 22 | * This file contains private declarations for abstracting various |
| 23 | * event polling/dispatching mechanisms (e.g. select, poll, epoll) |
| 24 | * to the ioqueue. |
| 25 | */ |
| 26 | |
| 27 | #include <pj/list.h> |
| 28 | |
| 29 | /* |
| 30 | * The select ioqueue relies on socket functions (pj_sock_xxx()) to return |
| 31 | * the correct error code. |
| 32 | */ |
| 33 | #if PJ_RETURN_OS_ERROR(100) != PJ_STATUS_FROM_OS(100) |
| 34 | # error "Proper error reporting must be enabled for ioqueue to work!" |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | struct generic_operation |
| 39 | { |
| 40 | PJ_DECL_LIST_MEMBER(struct generic_operation); |
| 41 | pj_ioqueue_operation_e op; |
| 42 | }; |
| 43 | |
| 44 | struct read_operation |
| 45 | { |
| 46 | PJ_DECL_LIST_MEMBER(struct read_operation); |
| 47 | pj_ioqueue_operation_e op; |
| 48 | |
| 49 | void *buf; |
| 50 | pj_size_t size; |
| 51 | unsigned flags; |
| 52 | pj_sockaddr_t *rmt_addr; |
| 53 | int *rmt_addrlen; |
| 54 | }; |
| 55 | |
| 56 | struct write_operation |
| 57 | { |
| 58 | PJ_DECL_LIST_MEMBER(struct write_operation); |
| 59 | pj_ioqueue_operation_e op; |
| 60 | |
| 61 | char *buf; |
| 62 | pj_size_t size; |
| 63 | pj_ssize_t written; |
| 64 | unsigned flags; |
| 65 | pj_sockaddr_in rmt_addr; |
| 66 | int rmt_addrlen; |
| 67 | }; |
| 68 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 69 | struct accept_operation |
| 70 | { |
| 71 | PJ_DECL_LIST_MEMBER(struct accept_operation); |
| 72 | pj_ioqueue_operation_e op; |
| 73 | |
| 74 | pj_sock_t *accept_fd; |
| 75 | pj_sockaddr_t *local_addr; |
| 76 | pj_sockaddr_t *rmt_addr; |
| 77 | int *addrlen; |
| 78 | }; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 79 | |
| 80 | union operation_key |
| 81 | { |
| 82 | struct generic_operation generic; |
| 83 | struct read_operation read; |
| 84 | struct write_operation write; |
| 85 | #if PJ_HAS_TCP |
| 86 | struct accept_operation accept; |
| 87 | #endif |
| 88 | }; |
| 89 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 90 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 91 | # define UNREG_FIELDS \ |
| 92 | unsigned ref_count; \ |
| 93 | pj_bool_t closing; \ |
| 94 | pj_time_val free_time; \ |
| 95 | |
| 96 | #else |
| 97 | # define UNREG_FIELDS |
| 98 | #endif |
| 99 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 100 | #define DECLARE_COMMON_KEY \ |
| 101 | PJ_DECL_LIST_MEMBER(struct pj_ioqueue_key_t); \ |
| 102 | pj_ioqueue_t *ioqueue; \ |
| 103 | pj_mutex_t *mutex; \ |
Benny Prijono | ac52df4 | 2006-03-25 10:06:00 +0000 | [diff] [blame] | 104 | pj_bool_t inside_callback; \ |
| 105 | pj_bool_t destroy_requested; \ |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 106 | pj_sock_t fd; \ |
| 107 | int fd_type; \ |
| 108 | void *user_data; \ |
| 109 | pj_ioqueue_callback cb; \ |
| 110 | int connecting; \ |
| 111 | struct read_operation read_list; \ |
| 112 | struct write_operation write_list; \ |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 113 | struct accept_operation accept_list; \ |
| 114 | UNREG_FIELDS |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | #define DECLARE_COMMON_IOQUEUE \ |
| 118 | pj_lock_t *lock; \ |
| 119 | pj_bool_t auto_delete_lock; |
| 120 | |
| 121 | |
| 122 | enum ioqueue_event_type |
| 123 | { |
| 124 | NO_EVENT, |
| 125 | READABLE_EVENT, |
| 126 | WRITEABLE_EVENT, |
| 127 | EXCEPTION_EVENT, |
| 128 | }; |
| 129 | |
| 130 | static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue, |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 131 | pj_ioqueue_key_t *key, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 132 | enum ioqueue_event_type event_type ); |
| 133 | static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue, |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 134 | pj_ioqueue_key_t *key, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 135 | enum ioqueue_event_type event_type); |
| 136 | |