Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +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 | #include <pj/activesock.h> |
Benny Prijono | bd344ff | 2008-08-04 09:59:02 +0000 | [diff] [blame] | 20 | #include <pj/compat/socket.h> |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 21 | #include <pj/assert.h> |
| 22 | #include <pj/errno.h> |
| 23 | #include <pj/pool.h> |
| 24 | #include <pj/sock.h> |
| 25 | #include <pj/string.h> |
| 26 | |
| 27 | #define PJ_ACTIVESOCK_MAX_LOOP 50 |
| 28 | |
| 29 | |
| 30 | enum read_type |
| 31 | { |
| 32 | TYPE_NONE, |
| 33 | TYPE_RECV, |
| 34 | TYPE_RECV_FROM |
| 35 | }; |
| 36 | |
| 37 | struct read_op |
| 38 | { |
| 39 | pj_ioqueue_op_key_t op_key; |
| 40 | pj_uint8_t *pkt; |
| 41 | unsigned max_size; |
| 42 | pj_size_t size; |
| 43 | pj_sockaddr src_addr; |
| 44 | int src_addr_len; |
| 45 | }; |
| 46 | |
| 47 | struct accept_op |
| 48 | { |
| 49 | pj_ioqueue_op_key_t op_key; |
| 50 | pj_sock_t new_sock; |
| 51 | pj_sockaddr rem_addr; |
| 52 | int rem_addr_len; |
| 53 | }; |
| 54 | |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 55 | struct send_data |
| 56 | { |
| 57 | pj_uint8_t *data; |
| 58 | pj_ssize_t len; |
| 59 | pj_ssize_t sent; |
| 60 | unsigned flags; |
| 61 | }; |
| 62 | |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 63 | struct pj_activesock_t |
| 64 | { |
| 65 | pj_ioqueue_key_t *key; |
| 66 | pj_bool_t stream_oriented; |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 67 | pj_bool_t whole_data; |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 68 | pj_ioqueue_t *ioqueue; |
| 69 | void *user_data; |
| 70 | unsigned async_count; |
| 71 | unsigned max_loop; |
| 72 | pj_activesock_cb cb; |
| 73 | |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 74 | struct send_data send_data; |
| 75 | |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 76 | struct read_op *read_op; |
| 77 | pj_uint32_t read_flags; |
| 78 | enum read_type read_type; |
| 79 | |
| 80 | struct accept_op *accept_op; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | static void ioqueue_on_read_complete(pj_ioqueue_key_t *key, |
| 85 | pj_ioqueue_op_key_t *op_key, |
| 86 | pj_ssize_t bytes_read); |
| 87 | static void ioqueue_on_write_complete(pj_ioqueue_key_t *key, |
| 88 | pj_ioqueue_op_key_t *op_key, |
| 89 | pj_ssize_t bytes_sent); |
Benny Prijono | 1dd5420 | 2008-07-25 10:45:34 +0000 | [diff] [blame] | 90 | #if PJ_HAS_TCP |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 91 | static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key, |
| 92 | pj_ioqueue_op_key_t *op_key, |
| 93 | pj_sock_t sock, |
| 94 | pj_status_t status); |
| 95 | static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key, |
| 96 | pj_status_t status); |
Benny Prijono | 1dd5420 | 2008-07-25 10:45:34 +0000 | [diff] [blame] | 97 | #endif |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 98 | |
| 99 | PJ_DEF(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg) |
| 100 | { |
| 101 | pj_bzero(cfg, sizeof(*cfg)); |
| 102 | cfg->async_cnt = 1; |
| 103 | cfg->concurrency = -1; |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 104 | cfg->whole_data = PJ_TRUE; |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | PJ_DEF(pj_status_t) pj_activesock_create( pj_pool_t *pool, |
| 109 | pj_sock_t sock, |
| 110 | int sock_type, |
| 111 | const pj_activesock_cfg *opt, |
| 112 | pj_ioqueue_t *ioqueue, |
| 113 | const pj_activesock_cb *cb, |
Benny Prijono | ea8e436 | 2008-06-06 14:12:23 +0000 | [diff] [blame] | 114 | void *user_data, |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 115 | pj_activesock_t **p_asock) |
| 116 | { |
| 117 | pj_activesock_t *asock; |
| 118 | pj_ioqueue_callback ioq_cb; |
| 119 | pj_status_t status; |
| 120 | |
| 121 | PJ_ASSERT_RETURN(pool && ioqueue && cb && p_asock, PJ_EINVAL); |
| 122 | PJ_ASSERT_RETURN(sock!=0 && sock!=PJ_INVALID_SOCKET, PJ_EINVAL); |
| 123 | PJ_ASSERT_RETURN(sock_type==pj_SOCK_STREAM() || |
| 124 | sock_type==pj_SOCK_DGRAM(), PJ_EINVAL); |
| 125 | PJ_ASSERT_RETURN(!opt || opt->async_cnt >= 1, PJ_EINVAL); |
| 126 | |
| 127 | asock = PJ_POOL_ZALLOC_T(pool, pj_activesock_t); |
| 128 | asock->ioqueue = ioqueue; |
| 129 | asock->stream_oriented = (sock_type == pj_SOCK_STREAM()); |
| 130 | asock->async_count = (opt? opt->async_cnt : 1); |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 131 | asock->whole_data = (opt? opt->whole_data : 1); |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 132 | asock->max_loop = PJ_ACTIVESOCK_MAX_LOOP; |
Benny Prijono | ea8e436 | 2008-06-06 14:12:23 +0000 | [diff] [blame] | 133 | asock->user_data = user_data; |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 134 | pj_memcpy(&asock->cb, cb, sizeof(*cb)); |
| 135 | |
| 136 | pj_bzero(&ioq_cb, sizeof(ioq_cb)); |
| 137 | ioq_cb.on_read_complete = &ioqueue_on_read_complete; |
| 138 | ioq_cb.on_write_complete = &ioqueue_on_write_complete; |
Benny Prijono | 1dd5420 | 2008-07-25 10:45:34 +0000 | [diff] [blame] | 139 | #if PJ_HAS_TCP |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 140 | ioq_cb.on_connect_complete = &ioqueue_on_connect_complete; |
| 141 | ioq_cb.on_accept_complete = &ioqueue_on_accept_complete; |
Benny Prijono | 1dd5420 | 2008-07-25 10:45:34 +0000 | [diff] [blame] | 142 | #endif |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 143 | |
| 144 | status = pj_ioqueue_register_sock(pool, ioqueue, sock, asock, |
| 145 | &ioq_cb, &asock->key); |
| 146 | if (status != PJ_SUCCESS) { |
| 147 | pj_activesock_close(asock); |
| 148 | return status; |
| 149 | } |
| 150 | |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 151 | if (asock->whole_data) { |
| 152 | /* Must disable concurrency otherwise there is a race condition */ |
| 153 | pj_ioqueue_set_concurrency(asock->key, 0); |
| 154 | } else if (opt && opt->concurrency >= 0) { |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 155 | pj_ioqueue_set_concurrency(asock->key, opt->concurrency); |
| 156 | } |
| 157 | |
| 158 | *p_asock = asock; |
| 159 | return PJ_SUCCESS; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | PJ_DEF(pj_status_t) pj_activesock_create_udp( pj_pool_t *pool, |
| 164 | const pj_sockaddr *addr, |
| 165 | const pj_activesock_cfg *opt, |
| 166 | pj_ioqueue_t *ioqueue, |
| 167 | const pj_activesock_cb *cb, |
Benny Prijono | ea8e436 | 2008-06-06 14:12:23 +0000 | [diff] [blame] | 168 | void *user_data, |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 169 | pj_activesock_t **p_asock, |
| 170 | pj_sockaddr *bound_addr) |
| 171 | { |
| 172 | pj_sock_t sock_fd; |
| 173 | pj_sockaddr default_addr; |
| 174 | pj_status_t status; |
| 175 | |
| 176 | if (addr == NULL) { |
| 177 | pj_sockaddr_init(pj_AF_INET(), &default_addr, NULL, 0); |
| 178 | addr = &default_addr; |
| 179 | } |
| 180 | |
| 181 | status = pj_sock_socket(addr->addr.sa_family, pj_SOCK_DGRAM(), 0, |
| 182 | &sock_fd); |
| 183 | if (status != PJ_SUCCESS) { |
| 184 | return status; |
| 185 | } |
| 186 | |
| 187 | status = pj_sock_bind(sock_fd, addr, pj_sockaddr_get_len(addr)); |
| 188 | if (status != PJ_SUCCESS) { |
| 189 | pj_sock_close(sock_fd); |
| 190 | return status; |
| 191 | } |
| 192 | |
| 193 | status = pj_activesock_create(pool, sock_fd, pj_SOCK_DGRAM(), opt, |
Benny Prijono | ea8e436 | 2008-06-06 14:12:23 +0000 | [diff] [blame] | 194 | ioqueue, cb, user_data, p_asock); |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 195 | if (status != PJ_SUCCESS) { |
| 196 | pj_sock_close(sock_fd); |
| 197 | return status; |
| 198 | } |
| 199 | |
| 200 | if (bound_addr) { |
| 201 | int addr_len = sizeof(*bound_addr); |
| 202 | status = pj_sock_getsockname(sock_fd, bound_addr, &addr_len); |
| 203 | if (status != PJ_SUCCESS) { |
| 204 | pj_activesock_close(*p_asock); |
| 205 | return status; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return PJ_SUCCESS; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | PJ_DEF(pj_status_t) pj_activesock_close(pj_activesock_t *asock) |
| 214 | { |
| 215 | PJ_ASSERT_RETURN(asock, PJ_EINVAL); |
| 216 | if (asock->key) { |
| 217 | pj_ioqueue_unregister(asock->key); |
| 218 | asock->key = NULL; |
| 219 | } |
| 220 | return PJ_SUCCESS; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | PJ_DEF(pj_status_t) pj_activesock_set_user_data( pj_activesock_t *asock, |
| 225 | void *user_data) |
| 226 | { |
| 227 | PJ_ASSERT_RETURN(asock, PJ_EINVAL); |
| 228 | asock->user_data = user_data; |
| 229 | return PJ_SUCCESS; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | PJ_DEF(void*) pj_activesock_get_user_data(pj_activesock_t *asock) |
| 234 | { |
| 235 | PJ_ASSERT_RETURN(asock, NULL); |
| 236 | return asock->user_data; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | PJ_DEF(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock, |
| 241 | pj_pool_t *pool, |
| 242 | unsigned buff_size, |
| 243 | pj_uint32_t flags) |
| 244 | { |
Benny Prijono | bd344ff | 2008-08-04 09:59:02 +0000 | [diff] [blame] | 245 | void **readbuf; |
| 246 | unsigned i; |
| 247 | |
| 248 | PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 249 | |
| 250 | readbuf = (void**) pj_pool_calloc(pool, asock->async_count, |
| 251 | sizeof(void*)); |
| 252 | |
| 253 | for (i=0; i<asock->async_count; ++i) { |
| 254 | readbuf[i] = pj_pool_alloc(pool, buff_size); |
| 255 | } |
| 256 | |
| 257 | return pj_activesock_start_read2(asock, pool, buff_size, readbuf, flags); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | PJ_DEF(pj_status_t) pj_activesock_start_read2( pj_activesock_t *asock, |
| 262 | pj_pool_t *pool, |
| 263 | unsigned buff_size, |
| 264 | void *readbuf[], |
| 265 | pj_uint32_t flags) |
| 266 | { |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 267 | unsigned i; |
| 268 | pj_status_t status; |
| 269 | |
| 270 | PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 271 | PJ_ASSERT_RETURN(asock->read_type == TYPE_NONE, PJ_EINVALIDOP); |
| 272 | PJ_ASSERT_RETURN(asock->read_op == NULL, PJ_EINVALIDOP); |
| 273 | |
| 274 | asock->read_op = (struct read_op*) |
| 275 | pj_pool_calloc(pool, asock->async_count, |
| 276 | sizeof(struct read_op)); |
| 277 | asock->read_type = TYPE_RECV; |
| 278 | asock->read_flags = flags; |
| 279 | |
| 280 | for (i=0; i<asock->async_count; ++i) { |
| 281 | struct read_op *r = &asock->read_op[i]; |
| 282 | pj_ssize_t size_to_read; |
| 283 | |
Benny Prijono | bd344ff | 2008-08-04 09:59:02 +0000 | [diff] [blame] | 284 | r->pkt = (pj_uint8_t*)readbuf[i]; |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 285 | r->max_size = size_to_read = buff_size; |
| 286 | |
| 287 | status = pj_ioqueue_recv(asock->key, &r->op_key, r->pkt, &size_to_read, |
| 288 | PJ_IOQUEUE_ALWAYS_ASYNC | flags); |
| 289 | PJ_ASSERT_RETURN(status != PJ_SUCCESS, PJ_EBUG); |
| 290 | |
| 291 | if (status != PJ_EPENDING) |
| 292 | return status; |
| 293 | } |
| 294 | |
| 295 | return PJ_SUCCESS; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | PJ_DEF(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock, |
| 300 | pj_pool_t *pool, |
| 301 | unsigned buff_size, |
| 302 | pj_uint32_t flags) |
| 303 | { |
Benny Prijono | bd344ff | 2008-08-04 09:59:02 +0000 | [diff] [blame] | 304 | void **readbuf; |
| 305 | unsigned i; |
| 306 | |
| 307 | PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 308 | |
| 309 | readbuf = (void**) pj_pool_calloc(pool, asock->async_count, |
| 310 | sizeof(void*)); |
| 311 | |
| 312 | for (i=0; i<asock->async_count; ++i) { |
| 313 | readbuf[i] = pj_pool_alloc(pool, buff_size); |
| 314 | } |
| 315 | |
| 316 | return pj_activesock_start_recvfrom2(asock, pool, buff_size, |
| 317 | readbuf, flags); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | PJ_DEF(pj_status_t) pj_activesock_start_recvfrom2( pj_activesock_t *asock, |
| 322 | pj_pool_t *pool, |
| 323 | unsigned buff_size, |
| 324 | void *readbuf[], |
| 325 | pj_uint32_t flags) |
| 326 | { |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 327 | unsigned i; |
| 328 | pj_status_t status; |
| 329 | |
| 330 | PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 331 | PJ_ASSERT_RETURN(asock->read_type == TYPE_NONE, PJ_EINVALIDOP); |
| 332 | |
| 333 | asock->read_op = (struct read_op*) |
| 334 | pj_pool_calloc(pool, asock->async_count, |
| 335 | sizeof(struct read_op)); |
| 336 | asock->read_type = TYPE_RECV_FROM; |
| 337 | asock->read_flags = flags; |
| 338 | |
| 339 | for (i=0; i<asock->async_count; ++i) { |
| 340 | struct read_op *r = &asock->read_op[i]; |
| 341 | pj_ssize_t size_to_read; |
| 342 | |
Benny Prijono | bd344ff | 2008-08-04 09:59:02 +0000 | [diff] [blame] | 343 | r->pkt = (pj_uint8_t*) readbuf[i]; |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 344 | r->max_size = size_to_read = buff_size; |
| 345 | r->src_addr_len = sizeof(r->src_addr); |
| 346 | |
| 347 | status = pj_ioqueue_recvfrom(asock->key, &r->op_key, r->pkt, |
| 348 | &size_to_read, |
| 349 | PJ_IOQUEUE_ALWAYS_ASYNC | flags, |
| 350 | &r->src_addr, &r->src_addr_len); |
| 351 | PJ_ASSERT_RETURN(status != PJ_SUCCESS, PJ_EBUG); |
| 352 | |
| 353 | if (status != PJ_EPENDING) |
| 354 | return status; |
| 355 | } |
| 356 | |
| 357 | return PJ_SUCCESS; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | static void ioqueue_on_read_complete(pj_ioqueue_key_t *key, |
| 362 | pj_ioqueue_op_key_t *op_key, |
| 363 | pj_ssize_t bytes_read) |
| 364 | { |
| 365 | pj_activesock_t *asock; |
| 366 | struct read_op *r = (struct read_op*)op_key; |
| 367 | unsigned loop = 0; |
| 368 | pj_status_t status; |
| 369 | |
| 370 | asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 371 | |
| 372 | do { |
| 373 | unsigned flags; |
| 374 | |
| 375 | if (bytes_read > 0) { |
| 376 | /* |
| 377 | * We've got new data. |
| 378 | */ |
| 379 | pj_size_t remainder; |
| 380 | pj_bool_t ret; |
| 381 | |
| 382 | /* Append this new data to existing data. If socket is stream |
| 383 | * oriented, user might have left some data in the buffer. |
| 384 | * Otherwise if socket is datagram there will be nothing in |
| 385 | * existing packet hence the packet will contain only the new |
| 386 | * packet. |
| 387 | */ |
| 388 | r->size += bytes_read; |
| 389 | |
| 390 | /* Set default remainder to zero */ |
| 391 | remainder = 0; |
| 392 | |
| 393 | /* And return value to TRUE */ |
| 394 | ret = PJ_TRUE; |
| 395 | |
| 396 | /* Notify callback */ |
| 397 | if (asock->read_type == TYPE_RECV && asock->cb.on_data_read) { |
| 398 | ret = (*asock->cb.on_data_read)(asock, r->pkt, r->size, |
| 399 | PJ_SUCCESS, &remainder); |
| 400 | } else if (asock->read_type == TYPE_RECV_FROM && |
| 401 | asock->cb.on_data_recvfrom) |
| 402 | { |
| 403 | ret = (*asock->cb.on_data_recvfrom)(asock, r->pkt, r->size, |
| 404 | &r->src_addr, |
| 405 | r->src_addr_len, |
| 406 | PJ_SUCCESS); |
| 407 | } |
| 408 | |
| 409 | /* If callback returns false, we have been destroyed! */ |
| 410 | if (!ret) |
| 411 | return; |
| 412 | |
| 413 | /* Only stream oriented socket may leave data in the packet */ |
| 414 | if (asock->stream_oriented) { |
| 415 | r->size = remainder; |
| 416 | } else { |
| 417 | r->size = 0; |
| 418 | } |
| 419 | |
Benny Prijono | bd344ff | 2008-08-04 09:59:02 +0000 | [diff] [blame] | 420 | } else if (bytes_read <= 0 && |
| 421 | -bytes_read != PJ_STATUS_FROM_OS(OSERR_EWOULDBLOCK) && |
| 422 | -bytes_read != PJ_STATUS_FROM_OS(OSERR_EINPROGRESS) && |
| 423 | -bytes_read != PJ_STATUS_FROM_OS(OSERR_ECONNRESET)) |
| 424 | { |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 425 | pj_size_t remainder; |
| 426 | pj_bool_t ret; |
| 427 | |
| 428 | if (bytes_read == 0) { |
| 429 | /* For stream/connection oriented socket, this means the |
| 430 | * connection has been closed. For datagram sockets, it means |
| 431 | * we've received datagram with zero length. |
| 432 | */ |
| 433 | if (asock->stream_oriented) |
| 434 | status = PJ_EEOF; |
| 435 | else |
| 436 | status = PJ_SUCCESS; |
| 437 | } else { |
| 438 | /* This means we've got an error. If this is stream/connection |
| 439 | * oriented, it means connection has been closed. For datagram |
| 440 | * sockets, it means we've got some error (e.g. EWOULDBLOCK). |
| 441 | */ |
| 442 | status = -bytes_read; |
| 443 | } |
| 444 | |
| 445 | /* Set default remainder to zero */ |
| 446 | remainder = 0; |
| 447 | |
| 448 | /* And return value to TRUE */ |
| 449 | ret = PJ_TRUE; |
| 450 | |
| 451 | /* Notify callback */ |
| 452 | if (asock->read_type == TYPE_RECV && asock->cb.on_data_read) { |
| 453 | /* For connection oriented socket, we still need to report |
| 454 | * the remainder data (if any) to the user to let user do |
| 455 | * processing with the remainder data before it closes the |
| 456 | * connection. |
| 457 | * If there is no remainder data, set the packet to NULL. |
| 458 | */ |
| 459 | ret = (*asock->cb.on_data_read)(asock, (r->size? r->pkt:NULL), |
| 460 | r->size, status, &remainder); |
| 461 | |
| 462 | } else if (asock->read_type == TYPE_RECV_FROM && |
| 463 | asock->cb.on_data_recvfrom) |
| 464 | { |
| 465 | /* This would always be datagram oriented hence there's |
| 466 | * nothing in the packet. We can't be sure if there will be |
| 467 | * anything useful in the source_addr, so just put NULL |
| 468 | * there too. |
| 469 | */ |
Benny Prijono | 758decb | 2008-08-26 17:10:51 +0000 | [diff] [blame] | 470 | /* In some scenarios, status may be PJ_SUCCESS. The upper |
| 471 | * layer application may not expect the callback to be called |
| 472 | * with successful status and NULL data, so lets not call the |
| 473 | * callback if the status is PJ_SUCCESS. |
| 474 | */ |
| 475 | if (status != PJ_SUCCESS ) { |
| 476 | ret = (*asock->cb.on_data_recvfrom)(asock, NULL, 0, |
| 477 | NULL, 0, status); |
| 478 | } |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* If callback returns false, we have been destroyed! */ |
| 482 | if (!ret) |
| 483 | return; |
| 484 | |
| 485 | /* Only stream oriented socket may leave data in the packet */ |
| 486 | if (asock->stream_oriented) { |
| 487 | r->size = remainder; |
| 488 | } else { |
| 489 | r->size = 0; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /* Read next data. We limit ourselves to processing max_loop immediate |
| 494 | * data, so when the loop counter has exceeded this value, force the |
| 495 | * read()/recvfrom() to return pending operation to allow the program |
| 496 | * to do other jobs. |
| 497 | */ |
| 498 | bytes_read = r->max_size - r->size; |
| 499 | flags = asock->read_flags; |
| 500 | if (++loop >= asock->max_loop) |
| 501 | flags |= PJ_IOQUEUE_ALWAYS_ASYNC; |
| 502 | |
| 503 | if (asock->read_type == TYPE_RECV) { |
| 504 | status = pj_ioqueue_recv(key, op_key, r->pkt + r->size, |
| 505 | &bytes_read, flags); |
| 506 | } else { |
| 507 | r->src_addr_len = sizeof(r->src_addr); |
| 508 | status = pj_ioqueue_recvfrom(key, op_key, r->pkt + r->size, |
| 509 | &bytes_read, flags, |
| 510 | &r->src_addr, &r->src_addr_len); |
| 511 | } |
| 512 | |
Benny Prijono | 758decb | 2008-08-26 17:10:51 +0000 | [diff] [blame] | 513 | if (status != PJ_EPENDING && status != PJ_ECANCELLED) { |
| 514 | bytes_read = -status; |
| 515 | } else { |
| 516 | break; |
| 517 | } |
| 518 | } while (1); |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 519 | |
| 520 | } |
| 521 | |
| 522 | |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 523 | static pj_status_t send_remaining(pj_activesock_t *asock, |
| 524 | pj_ioqueue_op_key_t *send_key) |
| 525 | { |
| 526 | struct send_data *sd = (struct send_data*)send_key->activesock_data; |
| 527 | pj_status_t status; |
| 528 | |
| 529 | do { |
| 530 | pj_ssize_t size; |
| 531 | |
| 532 | size = sd->len - sd->sent; |
| 533 | status = pj_ioqueue_send(asock->key, send_key, |
| 534 | sd->data+sd->sent, &size, sd->flags); |
| 535 | if (status != PJ_SUCCESS) { |
| 536 | /* Pending or error */ |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | sd->sent += size; |
| 541 | if (sd->sent == sd->len) { |
| 542 | /* The whole data has been sent. */ |
| 543 | return PJ_SUCCESS; |
| 544 | } |
| 545 | |
| 546 | } while (sd->sent < sd->len); |
| 547 | |
| 548 | return status; |
| 549 | } |
| 550 | |
| 551 | |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 552 | PJ_DEF(pj_status_t) pj_activesock_send( pj_activesock_t *asock, |
| 553 | pj_ioqueue_op_key_t *send_key, |
| 554 | const void *data, |
| 555 | pj_ssize_t *size, |
| 556 | unsigned flags) |
| 557 | { |
| 558 | PJ_ASSERT_RETURN(asock && send_key && data && size, PJ_EINVAL); |
| 559 | |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 560 | send_key->activesock_data = NULL; |
| 561 | |
| 562 | if (asock->whole_data) { |
| 563 | pj_ssize_t whole; |
| 564 | pj_status_t status; |
| 565 | |
| 566 | whole = *size; |
| 567 | |
| 568 | status = pj_ioqueue_send(asock->key, send_key, data, size, flags); |
| 569 | if (status != PJ_SUCCESS) { |
| 570 | /* Pending or error */ |
| 571 | return status; |
| 572 | } |
| 573 | |
| 574 | if (*size == whole) { |
| 575 | /* The whole data has been sent. */ |
| 576 | return PJ_SUCCESS; |
| 577 | } |
| 578 | |
| 579 | /* Data was partially sent */ |
| 580 | asock->send_data.data = (pj_uint8_t*)data; |
| 581 | asock->send_data.len = whole; |
| 582 | asock->send_data.sent = *size; |
| 583 | asock->send_data.flags = flags; |
| 584 | send_key->activesock_data = &asock->send_data; |
| 585 | |
| 586 | /* Try again */ |
| 587 | status = send_remaining(asock, send_key); |
| 588 | if (status == PJ_SUCCESS) { |
| 589 | *size = whole; |
| 590 | } |
| 591 | return status; |
| 592 | |
| 593 | } else { |
| 594 | return pj_ioqueue_send(asock->key, send_key, data, size, flags); |
| 595 | } |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
| 599 | PJ_DEF(pj_status_t) pj_activesock_sendto( pj_activesock_t *asock, |
| 600 | pj_ioqueue_op_key_t *send_key, |
| 601 | const void *data, |
| 602 | pj_ssize_t *size, |
| 603 | unsigned flags, |
| 604 | const pj_sockaddr_t *addr, |
| 605 | int addr_len) |
| 606 | { |
| 607 | PJ_ASSERT_RETURN(asock && send_key && data && size && addr && addr_len, |
| 608 | PJ_EINVAL); |
| 609 | |
| 610 | return pj_ioqueue_sendto(asock->key, send_key, data, size, flags, |
| 611 | addr, addr_len); |
| 612 | } |
| 613 | |
| 614 | |
| 615 | static void ioqueue_on_write_complete(pj_ioqueue_key_t *key, |
| 616 | pj_ioqueue_op_key_t *op_key, |
| 617 | pj_ssize_t bytes_sent) |
| 618 | { |
| 619 | pj_activesock_t *asock; |
| 620 | |
| 621 | asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 622 | |
Benny Prijono | 417d605 | 2008-07-29 20:15:15 +0000 | [diff] [blame] | 623 | if (bytes_sent > 0 && op_key->activesock_data) { |
| 624 | /* whole_data is requested. Make sure we send all the data */ |
| 625 | struct send_data *sd = (struct send_data*)op_key->activesock_data; |
| 626 | |
| 627 | sd->sent += bytes_sent; |
| 628 | if (sd->sent == sd->len) { |
| 629 | /* all has been sent */ |
| 630 | bytes_sent = sd->sent; |
| 631 | op_key->activesock_data = NULL; |
| 632 | } else { |
| 633 | /* send remaining data */ |
| 634 | pj_status_t status; |
| 635 | |
| 636 | status = send_remaining(asock, op_key); |
| 637 | if (status == PJ_EPENDING) |
| 638 | return; |
| 639 | else if (status == PJ_SUCCESS) |
| 640 | bytes_sent = sd->sent; |
| 641 | else |
| 642 | bytes_sent = -status; |
| 643 | |
| 644 | op_key->activesock_data = NULL; |
| 645 | } |
| 646 | } |
| 647 | |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 648 | if (asock->cb.on_data_sent) { |
| 649 | pj_bool_t ret; |
| 650 | |
| 651 | ret = (*asock->cb.on_data_sent)(asock, op_key, bytes_sent); |
| 652 | |
| 653 | /* If callback returns false, we have been destroyed! */ |
| 654 | if (!ret) |
| 655 | return; |
| 656 | } |
| 657 | } |
| 658 | |
Benny Prijono | 1dd5420 | 2008-07-25 10:45:34 +0000 | [diff] [blame] | 659 | #if PJ_HAS_TCP |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 660 | PJ_DEF(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock, |
| 661 | pj_pool_t *pool) |
| 662 | { |
| 663 | unsigned i; |
| 664 | |
| 665 | PJ_ASSERT_RETURN(asock, PJ_EINVAL); |
| 666 | PJ_ASSERT_RETURN(asock->accept_op==NULL, PJ_EINVALIDOP); |
| 667 | |
| 668 | asock->accept_op = (struct accept_op*) |
| 669 | pj_pool_calloc(pool, asock->async_count, |
| 670 | sizeof(struct accept_op)); |
| 671 | for (i=0; i<asock->async_count; ++i) { |
| 672 | struct accept_op *a = &asock->accept_op[i]; |
| 673 | pj_status_t status; |
| 674 | |
| 675 | do { |
| 676 | a->new_sock = PJ_INVALID_SOCKET; |
| 677 | a->rem_addr_len = sizeof(a->rem_addr); |
| 678 | |
| 679 | status = pj_ioqueue_accept(asock->key, &a->op_key, &a->new_sock, |
| 680 | NULL, &a->rem_addr, &a->rem_addr_len); |
| 681 | if (status == PJ_SUCCESS) { |
| 682 | /* We've got immediate connection. Not sure if it's a good |
| 683 | * idea to call the callback now (probably application will |
| 684 | * not be prepared to process it), so lets just silently |
| 685 | * close the socket. |
| 686 | */ |
| 687 | pj_sock_close(a->new_sock); |
| 688 | } |
| 689 | } while (status == PJ_SUCCESS); |
| 690 | |
| 691 | if (status != PJ_EPENDING) { |
| 692 | return status; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | return PJ_SUCCESS; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key, |
| 701 | pj_ioqueue_op_key_t *op_key, |
| 702 | pj_sock_t new_sock, |
| 703 | pj_status_t status) |
| 704 | { |
| 705 | pj_activesock_t *asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 706 | struct accept_op *accept_op = (struct accept_op*) op_key; |
| 707 | |
| 708 | do { |
| 709 | if (status==PJ_SUCCESS && asock->cb.on_accept_complete) { |
| 710 | pj_bool_t ret; |
| 711 | |
| 712 | /* Notify callback */ |
| 713 | ret = (*asock->cb.on_accept_complete)(asock, new_sock, |
| 714 | &accept_op->rem_addr, |
| 715 | accept_op->rem_addr_len); |
| 716 | |
| 717 | /* If callback returns false, we have been destroyed! */ |
| 718 | if (!ret) |
| 719 | return; |
| 720 | |
| 721 | } else if (status==PJ_SUCCESS) { |
| 722 | /* Application doesn't handle the new socket, we need to |
| 723 | * close it to avoid resource leak. |
| 724 | */ |
| 725 | pj_sock_close(accept_op->new_sock); |
| 726 | } |
| 727 | |
| 728 | /* Prepare next accept() */ |
| 729 | accept_op->new_sock = PJ_INVALID_SOCKET; |
| 730 | accept_op->rem_addr_len = sizeof(accept_op->rem_addr); |
| 731 | |
| 732 | status = pj_ioqueue_accept(asock->key, op_key, &accept_op->new_sock, |
| 733 | NULL, &accept_op->rem_addr, |
| 734 | &accept_op->rem_addr_len); |
| 735 | |
| 736 | } while (status != PJ_EPENDING && status != PJ_ECANCELLED); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | PJ_DEF(pj_status_t) pj_activesock_start_connect( pj_activesock_t *asock, |
| 741 | pj_pool_t *pool, |
| 742 | const pj_sockaddr_t *remaddr, |
| 743 | int addr_len) |
| 744 | { |
| 745 | PJ_UNUSED_ARG(pool); |
| 746 | return pj_ioqueue_connect(asock->key, remaddr, addr_len); |
| 747 | } |
| 748 | |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 749 | static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key, |
| 750 | pj_status_t status) |
| 751 | { |
| 752 | pj_activesock_t *asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 753 | |
| 754 | if (asock->cb.on_connect_complete) { |
| 755 | pj_bool_t ret; |
| 756 | |
| 757 | ret = (*asock->cb.on_connect_complete)(asock, status); |
| 758 | |
| 759 | if (!ret) { |
| 760 | /* We've been destroyed */ |
| 761 | return; |
| 762 | } |
| 763 | } |
| 764 | } |
Benny Prijono | 1dd5420 | 2008-07-25 10:45:34 +0000 | [diff] [blame] | 765 | #endif /* PJ_HAS_TCP */ |
Benny Prijono | 4bac2c1 | 2008-05-11 18:12:16 +0000 | [diff] [blame] | 766 | |