| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/activesock.h> |
| 20 |
#include <pj/compat/socket.h> |
| 21 |
#include <pj/assert.h> |
| 22 |
#include <pj/errno.h> |
| 23 |
#include <pj/log.h> |
| 24 |
#include <pj/pool.h> |
| 25 |
#include <pj/sock.h> |
| 26 |
#include <pj/string.h> |
| 27 |
|
| 28 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 29 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 30 |
# include <CFNetwork/CFNetwork.h> |
| 31 |
|
| 32 |
static pj_bool_t ios_bg_support = PJ_TRUE; |
| 33 |
#endif |
| 34 |
|
| 35 |
#define PJ_ACTIVESOCK_MAX_LOOP 50 |
| 36 |
|
| 37 |
|
| 38 |
enum read_type |
| 39 |
{ |
| 40 |
TYPE_NONE, |
| 41 |
TYPE_RECV, |
| 42 |
TYPE_RECV_FROM |
| 43 |
}; |
| 44 |
|
| 45 |
enum shutdown_dir |
| 46 |
{ |
| 47 |
SHUT_NONE = 0, |
| 48 |
SHUT_RX = 1, |
| 49 |
SHUT_TX = 2 |
| 50 |
}; |
| 51 |
|
| 52 |
struct read_op |
| 53 |
{ |
| 54 |
pj_ioqueue_op_key_t op_key; |
| 55 |
pj_uint8_t *pkt; |
| 56 |
unsigned max_size; |
| 57 |
pj_size_t size; |
| 58 |
pj_sockaddr src_addr; |
| 59 |
int src_addr_len; |
| 60 |
}; |
| 61 |
|
| 62 |
struct accept_op |
| 63 |
{ |
| 64 |
pj_ioqueue_op_key_t op_key; |
| 65 |
pj_sock_t new_sock; |
| 66 |
pj_sockaddr rem_addr; |
| 67 |
int rem_addr_len; |
| 68 |
}; |
| 69 |
|
| 70 |
struct send_data |
| 71 |
{ |
| 72 |
pj_uint8_t *data; |
| 73 |
pj_ssize_t len; |
| 74 |
pj_ssize_t sent; |
| 75 |
unsigned flags; |
| 76 |
}; |
| 77 |
|
| 78 |
struct pj_activesock_t |
| 79 |
{ |
| 80 |
pj_ioqueue_key_t *key; |
| 81 |
pj_bool_t stream_oriented; |
| 82 |
pj_bool_t whole_data; |
| 83 |
pj_ioqueue_t *ioqueue; |
| 84 |
void *user_data; |
| 85 |
unsigned async_count; |
| 86 |
unsigned shutdown; |
| 87 |
unsigned max_loop; |
| 88 |
pj_activesock_cb cb; |
| 89 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 90 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 91 |
int bg_setting; |
| 92 |
pj_sock_t sock; |
| 93 |
CFReadStreamRef readStream; |
| 94 |
#endif |
| 95 |
|
| 96 |
unsigned err_counter; |
| 97 |
pj_status_t last_err; |
| 98 |
|
| 99 |
struct send_data send_data; |
| 100 |
|
| 101 |
struct read_op *read_op; |
| 102 |
pj_uint32_t read_flags; |
| 103 |
enum read_type read_type; |
| 104 |
|
| 105 |
struct accept_op *accept_op; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
static void ioqueue_on_read_complete(pj_ioqueue_key_t *key, |
| 110 |
pj_ioqueue_op_key_t *op_key, |
| 111 |
pj_ssize_t bytes_read); |
| 112 |
static void ioqueue_on_write_complete(pj_ioqueue_key_t *key, |
| 113 |
pj_ioqueue_op_key_t *op_key, |
| 114 |
pj_ssize_t bytes_sent); |
| 115 |
#if PJ_HAS_TCP |
| 116 |
static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key, |
| 117 |
pj_ioqueue_op_key_t *op_key, |
| 118 |
pj_sock_t sock, |
| 119 |
pj_status_t status); |
| 120 |
static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key, |
| 121 |
pj_status_t status); |
| 122 |
#endif |
| 123 |
|
| 124 |
PJ_DEF(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg) |
| 125 |
{ |
| 126 |
pj_bzero(cfg, sizeof(*cfg)); |
| 127 |
cfg->async_cnt = 1; |
| 128 |
cfg->concurrency = -1; |
| 129 |
cfg->whole_data = PJ_TRUE; |
| 130 |
cfg->sock_cloexec = PJ_TRUE; |
| 131 |
} |
| 132 |
|
| 133 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 134 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 135 |
static void activesock_destroy_iphone_os_stream(pj_activesock_t *asock) |
| 136 |
{ |
| 137 |
if (asock->readStream) { |
| 138 |
CFReadStreamClose(asock->readStream); |
| 139 |
CFRelease(asock->readStream); |
| 140 |
asock->readStream = NULL; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static void activesock_create_iphone_os_stream(pj_activesock_t *asock) |
| 145 |
{ |
| 146 |
#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \ |
| 147 |
__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0) |
| 148 |
|
| 149 |
if (ios_bg_support && asock->bg_setting && asock->stream_oriented) { |
| 150 |
activesock_destroy_iphone_os_stream(asock); |
| 151 |
|
| 152 |
CFStreamCreatePairWithSocket(kCFAllocatorDefault, asock->sock, |
| 153 |
&asock->readStream, NULL); |
| 154 |
|
| 155 |
if (!asock->readStream || |
| 156 |
CFReadStreamSetProperty(asock->readStream, |
| 157 |
kCFStreamNetworkServiceType, |
| 158 |
kCFStreamNetworkServiceTypeVoIP) |
| 159 |
!= TRUE || |
| 160 |
CFReadStreamOpen(asock->readStream) != TRUE) |
| 161 |
{ |
| 162 |
PJ_LOG(2,("", "Failed to configure TCP transport for VoIP " |
| 163 |
"usage. Usage of THIS particular TCP transport in " |
| 164 |
"background mode will not be supported.")); |
| 165 |
|
| 166 |
|
| 167 |
activesock_destroy_iphone_os_stream(asock); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
PJ_DEF(void) pj_activesock_set_iphone_os_bg(pj_activesock_t *asock, |
| 176 |
int val) |
| 177 |
{ |
| 178 |
asock->bg_setting = val; |
| 179 |
if (asock->bg_setting) |
| 180 |
activesock_create_iphone_os_stream(asock); |
| 181 |
else |
| 182 |
activesock_destroy_iphone_os_stream(asock); |
| 183 |
} |
| 184 |
|
| 185 |
PJ_DEF(void) pj_activesock_enable_iphone_os_bg(pj_bool_t val) |
| 186 |
{ |
| 187 |
ios_bg_support = val; |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
PJ_DEF(pj_status_t) pj_activesock_create( pj_pool_t *pool, |
| 192 |
pj_sock_t sock, |
| 193 |
int sock_type, |
| 194 |
const pj_activesock_cfg *opt, |
| 195 |
pj_ioqueue_t *ioqueue, |
| 196 |
const pj_activesock_cb *cb, |
| 197 |
void *user_data, |
| 198 |
pj_activesock_t **p_asock) |
| 199 |
{ |
| 200 |
pj_activesock_t *asock; |
| 201 |
pj_ioqueue_callback ioq_cb; |
| 202 |
pj_status_t status; |
| 203 |
|
| 204 |
PJ_ASSERT_RETURN(pool && ioqueue && cb && p_asock, PJ_EINVAL); |
| 205 |
PJ_ASSERT_RETURN(sock>=0 && sock!=PJ_INVALID_SOCKET, PJ_EINVAL); |
| 206 |
PJ_ASSERT_RETURN((sock_type & 0xF)==pj_SOCK_STREAM() || |
| 207 |
(sock_type & 0xF)==pj_SOCK_DGRAM(), PJ_EINVAL); |
| 208 |
PJ_ASSERT_RETURN(!opt || opt->async_cnt >= 1, PJ_EINVAL); |
| 209 |
|
| 210 |
asock = PJ_POOL_ZALLOC_T(pool, pj_activesock_t); |
| 211 |
asock->ioqueue = ioqueue; |
| 212 |
asock->stream_oriented = ((sock_type & 0xF) == pj_SOCK_STREAM()); |
| 213 |
asock->async_count = (opt? opt->async_cnt : 1); |
| 214 |
asock->whole_data = (opt? opt->whole_data : 1); |
| 215 |
asock->max_loop = PJ_ACTIVESOCK_MAX_LOOP; |
| 216 |
asock->user_data = user_data; |
| 217 |
pj_memcpy(&asock->cb, cb, sizeof(*cb)); |
| 218 |
|
| 219 |
pj_bzero(&ioq_cb, sizeof(ioq_cb)); |
| 220 |
ioq_cb.on_read_complete = &ioqueue_on_read_complete; |
| 221 |
ioq_cb.on_write_complete = &ioqueue_on_write_complete; |
| 222 |
#if PJ_HAS_TCP |
| 223 |
ioq_cb.on_connect_complete = &ioqueue_on_connect_complete; |
| 224 |
ioq_cb.on_accept_complete = &ioqueue_on_accept_complete; |
| 225 |
#endif |
| 226 |
|
| 227 |
status = pj_ioqueue_register_sock2(pool, ioqueue, sock, |
| 228 |
(opt? opt->grp_lock : NULL), |
| 229 |
asock, &ioq_cb, &asock->key); |
| 230 |
if (status != PJ_SUCCESS) { |
| 231 |
pj_activesock_close(asock); |
| 232 |
return status; |
| 233 |
} |
| 234 |
|
| 235 |
if (asock->whole_data) { |
| 236 |
|
| 237 |
pj_ioqueue_set_concurrency(asock->key, 0); |
| 238 |
} else if (opt && opt->concurrency >= 0) { |
| 239 |
pj_ioqueue_set_concurrency(asock->key, opt->concurrency); |
| 240 |
} |
| 241 |
|
| 242 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 243 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 244 |
asock->sock = sock; |
| 245 |
asock->bg_setting = PJ_ACTIVESOCK_TCP_IPHONE_OS_BG; |
| 246 |
#endif |
| 247 |
|
| 248 |
*p_asock = asock; |
| 249 |
return PJ_SUCCESS; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
PJ_DEF(pj_status_t) pj_activesock_create_udp( pj_pool_t *pool, |
| 254 |
const pj_sockaddr *addr, |
| 255 |
const pj_activesock_cfg *opt, |
| 256 |
pj_ioqueue_t *ioqueue, |
| 257 |
const pj_activesock_cb *cb, |
| 258 |
void *user_data, |
| 259 |
pj_activesock_t **p_asock, |
| 260 |
pj_sockaddr *bound_addr) |
| 261 |
{ |
| 262 |
pj_sock_t sock_fd; |
| 263 |
pj_sockaddr default_addr; |
| 264 |
pj_status_t status; |
| 265 |
int sock_type = pj_SOCK_DGRAM(); |
| 266 |
|
| 267 |
if (opt && opt->sock_cloexec) |
| 268 |
sock_type |= pj_SOCK_CLOEXEC(); |
| 269 |
|
| 270 |
if (addr == NULL) { |
| 271 |
pj_sockaddr_init(pj_AF_INET(), &default_addr, NULL, 0); |
| 272 |
addr = &default_addr; |
| 273 |
} |
| 274 |
|
| 275 |
status = pj_sock_socket(addr->addr.sa_family, sock_type, 0, |
| 276 |
&sock_fd); |
| 277 |
if (status != PJ_SUCCESS) { |
| 278 |
return status; |
| 279 |
} |
| 280 |
|
| 281 |
status = pj_sock_bind(sock_fd, addr, pj_sockaddr_get_len(addr)); |
| 282 |
if (status != PJ_SUCCESS) { |
| 283 |
pj_sock_close(sock_fd); |
| 284 |
return status; |
| 285 |
} |
| 286 |
|
| 287 |
status = pj_activesock_create(pool, sock_fd, sock_type, opt, |
| 288 |
ioqueue, cb, user_data, p_asock); |
| 289 |
if (status != PJ_SUCCESS) { |
| 290 |
pj_sock_close(sock_fd); |
| 291 |
return status; |
| 292 |
} |
| 293 |
|
| 294 |
if (bound_addr) { |
| 295 |
int addr_len = sizeof(*bound_addr); |
| 296 |
status = pj_sock_getsockname(sock_fd, bound_addr, &addr_len); |
| 297 |
if (status != PJ_SUCCESS) { |
| 298 |
pj_activesock_close(*p_asock); |
| 299 |
return status; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return PJ_SUCCESS; |
| 304 |
} |
| 305 |
|
| 306 |
PJ_DEF(pj_status_t) pj_activesock_close(pj_activesock_t *asock) |
| 307 |
{ |
| 308 |
pj_ioqueue_key_t *key; |
| 309 |
pj_bool_t unregister = PJ_FALSE; |
| 310 |
|
| 311 |
PJ_ASSERT_RETURN(asock, PJ_EINVAL); |
| 312 |
asock->shutdown = SHUT_RX | SHUT_TX; |
| 313 |
|
| 314 |
|
| 315 |
key = asock->key; |
| 316 |
if (key) { |
| 317 |
pj_ioqueue_lock_key(key); |
| 318 |
unregister = (asock->key != NULL); |
| 319 |
asock->key = NULL; |
| 320 |
pj_ioqueue_unlock_key(key); |
| 321 |
} |
| 322 |
|
| 323 |
if (unregister) { |
| 324 |
pj_ioqueue_unregister(key); |
| 325 |
|
| 326 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 327 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 328 |
activesock_destroy_iphone_os_stream(asock); |
| 329 |
#endif |
| 330 |
} |
| 331 |
return PJ_SUCCESS; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
PJ_DEF(pj_status_t) pj_activesock_set_user_data( pj_activesock_t *asock, |
| 336 |
void *user_data) |
| 337 |
{ |
| 338 |
PJ_ASSERT_RETURN(asock, PJ_EINVAL); |
| 339 |
asock->user_data = user_data; |
| 340 |
return PJ_SUCCESS; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
PJ_DEF(void*) pj_activesock_get_user_data(pj_activesock_t *asock) |
| 345 |
{ |
| 346 |
PJ_ASSERT_RETURN(asock, NULL); |
| 347 |
return asock->user_data; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
PJ_DEF(pj_status_t) pj_activesock_start_read(pj_activesock_t *asock, |
| 352 |
pj_pool_t *pool, |
| 353 |
unsigned buff_size, |
| 354 |
pj_uint32_t flags) |
| 355 |
{ |
| 356 |
void **readbuf; |
| 357 |
unsigned i; |
| 358 |
|
| 359 |
PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 360 |
|
| 361 |
readbuf = (void**) pj_pool_calloc(pool, asock->async_count, |
| 362 |
sizeof(void*)); |
| 363 |
|
| 364 |
for (i=0; i<asock->async_count; ++i) { |
| 365 |
readbuf[i] = pj_pool_alloc(pool, buff_size); |
| 366 |
} |
| 367 |
|
| 368 |
return pj_activesock_start_read2(asock, pool, buff_size, readbuf, flags); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
PJ_DEF(pj_status_t) pj_activesock_start_read2( pj_activesock_t *asock, |
| 373 |
pj_pool_t *pool, |
| 374 |
unsigned buff_size, |
| 375 |
void *readbuf[], |
| 376 |
pj_uint32_t flags) |
| 377 |
{ |
| 378 |
unsigned i; |
| 379 |
pj_status_t status; |
| 380 |
|
| 381 |
PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 382 |
PJ_ASSERT_RETURN(asock->read_type == TYPE_NONE, PJ_EINVALIDOP); |
| 383 |
PJ_ASSERT_RETURN(asock->read_op == NULL, PJ_EINVALIDOP); |
| 384 |
|
| 385 |
asock->read_op = (struct read_op*) |
| 386 |
pj_pool_calloc(pool, asock->async_count, |
| 387 |
sizeof(struct read_op)); |
| 388 |
asock->read_type = TYPE_RECV; |
| 389 |
asock->read_flags = flags; |
| 390 |
|
| 391 |
for (i=0; i<asock->async_count; ++i) { |
| 392 |
struct read_op *r = &asock->read_op[i]; |
| 393 |
pj_ssize_t size_to_read; |
| 394 |
|
| 395 |
r->pkt = (pj_uint8_t*)readbuf[i]; |
| 396 |
size_to_read = r->max_size = buff_size; |
| 397 |
|
| 398 |
status = pj_ioqueue_recv(asock->key, &r->op_key, r->pkt, &size_to_read, |
| 399 |
PJ_IOQUEUE_ALWAYS_ASYNC | flags); |
| 400 |
PJ_ASSERT_RETURN(status != PJ_SUCCESS, PJ_EBUG); |
| 401 |
|
| 402 |
if (status != PJ_EPENDING) |
| 403 |
return status; |
| 404 |
} |
| 405 |
|
| 406 |
return PJ_SUCCESS; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
PJ_DEF(pj_status_t) pj_activesock_start_recvfrom(pj_activesock_t *asock, |
| 411 |
pj_pool_t *pool, |
| 412 |
unsigned buff_size, |
| 413 |
pj_uint32_t flags) |
| 414 |
{ |
| 415 |
void **readbuf; |
| 416 |
unsigned i; |
| 417 |
|
| 418 |
PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 419 |
|
| 420 |
readbuf = (void**) pj_pool_calloc(pool, asock->async_count, |
| 421 |
sizeof(void*)); |
| 422 |
|
| 423 |
for (i=0; i<asock->async_count; ++i) { |
| 424 |
readbuf[i] = pj_pool_alloc(pool, buff_size); |
| 425 |
} |
| 426 |
|
| 427 |
return pj_activesock_start_recvfrom2(asock, pool, buff_size, |
| 428 |
readbuf, flags); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
PJ_DEF(pj_status_t) pj_activesock_start_recvfrom2( pj_activesock_t *asock, |
| 433 |
pj_pool_t *pool, |
| 434 |
unsigned buff_size, |
| 435 |
void *readbuf[], |
| 436 |
pj_uint32_t flags) |
| 437 |
{ |
| 438 |
unsigned i; |
| 439 |
pj_status_t status; |
| 440 |
|
| 441 |
PJ_ASSERT_RETURN(asock && pool && buff_size, PJ_EINVAL); |
| 442 |
PJ_ASSERT_RETURN(asock->read_type == TYPE_NONE, PJ_EINVALIDOP); |
| 443 |
|
| 444 |
asock->read_op = (struct read_op*) |
| 445 |
pj_pool_calloc(pool, asock->async_count, |
| 446 |
sizeof(struct read_op)); |
| 447 |
asock->read_type = TYPE_RECV_FROM; |
| 448 |
asock->read_flags = flags; |
| 449 |
|
| 450 |
for (i=0; i<asock->async_count; ++i) { |
| 451 |
struct read_op *r = &asock->read_op[i]; |
| 452 |
pj_ssize_t size_to_read; |
| 453 |
|
| 454 |
r->pkt = (pj_uint8_t*) readbuf[i]; |
| 455 |
size_to_read = r->max_size = buff_size; |
| 456 |
r->src_addr_len = sizeof(r->src_addr); |
| 457 |
|
| 458 |
status = pj_ioqueue_recvfrom(asock->key, &r->op_key, r->pkt, |
| 459 |
&size_to_read, |
| 460 |
PJ_IOQUEUE_ALWAYS_ASYNC | flags, |
| 461 |
&r->src_addr, &r->src_addr_len); |
| 462 |
PJ_ASSERT_RETURN(status != PJ_SUCCESS, PJ_EBUG); |
| 463 |
|
| 464 |
if (status != PJ_EPENDING) |
| 465 |
return status; |
| 466 |
} |
| 467 |
|
| 468 |
return PJ_SUCCESS; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
static void ioqueue_on_read_complete(pj_ioqueue_key_t *key, |
| 473 |
pj_ioqueue_op_key_t *op_key, |
| 474 |
pj_ssize_t bytes_read) |
| 475 |
{ |
| 476 |
pj_activesock_t *asock; |
| 477 |
struct read_op *r = (struct read_op*)op_key; |
| 478 |
unsigned loop = 0; |
| 479 |
pj_status_t status; |
| 480 |
|
| 481 |
asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 482 |
|
| 483 |
|
| 484 |
if (asock->shutdown & SHUT_RX) |
| 485 |
return; |
| 486 |
|
| 487 |
do { |
| 488 |
unsigned flags; |
| 489 |
|
| 490 |
if (bytes_read > 0) { |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
pj_size_t remainder; |
| 495 |
pj_bool_t ret; |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
r->size += bytes_read; |
| 504 |
|
| 505 |
|
| 506 |
remainder = 0; |
| 507 |
|
| 508 |
|
| 509 |
ret = PJ_TRUE; |
| 510 |
|
| 511 |
|
| 512 |
if (asock->read_type == TYPE_RECV && asock->cb.on_data_read) { |
| 513 |
ret = (*asock->cb.on_data_read)(asock, r->pkt, r->size, |
| 514 |
PJ_SUCCESS, &remainder); |
| 515 |
PJ_ASSERT_ON_FAIL( |
| 516 |
!ret || !asock->stream_oriented || remainder <= r->size, |
| 517 |
{ |
| 518 |
PJ_LOG(2, ("", |
| 519 |
"App bug! Invalid remainder length from " |
| 520 |
"activesock on_data_read().")); |
| 521 |
remainder = 0; |
| 522 |
}); |
| 523 |
} else if (asock->read_type == TYPE_RECV_FROM && |
| 524 |
asock->cb.on_data_recvfrom) |
| 525 |
{ |
| 526 |
ret = (*asock->cb.on_data_recvfrom)(asock, r->pkt, r->size, |
| 527 |
&r->src_addr, |
| 528 |
r->src_addr_len, |
| 529 |
PJ_SUCCESS); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
if (!ret) |
| 534 |
return; |
| 535 |
|
| 536 |
|
| 537 |
if (asock->stream_oriented) { |
| 538 |
r->size = remainder; |
| 539 |
} else { |
| 540 |
r->size = 0; |
| 541 |
} |
| 542 |
|
| 543 |
} else if (bytes_read <= 0 && |
| 544 |
-bytes_read != PJ_STATUS_FROM_OS(OSERR_EWOULDBLOCK) && |
| 545 |
-bytes_read != PJ_STATUS_FROM_OS(OSERR_EINPROGRESS) && |
| 546 |
(asock->stream_oriented || |
| 547 |
-bytes_read != PJ_STATUS_FROM_OS(OSERR_ECONNRESET))) |
| 548 |
{ |
| 549 |
pj_size_t remainder; |
| 550 |
pj_bool_t ret; |
| 551 |
|
| 552 |
if (bytes_read == 0) { |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
if (asock->stream_oriented) |
| 558 |
status = PJ_EEOF; |
| 559 |
else |
| 560 |
status = PJ_SUCCESS; |
| 561 |
} else { |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
status = (pj_status_t)-bytes_read; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
remainder = 0; |
| 571 |
|
| 572 |
|
| 573 |
ret = PJ_TRUE; |
| 574 |
|
| 575 |
|
| 576 |
if (asock->read_type == TYPE_RECV && asock->cb.on_data_read) { |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
ret = (*asock->cb.on_data_read)(asock, r->pkt, r->size, |
| 591 |
status, &remainder); |
| 592 |
PJ_ASSERT_ON_FAIL( |
| 593 |
!ret || !asock->stream_oriented || remainder <= r->size, |
| 594 |
{ |
| 595 |
PJ_LOG(2, ("", |
| 596 |
"App bug! Invalid remainder length from " |
| 597 |
"activesock on_data_read().")); |
| 598 |
remainder = 0; |
| 599 |
}); |
| 600 |
|
| 601 |
} else if (asock->read_type == TYPE_RECV_FROM && |
| 602 |
asock->cb.on_data_recvfrom) |
| 603 |
{ |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
if (status != PJ_SUCCESS ) { |
| 615 |
ret = (*asock->cb.on_data_recvfrom)(asock, NULL, 0, |
| 616 |
NULL, 0, status); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
if (!ret) |
| 622 |
return; |
| 623 |
|
| 624 |
|
| 625 |
if (asock->shutdown & SHUT_RX) |
| 626 |
return; |
| 627 |
|
| 628 |
|
| 629 |
if (asock->stream_oriented) { |
| 630 |
r->size = remainder; |
| 631 |
} else { |
| 632 |
r->size = 0; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
bytes_read = r->max_size - r->size; |
| 642 |
flags = asock->read_flags; |
| 643 |
if (++loop >= asock->max_loop) |
| 644 |
flags |= PJ_IOQUEUE_ALWAYS_ASYNC; |
| 645 |
|
| 646 |
if (asock->read_type == TYPE_RECV) { |
| 647 |
status = pj_ioqueue_recv(key, op_key, r->pkt + r->size, |
| 648 |
&bytes_read, flags); |
| 649 |
} else { |
| 650 |
r->src_addr_len = sizeof(r->src_addr); |
| 651 |
status = pj_ioqueue_recvfrom(key, op_key, r->pkt + r->size, |
| 652 |
&bytes_read, flags, |
| 653 |
&r->src_addr, &r->src_addr_len); |
| 654 |
} |
| 655 |
|
| 656 |
if (status == PJ_SUCCESS) { |
| 657 |
|
| 658 |
; |
| 659 |
} else if (status != PJ_EPENDING && status != PJ_ECANCELLED) { |
| 660 |
|
| 661 |
bytes_read = -status; |
| 662 |
} else { |
| 663 |
break; |
| 664 |
} |
| 665 |
} while (1); |
| 666 |
|
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
static pj_status_t send_remaining(pj_activesock_t *asock, |
| 671 |
pj_ioqueue_op_key_t *send_key) |
| 672 |
{ |
| 673 |
struct send_data *sd = (struct send_data*)send_key->activesock_data; |
| 674 |
pj_status_t status; |
| 675 |
|
| 676 |
do { |
| 677 |
pj_ssize_t size; |
| 678 |
|
| 679 |
size = sd->len - sd->sent; |
| 680 |
status = pj_ioqueue_send(asock->key, send_key, |
| 681 |
sd->data+sd->sent, &size, sd->flags); |
| 682 |
if (status != PJ_SUCCESS) { |
| 683 |
|
| 684 |
break; |
| 685 |
} |
| 686 |
|
| 687 |
sd->sent += size; |
| 688 |
if (sd->sent == sd->len) { |
| 689 |
|
| 690 |
return PJ_SUCCESS; |
| 691 |
} |
| 692 |
|
| 693 |
} while (sd->sent < sd->len); |
| 694 |
|
| 695 |
return status; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
PJ_DEF(pj_status_t) pj_activesock_send( pj_activesock_t *asock, |
| 700 |
pj_ioqueue_op_key_t *send_key, |
| 701 |
const void *data, |
| 702 |
pj_ssize_t *size, |
| 703 |
unsigned flags) |
| 704 |
{ |
| 705 |
PJ_ASSERT_RETURN(asock && send_key && data && size, PJ_EINVAL); |
| 706 |
|
| 707 |
if (asock->shutdown & SHUT_TX) |
| 708 |
return PJ_EINVALIDOP; |
| 709 |
|
| 710 |
send_key->activesock_data = NULL; |
| 711 |
|
| 712 |
if (asock->whole_data) { |
| 713 |
pj_ssize_t whole; |
| 714 |
pj_status_t status; |
| 715 |
|
| 716 |
whole = *size; |
| 717 |
|
| 718 |
status = pj_ioqueue_send(asock->key, send_key, data, size, flags); |
| 719 |
if (status != PJ_SUCCESS) { |
| 720 |
|
| 721 |
return status; |
| 722 |
} |
| 723 |
|
| 724 |
if (*size == whole) { |
| 725 |
|
| 726 |
return PJ_SUCCESS; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
asock->send_data.data = (pj_uint8_t*)data; |
| 731 |
asock->send_data.len = whole; |
| 732 |
asock->send_data.sent = *size; |
| 733 |
asock->send_data.flags = flags; |
| 734 |
send_key->activesock_data = &asock->send_data; |
| 735 |
|
| 736 |
|
| 737 |
status = send_remaining(asock, send_key); |
| 738 |
if (status == PJ_SUCCESS) { |
| 739 |
*size = whole; |
| 740 |
} |
| 741 |
return status; |
| 742 |
|
| 743 |
} else { |
| 744 |
return pj_ioqueue_send(asock->key, send_key, data, size, flags); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
PJ_DEF(pj_status_t) pj_activesock_sendto( pj_activesock_t *asock, |
| 750 |
pj_ioqueue_op_key_t *send_key, |
| 751 |
const void *data, |
| 752 |
pj_ssize_t *size, |
| 753 |
unsigned flags, |
| 754 |
const pj_sockaddr_t *addr, |
| 755 |
int addr_len) |
| 756 |
{ |
| 757 |
PJ_ASSERT_RETURN(asock && send_key && data && size && addr && addr_len, |
| 758 |
PJ_EINVAL); |
| 759 |
|
| 760 |
if (asock->shutdown & SHUT_TX) |
| 761 |
return PJ_EINVALIDOP; |
| 762 |
|
| 763 |
return pj_ioqueue_sendto(asock->key, send_key, data, size, flags, |
| 764 |
addr, addr_len); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
static void ioqueue_on_write_complete(pj_ioqueue_key_t *key, |
| 769 |
pj_ioqueue_op_key_t *op_key, |
| 770 |
pj_ssize_t bytes_sent) |
| 771 |
{ |
| 772 |
pj_activesock_t *asock; |
| 773 |
|
| 774 |
asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
if (asock->shutdown & SHUT_TX) { |
| 784 |
if (asock->cb.on_data_sent) |
| 785 |
(*asock->cb.on_data_sent)(asock, op_key, bytes_sent); |
| 786 |
return; |
| 787 |
} |
| 788 |
|
| 789 |
if (bytes_sent > 0 && op_key->activesock_data) { |
| 790 |
|
| 791 |
struct send_data *sd = (struct send_data*)op_key->activesock_data; |
| 792 |
|
| 793 |
sd->sent += bytes_sent; |
| 794 |
if (sd->sent == sd->len) { |
| 795 |
|
| 796 |
bytes_sent = sd->sent; |
| 797 |
op_key->activesock_data = NULL; |
| 798 |
} else { |
| 799 |
|
| 800 |
pj_status_t status; |
| 801 |
|
| 802 |
status = send_remaining(asock, op_key); |
| 803 |
if (status == PJ_EPENDING) |
| 804 |
return; |
| 805 |
else if (status == PJ_SUCCESS) |
| 806 |
bytes_sent = sd->sent; |
| 807 |
else |
| 808 |
bytes_sent = -status; |
| 809 |
|
| 810 |
op_key->activesock_data = NULL; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
if (asock->cb.on_data_sent) { |
| 815 |
pj_bool_t ret; |
| 816 |
|
| 817 |
ret = (*asock->cb.on_data_sent)(asock, op_key, bytes_sent); |
| 818 |
|
| 819 |
|
| 820 |
if (!ret) |
| 821 |
return; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
#if PJ_HAS_TCP |
| 826 |
PJ_DEF(pj_status_t) pj_activesock_start_accept(pj_activesock_t *asock, |
| 827 |
pj_pool_t *pool) |
| 828 |
{ |
| 829 |
unsigned i; |
| 830 |
|
| 831 |
PJ_ASSERT_RETURN(asock, PJ_EINVAL); |
| 832 |
PJ_ASSERT_RETURN(asock->accept_op==NULL, PJ_EINVALIDOP); |
| 833 |
|
| 834 |
|
| 835 |
if (asock->shutdown) |
| 836 |
return PJ_EINVALIDOP; |
| 837 |
|
| 838 |
asock->accept_op = (struct accept_op*) |
| 839 |
pj_pool_calloc(pool, asock->async_count, |
| 840 |
sizeof(struct accept_op)); |
| 841 |
for (i=0; i<asock->async_count; ++i) { |
| 842 |
struct accept_op *a = &asock->accept_op[i]; |
| 843 |
pj_status_t status; |
| 844 |
|
| 845 |
do { |
| 846 |
a->new_sock = PJ_INVALID_SOCKET; |
| 847 |
a->rem_addr_len = sizeof(a->rem_addr); |
| 848 |
|
| 849 |
status = pj_ioqueue_accept(asock->key, &a->op_key, &a->new_sock, |
| 850 |
NULL, &a->rem_addr, &a->rem_addr_len); |
| 851 |
if (status == PJ_SUCCESS) { |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
pj_sock_close(a->new_sock); |
| 858 |
} |
| 859 |
} while (status == PJ_SUCCESS); |
| 860 |
|
| 861 |
if (status != PJ_EPENDING) { |
| 862 |
return status; |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
return PJ_SUCCESS; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key, |
| 871 |
pj_ioqueue_op_key_t *op_key, |
| 872 |
pj_sock_t new_sock, |
| 873 |
pj_status_t status) |
| 874 |
{ |
| 875 |
pj_activesock_t *asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 876 |
struct accept_op *accept_op = (struct accept_op*) op_key; |
| 877 |
|
| 878 |
PJ_UNUSED_ARG(new_sock); |
| 879 |
|
| 880 |
|
| 881 |
if (asock->shutdown) |
| 882 |
return; |
| 883 |
|
| 884 |
do { |
| 885 |
if (status == asock->last_err && status != PJ_SUCCESS) { |
| 886 |
asock->err_counter++; |
| 887 |
if (asock->err_counter >= PJ_ACTIVESOCK_MAX_CONSECUTIVE_ACCEPT_ERROR) { |
| 888 |
PJ_LOG(3, ("", "Received %d consecutive errors: %d for the accept()" |
| 889 |
" operation, stopping further ioqueue accepts.", |
| 890 |
asock->err_counter, asock->last_err)); |
| 891 |
|
| 892 |
if ((status == PJ_STATUS_FROM_OS(OSERR_EWOULDBLOCK)) && |
| 893 |
(asock->cb.on_accept_complete2)) |
| 894 |
{ |
| 895 |
(*asock->cb.on_accept_complete2)(asock, |
| 896 |
accept_op->new_sock, |
| 897 |
&accept_op->rem_addr, |
| 898 |
accept_op->rem_addr_len, |
| 899 |
PJ_ESOCKETSTOP); |
| 900 |
} |
| 901 |
return; |
| 902 |
} |
| 903 |
} else { |
| 904 |
asock->err_counter = 0; |
| 905 |
asock->last_err = status; |
| 906 |
} |
| 907 |
|
| 908 |
if (status==PJ_SUCCESS && (asock->cb.on_accept_complete2 || |
| 909 |
asock->cb.on_accept_complete)) { |
| 910 |
pj_bool_t ret; |
| 911 |
|
| 912 |
|
| 913 |
if (asock->cb.on_accept_complete2) { |
| 914 |
ret = (*asock->cb.on_accept_complete2)(asock, |
| 915 |
accept_op->new_sock, |
| 916 |
&accept_op->rem_addr, |
| 917 |
accept_op->rem_addr_len, |
| 918 |
status); |
| 919 |
} else { |
| 920 |
ret = (*asock->cb.on_accept_complete)(asock, |
| 921 |
accept_op->new_sock, |
| 922 |
&accept_op->rem_addr, |
| 923 |
accept_op->rem_addr_len); |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
if (!ret) |
| 928 |
return; |
| 929 |
|
| 930 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 931 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 932 |
activesock_create_iphone_os_stream(asock); |
| 933 |
#endif |
| 934 |
} else if (status==PJ_SUCCESS) { |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
pj_sock_close(accept_op->new_sock); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
if (asock->shutdown) |
| 943 |
return; |
| 944 |
|
| 945 |
|
| 946 |
accept_op->new_sock = PJ_INVALID_SOCKET; |
| 947 |
accept_op->rem_addr_len = sizeof(accept_op->rem_addr); |
| 948 |
|
| 949 |
status = pj_ioqueue_accept(asock->key, op_key, &accept_op->new_sock, |
| 950 |
NULL, &accept_op->rem_addr, |
| 951 |
&accept_op->rem_addr_len); |
| 952 |
|
| 953 |
} while (status != PJ_EPENDING && status != PJ_ECANCELLED); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
PJ_DEF(pj_status_t) pj_activesock_start_connect( pj_activesock_t *asock, |
| 958 |
pj_pool_t *pool, |
| 959 |
const pj_sockaddr_t *remaddr, |
| 960 |
int addr_len) |
| 961 |
{ |
| 962 |
PJ_UNUSED_ARG(pool); |
| 963 |
|
| 964 |
if (asock->shutdown) |
| 965 |
return PJ_EINVALIDOP; |
| 966 |
|
| 967 |
return pj_ioqueue_connect(asock->key, remaddr, addr_len); |
| 968 |
} |
| 969 |
|
| 970 |
static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key, |
| 971 |
pj_status_t status) |
| 972 |
{ |
| 973 |
pj_activesock_t *asock = (pj_activesock_t*) pj_ioqueue_get_user_data(key); |
| 974 |
|
| 975 |
|
| 976 |
if (asock->shutdown) |
| 977 |
return; |
| 978 |
|
| 979 |
if (asock->cb.on_connect_complete) { |
| 980 |
pj_bool_t ret; |
| 981 |
|
| 982 |
ret = (*asock->cb.on_connect_complete)(asock, status); |
| 983 |
|
| 984 |
if (!ret) { |
| 985 |
|
| 986 |
return; |
| 987 |
} |
| 988 |
|
| 989 |
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ |
| 990 |
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 |
| 991 |
activesock_create_iphone_os_stream(asock); |
| 992 |
#endif |
| 993 |
|
| 994 |
} |
| 995 |
} |
| 996 |
#endif |
| 997 |
|