| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/ioqueue.h> |
| 20 |
#include <pj/os.h> |
| 21 |
#include <pj/lock.h> |
| 22 |
#include <pj/pool.h> |
| 23 |
#include <pj/string.h> |
| 24 |
#include <pj/sock.h> |
| 25 |
#include <pj/array.h> |
| 26 |
#include <pj/log.h> |
| 27 |
#include <pj/assert.h> |
| 28 |
#include <pj/errno.h> |
| 29 |
#include <pj/compat/socket.h> |
| 30 |
|
| 31 |
#define THIS_FILE "ioq_winnt" |
| 32 |
|
| 33 |
|
| 34 |
#if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_IOCP |
| 35 |
|
| 36 |
#define THIS_FILE "ioq_winnt" |
| 37 |
|
| 38 |
#if defined(PJ_HAS_WINSOCK2_H) && PJ_HAS_WINSOCK2_H != 0 |
| 39 |
# include <winsock2.h> |
| 40 |
#elif defined(PJ_HAS_WINSOCK_H) && PJ_HAS_WINSOCK_H != 0 |
| 41 |
# include <winsock.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#if defined(PJ_HAS_MSWSOCK_H) && PJ_HAS_MSWSOCK_H != 0 |
| 45 |
# include <mswsock.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
|
| 49 |
#pragma comment(lib, "mswsock.lib") |
| 50 |
|
| 51 |
#if 0 |
| 52 |
# define TRACE(args) PJ_LOG(3,args) |
| 53 |
#else |
| 54 |
# define TRACE(args) |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define ACCEPT_ADDR_LEN (sizeof(pj_sockaddr_in)+16) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#define TIMEOUT_CANCEL_OP 5000 |
| 70 |
|
| 71 |
|
| 72 |
typedef struct generic_overlapped |
| 73 |
{ |
| 74 |
WSAOVERLAPPED overlapped; |
| 75 |
pj_ioqueue_operation_e operation; |
| 76 |
} generic_overlapped; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
typedef struct ioqueue_overlapped |
| 82 |
{ |
| 83 |
WSAOVERLAPPED overlapped; |
| 84 |
pj_ioqueue_operation_e operation; |
| 85 |
WSABUF wsabuf; |
| 86 |
pj_sockaddr_in dummy_addr; |
| 87 |
int dummy_addrlen; |
| 88 |
|
| 89 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 90 |
pj_ssize_t bytes_read; |
| 91 |
#endif |
| 92 |
} ioqueue_overlapped; |
| 93 |
|
| 94 |
#if PJ_HAS_TCP |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
typedef struct ioqueue_accept_rec |
| 99 |
{ |
| 100 |
WSAOVERLAPPED overlapped; |
| 101 |
pj_ioqueue_operation_e operation; |
| 102 |
pj_sock_t newsock; |
| 103 |
pj_sock_t *newsock_ptr; |
| 104 |
int *addrlen; |
| 105 |
void *remote; |
| 106 |
void *local; |
| 107 |
char accept_buf[2 * ACCEPT_ADDR_LEN]; |
| 108 |
} ioqueue_accept_rec; |
| 109 |
#endif |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
union operation_key |
| 115 |
{ |
| 116 |
generic_overlapped generic; |
| 117 |
ioqueue_overlapped overlapped; |
| 118 |
#if PJ_HAS_TCP |
| 119 |
ioqueue_accept_rec accept; |
| 120 |
#endif |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
struct pending_op |
| 129 |
{ |
| 130 |
PJ_DECL_LIST_MEMBER(struct pending_op); |
| 131 |
union operation_key pending_key; |
| 132 |
pj_ioqueue_op_key_t *app_op_key; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
enum handle_type |
| 137 |
{ |
| 138 |
HND_IS_UNKNOWN, |
| 139 |
HND_IS_FILE, |
| 140 |
HND_IS_SOCKET, |
| 141 |
}; |
| 142 |
|
| 143 |
enum { POST_QUIT_LEN = 0xFFFFDEADUL }; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
struct pj_ioqueue_key_t |
| 149 |
{ |
| 150 |
PJ_DECL_LIST_MEMBER(struct pj_ioqueue_key_t); |
| 151 |
|
| 152 |
pj_pool_t *pool; |
| 153 |
pj_ioqueue_t *ioqueue; |
| 154 |
HANDLE hnd; |
| 155 |
void *user_data; |
| 156 |
enum handle_type hnd_type; |
| 157 |
pj_ioqueue_callback cb; |
| 158 |
pj_bool_t allow_concurrent; |
| 159 |
pj_grp_lock_t *grp_lock; |
| 160 |
|
| 161 |
#if PJ_HAS_TCP |
| 162 |
int connecting; |
| 163 |
#endif |
| 164 |
|
| 165 |
pj_bool_t closing; |
| 166 |
struct pending_op pending_list; |
| 167 |
struct pending_op free_pending_list; |
| 168 |
|
| 169 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 170 |
pj_thread_t *read_callback_thread; |
| 171 |
struct pending_op read_cb_list; |
| 172 |
pj_thread_t *write_callback_thread; |
| 173 |
struct pending_op write_cb_list; |
| 174 |
#endif |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
struct pj_ioqueue_t |
| 181 |
{ |
| 182 |
pj_pool_t *pool; |
| 183 |
pj_ioqueue_cfg cfg; |
| 184 |
HANDLE iocp; |
| 185 |
pj_lock_t *lock; |
| 186 |
pj_bool_t auto_delete_lock; |
| 187 |
pj_bool_t default_concurrency; |
| 188 |
pj_size_t max_fd; |
| 189 |
|
| 190 |
pj_ioqueue_key_t active_list; |
| 191 |
pj_ioqueue_key_t free_list; |
| 192 |
|
| 193 |
|
| 194 |
#if PJ_HAS_TCP |
| 195 |
unsigned event_count; |
| 196 |
HANDLE event_pool[MAXIMUM_WAIT_OBJECTS+1]; |
| 197 |
unsigned connecting_count; |
| 198 |
HANDLE connecting_handles[MAXIMUM_WAIT_OBJECTS+1]; |
| 199 |
pj_ioqueue_key_t *connecting_keys[MAXIMUM_WAIT_OBJECTS+1]; |
| 200 |
#endif |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
typedef BOOL(WINAPI* FnCancelIoEx)(HANDLE hFile, LPOVERLAPPED lpOverlapped); |
| 208 |
static FnCancelIoEx fnCancelIoEx = NULL; |
| 209 |
|
| 210 |
#define OPKEY_OPERATION(op_key) ((union operation_key*)op_key)->generic.operation |
| 211 |
|
| 212 |
|
| 213 |
static void key_on_destroy(void* data); |
| 214 |
static void increment_counter(pj_ioqueue_key_t* key); |
| 215 |
static void decrement_counter(pj_ioqueue_key_t* key); |
| 216 |
static pj_status_t cancel_all_pending_op(pj_ioqueue_key_t *key); |
| 217 |
|
| 218 |
#define PENDING_OP_POS(op_key) (PJ_ARRAY_SIZE(op_key->internal__) - 1) |
| 219 |
|
| 220 |
static struct pending_op* get_pending_op(pj_ioqueue_op_key_t *op_key) |
| 221 |
{ |
| 222 |
return (struct pending_op*) |
| 223 |
(op_key->internal__[PENDING_OP_POS(op_key)]); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
#if PJ_HAS_TCP |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key, |
| 232 |
ioqueue_accept_rec *accept_overlapped) |
| 233 |
{ |
| 234 |
struct sockaddr *local; |
| 235 |
struct sockaddr *remote; |
| 236 |
int locallen, remotelen; |
| 237 |
pj_status_t status; |
| 238 |
|
| 239 |
PJ_CHECK_STACK(); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
status = setsockopt(accept_overlapped->newsock, SOL_SOCKET, |
| 245 |
SO_UPDATE_ACCEPT_CONTEXT, |
| 246 |
(char*)&key->hnd, |
| 247 |
sizeof(SOCKET)); |
| 248 |
(void)status; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
if (accept_overlapped->addrlen) { |
| 255 |
GetAcceptExSockaddrs( accept_overlapped->accept_buf, |
| 256 |
0, |
| 257 |
ACCEPT_ADDR_LEN, |
| 258 |
ACCEPT_ADDR_LEN, |
| 259 |
&local, |
| 260 |
&locallen, |
| 261 |
&remote, |
| 262 |
&remotelen); |
| 263 |
if (*accept_overlapped->addrlen >= locallen) { |
| 264 |
if (accept_overlapped->local) |
| 265 |
pj_memcpy(accept_overlapped->local, local, locallen); |
| 266 |
if (accept_overlapped->remote) |
| 267 |
pj_memcpy(accept_overlapped->remote, remote, locallen); |
| 268 |
} else { |
| 269 |
if (accept_overlapped->local) |
| 270 |
pj_bzero(accept_overlapped->local, |
| 271 |
*accept_overlapped->addrlen); |
| 272 |
if (accept_overlapped->remote) |
| 273 |
pj_bzero(accept_overlapped->remote, |
| 274 |
*accept_overlapped->addrlen); |
| 275 |
} |
| 276 |
|
| 277 |
*accept_overlapped->addrlen = locallen; |
| 278 |
} |
| 279 |
if (accept_overlapped->newsock_ptr) |
| 280 |
*accept_overlapped->newsock_ptr = accept_overlapped->newsock; |
| 281 |
accept_overlapped->operation = 0; |
| 282 |
} |
| 283 |
|
| 284 |
static void erase_connecting_socket( pj_ioqueue_t *ioqueue, unsigned pos) |
| 285 |
{ |
| 286 |
pj_ioqueue_key_t *key = ioqueue->connecting_keys[pos]; |
| 287 |
HANDLE hEvent = ioqueue->connecting_handles[pos]; |
| 288 |
|
| 289 |
|
| 290 |
pj_array_erase(ioqueue->connecting_keys, sizeof(key), |
| 291 |
ioqueue->connecting_count, pos); |
| 292 |
pj_array_erase(ioqueue->connecting_handles, sizeof(HANDLE), |
| 293 |
ioqueue->connecting_count, pos); |
| 294 |
--ioqueue->connecting_count; |
| 295 |
|
| 296 |
|
| 297 |
WSAEventSelect((pj_sock_t)key->hnd, hEvent, 0); |
| 298 |
|
| 299 |
|
| 300 |
if (ioqueue->event_count < MAXIMUM_WAIT_OBJECTS) { |
| 301 |
ioqueue->event_pool[ioqueue->event_count++] = hEvent; |
| 302 |
} else { |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
pj_assert(0); |
| 307 |
CloseHandle(hEvent); |
| 308 |
} |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
static int check_connecting( pj_ioqueue_t *ioqueue ) |
| 320 |
{ |
| 321 |
if (ioqueue->connecting_count) { |
| 322 |
int i, count; |
| 323 |
struct |
| 324 |
{ |
| 325 |
pj_ioqueue_key_t *key; |
| 326 |
pj_status_t status; |
| 327 |
} events[PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL-1]; |
| 328 |
|
| 329 |
pj_lock_acquire(ioqueue->lock); |
| 330 |
for (count=0; count<PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL-1; ++count) { |
| 331 |
DWORD result; |
| 332 |
|
| 333 |
result = WaitForMultipleObjects(ioqueue->connecting_count, |
| 334 |
ioqueue->connecting_handles, |
| 335 |
FALSE, 0); |
| 336 |
if (result >= WAIT_OBJECT_0 && |
| 337 |
result < WAIT_OBJECT_0+ioqueue->connecting_count) |
| 338 |
{ |
| 339 |
WSANETWORKEVENTS net_events; |
| 340 |
|
| 341 |
|
| 342 |
unsigned pos = result - WAIT_OBJECT_0; |
| 343 |
events[count].key = ioqueue->connecting_keys[pos]; |
| 344 |
|
| 345 |
|
| 346 |
WSAEnumNetworkEvents((pj_sock_t)events[count].key->hnd, |
| 347 |
ioqueue->connecting_handles[pos], |
| 348 |
&net_events); |
| 349 |
events[count].status = |
| 350 |
PJ_STATUS_FROM_OS(net_events.iErrorCode[FD_CONNECT_BIT]); |
| 351 |
|
| 352 |
|
| 353 |
erase_connecting_socket(ioqueue, pos); |
| 354 |
} else { |
| 355 |
|
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
pj_lock_release(ioqueue->lock); |
| 360 |
|
| 361 |
|
| 362 |
for (i=0; i<count; ++i) { |
| 363 |
if (events[i].key->cb.on_connect_complete) { |
| 364 |
events[i].key->cb.on_connect_complete(events[i].key, |
| 365 |
events[i].status); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return count; |
| 370 |
} |
| 371 |
|
| 372 |
return 0; |
| 373 |
|
| 374 |
} |
| 375 |
#endif |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
PJ_DEF(const char*) pj_ioqueue_name(void) |
| 381 |
{ |
| 382 |
return "iocp"; |
| 383 |
} |
| 384 |
|
| 385 |
PJ_DEF(void) pj_ioqueue_cfg_default(pj_ioqueue_cfg *cfg) |
| 386 |
{ |
| 387 |
pj_bzero(cfg, sizeof(*cfg)); |
| 388 |
cfg->epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS; |
| 389 |
cfg->default_concurrency = PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY; |
| 390 |
} |
| 391 |
|
| 392 |
PJ_DEF(pj_status_t) pj_ioqueue_clear_key( pj_ioqueue_key_t *key ) |
| 393 |
{ |
| 394 |
PJ_ASSERT_RETURN(key, PJ_EINVAL); |
| 395 |
|
| 396 |
cancel_all_pending_op(key); |
| 397 |
|
| 398 |
return PJ_SUCCESS; |
| 399 |
|
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
PJ_DEF(pj_status_t) pj_ioqueue_create( pj_pool_t *pool, |
| 406 |
pj_size_t max_fd, |
| 407 |
pj_ioqueue_t **p_ioqueue) |
| 408 |
{ |
| 409 |
return pj_ioqueue_create2(pool, max_fd, NULL, p_ioqueue); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
PJ_DEF(pj_status_t) pj_ioqueue_create2(pj_pool_t *pool, |
| 416 |
pj_size_t max_fd, |
| 417 |
const pj_ioqueue_cfg *cfg, |
| 418 |
pj_ioqueue_t **p_ioqueue) |
| 419 |
{ |
| 420 |
pj_ioqueue_t *ioqueue; |
| 421 |
pj_size_t i; |
| 422 |
pj_status_t rc; |
| 423 |
|
| 424 |
PJ_UNUSED_ARG(max_fd); |
| 425 |
PJ_ASSERT_RETURN(pool && p_ioqueue, PJ_EINVAL); |
| 426 |
|
| 427 |
rc = sizeof(union operation_key); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
PJ_ASSERT_RETURN(sizeof(pj_ioqueue_op_key_t)-3*sizeof(void*) >= |
| 437 |
sizeof(union operation_key), PJ_EBUG); |
| 438 |
|
| 439 |
if (!fnCancelIoEx) { |
| 440 |
fnCancelIoEx = (FnCancelIoEx) |
| 441 |
GetProcAddress(GetModuleHandle(PJ_T("Kernel32.dll")), |
| 442 |
"CancelIoEx"); |
| 443 |
if (!fnCancelIoEx) { |
| 444 |
rc = PJ_RETURN_OS_ERROR(GetLastError()); |
| 445 |
PJ_PERROR(1, (THIS_FILE, rc, |
| 446 |
"Failed in getting address of CancelIoEx()")); |
| 447 |
return rc; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
ioqueue = pj_pool_zalloc(pool, sizeof(*ioqueue)); |
| 453 |
ioqueue->pool = pool; |
| 454 |
if (cfg) |
| 455 |
pj_memcpy(&ioqueue->cfg, cfg, sizeof(*cfg)); |
| 456 |
else |
| 457 |
pj_ioqueue_cfg_default(&ioqueue->cfg); |
| 458 |
|
| 459 |
ioqueue->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); |
| 460 |
if (ioqueue->iocp == NULL) |
| 461 |
return PJ_RETURN_OS_ERROR(GetLastError()); |
| 462 |
|
| 463 |
|
| 464 |
rc = pj_lock_create_recursive_mutex(pool, NULL, &ioqueue->lock); |
| 465 |
if (rc != PJ_SUCCESS) { |
| 466 |
CloseHandle(ioqueue->iocp); |
| 467 |
return rc; |
| 468 |
} |
| 469 |
|
| 470 |
ioqueue->auto_delete_lock = PJ_TRUE; |
| 471 |
ioqueue->default_concurrency = PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY; |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
pj_list_init(&ioqueue->active_list); |
| 477 |
pj_list_init(&ioqueue->free_list); |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
for (i=0; i<max_fd; ++i) { |
| 483 |
pj_ioqueue_key_t *key; |
| 484 |
|
| 485 |
key = pj_pool_zalloc(pool, sizeof(pj_ioqueue_key_t)); |
| 486 |
|
| 487 |
|
| 488 |
pj_list_init(&key->pending_list); |
| 489 |
pj_list_init(&key->free_pending_list); |
| 490 |
|
| 491 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 492 |
pj_list_init(&key->read_cb_list); |
| 493 |
pj_list_init(&key->write_cb_list); |
| 494 |
#endif |
| 495 |
|
| 496 |
pj_list_push_back(&ioqueue->free_list, key); |
| 497 |
} |
| 498 |
ioqueue->max_fd = max_fd; |
| 499 |
|
| 500 |
*p_ioqueue = ioqueue; |
| 501 |
|
| 502 |
PJ_LOG(4, (THIS_FILE, "WinNT IOCP I/O Queue created (%p)", ioqueue)); |
| 503 |
return PJ_SUCCESS; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
PJ_DEF(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioqueue ) |
| 510 |
{ |
| 511 |
#if PJ_HAS_TCP |
| 512 |
unsigned i; |
| 513 |
#endif |
| 514 |
pj_ioqueue_key_t *key, *next; |
| 515 |
pj_time_val stop; |
| 516 |
|
| 517 |
PJ_CHECK_STACK(); |
| 518 |
PJ_ASSERT_RETURN(ioqueue, PJ_EINVAL); |
| 519 |
|
| 520 |
pj_lock_acquire(ioqueue->lock); |
| 521 |
|
| 522 |
#if PJ_HAS_TCP |
| 523 |
|
| 524 |
for (i=0; i<ioqueue->event_count; ++i) { |
| 525 |
CloseHandle(ioqueue->event_pool[i]); |
| 526 |
} |
| 527 |
ioqueue->event_count = 0; |
| 528 |
#endif |
| 529 |
|
| 530 |
|
| 531 |
key = ioqueue->active_list.next; |
| 532 |
while (key != &ioqueue->active_list) { |
| 533 |
next = key->next; |
| 534 |
pj_ioqueue_unregister(key); |
| 535 |
key = next; |
| 536 |
} |
| 537 |
|
| 538 |
pj_lock_release(ioqueue->lock); |
| 539 |
|
| 540 |
|
| 541 |
pj_gettickcount(&stop); |
| 542 |
stop.msec += TIMEOUT_CANCEL_OP; |
| 543 |
pj_time_val_normalize(&stop); |
| 544 |
|
| 545 |
while (1) { |
| 546 |
pj_time_val timeout = {0, 100}; |
| 547 |
pj_size_t pending_key_cnt; |
| 548 |
|
| 549 |
pending_key_cnt = ioqueue->max_fd - pj_list_size(&ioqueue->free_list); |
| 550 |
if (!pending_key_cnt) |
| 551 |
break; |
| 552 |
|
| 553 |
pj_ioqueue_poll(ioqueue, &timeout); |
| 554 |
|
| 555 |
pj_gettickcount(&timeout); |
| 556 |
if (PJ_TIME_VAL_GTE(timeout, stop)) { |
| 557 |
PJ_LOG(3, (THIS_FILE, "Warning, IOCP destroy timeout in waiting " |
| 558 |
"for cancelling ops, after %dms, pending keys=%d", |
| 559 |
TIMEOUT_CANCEL_OP, (int)pending_key_cnt)); |
| 560 |
break; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
if (CloseHandle(ioqueue->iocp) != TRUE) |
| 565 |
return PJ_RETURN_OS_ERROR(GetLastError()); |
| 566 |
|
| 567 |
if (ioqueue->auto_delete_lock) |
| 568 |
pj_lock_destroy(ioqueue->lock); |
| 569 |
|
| 570 |
return PJ_SUCCESS; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
PJ_DEF(pj_status_t) pj_ioqueue_set_default_concurrency(pj_ioqueue_t *ioqueue, |
| 575 |
pj_bool_t allow) |
| 576 |
{ |
| 577 |
PJ_ASSERT_RETURN(ioqueue != NULL, PJ_EINVAL); |
| 578 |
ioqueue->default_concurrency = allow; |
| 579 |
return PJ_SUCCESS; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
PJ_DEF(pj_status_t) pj_ioqueue_set_lock( pj_ioqueue_t *ioqueue, |
| 586 |
pj_lock_t *lock, |
| 587 |
pj_bool_t auto_delete ) |
| 588 |
{ |
| 589 |
PJ_ASSERT_RETURN(ioqueue && lock, PJ_EINVAL); |
| 590 |
|
| 591 |
if (ioqueue->auto_delete_lock) { |
| 592 |
pj_lock_destroy(ioqueue->lock); |
| 593 |
} |
| 594 |
|
| 595 |
ioqueue->lock = lock; |
| 596 |
ioqueue->auto_delete_lock = auto_delete; |
| 597 |
|
| 598 |
return PJ_SUCCESS; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
PJ_DEF(pj_status_t) pj_ioqueue_register_sock2(pj_pool_t *pool, |
| 606 |
pj_ioqueue_t *ioqueue, |
| 607 |
pj_sock_t sock, |
| 608 |
pj_grp_lock_t *grp_lock, |
| 609 |
void *user_data, |
| 610 |
const pj_ioqueue_callback *cb, |
| 611 |
pj_ioqueue_key_t **key ) |
| 612 |
{ |
| 613 |
HANDLE hioq; |
| 614 |
pj_ioqueue_key_t *rec; |
| 615 |
u_long value; |
| 616 |
int rc; |
| 617 |
pj_status_t status; |
| 618 |
|
| 619 |
PJ_ASSERT_RETURN(pool && ioqueue && cb && key, PJ_EINVAL); |
| 620 |
|
| 621 |
pj_lock_acquire(ioqueue->lock); |
| 622 |
|
| 623 |
|
| 624 |
if (pj_list_empty(&ioqueue->free_list)) { |
| 625 |
pj_lock_release(ioqueue->lock); |
| 626 |
return PJ_ETOOMANY; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
rec = ioqueue->free_list.next; |
| 631 |
|
| 632 |
|
| 633 |
rec->pool = pj_pool_create(ioqueue->pool->factory, "key%p", |
| 634 |
512, 512, NULL); |
| 635 |
if (!rec->pool) { |
| 636 |
pj_lock_release(ioqueue->lock); |
| 637 |
return PJ_ENOMEM; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
pj_list_erase(rec); |
| 642 |
pj_list_push_back(&ioqueue->active_list, rec); |
| 643 |
|
| 644 |
pj_lock_release(ioqueue->lock); |
| 645 |
|
| 646 |
|
| 647 |
rec->closing = 0; |
| 648 |
|
| 649 |
|
| 650 |
rec->ioqueue = ioqueue; |
| 651 |
rec->hnd = (HANDLE)sock; |
| 652 |
rec->hnd_type = HND_IS_SOCKET; |
| 653 |
rec->user_data = user_data; |
| 654 |
pj_memcpy(&rec->cb, cb, sizeof(pj_ioqueue_callback)); |
| 655 |
|
| 656 |
|
| 657 |
status = pj_ioqueue_set_concurrency(rec, ioqueue->default_concurrency); |
| 658 |
if (status != PJ_SUCCESS) |
| 659 |
return status; |
| 660 |
|
| 661 |
#if PJ_HAS_TCP |
| 662 |
rec->connecting = 0; |
| 663 |
#endif |
| 664 |
|
| 665 |
|
| 666 |
value = 1; |
| 667 |
rc = ioctlsocket(sock, FIONBIO, &value); |
| 668 |
if (rc != 0) |
| 669 |
return PJ_RETURN_OS_ERROR(WSAGetLastError()); |
| 670 |
|
| 671 |
|
| 672 |
hioq = CreateIoCompletionPort((HANDLE)sock, ioqueue->iocp, |
| 673 |
(ULONG_PTR)rec, 0); |
| 674 |
if (!hioq) |
| 675 |
return PJ_RETURN_OS_ERROR(GetLastError()); |
| 676 |
|
| 677 |
|
| 678 |
if (!grp_lock) { |
| 679 |
status = pj_grp_lock_create_w_handler(rec->pool, NULL, rec, |
| 680 |
&key_on_destroy, &grp_lock); |
| 681 |
} else { |
| 682 |
status = pj_grp_lock_add_handler(grp_lock, rec->pool, rec, |
| 683 |
&key_on_destroy); |
| 684 |
} |
| 685 |
if (status != PJ_SUCCESS) { |
| 686 |
key_on_destroy(rec); |
| 687 |
return status; |
| 688 |
} |
| 689 |
|
| 690 |
rec->grp_lock = grp_lock; |
| 691 |
|
| 692 |
|
| 693 |
increment_counter(rec); |
| 694 |
|
| 695 |
TRACE((THIS_FILE, "REG key %p", rec)); |
| 696 |
|
| 697 |
|
| 698 |
*key = rec; |
| 699 |
return PJ_SUCCESS; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
PJ_DEF(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool, |
| 707 |
pj_ioqueue_t *ioqueue, |
| 708 |
pj_sock_t sock, |
| 709 |
void *user_data, |
| 710 |
const pj_ioqueue_callback *cb, |
| 711 |
pj_ioqueue_key_t **key ) |
| 712 |
{ |
| 713 |
return pj_ioqueue_register_sock2(pool, ioqueue, sock, NULL, user_data, cb, |
| 714 |
key); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
PJ_DEF(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key ) |
| 722 |
{ |
| 723 |
PJ_ASSERT_RETURN(key, NULL); |
| 724 |
return key->user_data; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
PJ_DEF(pj_status_t) pj_ioqueue_set_user_data( pj_ioqueue_key_t *key, |
| 731 |
void *user_data, |
| 732 |
void **old_data ) |
| 733 |
{ |
| 734 |
PJ_ASSERT_RETURN(key, PJ_EINVAL); |
| 735 |
|
| 736 |
if (old_data) |
| 737 |
*old_data = key->user_data; |
| 738 |
|
| 739 |
key->user_data = user_data; |
| 740 |
return PJ_SUCCESS; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
static void key_on_destroy(void *data) { |
| 745 |
pj_ioqueue_key_t *key = (pj_ioqueue_key_t*)data; |
| 746 |
pj_ioqueue_t* ioqueue = key->ioqueue; |
| 747 |
|
| 748 |
|
| 749 |
key->grp_lock = NULL; |
| 750 |
pj_pool_safe_release(&key->pool); |
| 751 |
|
| 752 |
|
| 753 |
pj_assert(pj_list_empty(&key->pending_list)); |
| 754 |
pj_list_init(&key->free_pending_list); |
| 755 |
|
| 756 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 757 |
pj_assert(pj_list_empty(&key->read_cb_list)); |
| 758 |
pj_list_init(&key->read_cb_list); |
| 759 |
pj_assert(pj_list_empty(&key->write_cb_list)); |
| 760 |
pj_list_init(&key->write_cb_list); |
| 761 |
#endif |
| 762 |
|
| 763 |
|
| 764 |
pj_lock_acquire(ioqueue->lock); |
| 765 |
pj_list_erase(key); |
| 766 |
pj_list_push_back(&ioqueue->free_list, key); |
| 767 |
|
| 768 |
TRACE((THIS_FILE, "FREE key %p", key)); |
| 769 |
|
| 770 |
pj_lock_release(ioqueue->lock); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
static void increment_counter(pj_ioqueue_key_t* key) |
| 776 |
{ |
| 777 |
pj_grp_lock_add_ref_dbg(key->grp_lock, "ioqueue", 0); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
static void decrement_counter(pj_ioqueue_key_t *key) |
| 785 |
{ |
| 786 |
pj_grp_lock_dec_ref_dbg(key->grp_lock, "ioqueue", 0); |
| 787 |
} |
| 788 |
|
| 789 |
static struct pending_op *alloc_pending_op(pj_ioqueue_key_t *key, |
| 790 |
pj_ioqueue_op_key_t *op_key, |
| 791 |
void *buf, |
| 792 |
pj_ssize_t len) |
| 793 |
{ |
| 794 |
struct pending_op *op = NULL; |
| 795 |
int ref_cnt; |
| 796 |
|
| 797 |
pj_assert(key && op_key); |
| 798 |
|
| 799 |
|
| 800 |
pj_ioqueue_lock_key(key); |
| 801 |
ref_cnt = pj_grp_lock_get_ref(key->grp_lock); |
| 802 |
|
| 803 |
if (pj_list_empty(&key->free_pending_list)) { |
| 804 |
op = PJ_POOL_ZALLOC_T(key->pool, struct pending_op); |
| 805 |
if (!op) { |
| 806 |
pj_ioqueue_unlock_key(key); |
| 807 |
return NULL; |
| 808 |
} |
| 809 |
pj_list_init(op); |
| 810 |
} else { |
| 811 |
op = key->free_pending_list.next; |
| 812 |
pj_list_erase(op); |
| 813 |
} |
| 814 |
pj_list_push_back(&key->pending_list, op); |
| 815 |
increment_counter(key); |
| 816 |
pj_ioqueue_unlock_key(key); |
| 817 |
|
| 818 |
|
| 819 |
op->app_op_key = op_key; |
| 820 |
op->pending_key.overlapped.wsabuf.buf = (CHAR*)buf; |
| 821 |
op->pending_key.overlapped.wsabuf.len = (ULONG)len; |
| 822 |
|
| 823 |
|
| 824 |
op_key->internal__[PENDING_OP_POS(op_key)] = op; |
| 825 |
|
| 826 |
TRACE((THIS_FILE, "ALLOC op key %p (cnt=%d) op %p", key, ref_cnt-1, op)); |
| 827 |
|
| 828 |
return op; |
| 829 |
} |
| 830 |
|
| 831 |
static void release_pending_op(pj_ioqueue_key_t *key, struct pending_op *op) |
| 832 |
{ |
| 833 |
int ref_cnt; |
| 834 |
|
| 835 |
pj_assert(key && op); |
| 836 |
pj_ioqueue_lock_key(key); |
| 837 |
pj_list_erase(op); |
| 838 |
pj_list_push_back(&key->free_pending_list, op); |
| 839 |
decrement_counter(key); |
| 840 |
ref_cnt = pj_grp_lock_get_ref(key->grp_lock); |
| 841 |
pj_ioqueue_unlock_key(key); |
| 842 |
|
| 843 |
TRACE((THIS_FILE, "RELEASE op key %p (cnt=%d) op %p", key, ref_cnt-1, op)); |
| 844 |
} |
| 845 |
|
| 846 |
static pj_status_t cancel_all_pending_op(pj_ioqueue_key_t *key) |
| 847 |
{ |
| 848 |
|
| 849 |
BOOL rc = fnCancelIoEx(key->hnd, NULL); |
| 850 |
|
| 851 |
pj_ioqueue_lock_key(key); |
| 852 |
|
| 853 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 854 |
|
| 855 |
while (!pj_list_empty(&key->read_cb_list)) { |
| 856 |
struct pending_op *op = key->read_cb_list.next; |
| 857 |
pj_list_erase(op); |
| 858 |
pj_list_push_back(&key->free_pending_list, op); |
| 859 |
decrement_counter(key); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
if (key->write_callback_thread == pj_thread_this()) { |
| 872 |
while (!pj_list_empty(&key->write_cb_list)) { |
| 873 |
struct pending_op *op = key->write_cb_list.next; |
| 874 |
void (*on_wr_complete)(pj_ioqueue_key_t*, |
| 875 |
pj_ioqueue_op_key_t*, |
| 876 |
pj_ssize_t); |
| 877 |
|
| 878 |
pj_list_erase(op); |
| 879 |
on_wr_complete = key->cb.on_write_complete; |
| 880 |
OPKEY_OPERATION(op->app_op_key) = 0; |
| 881 |
pj_ioqueue_unlock_key(key); |
| 882 |
|
| 883 |
if (on_wr_complete) { |
| 884 |
on_wr_complete(key, op->app_op_key, |
| 885 |
op->pending_key.overlapped.bytes_read); |
| 886 |
} |
| 887 |
release_pending_op(key, op); |
| 888 |
pj_ioqueue_lock_key(key); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
do { |
| 894 |
unsigned counter = 0; |
| 895 |
|
| 896 |
while (key->read_callback_thread && |
| 897 |
key->read_callback_thread != pj_thread_this()) |
| 898 |
{ |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
pj_ioqueue_unlock_key(key); |
| 903 |
pj_thread_sleep(10); |
| 904 |
pj_ioqueue_lock_key(key); |
| 905 |
|
| 906 |
|
| 907 |
while (!pj_list_empty(&key->read_cb_list)) { |
| 908 |
struct pending_op *op = key->read_cb_list.next; |
| 909 |
pj_list_erase(op); |
| 910 |
pj_list_push_back(&key->free_pending_list, op); |
| 911 |
decrement_counter(key); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
if (++counter > 100) { |
| 916 |
PJ_LOG(1,(THIS_FILE, "Timeout waiting for read callback " |
| 917 |
"to finish on key=%p", key)); |
| 918 |
break; |
| 919 |
} |
| 920 |
} |
| 921 |
} while (0); |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
do { |
| 927 |
unsigned counter = 0; |
| 928 |
|
| 929 |
while (key->write_callback_thread && |
| 930 |
key->write_callback_thread != pj_thread_this()) |
| 931 |
{ |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
pj_ioqueue_unlock_key(key); |
| 936 |
pj_thread_sleep(10); |
| 937 |
pj_ioqueue_lock_key(key); |
| 938 |
|
| 939 |
|
| 940 |
if (++counter > 100) { |
| 941 |
PJ_LOG(1,(THIS_FILE, "Timeout waiting for write callback " |
| 942 |
"to finish on key=%p", key)); |
| 943 |
break; |
| 944 |
} |
| 945 |
} |
| 946 |
} while (0); |
| 947 |
#endif |
| 948 |
|
| 949 |
key->connecting = 0; |
| 950 |
pj_ioqueue_unlock_key(key); |
| 951 |
|
| 952 |
if (rc == 0) { |
| 953 |
DWORD dwError = WSAGetLastError(); |
| 954 |
if (dwError != ERROR_NOT_FOUND) { |
| 955 |
TRACE((THIS_FILE, "CANCEL key %p error %d", key, dwError)); |
| 956 |
return PJ_RETURN_OS_ERROR(dwError); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
TRACE((THIS_FILE, "CANCEL key %p success", key)); |
| 961 |
return PJ_SUCCESS; |
| 962 |
} |
| 963 |
|
| 964 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 965 |
static unsigned ioqueue_dispatch_read_event_no_lock(pj_ioqueue_key_t* h, |
| 966 |
unsigned max_event) |
| 967 |
{ |
| 968 |
unsigned event_cnt = 0; |
| 969 |
|
| 970 |
while (1) { |
| 971 |
struct pending_op *read_op = NULL; |
| 972 |
void (*on_read_complete)(pj_ioqueue_key_t *key, |
| 973 |
pj_ioqueue_op_key_t *op_key, |
| 974 |
pj_ssize_t bytes_read); |
| 975 |
|
| 976 |
|
| 977 |
pj_ioqueue_lock_key(h); |
| 978 |
|
| 979 |
if (!h->closing && !pj_list_empty(&h->read_cb_list) && |
| 980 |
(max_event == 0 || event_cnt < max_event)) |
| 981 |
{ |
| 982 |
read_op = h->read_cb_list.next; |
| 983 |
pj_list_erase(read_op); |
| 984 |
on_read_complete = h->cb.on_read_complete; |
| 985 |
OPKEY_OPERATION(read_op->app_op_key) = 0; |
| 986 |
} else { |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
h->read_callback_thread = NULL; |
| 991 |
on_read_complete = NULL; |
| 992 |
} |
| 993 |
|
| 994 |
pj_ioqueue_unlock_key(h); |
| 995 |
PJ_RACE_ME(5); |
| 996 |
|
| 997 |
|
| 998 |
if (on_read_complete) { |
| 999 |
(*on_read_complete)(h, read_op->app_op_key, |
| 1000 |
read_op->pending_key.overlapped.bytes_read); |
| 1001 |
|
| 1002 |
release_pending_op(h, read_op); |
| 1003 |
++event_cnt; |
| 1004 |
} else { |
| 1005 |
break; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
return event_cnt; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static unsigned ioqueue_dispatch_write_event_no_lock(pj_ioqueue_key_t* h, |
| 1013 |
unsigned max_event) |
| 1014 |
{ |
| 1015 |
unsigned event_cnt = 0; |
| 1016 |
|
| 1017 |
while (1) { |
| 1018 |
struct pending_op *write_op = NULL; |
| 1019 |
void (*on_write_complete)(pj_ioqueue_key_t *key, |
| 1020 |
pj_ioqueue_op_key_t *op_key, |
| 1021 |
pj_ssize_t bytes_sent); |
| 1022 |
|
| 1023 |
|
| 1024 |
pj_ioqueue_lock_key(h); |
| 1025 |
|
| 1026 |
if (!pj_list_empty(&h->write_cb_list) && |
| 1027 |
(max_event == 0 || event_cnt < max_event)) |
| 1028 |
{ |
| 1029 |
write_op = h->write_cb_list.next; |
| 1030 |
pj_list_erase(write_op); |
| 1031 |
on_write_complete = h->cb.on_write_complete; |
| 1032 |
OPKEY_OPERATION(write_op->app_op_key) = 0; |
| 1033 |
} else { |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
h->write_callback_thread = NULL; |
| 1038 |
on_write_complete = NULL; |
| 1039 |
} |
| 1040 |
|
| 1041 |
pj_ioqueue_unlock_key(h); |
| 1042 |
PJ_RACE_ME(5); |
| 1043 |
|
| 1044 |
|
| 1045 |
if (on_write_complete) { |
| 1046 |
(*on_write_complete)(h, write_op->app_op_key, |
| 1047 |
write_op->pending_key.overlapped.bytes_read); |
| 1048 |
|
| 1049 |
release_pending_op(h, write_op); |
| 1050 |
++event_cnt; |
| 1051 |
} else { |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
return event_cnt; |
| 1057 |
} |
| 1058 |
#endif |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
static pj_bool_t poll_iocp( HANDLE hIocp, DWORD dwTimeout, |
| 1065 |
pj_ssize_t *p_bytes, pj_ioqueue_key_t **p_key ) |
| 1066 |
{ |
| 1067 |
DWORD dwBytesTransferred; |
| 1068 |
ULONG_PTR dwKey; |
| 1069 |
generic_overlapped *pOv; |
| 1070 |
pj_ioqueue_key_t *key; |
| 1071 |
pj_ssize_t size_status = -1; |
| 1072 |
BOOL rcGetQueued; |
| 1073 |
struct pending_op *op = NULL; |
| 1074 |
pj_ioqueue_op_key_t *op_key = NULL; |
| 1075 |
|
| 1076 |
|
| 1077 |
rcGetQueued = GetQueuedCompletionStatus(hIocp, &dwBytesTransferred, |
| 1078 |
&dwKey, (OVERLAPPED**)&pOv, |
| 1079 |
dwTimeout); |
| 1080 |
if (!rcGetQueued && pOv) { |
| 1081 |
PJ_PERROR(4, (THIS_FILE, PJ_STATUS_FROM_OS(GetLastError()), |
| 1082 |
"GetQueuedCompletionStatus() error dwKey:%p, pOv:%p", |
| 1083 |
(void *)dwKey, pOv)); |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
if (pOv) { |
| 1092 |
pj_bool_t has_lock; |
| 1093 |
pj_ioqueue_operation_e operation = pOv->operation; |
| 1094 |
|
| 1095 |
|
| 1096 |
key = (pj_ioqueue_key_t*)dwKey; |
| 1097 |
size_status = dwBytesTransferred; |
| 1098 |
|
| 1099 |
|
| 1100 |
if (p_bytes) |
| 1101 |
*p_bytes = size_status; |
| 1102 |
if (p_key) |
| 1103 |
*p_key = key; |
| 1104 |
|
| 1105 |
switch(operation) |
| 1106 |
{ |
| 1107 |
case PJ_IOQUEUE_OP_RECV: |
| 1108 |
case PJ_IOQUEUE_OP_RECV_FROM: |
| 1109 |
case PJ_IOQUEUE_OP_SEND: |
| 1110 |
case PJ_IOQUEUE_OP_SEND_TO: |
| 1111 |
case PJ_IOQUEUE_OP_ACCEPT: |
| 1112 |
op = (struct pending_op*) |
| 1113 |
((char*)pOv - offsetof(struct pending_op, pending_key)); |
| 1114 |
op_key = op->app_op_key; |
| 1115 |
break; |
| 1116 |
default: |
| 1117 |
|
| 1118 |
pj_assert(0); |
| 1119 |
op = (struct pending_op*) |
| 1120 |
((char*)pOv - offsetof(struct pending_op, pending_key)); |
| 1121 |
release_pending_op(key, op); |
| 1122 |
return PJ_TRUE; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
if (key->closing) { |
| 1130 |
if (operation == PJ_IOQUEUE_OP_WRITE || |
| 1131 |
operation == PJ_IOQUEUE_OP_SEND || |
| 1132 |
operation == PJ_IOQUEUE_OP_SEND_TO) |
| 1133 |
{ |
| 1134 |
increment_counter(key); |
| 1135 |
OPKEY_OPERATION(op_key) = 0; |
| 1136 |
if (key->cb.on_write_complete) { |
| 1137 |
key->cb.on_write_complete(key, op_key, |
| 1138 |
-(pj_ssize_t)PJ_ECANCELLED); |
| 1139 |
} |
| 1140 |
release_pending_op(key, op); |
| 1141 |
decrement_counter(key); |
| 1142 |
} else { |
| 1143 |
release_pending_op(key, op); |
| 1144 |
} |
| 1145 |
return PJ_TRUE; |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
if (key->allow_concurrent == PJ_FALSE) { |
| 1152 |
has_lock = PJ_TRUE; |
| 1153 |
pj_ioqueue_lock_key(key); |
| 1154 |
|
| 1155 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 1156 |
if (operation == PJ_IOQUEUE_OP_READ || |
| 1157 |
operation == PJ_IOQUEUE_OP_RECV || |
| 1158 |
operation == PJ_IOQUEUE_OP_RECV_FROM) |
| 1159 |
{ |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
if (key->read_callback_thread) { |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
op->pending_key.overlapped.bytes_read = size_status; |
| 1169 |
pj_list_erase(op); |
| 1170 |
pj_list_push_back(&key->read_cb_list, op); |
| 1171 |
pj_ioqueue_unlock_key(key); |
| 1172 |
return PJ_TRUE; |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
key->read_callback_thread = pj_thread_this(); |
| 1180 |
|
| 1181 |
|
| 1182 |
has_lock = PJ_FALSE; |
| 1183 |
pj_ioqueue_unlock_key(key); |
| 1184 |
PJ_RACE_ME(5); |
| 1185 |
} |
| 1186 |
else if (operation == PJ_IOQUEUE_OP_WRITE || |
| 1187 |
operation == PJ_IOQUEUE_OP_SEND || |
| 1188 |
operation == PJ_IOQUEUE_OP_SEND_TO) |
| 1189 |
{ |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
if (key->write_callback_thread) { |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
op->pending_key.overlapped.bytes_read = size_status; |
| 1199 |
pj_list_erase(op); |
| 1200 |
pj_list_push_back(&key->write_cb_list, op); |
| 1201 |
pj_ioqueue_unlock_key(key); |
| 1202 |
return PJ_TRUE; |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
key->write_callback_thread = pj_thread_this(); |
| 1210 |
|
| 1211 |
|
| 1212 |
has_lock = PJ_FALSE; |
| 1213 |
pj_ioqueue_unlock_key(key); |
| 1214 |
PJ_RACE_ME(5); |
| 1215 |
} |
| 1216 |
#endif |
| 1217 |
} else { |
| 1218 |
has_lock = PJ_FALSE; |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
if (key->closing) { |
| 1223 |
if (has_lock) { |
| 1224 |
pj_ioqueue_unlock_key(key); |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
if (operation == PJ_IOQUEUE_OP_WRITE || |
| 1230 |
operation == PJ_IOQUEUE_OP_SEND || |
| 1231 |
operation == PJ_IOQUEUE_OP_SEND_TO) |
| 1232 |
{ |
| 1233 |
increment_counter(key); |
| 1234 |
OPKEY_OPERATION(op_key) = 0; |
| 1235 |
if (key->cb.on_write_complete) { |
| 1236 |
key->cb.on_write_complete(key, op_key, |
| 1237 |
-(pj_ssize_t)PJ_ECANCELLED); |
| 1238 |
} |
| 1239 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
pj_ioqueue_lock_key(key); |
| 1245 |
key->write_callback_thread = NULL; |
| 1246 |
pj_ioqueue_unlock_key(key); |
| 1247 |
#endif |
| 1248 |
release_pending_op(key, op); |
| 1249 |
decrement_counter(key); |
| 1250 |
} else { |
| 1251 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 1252 |
if (operation == PJ_IOQUEUE_OP_READ || |
| 1253 |
operation == PJ_IOQUEUE_OP_RECV || |
| 1254 |
operation == PJ_IOQUEUE_OP_RECV_FROM) |
| 1255 |
{ |
| 1256 |
pj_ioqueue_lock_key(key); |
| 1257 |
key->read_callback_thread = NULL; |
| 1258 |
pj_ioqueue_unlock_key(key); |
| 1259 |
} |
| 1260 |
#endif |
| 1261 |
release_pending_op(key, op); |
| 1262 |
} |
| 1263 |
return PJ_TRUE; |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
increment_counter(key); |
| 1270 |
|
| 1271 |
|
| 1272 |
switch (operation) { |
| 1273 |
case PJ_IOQUEUE_OP_READ: |
| 1274 |
case PJ_IOQUEUE_OP_RECV: |
| 1275 |
case PJ_IOQUEUE_OP_RECV_FROM: |
| 1276 |
|
| 1277 |
OPKEY_OPERATION(op_key) = 0; |
| 1278 |
if (key->cb.on_read_complete) |
| 1279 |
key->cb.on_read_complete(key, op_key, size_status); |
| 1280 |
break; |
| 1281 |
case PJ_IOQUEUE_OP_WRITE: |
| 1282 |
case PJ_IOQUEUE_OP_SEND: |
| 1283 |
case PJ_IOQUEUE_OP_SEND_TO: |
| 1284 |
|
| 1285 |
OPKEY_OPERATION(op_key) = 0; |
| 1286 |
if (key->cb.on_write_complete) |
| 1287 |
key->cb.on_write_complete(key, op_key, size_status); |
| 1288 |
break; |
| 1289 |
#if PJ_HAS_TCP |
| 1290 |
case PJ_IOQUEUE_OP_ACCEPT: |
| 1291 |
|
| 1292 |
OPKEY_OPERATION(op_key) = 0; |
| 1293 |
ioqueue_on_accept_complete(key, (ioqueue_accept_rec*)pOv); |
| 1294 |
if (key->cb.on_accept_complete) { |
| 1295 |
ioqueue_accept_rec *accept_rec = (ioqueue_accept_rec*)pOv; |
| 1296 |
pj_status_t status = PJ_SUCCESS; |
| 1297 |
pj_sock_t newsock; |
| 1298 |
|
| 1299 |
newsock = accept_rec->newsock; |
| 1300 |
accept_rec->newsock = PJ_INVALID_SOCKET; |
| 1301 |
|
| 1302 |
if (newsock == PJ_INVALID_SOCKET) { |
| 1303 |
int dwError = WSAGetLastError(); |
| 1304 |
if (dwError == 0) dwError = OSERR_ENOTCONN; |
| 1305 |
status = PJ_RETURN_OS_ERROR(dwError); |
| 1306 |
} |
| 1307 |
|
| 1308 |
key->cb.on_accept_complete(key, op_key, newsock, status); |
| 1309 |
} |
| 1310 |
break; |
| 1311 |
case PJ_IOQUEUE_OP_CONNECT: |
| 1312 |
#endif |
| 1313 |
case PJ_IOQUEUE_OP_NONE: |
| 1314 |
pj_assert(0); |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (has_lock) |
| 1319 |
pj_ioqueue_unlock_key(key); |
| 1320 |
|
| 1321 |
release_pending_op(key, op); |
| 1322 |
|
| 1323 |
#if PJ_IOQUEUE_CALLBACK_NO_LOCK |
| 1324 |
if (operation == PJ_IOQUEUE_OP_READ || |
| 1325 |
operation == PJ_IOQUEUE_OP_RECV || |
| 1326 |
operation == PJ_IOQUEUE_OP_RECV_FROM) |
| 1327 |
{ |
| 1328 |
|
| 1329 |
ioqueue_dispatch_read_event_no_lock(key, 0); |
| 1330 |
} |
| 1331 |
else if (operation == PJ_IOQUEUE_OP_WRITE || |
| 1332 |
operation == PJ_IOQUEUE_OP_SEND || |
| 1333 |
operation == PJ_IOQUEUE_OP_SEND_TO) |
| 1334 |
{ |
| 1335 |
|
| 1336 |
ioqueue_dispatch_write_event_no_lock(key, 0); |
| 1337 |
} |
| 1338 |
#endif |
| 1339 |
|
| 1340 |
decrement_counter(key); |
| 1341 |
|
| 1342 |
return PJ_TRUE; |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
return PJ_FALSE; |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
PJ_DEF(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key ) |
| 1353 |
{ |
| 1354 |
|
| 1355 |
|
| 1356 |
enum { RETRY = 10 }; |
| 1357 |
|
| 1358 |
PJ_ASSERT_RETURN(key, PJ_EINVAL); |
| 1359 |
|
| 1360 |
|
| 1361 |
if (!key->grp_lock || key->closing) |
| 1362 |
return PJ_SUCCESS; |
| 1363 |
|
| 1364 |
#if PJ_HAS_TCP |
| 1365 |
if (key->connecting) { |
| 1366 |
unsigned pos; |
| 1367 |
pj_ioqueue_t *ioqueue; |
| 1368 |
|
| 1369 |
ioqueue = key->ioqueue; |
| 1370 |
|
| 1371 |
|
| 1372 |
pj_lock_acquire(ioqueue->lock); |
| 1373 |
for (pos=0; pos < ioqueue->connecting_count; ++pos) { |
| 1374 |
if (ioqueue->connecting_keys[pos] == key) { |
| 1375 |
erase_connecting_socket(ioqueue, pos); |
| 1376 |
break; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
key->connecting = 0; |
| 1380 |
pj_lock_release(ioqueue->lock); |
| 1381 |
} |
| 1382 |
#endif |
| 1383 |
|
| 1384 |
|
| 1385 |
key->closing = 1; |
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
cancel_all_pending_op(key); |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
pj_sock_close((pj_sock_t)key->hnd); |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
key->cb.on_accept_complete = NULL; |
| 1425 |
key->cb.on_connect_complete = NULL; |
| 1426 |
key->cb.on_read_complete = NULL; |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
TRACE((THIS_FILE, "UNREG key %p ref cnt %d", |
| 1458 |
key, pj_grp_lock_get_ref(key->grp_lock))); |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
decrement_counter(key); |
| 1465 |
|
| 1466 |
return PJ_SUCCESS; |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
PJ_DEF(int) pj_ioqueue_poll( pj_ioqueue_t *ioqueue, const pj_time_val *timeout) |
| 1475 |
{ |
| 1476 |
DWORD dwMsec; |
| 1477 |
#if PJ_HAS_TCP |
| 1478 |
int connect_count = 0; |
| 1479 |
#endif |
| 1480 |
int event_count = 0; |
| 1481 |
|
| 1482 |
PJ_ASSERT_RETURN(ioqueue, -PJ_EINVAL); |
| 1483 |
|
| 1484 |
|
| 1485 |
dwMsec = timeout ? timeout->sec*1000 + timeout->msec : INFINITE; |
| 1486 |
|
| 1487 |
|
| 1488 |
event_count = poll_iocp(ioqueue->iocp, dwMsec, NULL, NULL); |
| 1489 |
|
| 1490 |
#if PJ_HAS_TCP |
| 1491 |
|
| 1492 |
if (event_count == 0) { |
| 1493 |
connect_count = check_connecting(ioqueue); |
| 1494 |
if (connect_count > 0) |
| 1495 |
event_count += connect_count; |
| 1496 |
} |
| 1497 |
#endif |
| 1498 |
|
| 1499 |
|
| 1500 |
return event_count; |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
PJ_DEF(pj_status_t) pj_ioqueue_recv( pj_ioqueue_key_t *key, |
| 1509 |
pj_ioqueue_op_key_t *op_key, |
| 1510 |
void *buffer, |
| 1511 |
pj_ssize_t *length, |
| 1512 |
pj_uint32_t flags ) |
| 1513 |
{ |
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
int rc; |
| 1520 |
DWORD bytesRead; |
| 1521 |
DWORD dwFlags = 0; |
| 1522 |
union operation_key *op_key_rec; |
| 1523 |
struct pending_op *op; |
| 1524 |
|
| 1525 |
PJ_CHECK_STACK(); |
| 1526 |
PJ_ASSERT_RETURN(key && op_key && buffer && length, PJ_EINVAL); |
| 1527 |
|
| 1528 |
|
| 1529 |
if (key->closing) |
| 1530 |
return PJ_ECANCELLED; |
| 1531 |
|
| 1532 |
op_key_rec = (union operation_key*)op_key->internal__; |
| 1533 |
op_key_rec->overlapped.wsabuf.buf = buffer; |
| 1534 |
op_key_rec->overlapped.wsabuf.len = (ULONG)*length; |
| 1535 |
|
| 1536 |
dwFlags = flags; |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
if ((flags & PJ_IOQUEUE_ALWAYS_ASYNC) == 0) { |
| 1542 |
rc = WSARecv((SOCKET)key->hnd, &op_key_rec->overlapped.wsabuf, 1, |
| 1543 |
&bytesRead, &dwFlags, NULL, NULL); |
| 1544 |
if (rc == 0) { |
| 1545 |
*length = bytesRead; |
| 1546 |
return PJ_SUCCESS; |
| 1547 |
} else { |
| 1548 |
DWORD dwError = WSAGetLastError(); |
| 1549 |
if (dwError != WSAEWOULDBLOCK) { |
| 1550 |
*length = -1; |
| 1551 |
return PJ_RETURN_OS_ERROR(dwError); |
| 1552 |
} |
| 1553 |
} |
| 1554 |
} |
| 1555 |
|
| 1556 |
op = alloc_pending_op(key, op_key, buffer, *length); |
| 1557 |
if (!op) |
| 1558 |
return PJ_ENOMEM; |
| 1559 |
|
| 1560 |
op_key_rec = &op->pending_key; |
| 1561 |
|
| 1562 |
dwFlags &= ~(PJ_IOQUEUE_ALWAYS_ASYNC); |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
pj_bzero( &op_key_rec->overlapped.overlapped, |
| 1569 |
sizeof(op_key_rec->overlapped.overlapped)); |
| 1570 |
op_key_rec->overlapped.operation = PJ_IOQUEUE_OP_RECV; |
| 1571 |
OPKEY_OPERATION(op_key) = PJ_IOQUEUE_OP_RECV; |
| 1572 |
|
| 1573 |
rc = WSARecv((SOCKET)key->hnd, &op_key_rec->overlapped.wsabuf, 1, |
| 1574 |
&bytesRead, &dwFlags, |
| 1575 |
&op_key_rec->overlapped.overlapped, NULL); |
| 1576 |
if (rc == SOCKET_ERROR) { |
| 1577 |
DWORD dwStatus = WSAGetLastError(); |
| 1578 |
if (dwStatus!=WSA_IO_PENDING) { |
| 1579 |
*length = -1; |
| 1580 |
release_pending_op(key, op); |
| 1581 |
return PJ_STATUS_FROM_OS(dwStatus); |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
return PJ_EPENDING; |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
PJ_DEF(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_key_t *key, |
| 1595 |
pj_ioqueue_op_key_t *op_key, |
| 1596 |
void *buffer, |
| 1597 |
pj_ssize_t *length, |
| 1598 |
pj_uint32_t flags, |
| 1599 |
pj_sockaddr_t *addr, |
| 1600 |
int *addrlen) |
| 1601 |
{ |
| 1602 |
int rc; |
| 1603 |
DWORD bytesRead; |
| 1604 |
DWORD dwFlags = 0; |
| 1605 |
union operation_key *op_key_rec; |
| 1606 |
struct pending_op *op; |
| 1607 |
|
| 1608 |
PJ_CHECK_STACK(); |
| 1609 |
PJ_ASSERT_RETURN(key && op_key && buffer, PJ_EINVAL); |
| 1610 |
|
| 1611 |
|
| 1612 |
if (key->closing) |
| 1613 |
return PJ_ECANCELLED; |
| 1614 |
|
| 1615 |
op_key_rec = (union operation_key*)op_key->internal__; |
| 1616 |
op_key_rec->overlapped.wsabuf.buf = buffer; |
| 1617 |
op_key_rec->overlapped.wsabuf.len = (ULONG)*length; |
| 1618 |
|
| 1619 |
dwFlags = flags; |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
if ((flags & PJ_IOQUEUE_ALWAYS_ASYNC) == 0) { |
| 1625 |
rc = WSARecvFrom((SOCKET)key->hnd, &op_key_rec->overlapped.wsabuf, 1, |
| 1626 |
&bytesRead, &dwFlags, addr, addrlen, NULL, NULL); |
| 1627 |
if (rc == 0) { |
| 1628 |
*length = bytesRead; |
| 1629 |
return PJ_SUCCESS; |
| 1630 |
} else { |
| 1631 |
DWORD dwError = WSAGetLastError(); |
| 1632 |
if (dwError != WSAEWOULDBLOCK) { |
| 1633 |
*length = -1; |
| 1634 |
return PJ_RETURN_OS_ERROR(dwError); |
| 1635 |
} |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
op = alloc_pending_op(key, op_key, buffer, *length); |
| 1640 |
if (!op) |
| 1641 |
return PJ_ENOMEM; |
| 1642 |
|
| 1643 |
op_key_rec = &op->pending_key; |
| 1644 |
|
| 1645 |
dwFlags &= ~(PJ_IOQUEUE_ALWAYS_ASYNC); |
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
pj_bzero( &op_key_rec->overlapped.overlapped, |
| 1652 |
sizeof(op_key_rec->overlapped.overlapped)); |
| 1653 |
op_key_rec->overlapped.operation = PJ_IOQUEUE_OP_RECV; |
| 1654 |
OPKEY_OPERATION(op_key) = PJ_IOQUEUE_OP_RECV; |
| 1655 |
|
| 1656 |
rc = WSARecvFrom((SOCKET)key->hnd, &op_key_rec->overlapped.wsabuf, 1, |
| 1657 |
&bytesRead, &dwFlags, addr, addrlen, |
| 1658 |
&op_key_rec->overlapped.overlapped, NULL); |
| 1659 |
if (rc == SOCKET_ERROR) { |
| 1660 |
DWORD dwStatus = WSAGetLastError(); |
| 1661 |
if (dwStatus!=WSA_IO_PENDING) { |
| 1662 |
*length = -1; |
| 1663 |
release_pending_op(key, op); |
| 1664 |
return PJ_STATUS_FROM_OS(dwStatus); |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
return PJ_EPENDING; |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
PJ_DEF(pj_status_t) pj_ioqueue_send( pj_ioqueue_key_t *key, |
| 1678 |
pj_ioqueue_op_key_t *op_key, |
| 1679 |
const void *data, |
| 1680 |
pj_ssize_t *length, |
| 1681 |
pj_uint32_t flags ) |
| 1682 |
{ |
| 1683 |
return pj_ioqueue_sendto(key, op_key, data, length, flags, NULL, 0); |
| 1684 |
} |
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
PJ_DEF(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_key_t *key, |
| 1693 |
pj_ioqueue_op_key_t *op_key, |
| 1694 |
const void *data, |
| 1695 |
pj_ssize_t *length, |
| 1696 |
pj_uint32_t flags, |
| 1697 |
const pj_sockaddr_t *addr, |
| 1698 |
int addrlen) |
| 1699 |
{ |
| 1700 |
int rc; |
| 1701 |
DWORD bytesWritten; |
| 1702 |
DWORD dwFlags; |
| 1703 |
union operation_key *op_key_rec; |
| 1704 |
struct pending_op *op; |
| 1705 |
|
| 1706 |
PJ_CHECK_STACK(); |
| 1707 |
PJ_ASSERT_RETURN(key && op_key && data, PJ_EINVAL); |
| 1708 |
|
| 1709 |
|
| 1710 |
if (key->closing) |
| 1711 |
return PJ_ECANCELLED; |
| 1712 |
|
| 1713 |
op_key_rec = (union operation_key*)op_key->internal__; |
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
op_key_rec->overlapped.wsabuf.buf = (void*)data; |
| 1719 |
op_key_rec->overlapped.wsabuf.len = (ULONG)*length; |
| 1720 |
|
| 1721 |
dwFlags = flags; |
| 1722 |
|
| 1723 |
if ((flags & PJ_IOQUEUE_ALWAYS_ASYNC) == 0) { |
| 1724 |
rc = WSASendTo((SOCKET)key->hnd, &op_key_rec->overlapped.wsabuf, 1, |
| 1725 |
&bytesWritten, dwFlags, addr, addrlen, |
| 1726 |
NULL, NULL); |
| 1727 |
if (rc == 0) { |
| 1728 |
*length = bytesWritten; |
| 1729 |
return PJ_SUCCESS; |
| 1730 |
} else { |
| 1731 |
DWORD dwStatus = WSAGetLastError(); |
| 1732 |
if (dwStatus != WSAEWOULDBLOCK) { |
| 1733 |
*length = -1; |
| 1734 |
return PJ_RETURN_OS_ERROR(dwStatus); |
| 1735 |
} |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
op = alloc_pending_op(key, op_key, (void *)data, *length); |
| 1740 |
if (!op) |
| 1741 |
return PJ_ENOMEM; |
| 1742 |
|
| 1743 |
op_key_rec = &op->pending_key; |
| 1744 |
|
| 1745 |
dwFlags &= ~(PJ_IOQUEUE_ALWAYS_ASYNC); |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
pj_bzero( &op_key_rec->overlapped.overlapped, |
| 1752 |
sizeof(op_key_rec->overlapped.overlapped)); |
| 1753 |
op_key_rec->overlapped.operation = PJ_IOQUEUE_OP_SEND; |
| 1754 |
OPKEY_OPERATION(op_key) = PJ_IOQUEUE_OP_SEND; |
| 1755 |
|
| 1756 |
rc = WSASendTo((SOCKET)key->hnd, &op_key_rec->overlapped.wsabuf, 1, |
| 1757 |
&bytesWritten, dwFlags, addr, addrlen, |
| 1758 |
&op_key_rec->overlapped.overlapped, NULL); |
| 1759 |
if (rc == SOCKET_ERROR) { |
| 1760 |
DWORD dwStatus = WSAGetLastError(); |
| 1761 |
if (dwStatus!=WSA_IO_PENDING) { |
| 1762 |
release_pending_op(key, op); |
| 1763 |
return PJ_STATUS_FROM_OS(dwStatus); |
| 1764 |
} |
| 1765 |
} |
| 1766 |
|
| 1767 |
|
| 1768 |
return PJ_EPENDING; |
| 1769 |
} |
| 1770 |
|
| 1771 |
#if PJ_HAS_TCP |
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
PJ_DEF(pj_status_t) pj_ioqueue_accept( pj_ioqueue_key_t *key, |
| 1779 |
pj_ioqueue_op_key_t *op_key, |
| 1780 |
pj_sock_t *new_sock, |
| 1781 |
pj_sockaddr_t *local, |
| 1782 |
pj_sockaddr_t *remote, |
| 1783 |
int *addrlen) |
| 1784 |
{ |
| 1785 |
BOOL rc; |
| 1786 |
DWORD bytesReceived; |
| 1787 |
pj_status_t status; |
| 1788 |
union operation_key *op_key_rec; |
| 1789 |
SOCKET sock; |
| 1790 |
struct pending_op *op; |
| 1791 |
|
| 1792 |
PJ_CHECK_STACK(); |
| 1793 |
PJ_ASSERT_RETURN(key && op_key && new_sock, PJ_EINVAL); |
| 1794 |
|
| 1795 |
|
| 1796 |
if (key->closing) |
| 1797 |
return PJ_ECANCELLED; |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
sock = WSAAccept((SOCKET)key->hnd, remote, addrlen, NULL, 0); |
| 1803 |
if (sock != INVALID_SOCKET) { |
| 1804 |
|
| 1805 |
if (local && addrlen) { |
| 1806 |
int status_; |
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
status_ = setsockopt(sock, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, |
| 1812 |
(char*)&key->hnd, sizeof(SOCKET)); |
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
status_ = getsockname(sock, local, addrlen); |
| 1818 |
if (status_ != 0) { |
| 1819 |
DWORD dwError = WSAGetLastError(); |
| 1820 |
closesocket(sock); |
| 1821 |
return PJ_RETURN_OS_ERROR(dwError); |
| 1822 |
} |
| 1823 |
} |
| 1824 |
|
| 1825 |
*new_sock = sock; |
| 1826 |
return PJ_SUCCESS; |
| 1827 |
|
| 1828 |
} else { |
| 1829 |
DWORD dwError = WSAGetLastError(); |
| 1830 |
if (dwError != WSAEWOULDBLOCK) { |
| 1831 |
return PJ_RETURN_OS_ERROR(dwError); |
| 1832 |
} |
| 1833 |
} |
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
op = alloc_pending_op(key, op_key, NULL, 0); |
| 1840 |
if (!op) |
| 1841 |
return PJ_ENOMEM; |
| 1842 |
|
| 1843 |
op_key_rec = &op->pending_key; |
| 1844 |
|
| 1845 |
status = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, |
| 1846 |
&op_key_rec->accept.newsock); |
| 1847 |
if (status != PJ_SUCCESS) { |
| 1848 |
release_pending_op(key, op); |
| 1849 |
return status; |
| 1850 |
} |
| 1851 |
|
| 1852 |
OPKEY_OPERATION(op_key) = PJ_IOQUEUE_OP_ACCEPT; |
| 1853 |
op_key_rec->accept.operation = PJ_IOQUEUE_OP_ACCEPT; |
| 1854 |
op_key_rec->accept.addrlen = addrlen; |
| 1855 |
op_key_rec->accept.local = local; |
| 1856 |
op_key_rec->accept.remote = remote; |
| 1857 |
op_key_rec->accept.newsock_ptr = new_sock; |
| 1858 |
pj_bzero( &op_key_rec->accept.overlapped, |
| 1859 |
sizeof(op_key_rec->accept.overlapped)); |
| 1860 |
|
| 1861 |
rc = AcceptEx( (SOCKET)key->hnd, (SOCKET)op_key_rec->accept.newsock, |
| 1862 |
op_key_rec->accept.accept_buf, |
| 1863 |
0, ACCEPT_ADDR_LEN, ACCEPT_ADDR_LEN, |
| 1864 |
&bytesReceived, |
| 1865 |
&op_key_rec->accept.overlapped ); |
| 1866 |
|
| 1867 |
if (rc == TRUE) { |
| 1868 |
ioqueue_on_accept_complete(key, &op_key_rec->accept); |
| 1869 |
release_pending_op(key, op); |
| 1870 |
return PJ_SUCCESS; |
| 1871 |
} else { |
| 1872 |
DWORD dwStatus = WSAGetLastError(); |
| 1873 |
if (dwStatus!=WSA_IO_PENDING) { |
| 1874 |
release_pending_op(key, op); |
| 1875 |
return PJ_STATUS_FROM_OS(dwStatus); |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
|
| 1880 |
return PJ_EPENDING; |
| 1881 |
} |
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
PJ_DEF(pj_status_t) pj_ioqueue_connect( pj_ioqueue_key_t *key, |
| 1891 |
const pj_sockaddr_t *addr, |
| 1892 |
int addrlen ) |
| 1893 |
{ |
| 1894 |
HANDLE hEvent; |
| 1895 |
pj_ioqueue_t *ioqueue; |
| 1896 |
|
| 1897 |
PJ_CHECK_STACK(); |
| 1898 |
PJ_ASSERT_RETURN(key && addr && addrlen, PJ_EINVAL); |
| 1899 |
|
| 1900 |
|
| 1901 |
if (key->closing) |
| 1902 |
return PJ_ECANCELLED; |
| 1903 |
|
| 1904 |
|
| 1905 |
if (connect((pj_sock_t)key->hnd, addr, addrlen) != 0) { |
| 1906 |
DWORD dwStatus; |
| 1907 |
dwStatus = WSAGetLastError(); |
| 1908 |
if (dwStatus != WSAEWOULDBLOCK) { |
| 1909 |
return PJ_RETURN_OS_ERROR(dwStatus); |
| 1910 |
} |
| 1911 |
} else { |
| 1912 |
|
| 1913 |
return PJ_SUCCESS; |
| 1914 |
} |
| 1915 |
|
| 1916 |
ioqueue = key->ioqueue; |
| 1917 |
|
| 1918 |
|
| 1919 |
pj_lock_acquire(ioqueue->lock); |
| 1920 |
|
| 1921 |
if (ioqueue->connecting_count >= MAXIMUM_WAIT_OBJECTS) { |
| 1922 |
pj_lock_release(ioqueue->lock); |
| 1923 |
return PJ_ETOOMANYCONN; |
| 1924 |
} |
| 1925 |
|
| 1926 |
|
| 1927 |
if (ioqueue->event_count) { |
| 1928 |
hEvent = ioqueue->event_pool[ioqueue->event_count - 1]; |
| 1929 |
--ioqueue->event_count; |
| 1930 |
} else { |
| 1931 |
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1932 |
if (hEvent == NULL) { |
| 1933 |
DWORD dwStatus = GetLastError(); |
| 1934 |
pj_lock_release(ioqueue->lock); |
| 1935 |
return PJ_STATUS_FROM_OS(dwStatus); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
key->connecting = 1; |
| 1943 |
|
| 1944 |
|
| 1945 |
if (WSAEventSelect((pj_sock_t)key->hnd, hEvent, FD_CONNECT) != 0) { |
| 1946 |
CloseHandle(hEvent); |
| 1947 |
pj_lock_release(ioqueue->lock); |
| 1948 |
return PJ_RETURN_OS_ERROR(WSAGetLastError()); |
| 1949 |
} |
| 1950 |
|
| 1951 |
|
| 1952 |
ioqueue->connecting_keys[ ioqueue->connecting_count ] = key; |
| 1953 |
ioqueue->connecting_handles[ ioqueue->connecting_count ] = hEvent; |
| 1954 |
ioqueue->connecting_count++; |
| 1955 |
|
| 1956 |
pj_lock_release(ioqueue->lock); |
| 1957 |
|
| 1958 |
return PJ_EPENDING; |
| 1959 |
} |
| 1960 |
#endif |
| 1961 |
|
| 1962 |
|
| 1963 |
PJ_DEF(void) pj_ioqueue_op_key_init( pj_ioqueue_op_key_t *op_key, |
| 1964 |
pj_size_t size ) |
| 1965 |
{ |
| 1966 |
pj_bzero(op_key, size); |
| 1967 |
} |
| 1968 |
|
| 1969 |
PJ_DEF(pj_bool_t) pj_ioqueue_is_pending( pj_ioqueue_key_t *key, |
| 1970 |
pj_ioqueue_op_key_t *op_key ) |
| 1971 |
{ |
| 1972 |
struct generic_overlapped* op_rec; |
| 1973 |
|
| 1974 |
PJ_UNUSED_ARG(key); |
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
op_rec = (struct generic_overlapped*)op_key; |
| 1980 |
return op_rec->operation != 0; |
| 1981 |
|
| 1982 |
#if 0 |
| 1983 |
BOOL rc; |
| 1984 |
DWORD bytesTransferred; |
| 1985 |
struct pending_op *op; |
| 1986 |
|
| 1987 |
op = get_pending_op(op_key); |
| 1988 |
if (!op) |
| 1989 |
return PJ_FALSE; |
| 1990 |
|
| 1991 |
rc = GetOverlappedResult( key->hnd, (LPOVERLAPPED)&op->pending_key, |
| 1992 |
&bytesTransferred, FALSE ); |
| 1993 |
|
| 1994 |
if (rc == FALSE) { |
| 1995 |
return GetLastError()==ERROR_IO_INCOMPLETE; |
| 1996 |
} |
| 1997 |
|
| 1998 |
return FALSE; |
| 1999 |
#endif |
| 2000 |
} |
| 2001 |
|
| 2002 |
|
| 2003 |
PJ_DEF(pj_status_t) pj_ioqueue_post_completion( pj_ioqueue_key_t *key, |
| 2004 |
pj_ioqueue_op_key_t *op_key, |
| 2005 |
pj_ssize_t bytes_status ) |
| 2006 |
{ |
| 2007 |
BOOL rc; |
| 2008 |
struct pending_op* op; |
| 2009 |
|
| 2010 |
op = get_pending_op(op_key); |
| 2011 |
if (!op) |
| 2012 |
return PJ_EINVAL; |
| 2013 |
|
| 2014 |
rc = PostQueuedCompletionStatus(key->ioqueue->iocp, (DWORD)bytes_status, |
| 2015 |
(ULONG_PTR)key, |
| 2016 |
(OVERLAPPED*)&op->pending_key ); |
| 2017 |
if (rc == FALSE) { |
| 2018 |
return PJ_RETURN_OS_ERROR(GetLastError()); |
| 2019 |
} |
| 2020 |
|
| 2021 |
return PJ_SUCCESS; |
| 2022 |
} |
| 2023 |
|
| 2024 |
PJ_DEF(pj_status_t) pj_ioqueue_set_concurrency(pj_ioqueue_key_t *key, |
| 2025 |
pj_bool_t allow) |
| 2026 |
{ |
| 2027 |
PJ_ASSERT_RETURN(key, PJ_EINVAL); |
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
PJ_ASSERT_RETURN(allow || PJ_IOQUEUE_HAS_SAFE_UNREG, PJ_EINVAL); |
| 2033 |
|
| 2034 |
key->allow_concurrent = allow; |
| 2035 |
return PJ_SUCCESS; |
| 2036 |
} |
| 2037 |
|
| 2038 |
PJ_DEF(pj_status_t) pj_ioqueue_lock_key(pj_ioqueue_key_t *key) |
| 2039 |
{ |
| 2040 |
PJ_ASSERT_RETURN(key && key->grp_lock, PJ_EINVAL); |
| 2041 |
return pj_grp_lock_acquire(key->grp_lock); |
| 2042 |
} |
| 2043 |
|
| 2044 |
PJ_DEF(pj_status_t) pj_ioqueue_unlock_key(pj_ioqueue_key_t *key) |
| 2045 |
{ |
| 2046 |
PJ_ASSERT_RETURN(key && key->grp_lock, PJ_EINVAL); |
| 2047 |
return pj_grp_lock_release(key->grp_lock); |
| 2048 |
} |
| 2049 |
|
| 2050 |
PJ_DEF(pj_oshandle_t) pj_ioqueue_get_os_handle( pj_ioqueue_t *ioqueue ) |
| 2051 |
{ |
| 2052 |
return ioqueue ? (pj_oshandle_t)ioqueue->iocp : NULL; |
| 2053 |
} |
| 2054 |
|
| 2055 |
|
| 2056 |
#endif |