Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame] | 1 | /* $Id$ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 2 | */ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 3 | /* |
| 4 | * sock_select.c |
| 5 | * |
| 6 | * This is the implementation of IOQueue using pj_sock_select(). |
| 7 | * It runs anywhere where pj_sock_select() is available (currently |
| 8 | * Win32, Linux, Linux kernel, etc.). |
| 9 | */ |
| 10 | |
| 11 | #include <pj/ioqueue.h> |
| 12 | #include <pj/os.h> |
| 13 | #include <pj/lock.h> |
| 14 | #include <pj/log.h> |
| 15 | #include <pj/list.h> |
| 16 | #include <pj/pool.h> |
| 17 | #include <pj/string.h> |
| 18 | #include <pj/assert.h> |
| 19 | #include <pj/sock.h> |
| 20 | #include <pj/compat/socket.h> |
| 21 | #include <pj/sock_select.h> |
| 22 | #include <pj/errno.h> |
| 23 | |
| 24 | /* |
| 25 | * ISSUES with ioqueue_select() |
| 26 | * |
| 27 | * EAGAIN/EWOULDBLOCK error in recv(): |
| 28 | * - when multiple threads are working with the ioqueue, application |
| 29 | * may receive EAGAIN or EWOULDBLOCK in the receive callback. |
| 30 | * This error happens because more than one thread is watching for |
| 31 | * the same descriptor set, so when all of them call recv() or recvfrom() |
| 32 | * simultaneously, only one will succeed and the rest will get the error. |
| 33 | * |
| 34 | */ |
| 35 | #define THIS_FILE "ioq_select" |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 36 |
|
| 37 | /*
|
| 38 | * The select ioqueue relies on socket functions (pj_sock_xxx()) to return
|
| 39 | * the correct error code.
|
| 40 | */
|
| 41 | #if PJ_RETURN_OS_ERROR(100) != PJ_STATUS_FROM_OS(100)
|
| 42 | # error "Error reporting must be enabled for this function to work!"
|
| 43 | #endif
|
| 44 |
|
| 45 | /**
|
| 46 | * Get the number of descriptors in the set. This is defined in sock_select.c
|
| 47 | * This function will only return the number of sockets set from PJ_FD_SET
|
| 48 | * operation. When the set is modified by other means (such as by select()),
|
| 49 | * the count will not be reflected here.
|
| 50 | *
|
| 51 | * That's why don't export this function in the header file, to avoid
|
| 52 | * misunderstanding.
|
| 53 | *
|
| 54 | * @param fdsetp The descriptor set.
|
| 55 | *
|
| 56 | * @return Number of descriptors in the set.
|
| 57 | */
|
| 58 | PJ_DECL(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp);
|
| 59 |
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 60 | |
| 61 | |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * During debugging build, VALIDATE_FD_SET is set. |
| 65 | * This will check the validity of the fd_sets. |
| 66 | */ |
| 67 | #if defined(PJ_DEBUG) && PJ_DEBUG != 0 |
| 68 | # define VALIDATE_FD_SET 1 |
| 69 | #else |
| 70 | # define VALIDATE_FD_SET 0 |
| 71 | #endif |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 72 |
|
| 73 | struct generic_operation
|
| 74 | {
|
| 75 | PJ_DECL_LIST_MEMBER(struct generic_operation);
|
| 76 | pj_ioqueue_operation_e op;
|
| 77 | };
|
| 78 |
|
| 79 | struct read_operation
|
| 80 | {
|
| 81 | PJ_DECL_LIST_MEMBER(struct read_operation);
|
| 82 | pj_ioqueue_operation_e op;
|
| 83 |
|
| 84 | void *buf;
|
| 85 | pj_size_t size;
|
| 86 | unsigned flags;
|
| 87 | pj_sockaddr_t *rmt_addr;
|
| 88 | int *rmt_addrlen;
|
| 89 | };
|
| 90 |
|
| 91 | struct write_operation
|
| 92 | {
|
| 93 | PJ_DECL_LIST_MEMBER(struct write_operation);
|
| 94 | pj_ioqueue_operation_e op;
|
| 95 |
|
| 96 | char *buf;
|
| 97 | pj_size_t size;
|
| 98 | pj_ssize_t written;
|
| 99 | unsigned flags;
|
| 100 | pj_sockaddr_in rmt_addr;
|
| 101 | int rmt_addrlen;
|
| 102 | };
|
| 103 |
|
| 104 | #if PJ_HAS_TCP
|
| 105 | struct accept_operation
|
| 106 | {
|
| 107 | PJ_DECL_LIST_MEMBER(struct accept_operation);
|
| 108 | pj_ioqueue_operation_e op;
|
| 109 |
|
| 110 | pj_sock_t *accept_fd;
|
| 111 | pj_sockaddr_t *local_addr;
|
| 112 | pj_sockaddr_t *rmt_addr;
|
| 113 | int *addrlen;
|
| 114 | };
|
| 115 | #endif
|
| 116 |
|
| 117 | union operation_key
|
| 118 | {
|
| 119 | struct generic_operation generic;
|
| 120 | struct read_operation read;
|
| 121 | struct write_operation write;
|
| 122 | #if PJ_HAS_TCP
|
| 123 | struct accept_operation accept;
|
| 124 | #endif
|
| 125 | };
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * This describes each key. |
| 129 | */ |
| 130 | struct pj_ioqueue_key_t |
| 131 | { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 132 | PJ_DECL_LIST_MEMBER(struct pj_ioqueue_key_t);
|
| 133 | pj_ioqueue_t *ioqueue; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 134 | pj_sock_t fd; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 135 | void *user_data; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 136 | pj_ioqueue_callback cb;
|
| 137 | int connecting;
|
| 138 | struct read_operation read_list;
|
| 139 | struct write_operation write_list;
|
| 140 | #if PJ_HAS_TCP
|
| 141 | struct accept_operation accept_list;
|
| 142 | #endif |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * This describes the I/O queue itself. |
| 147 | */ |
| 148 | struct pj_ioqueue_t |
| 149 | { |
| 150 | pj_lock_t *lock; |
| 151 | pj_bool_t auto_delete_lock; |
| 152 | unsigned max, count; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 153 | pj_ioqueue_key_t key_list; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 154 | pj_fd_set_t rfdset; |
| 155 | pj_fd_set_t wfdset; |
| 156 | #if PJ_HAS_TCP |
| 157 | pj_fd_set_t xfdset; |
| 158 | #endif |
| 159 | }; |
| 160 | |
| 161 | /* |
| 162 | * pj_ioqueue_create() |
| 163 | * |
| 164 | * Create select ioqueue. |
| 165 | */ |
| 166 | PJ_DEF(pj_status_t) pj_ioqueue_create( pj_pool_t *pool, |
| 167 | pj_size_t max_fd, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 168 | pj_ioqueue_t **p_ioqueue) |
| 169 | { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 170 | pj_ioqueue_t *ioqueue; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 171 | pj_status_t rc; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 172 |
|
| 173 | /* Check that arguments are valid. */
|
| 174 | PJ_ASSERT_RETURN(pool != NULL && p_ioqueue != NULL &&
|
| 175 | max_fd > 0 && max_fd <= PJ_IOQUEUE_MAX_HANDLES,
|
| 176 | PJ_EINVAL);
|
| 177 |
|
| 178 | /* Check that size of pj_ioqueue_op_key_t is sufficient */
|
| 179 | PJ_ASSERT_RETURN(sizeof(pj_ioqueue_op_key_t)-sizeof(void*) >=
|
| 180 | sizeof(union operation_key), PJ_EBUG);
|
| 181 |
|
| 182 | ioqueue = pj_pool_alloc(pool, sizeof(pj_ioqueue_t)); |
| 183 | ioqueue->max = max_fd; |
| 184 | ioqueue->count = 0; |
| 185 | PJ_FD_ZERO(&ioqueue->rfdset); |
| 186 | PJ_FD_ZERO(&ioqueue->wfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 187 | #if PJ_HAS_TCP |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 188 | PJ_FD_ZERO(&ioqueue->xfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 189 | #endif |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 190 | pj_list_init(&ioqueue->key_list); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 191 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 192 | rc = pj_lock_create_recursive_mutex(pool, "ioq%p", &ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 193 | if (rc != PJ_SUCCESS) |
| 194 | return rc; |
| 195 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 196 | ioqueue->auto_delete_lock = PJ_TRUE; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 197 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 198 | PJ_LOG(4, ("pjlib", "select() I/O Queue created (%p)", ioqueue)); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 199 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 200 | *p_ioqueue = ioqueue; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 201 | return PJ_SUCCESS; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * pj_ioqueue_destroy() |
| 206 | * |
| 207 | * Destroy ioqueue. |
| 208 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 209 | PJ_DEF(pj_status_t) pj_ioqueue_destroy(pj_ioqueue_t *ioqueue) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 210 | { |
| 211 | pj_status_t rc = PJ_SUCCESS; |
| 212 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 213 | PJ_ASSERT_RETURN(ioqueue, PJ_EINVAL); |
| 214 |
|
| 215 | pj_lock_acquire(ioqueue->lock);
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 216 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 217 | if (ioqueue->auto_delete_lock) |
| 218 | rc = pj_lock_destroy(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 219 | |
| 220 | return rc; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 225 | * pj_ioqueue_register_sock() |
| 226 | * |
| 227 | * Register a handle to ioqueue. |
| 228 | */ |
| 229 | PJ_DEF(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool, |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 230 | pj_ioqueue_t *ioqueue, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 231 | pj_sock_t sock, |
| 232 | void *user_data, |
| 233 | const pj_ioqueue_callback *cb, |
| 234 | pj_ioqueue_key_t **p_key) |
| 235 | { |
| 236 | pj_ioqueue_key_t *key = NULL; |
| 237 | pj_uint32_t value; |
| 238 | pj_status_t rc = PJ_SUCCESS; |
| 239 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 240 | PJ_ASSERT_RETURN(pool && ioqueue && sock != PJ_INVALID_SOCKET && |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 241 | cb && p_key, PJ_EINVAL); |
| 242 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 243 | pj_lock_acquire(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 244 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 245 | if (ioqueue->count >= ioqueue->max) { |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 246 | rc = PJ_ETOOMANY; |
| 247 | goto on_return; |
| 248 | } |
| 249 | |
| 250 | /* Set socket to nonblocking. */ |
| 251 | value = 1; |
| 252 | #ifdef PJ_WIN32 |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 253 | if (ioctlsocket(sock, FIONBIO, (u_long*)&value)) { |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 254 | #else |
| 255 | if (ioctl(sock, FIONBIO, &value)) { |
| 256 | #endif |
| 257 | rc = pj_get_netos_error(); |
| 258 | goto on_return; |
| 259 | } |
| 260 | |
| 261 | /* Create key. */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 262 | key = (pj_ioqueue_key_t*)pj_pool_zalloc(pool, sizeof(pj_ioqueue_key_t));
|
| 263 | key->ioqueue = ioqueue; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 264 | key->fd = sock; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 265 | key->user_data = user_data;
|
| 266 | pj_list_init(&key->read_list);
|
| 267 | pj_list_init(&key->write_list);
|
| 268 | #if PJ_HAS_TCP
|
| 269 | pj_list_init(&key->accept_list);
|
| 270 | #endif |
| 271 |
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 272 | /* Save callback. */ |
| 273 | pj_memcpy(&key->cb, cb, sizeof(pj_ioqueue_callback)); |
| 274 | |
| 275 | /* Register */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 276 | pj_list_insert_before(&ioqueue->key_list, key); |
| 277 | ++ioqueue->count; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 278 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 279 | on_return:
|
| 280 | /* On error, socket may be left in non-blocking mode. */ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 281 | *p_key = key; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 282 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 283 | |
| 284 | return rc; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * pj_ioqueue_unregister() |
| 289 | * |
| 290 | * Unregister handle from ioqueue. |
| 291 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 292 | PJ_DEF(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key) |
| 293 | {
|
| 294 | pj_ioqueue_t *ioqueue;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 295 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 296 | PJ_ASSERT_RETURN(key, PJ_EINVAL); |
| 297 |
|
| 298 | ioqueue = key->ioqueue;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 299 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 300 | pj_lock_acquire(ioqueue->lock); |
| 301 | |
| 302 | pj_assert(ioqueue->count > 0); |
| 303 | --ioqueue->count; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 304 | pj_list_erase(key); |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 305 | PJ_FD_CLR(key->fd, &ioqueue->rfdset); |
| 306 | PJ_FD_CLR(key->fd, &ioqueue->wfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 307 | #if PJ_HAS_TCP |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 308 | PJ_FD_CLR(key->fd, &ioqueue->xfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 309 | #endif |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 310 |
|
| 311 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 312 | return PJ_SUCCESS; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * pj_ioqueue_get_user_data() |
| 317 | * |
| 318 | * Obtain value associated with a key. |
| 319 | */ |
| 320 | PJ_DEF(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key ) |
| 321 | { |
| 322 | PJ_ASSERT_RETURN(key != NULL, NULL); |
| 323 | return key->user_data; |
| 324 | } |
| 325 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 326 |
|
| 327 | /*
|
| 328 | * pj_ioqueue_set_user_data()
|
| 329 | */
|
| 330 | PJ_DEF(pj_status_t) pj_ioqueue_set_user_data( pj_ioqueue_key_t *key,
|
| 331 | void *user_data,
|
| 332 | void **old_data)
|
| 333 | {
|
| 334 | PJ_ASSERT_RETURN(key, PJ_EINVAL);
|
| 335 |
|
| 336 | if (old_data)
|
| 337 | *old_data = key->user_data;
|
| 338 | key->user_data = user_data;
|
| 339 |
|
| 340 | return PJ_SUCCESS;
|
| 341 | }
|
| 342 |
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 343 | |
| 344 | /* This supposed to check whether the fd_set values are consistent |
| 345 | * with the operation currently set in each key. |
| 346 | */ |
| 347 | #if VALIDATE_FD_SET |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 348 | static void validate_sets(const pj_ioqueue_t *ioqueue, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 349 | const pj_fd_set_t *rfdset, |
| 350 | const pj_fd_set_t *wfdset, |
| 351 | const pj_fd_set_t *xfdset) |
| 352 | { |
| 353 | pj_ioqueue_key_t *key; |
| 354 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 355 | key = ioqueue->key_list.next; |
| 356 | while (key != &ioqueue->key_list) { |
| 357 | if (!pj_list_empty(&key->read_list) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 358 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 359 | || !pj_list_empty(&key->accept_list) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 360 | #endif |
| 361 | ) |
| 362 | { |
| 363 | pj_assert(PJ_FD_ISSET(key->fd, rfdset)); |
| 364 | } |
| 365 | else { |
| 366 | pj_assert(PJ_FD_ISSET(key->fd, rfdset) == 0); |
| 367 | } |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 368 | if (!pj_list_empty(&key->write_list) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 369 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 370 | || key->connecting |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 371 | #endif |
| 372 | ) |
| 373 | { |
| 374 | pj_assert(PJ_FD_ISSET(key->fd, wfdset)); |
| 375 | } |
| 376 | else { |
| 377 | pj_assert(PJ_FD_ISSET(key->fd, wfdset) == 0); |
| 378 | } |
| 379 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 380 | if (key->connecting) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 381 | { |
| 382 | pj_assert(PJ_FD_ISSET(key->fd, xfdset)); |
| 383 | } |
| 384 | else { |
| 385 | pj_assert(PJ_FD_ISSET(key->fd, xfdset) == 0); |
| 386 | } |
| 387 | #endif /* PJ_HAS_TCP */ |
| 388 | |
| 389 | key = key->next; |
| 390 | } |
| 391 | } |
| 392 | #endif /* VALIDATE_FD_SET */ |
| 393 | |
| 394 | |
| 395 | /* |
| 396 | * pj_ioqueue_poll() |
| 397 | * |
| 398 | * Few things worth written: |
| 399 | * |
| 400 | * - we used to do only one callback called per poll, but it didn't go |
| 401 | * very well. The reason is because on some situation, the write |
| 402 | * callback gets called all the time, thus doesn't give the read |
| 403 | * callback to get called. This happens, for example, when user |
| 404 | * submit write operation inside the write callback. |
| 405 | * As the result, we changed the behaviour so that now multiple |
| 406 | * callbacks are called in a single poll. It should be fast too, |
| 407 | * just that we need to be carefull with the ioqueue data structs. |
| 408 | * |
| 409 | * - to guarantee preemptiveness etc, the poll function must strictly |
| 410 | * work on fd_set copy of the ioqueue (not the original one). |
| 411 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 412 | PJ_DEF(int) pj_ioqueue_poll( pj_ioqueue_t *ioqueue, const pj_time_val *timeout) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 413 | { |
| 414 | pj_fd_set_t rfdset, wfdset, xfdset; |
| 415 | int count; |
| 416 | pj_ioqueue_key_t *h; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 417 |
|
| 418 | PJ_ASSERT_RETURN(ioqueue, PJ_EINVAL);
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 419 | |
| 420 | /* Lock ioqueue before making fd_set copies */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 421 | pj_lock_acquire(ioqueue->lock); |
| 422 |
|
| 423 | /* We will only do select() when there are sockets to be polled.
|
| 424 | * Otherwise select() will return error.
|
| 425 | */ |
| 426 | if (PJ_FD_COUNT(&ioqueue->rfdset)==0 && |
| 427 | PJ_FD_COUNT(&ioqueue->wfdset)==0 && |
| 428 | PJ_FD_COUNT(&ioqueue->xfdset)==0) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 429 | { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 430 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 431 | if (timeout) |
| 432 | pj_thread_sleep(PJ_TIME_VAL_MSEC(*timeout)); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* Copy ioqueue's pj_fd_set_t to local variables. */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 437 | pj_memcpy(&rfdset, &ioqueue->rfdset, sizeof(pj_fd_set_t)); |
| 438 | pj_memcpy(&wfdset, &ioqueue->wfdset, sizeof(pj_fd_set_t)); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 439 | #if PJ_HAS_TCP |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 440 | pj_memcpy(&xfdset, &ioqueue->xfdset, sizeof(pj_fd_set_t)); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 441 | #else |
| 442 | PJ_FD_ZERO(&xfdset); |
| 443 | #endif |
| 444 | |
| 445 | #if VALIDATE_FD_SET |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 446 | validate_sets(ioqueue, &rfdset, &wfdset, &xfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 447 | #endif |
| 448 | |
| 449 | /* Unlock ioqueue before select(). */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 450 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 451 | |
| 452 | count = pj_sock_select(FD_SETSIZE, &rfdset, &wfdset, &xfdset, timeout); |
| 453 | |
| 454 | if (count <= 0) |
| 455 | return count; |
| 456 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 457 | /* Lock ioqueue again before scanning for signalled sockets.
|
| 458 | * We must strictly use recursive mutex since application may invoke
|
| 459 | * the ioqueue again inside the callback.
|
| 460 | */ |
| 461 | pj_lock_acquire(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 462 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 463 | /* Scan for writable sockets first to handle piggy-back data
|
| 464 | * coming with accept().
|
| 465 | */
|
| 466 | h = ioqueue->key_list.next;
|
| 467 | do_writable_scan:
|
| 468 | for ( ; h!=&ioqueue->key_list; h = h->next) {
|
| 469 | if ( (!pj_list_empty(&h->write_list) || h->connecting)
|
| 470 | && PJ_FD_ISSET(h->fd, &wfdset))
|
| 471 | {
|
| 472 | break;
|
| 473 | }
|
| 474 | }
|
| 475 | if (h != &ioqueue->key_list) {
|
| 476 | pj_assert(!pj_list_empty(&h->write_list) || h->connecting);
|
| 477 |
|
| 478 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
|
| 479 | if (h->connecting) {
|
| 480 | /* Completion of connect() operation */
|
| 481 | pj_ssize_t bytes_transfered;
|
| 482 |
|
| 483 | #if (defined(PJ_HAS_SO_ERROR) && PJ_HAS_SO_ERROR!=0)
|
| 484 | /* from connect(2):
|
| 485 | * On Linux, use getsockopt to read the SO_ERROR option at
|
| 486 | * level SOL_SOCKET to determine whether connect() completed
|
| 487 | * successfully (if SO_ERROR is zero).
|
| 488 | */
|
| 489 | int value;
|
| 490 | socklen_t vallen = sizeof(value);
|
| 491 | int gs_rc = getsockopt(h->fd, SOL_SOCKET, SO_ERROR,
|
| 492 | &value, &vallen);
|
| 493 | if (gs_rc != 0) {
|
| 494 | /* Argh!! What to do now???
|
| 495 | * Just indicate that the socket is connected. The
|
| 496 | * application will get error as soon as it tries to use
|
| 497 | * the socket to send/receive.
|
| 498 | */
|
| 499 | bytes_transfered = 0;
|
| 500 | } else {
|
| 501 | bytes_transfered = value;
|
| 502 | }
|
| 503 | #elif defined(PJ_WIN32) && PJ_WIN32!=0
|
| 504 | bytes_transfered = 0; /* success */
|
| 505 | #else
|
| 506 | /* Excellent information in D.J. Bernstein page:
|
| 507 | * http://cr.yp.to/docs/connect.html
|
| 508 | *
|
| 509 | * Seems like the most portable way of detecting connect()
|
| 510 | * failure is to call getpeername(). If socket is connected,
|
| 511 | * getpeername() will return 0. If the socket is not connected,
|
| 512 | * it will return ENOTCONN, and read(fd, &ch, 1) will produce
|
| 513 | * the right errno through error slippage. This is a combination
|
| 514 | * of suggestions from Douglas C. Schmidt and Ken Keys.
|
| 515 | */
|
| 516 | int gp_rc;
|
| 517 | struct sockaddr_in addr;
|
| 518 | socklen_t addrlen = sizeof(addr);
|
| 519 |
|
| 520 | gp_rc = getpeername(h->fd, (struct sockaddr*)&addr, &addrlen);
|
| 521 | bytes_transfered = gp_rc;
|
| 522 | #endif
|
| 523 |
|
| 524 | /* Clear operation. */
|
| 525 | h->connecting = 0;
|
| 526 | PJ_FD_CLR(h->fd, &ioqueue->wfdset);
|
| 527 | PJ_FD_CLR(h->fd, &ioqueue->xfdset);
|
| 528 |
|
| 529 | /* Call callback. */
|
| 530 | if (h->cb.on_connect_complete)
|
| 531 | (*h->cb.on_connect_complete)(h, bytes_transfered);
|
| 532 |
|
| 533 | /* Re-scan writable sockets. */
|
| 534 | goto do_writable_scan;
|
| 535 |
|
| 536 | } else
|
| 537 | #endif /* PJ_HAS_TCP */
|
| 538 | {
|
| 539 | /* Socket is writable. */
|
| 540 | struct write_operation *write_op;
|
| 541 | pj_ssize_t sent;
|
| 542 | pj_status_t send_rc;
|
| 543 |
|
| 544 | /* Get the first in the queue. */
|
| 545 | write_op = h->write_list.next;
|
| 546 |
|
| 547 | /* Send the data. */
|
| 548 | sent = write_op->size - write_op->written;
|
| 549 | if (write_op->op == PJ_IOQUEUE_OP_SEND) {
|
| 550 | send_rc = pj_sock_send(h->fd, write_op->buf+write_op->written,
|
| 551 | &sent, write_op->flags);
|
| 552 | } else if (write_op->op == PJ_IOQUEUE_OP_SEND_TO) {
|
| 553 | send_rc = pj_sock_sendto(h->fd,
|
| 554 | write_op->buf+write_op->written,
|
| 555 | &sent, write_op->flags,
|
| 556 | &write_op->rmt_addr,
|
| 557 | write_op->rmt_addrlen);
|
| 558 | } else {
|
| 559 | pj_assert(!"Invalid operation type!");
|
| 560 | send_rc = PJ_EBUG;
|
| 561 | }
|
| 562 |
|
| 563 | if (send_rc == PJ_SUCCESS) {
|
| 564 | write_op->written += sent;
|
| 565 | } else {
|
| 566 | pj_assert(send_rc > 0);
|
| 567 | write_op->written = -send_rc;
|
| 568 | }
|
| 569 |
|
| 570 | /* In any case we don't need to process this descriptor again. */
|
| 571 | PJ_FD_CLR(h->fd, &wfdset);
|
| 572 |
|
| 573 | /* Are we finished with this buffer? */
|
| 574 | if (send_rc!=PJ_SUCCESS ||
|
| 575 | write_op->written == (pj_ssize_t)write_op->size)
|
| 576 | {
|
| 577 | pj_list_erase(write_op);
|
| 578 |
|
| 579 | /* Clear operation if there's no more data to send. */
|
| 580 | if (pj_list_empty(&h->write_list))
|
| 581 | PJ_FD_CLR(h->fd, &ioqueue->wfdset);
|
| 582 |
|
| 583 | /* Call callback. */
|
| 584 | if (h->cb.on_write_complete) {
|
| 585 | (*h->cb.on_write_complete)(h,
|
| 586 | (pj_ioqueue_op_key_t*)write_op,
|
| 587 | write_op->written);
|
| 588 | }
|
| 589 | }
|
| 590 |
|
| 591 | /* Re-scan writable sockets. */
|
| 592 | goto do_writable_scan;
|
| 593 | }
|
| 594 | }
|
| 595 |
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 596 | /* Scan for readable socket. */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 597 | h = ioqueue->key_list.next; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 598 | do_readable_scan: |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 599 | for ( ; h!=&ioqueue->key_list; h = h->next) { |
| 600 | if ((!pj_list_empty(&h->read_list)
|
| 601 | #if PJ_HAS_TCP
|
| 602 | || !pj_list_empty(&h->accept_list)
|
| 603 | #endif
|
| 604 | ) && PJ_FD_ISSET(h->fd, &rfdset)) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 605 | { |
| 606 | break; |
| 607 | } |
| 608 | } |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 609 | if (h != &ioqueue->key_list) { |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 610 | pj_status_t rc; |
| 611 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 612 | #if PJ_HAS_TCP
|
| 613 | pj_assert(!pj_list_empty(&h->read_list) ||
|
| 614 | !pj_list_empty(&h->accept_list));
|
| 615 | #else
|
| 616 | pj_assert(!pj_list_empty(&h->read_list)); |
| 617 | #endif
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 618 | |
| 619 | # if PJ_HAS_TCP |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 620 | if (!pj_list_empty(&h->accept_list)) {
|
| 621 |
|
| 622 | struct accept_operation *accept_op; |
| 623 |
|
| 624 | /* Get one accept operation from the list. */ |
| 625 | accept_op = h->accept_list.next;
|
| 626 | pj_list_erase(accept_op); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 627 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 628 | rc=pj_sock_accept(h->fd, accept_op->accept_fd,
|
| 629 | accept_op->rmt_addr, accept_op->addrlen); |
| 630 | if (rc==PJ_SUCCESS && accept_op->local_addr) { |
| 631 | rc = pj_sock_getsockname(*accept_op->accept_fd,
|
| 632 | accept_op->local_addr, |
| 633 | accept_op->addrlen); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 636 | /* Clear bit in fdset if there is no more pending accept */
|
| 637 | if (pj_list_empty(&h->accept_list))
|
| 638 | PJ_FD_CLR(h->fd, &ioqueue->rfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 639 | |
| 640 | /* Call callback. */ |
| 641 | if (h->cb.on_accept_complete) |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 642 | (*h->cb.on_accept_complete)(h, (pj_ioqueue_op_key_t*)accept_op,
|
| 643 | *accept_op->accept_fd, rc); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 644 | |
| 645 | /* Re-scan readable sockets. */ |
| 646 | goto do_readable_scan; |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 647 | } |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 648 | else { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 649 | # endif
|
| 650 | struct read_operation *read_op;
|
| 651 | pj_ssize_t bytes_read;
|
| 652 |
|
| 653 | pj_assert(!pj_list_empty(&h->read_list));
|
| 654 |
|
| 655 | /* Get one pending read operation from the list. */
|
| 656 | read_op = h->read_list.next;
|
| 657 | pj_list_erase(read_op);
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 658 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 659 | bytes_read = read_op->size; |
| 660 | |
| 661 | if ((read_op->op == PJ_IOQUEUE_OP_RECV_FROM)) { |
| 662 | rc = pj_sock_recvfrom(h->fd, read_op->buf, &bytes_read, 0, |
| 663 | read_op->rmt_addr,
|
| 664 | read_op->rmt_addrlen); |
| 665 | } else if ((read_op->op == PJ_IOQUEUE_OP_RECV)) { |
| 666 | rc = pj_sock_recv(h->fd, read_op->buf, &bytes_read, 0); |
| 667 | } else {
|
| 668 | pj_assert(read_op->op == PJ_IOQUEUE_OP_READ); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 669 | /* |
| 670 | * User has specified pj_ioqueue_read(). |
| 671 | * On Win32, we should do ReadFile(). But because we got |
| 672 | * here because of select() anyway, user must have put a |
| 673 | * socket descriptor on h->fd, which in this case we can |
| 674 | * just call pj_sock_recv() instead of ReadFile(). |
| 675 | * On Unix, user may put a file in h->fd, so we'll have |
| 676 | * to call read() here. |
| 677 | * This may not compile on systems which doesn't have |
| 678 | * read(). That's why we only specify PJ_LINUX here so |
| 679 | * that error is easier to catch. |
| 680 | */ |
| 681 | # if defined(PJ_WIN32) && PJ_WIN32 != 0 |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 682 | rc = pj_sock_recv(h->fd, read_op->buf, &bytes_read, 0);
|
| 683 | //rc = ReadFile((HANDLE)h->fd, read_op->buf, read_op->size,
|
| 684 | // &bytes_read, NULL);
|
| 685 | # elif (defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0) |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 686 | bytes_read = read(h->fd, h->rd_buf, bytes_read); |
| 687 | rc = (bytes_read >= 0) ? PJ_SUCCESS : pj_get_os_error(); |
| 688 | # elif defined(PJ_LINUX_KERNEL) && PJ_LINUX_KERNEL != 0 |
| 689 | bytes_read = sys_read(h->fd, h->rd_buf, bytes_read); |
| 690 | rc = (bytes_read >= 0) ? PJ_SUCCESS : -bytes_read; |
| 691 | # else |
| 692 | # error "Implement read() for this platform!" |
| 693 | # endif |
| 694 | } |
| 695 | |
| 696 | if (rc != PJ_SUCCESS) { |
| 697 | # if defined(PJ_WIN32) && PJ_WIN32 != 0 |
| 698 | /* On Win32, for UDP, WSAECONNRESET on the receive side |
| 699 | * indicates that previous sending has triggered ICMP Port |
| 700 | * Unreachable message. |
| 701 | * But we wouldn't know at this point which one of previous |
| 702 | * key that has triggered the error, since UDP socket can |
| 703 | * be shared! |
| 704 | * So we'll just ignore it! |
| 705 | */ |
| 706 | |
| 707 | if (rc == PJ_STATUS_FROM_OS(WSAECONNRESET)) { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 708 | //PJ_LOG(4,(THIS_FILE, |
| 709 | // "Ignored ICMP port unreach. on key=%p", h)); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 710 | } |
| 711 | # endif |
| 712 | |
| 713 | /* In any case we would report this to caller. */ |
| 714 | bytes_read = -rc; |
| 715 | } |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 716 |
|
| 717 | /* Clear fdset if there is no pending read. */
|
| 718 | if (pj_list_empty(&h->read_list)) |
| 719 | PJ_FD_CLR(h->fd, &ioqueue->rfdset);
|
| 720 |
|
| 721 | /* In any case clear from temporary set. */ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 722 | PJ_FD_CLR(h->fd, &rfdset); |
| 723 | |
| 724 | /* Call callback. */ |
| 725 | if (h->cb.on_read_complete) |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 726 | (*h->cb.on_read_complete)(h, (pj_ioqueue_op_key_t*)read_op,
|
| 727 | bytes_read); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 728 | |
| 729 | /* Re-scan readable sockets. */ |
| 730 | goto do_readable_scan; |
| 731 | |
| 732 | } |
| 733 | } |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 734 |
|
| 735 | #if PJ_HAS_TCP
|
| 736 | /* Scan for exception socket for TCP connection error. */
|
| 737 | h = ioqueue->key_list.next;
|
| 738 | do_except_scan:
|
| 739 | for ( ; h!=&ioqueue->key_list; h = h->next) {
|
| 740 | if (h->connecting && PJ_FD_ISSET(h->fd, &xfdset))
|
| 741 | break;
|
| 742 | }
|
| 743 | if (h != &ioqueue->key_list) {
|
| 744 |
|
| 745 | pj_assert(h->connecting);
|
| 746 |
|
| 747 | /* Clear operation. */
|
| 748 | h->connecting = 0;
|
| 749 | PJ_FD_CLR(h->fd, &ioqueue->wfdset);
|
| 750 | PJ_FD_CLR(h->fd, &ioqueue->xfdset);
|
| 751 | PJ_FD_CLR(h->fd, &wfdset);
|
| 752 | PJ_FD_CLR(h->fd, &xfdset);
|
| 753 |
|
| 754 | /* Call callback. */
|
| 755 | if (h->cb.on_connect_complete)
|
| 756 | (*h->cb.on_connect_complete)(h, -1);
|
| 757 |
|
| 758 | /* Re-scan exception list. */
|
| 759 | goto do_except_scan;
|
| 760 | }
|
| 761 | #endif /* PJ_HAS_TCP */
|
| 762 |
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 763 | /* Shouldn't happen. */ |
| 764 | /* For strange reason on WinXP select() can return 1 while there is no |
| 765 | * pj_fd_set_t signaled. */ |
| 766 | /* pj_assert(0); */ |
| 767 | |
| 768 | //count = 0; |
| 769 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 770 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 771 | return count; |
| 772 | } |
| 773 | |
| 774 | /* |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 775 | * pj_ioqueue_recv() |
| 776 | * |
| 777 | * Start asynchronous recv() from the socket. |
| 778 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 779 | PJ_DEF(pj_status_t) pj_ioqueue_recv( pj_ioqueue_key_t *key,
|
| 780 | pj_ioqueue_op_key_t *op_key, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 781 | void *buffer, |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 782 | pj_ssize_t *length, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 783 | unsigned flags ) |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 784 | {
|
| 785 | pj_status_t status;
|
| 786 | pj_ssize_t size;
|
| 787 | struct read_operation *read_op;
|
| 788 | pj_ioqueue_t *ioqueue;
|
| 789 | |
| 790 | PJ_ASSERT_RETURN(key && op_key && buffer && length, PJ_EINVAL); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 791 | PJ_CHECK_STACK(); |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 792 |
|
| 793 | /* Try to see if there's data immediately available.
|
| 794 | */
|
| 795 | size = *length;
|
| 796 | status = pj_sock_recv(key->fd, buffer, &size, flags);
|
| 797 | if (status == PJ_SUCCESS) {
|
| 798 | /* Yes! Data is available! */
|
| 799 | *length = size;
|
| 800 | return PJ_SUCCESS;
|
| 801 | } else {
|
| 802 | /* If error is not EWOULDBLOCK (or EAGAIN on Linux), report
|
| 803 | * the error to caller.
|
| 804 | */
|
| 805 | if (status != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL))
|
| 806 | return status;
|
| 807 | } |
| 808 |
|
| 809 | /*
|
| 810 | * No data is immediately available.
|
| 811 | * Must schedule asynchronous operation to the ioqueue.
|
| 812 | */
|
| 813 | ioqueue = key->ioqueue; |
| 814 | pj_lock_acquire(ioqueue->lock); |
| 815 |
|
| 816 | read_op = (struct read_operation*)op_key;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 817 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 818 | read_op->op = PJ_IOQUEUE_OP_RECV; |
| 819 | read_op->buf = buffer; |
| 820 | read_op->size = *length; |
| 821 | read_op->flags = flags;
|
| 822 |
|
| 823 | pj_list_insert_before(&key->read_list, read_op); |
| 824 | PJ_FD_SET(key->fd, &ioqueue->rfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 825 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 826 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 827 | return PJ_EPENDING; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * pj_ioqueue_recvfrom() |
| 832 | * |
| 833 | * Start asynchronous recvfrom() from the socket. |
| 834 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 835 | PJ_DEF(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_key_t *key,
|
| 836 | pj_ioqueue_op_key_t *op_key, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 837 | void *buffer, |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 838 | pj_ssize_t *length, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 839 | unsigned flags, |
| 840 | pj_sockaddr_t *addr, |
| 841 | int *addrlen) |
| 842 | { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 843 | pj_status_t status;
|
| 844 | pj_ssize_t size;
|
| 845 | struct read_operation *read_op;
|
| 846 | pj_ioqueue_t *ioqueue;
|
| 847 |
|
| 848 | PJ_ASSERT_RETURN(key && op_key && buffer && length, PJ_EINVAL);
|
| 849 | PJ_CHECK_STACK();
|
| 850 |
|
| 851 | /* Try to see if there's data immediately available.
|
| 852 | */
|
| 853 | size = *length;
|
| 854 | status = pj_sock_recvfrom(key->fd, buffer, &size, flags,
|
| 855 | addr, addrlen);
|
| 856 | if (status == PJ_SUCCESS) {
|
| 857 | /* Yes! Data is available! */
|
| 858 | *length = size;
|
| 859 | return PJ_SUCCESS;
|
| 860 | } else {
|
| 861 | /* If error is not EWOULDBLOCK (or EAGAIN on Linux), report
|
| 862 | * the error to caller.
|
| 863 | */
|
| 864 | if (status != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL))
|
| 865 | return status;
|
| 866 | }
|
| 867 |
|
| 868 | /*
|
| 869 | * No data is immediately available.
|
| 870 | * Must schedule asynchronous operation to the ioqueue.
|
| 871 | */
|
| 872 | ioqueue = key->ioqueue;
|
| 873 | pj_lock_acquire(ioqueue->lock);
|
| 874 |
|
| 875 | read_op = (struct read_operation*)op_key;
|
| 876 |
|
| 877 | read_op->op = PJ_IOQUEUE_OP_RECV_FROM;
|
| 878 | read_op->buf = buffer;
|
| 879 | read_op->size = *length;
|
| 880 | read_op->flags = flags;
|
| 881 | read_op->rmt_addr = addr;
|
| 882 | read_op->rmt_addrlen = addrlen;
|
| 883 |
|
| 884 | pj_list_insert_before(&key->read_list, read_op);
|
| 885 | PJ_FD_SET(key->fd, &ioqueue->rfdset);
|
| 886 |
|
| 887 | pj_lock_release(ioqueue->lock);
|
| 888 | return PJ_EPENDING;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* |
| 892 | * pj_ioqueue_send() |
| 893 | * |
| 894 | * Start asynchronous send() to the descriptor. |
| 895 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 896 | PJ_DEF(pj_status_t) pj_ioqueue_send( pj_ioqueue_key_t *key,
|
| 897 | pj_ioqueue_op_key_t *op_key, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 898 | const void *data, |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 899 | pj_ssize_t *length, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 900 | unsigned flags) |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 901 | {
|
| 902 | pj_ioqueue_t *ioqueue;
|
| 903 | struct write_operation *write_op; |
| 904 | pj_status_t status;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 905 | pj_ssize_t sent; |
| 906 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 907 | PJ_ASSERT_RETURN(key && op_key && data && length, PJ_EINVAL); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 908 | PJ_CHECK_STACK(); |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 909 |
|
| 910 | /* Fast track:
|
| 911 | * Try to send data immediately, only if there's no pending write!
|
| 912 | * Note:
|
| 913 | * We are speculating that the list is empty here without properly
|
| 914 | * acquiring ioqueue's mutex first. This is intentional, to maximize
|
| 915 | * performance via parallelism.
|
| 916 | *
|
| 917 | * This should be safe, because:
|
| 918 | * - by convention, we require caller to make sure that the
|
| 919 | * key is not unregistered while other threads are invoking
|
| 920 | * an operation on the same key.
|
| 921 | * - pj_list_empty() is safe to be invoked by multiple threads,
|
| 922 | * even when other threads are modifying the list.
|
| 923 | */
|
| 924 | if (pj_list_empty(&key->write_list)) {
|
| 925 | /*
|
| 926 | * See if data can be sent immediately.
|
| 927 | */ |
| 928 | sent = *length; |
| 929 | status = pj_sock_send(key->fd, data, &sent, flags); |
| 930 | if (status == PJ_SUCCESS) {
|
| 931 | /* Success! */
|
| 932 | *length = sent;
|
| 933 | return PJ_SUCCESS;
|
| 934 | } else {
|
| 935 | /* If error is not EWOULDBLOCK (or EAGAIN on Linux), report
|
| 936 | * the error to caller.
|
| 937 | */
|
| 938 | if (status != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) {
|
| 939 | return status;
|
| 940 | }
|
| 941 | }
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 942 | } |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 943 |
|
| 944 | /*
|
| 945 | * Schedule asynchronous send.
|
| 946 | */
|
| 947 | ioqueue = key->ioqueue;
|
| 948 | pj_lock_acquire(ioqueue->lock); |
| 949 |
|
| 950 | write_op = (struct write_operation*)op_key; |
| 951 | write_op->op = PJ_IOQUEUE_OP_SEND; |
| 952 | write_op->buf = NULL;
|
| 953 | write_op->size = *length;
|
| 954 | write_op->written = 0;
|
| 955 | write_op->flags = flags;
|
| 956 |
|
| 957 | pj_list_insert_before(&key->write_list, write_op); |
| 958 | PJ_FD_SET(key->fd, &ioqueue->wfdset); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 959 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 960 | pj_lock_release(ioqueue->lock); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 961 | |
| 962 | return PJ_EPENDING; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | /* |
| 967 | * pj_ioqueue_sendto() |
| 968 | * |
| 969 | * Start asynchronous write() to the descriptor. |
| 970 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 971 | PJ_DEF(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_key_t *key,
|
| 972 | pj_ioqueue_op_key_t *op_key, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 973 | const void *data, |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 974 | pj_ssize_t *length, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 975 | unsigned flags, |
| 976 | const pj_sockaddr_t *addr, |
| 977 | int addrlen) |
| 978 | { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 979 | pj_ioqueue_t *ioqueue;
|
| 980 | struct write_operation *write_op;
|
| 981 | pj_status_t status;
|
| 982 | pj_ssize_t sent;
|
| 983 |
|
| 984 | PJ_ASSERT_RETURN(key && op_key && data && length, PJ_EINVAL);
|
| 985 | PJ_CHECK_STACK();
|
| 986 |
|
| 987 | /* Fast track:
|
| 988 | * Try to send data immediately, only if there's no pending write!
|
| 989 | * Note:
|
| 990 | * We are speculating that the list is empty here without properly
|
| 991 | * acquiring ioqueue's mutex first. This is intentional, to maximize
|
| 992 | * performance via parallelism.
|
| 993 | *
|
| 994 | * This should be safe, because:
|
| 995 | * - by convention, we require caller to make sure that the
|
| 996 | * key is not unregistered while other threads are invoking
|
| 997 | * an operation on the same key.
|
| 998 | * - pj_list_empty() is safe to be invoked by multiple threads,
|
| 999 | * even when other threads are modifying the list.
|
| 1000 | */
|
| 1001 | if (pj_list_empty(&key->write_list)) {
|
| 1002 | /*
|
| 1003 | * See if data can be sent immediately.
|
| 1004 | */
|
| 1005 | sent = *length;
|
| 1006 | status = pj_sock_sendto(key->fd, data, &sent, flags, addr, addrlen);
|
| 1007 | if (status == PJ_SUCCESS) {
|
| 1008 | /* Success! */
|
| 1009 | *length = sent;
|
| 1010 | return PJ_SUCCESS;
|
| 1011 | } else {
|
| 1012 | /* If error is not EWOULDBLOCK (or EAGAIN on Linux), report
|
| 1013 | * the error to caller.
|
| 1014 | */
|
| 1015 | if (status != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) {
|
| 1016 | return status;
|
| 1017 | }
|
| 1018 | }
|
| 1019 | }
|
| 1020 |
|
| 1021 | /*
|
| 1022 | * Check that address storage can hold the address parameter.
|
| 1023 | */
|
| 1024 | PJ_ASSERT_RETURN(addrlen <= sizeof(pj_sockaddr_in), PJ_EBUG);
|
| 1025 |
|
| 1026 | /*
|
| 1027 | * Schedule asynchronous send.
|
| 1028 | */
|
| 1029 | ioqueue = key->ioqueue;
|
| 1030 | pj_lock_acquire(ioqueue->lock);
|
| 1031 |
|
| 1032 | write_op = (struct write_operation*)op_key;
|
| 1033 | write_op->op = PJ_IOQUEUE_OP_SEND_TO;
|
| 1034 | write_op->buf = NULL;
|
| 1035 | write_op->size = *length;
|
| 1036 | write_op->written = 0;
|
| 1037 | write_op->flags = flags;
|
| 1038 | pj_memcpy(&write_op->rmt_addr, addr, addrlen);
|
| 1039 | write_op->rmt_addrlen = addrlen;
|
| 1040 |
|
| 1041 | pj_list_insert_before(&key->write_list, write_op);
|
| 1042 | PJ_FD_SET(key->fd, &ioqueue->wfdset);
|
| 1043 |
|
| 1044 | pj_lock_release(ioqueue->lock);
|
| 1045 |
|
| 1046 | return PJ_EPENDING;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | #if PJ_HAS_TCP |
| 1050 | /* |
| 1051 | * Initiate overlapped accept() operation. |
| 1052 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1053 | PJ_DEF(pj_status_t) pj_ioqueue_accept( pj_ioqueue_key_t *key,
|
| 1054 | pj_ioqueue_op_key_t *op_key, |
| 1055 | pj_sock_t *new_sock, |
| 1056 | pj_sockaddr_t *local, |
| 1057 | pj_sockaddr_t *remote, |
| 1058 | int *addrlen) |
| 1059 | {
|
| 1060 | pj_ioqueue_t *ioqueue;
|
| 1061 | struct accept_operation *accept_op;
|
| 1062 | pj_status_t status;
|
| 1063 | |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1064 | /* check parameters. All must be specified! */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1065 | PJ_ASSERT_RETURN(key && op_key && new_sock, PJ_EINVAL); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1066 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1067 | /* Fast track:
|
| 1068 | * See if there's new connection available immediately.
|
| 1069 | */
|
| 1070 | if (pj_list_empty(&key->accept_list)) {
|
| 1071 | status = pj_sock_accept(key->fd, new_sock, remote, addrlen);
|
| 1072 | if (status == PJ_SUCCESS) {
|
| 1073 | /* Yes! New connection is available! */
|
| 1074 | if (local && addrlen) {
|
| 1075 | status = pj_sock_getsockname(*new_sock, local, addrlen);
|
| 1076 | if (status != PJ_SUCCESS) {
|
| 1077 | pj_sock_close(*new_sock);
|
| 1078 | *new_sock = PJ_INVALID_SOCKET;
|
| 1079 | return status;
|
| 1080 | }
|
| 1081 | }
|
| 1082 | return PJ_SUCCESS;
|
| 1083 | } else {
|
| 1084 | /* If error is not EWOULDBLOCK (or EAGAIN on Linux), report
|
| 1085 | * the error to caller.
|
| 1086 | */
|
| 1087 | if (status != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) {
|
| 1088 | return status;
|
| 1089 | }
|
| 1090 | }
|
| 1091 | }
|
| 1092 |
|
| 1093 | /*
|
| 1094 | * No connection is available immediately.
|
| 1095 | * Schedule accept() operation to be completed when there is incoming
|
| 1096 | * connection available.
|
| 1097 | */
|
| 1098 | ioqueue = key->ioqueue;
|
| 1099 | accept_op = (struct accept_operation*)op_key;
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1100 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1101 | pj_lock_acquire(ioqueue->lock);
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1102 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1103 | accept_op->op = PJ_IOQUEUE_OP_ACCEPT; |
| 1104 | accept_op->accept_fd = new_sock; |
| 1105 | accept_op->rmt_addr = remote; |
| 1106 | accept_op->addrlen= addrlen; |
| 1107 | accept_op->local_addr = local; |
| 1108 |
|
| 1109 | pj_list_insert_before(&key->accept_list, accept_op); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1110 | PJ_FD_SET(key->fd, &ioqueue->rfdset); |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1111 |
|
| 1112 | pj_lock_release(ioqueue->lock);
|
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1113 | |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1114 | return PJ_EPENDING; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * Initiate overlapped connect() operation (well, it's non-blocking actually, |
| 1119 | * since there's no overlapped version of connect()). |
| 1120 | */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1121 | PJ_DEF(pj_status_t) pj_ioqueue_connect( pj_ioqueue_key_t *key, |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1122 | const pj_sockaddr_t *addr, |
| 1123 | int addrlen ) |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1124 | {
|
| 1125 | pj_ioqueue_t *ioqueue; |
| 1126 | pj_status_t status; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* check parameters. All must be specified! */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1129 | PJ_ASSERT_RETURN(key && addr && addrlen, PJ_EINVAL); |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1130 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1131 | /* Check if socket has not been marked for connecting */ |
| 1132 | if (key->connecting != 0)
|
| 1133 | return PJ_EPENDING; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1134 | |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1135 | status = pj_sock_connect(key->fd, addr, addrlen); |
| 1136 | if (status == PJ_SUCCESS) { |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1137 | /* Connected! */ |
| 1138 | return PJ_SUCCESS; |
| 1139 | } else { |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1140 | if (status == PJ_STATUS_FROM_OS(PJ_BLOCKING_CONNECT_ERROR_VAL)) { |
| 1141 | /* Pending! */
|
| 1142 | ioqueue = key->ioqueue; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1143 | pj_lock_acquire(ioqueue->lock); |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1144 | key->connecting = PJ_TRUE; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1145 | PJ_FD_SET(key->fd, &ioqueue->wfdset); |
| 1146 | PJ_FD_SET(key->fd, &ioqueue->xfdset); |
| 1147 | pj_lock_release(ioqueue->lock); |
| 1148 | return PJ_EPENDING; |
| 1149 | } else { |
| 1150 | /* Error! */ |
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame^] | 1151 | return status; |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | #endif /* PJ_HAS_TCP */ |
| 1156 | |