393 #define BLOCKING_IO_RETURN_INT(FD, FUNC) { \ 394 int ret; \ 395 threadEntry_t self; \ 396 fdEntry_t *fdEntry = getFdEntry(FD); \ 397 if (fdEntry == NULL) { \ 398 errno = EBADF; \ 399 return -1; \ 400 } \ 401 do { \ 402 startOp(fdEntry, &self); \ 403 ret = FUNC; \ 404 endOp(fdEntry, &self); \ 405 } while (ret == -1 && errno == EINTR); \ 406 return ret; \ 407 } 408 409 int NET_Read(int s, void* buf, size_t len) { 410 BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); 411 } 412 413 int NET_ReadV(int s, const struct iovec * vector, int count) { 414 BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); 415 } 416 417 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags, 418 struct sockaddr *from, int *fromlen) { 419 socklen_t socklen = *fromlen; 420 BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, &socklen) ); 421 *fromlen = socklen; 422 } 423 424 int NET_Send(int s, void *msg, int len, unsigned int flags) { 425 BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) ); 426 } 427 428 int NET_WriteV(int s, const struct iovec * vector, int count) { 429 BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) ); 430 } 431 432 int NET_SendTo(int s, const void *msg, int len, unsigned int 486 errno = sockopt_arg; 487 return -1; 488 } 489 } else { 490 return crc; 491 } 492 493 /* At this point, fd is connected. Set successful return code */ 494 return 0; 495 } 496 497 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) { 498 BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) ); 499 } 500 501 /* 502 * Wrapper for poll(s, timeout). 503 * Auto restarts with adjusted timeout if interrupted by 504 * signal other than our wakeup signal. 505 */ 506 int NET_Timeout(int s, long timeout) { 507 long prevtime = 0, newtime; 508 struct timeval t; 509 fdEntry_t *fdEntry = getFdEntry(s); 510 511 /* 512 * Check that fd hasn't been closed. 513 */ 514 if (fdEntry == NULL) { 515 errno = EBADF; 516 return -1; 517 } 518 519 /* 520 * Pick up current time as may need to adjust timeout 521 */ 522 if (timeout > 0) { 523 gettimeofday(&t, NULL); 524 prevtime = t.tv_sec * 1000 + t.tv_usec / 1000; 525 } 526 527 for(;;) { 528 struct pollfd pfd; 529 int rv; 530 threadEntry_t self; 531 532 /* 533 * Poll the fd. If interrupted by our wakeup signal 534 * errno will be set to EBADF. 535 */ 536 pfd.fd = s; 537 pfd.events = POLLIN | POLLERR; 538 539 startOp(fdEntry, &self); 540 rv = poll(&pfd, 1, timeout); 541 endOp(fdEntry, &self); 542 543 /* 544 * If interrupted then adjust timeout. If timeout 545 * has expired return 0 (indicating timeout expired). 546 */ 547 if (rv < 0 && errno == EINTR) { 548 if (timeout > 0) { 549 gettimeofday(&t, NULL); 550 newtime = t.tv_sec * 1000 + t.tv_usec / 1000; 551 timeout -= newtime - prevtime; 552 if (timeout <= 0) { 553 return 0; 554 } 555 prevtime = newtime; 556 } 557 } else { 558 return rv; 559 } 560 561 } 562 } | 393 #define BLOCKING_IO_RETURN_INT(FD, FUNC) { \ 394 int ret; \ 395 threadEntry_t self; \ 396 fdEntry_t *fdEntry = getFdEntry(FD); \ 397 if (fdEntry == NULL) { \ 398 errno = EBADF; \ 399 return -1; \ 400 } \ 401 do { \ 402 startOp(fdEntry, &self); \ 403 ret = FUNC; \ 404 endOp(fdEntry, &self); \ 405 } while (ret == -1 && errno == EINTR); \ 406 return ret; \ 407 } 408 409 int NET_Read(int s, void* buf, size_t len) { 410 BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); 411 } 412 413 int NET_NonBlockingRead(int s, void* buf, size_t len) { 414 BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT)); 415 } 416 417 int NET_ReadV(int s, const struct iovec * vector, int count) { 418 BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); 419 } 420 421 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags, 422 struct sockaddr *from, int *fromlen) { 423 socklen_t socklen = *fromlen; 424 BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, &socklen) ); 425 *fromlen = socklen; 426 } 427 428 int NET_Send(int s, void *msg, int len, unsigned int flags) { 429 BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) ); 430 } 431 432 int NET_WriteV(int s, const struct iovec * vector, int count) { 433 BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) ); 434 } 435 436 int NET_SendTo(int s, const void *msg, int len, unsigned int 490 errno = sockopt_arg; 491 return -1; 492 } 493 } else { 494 return crc; 495 } 496 497 /* At this point, fd is connected. Set successful return code */ 498 return 0; 499 } 500 501 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) { 502 BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) ); 503 } 504 505 /* 506 * Wrapper for poll(s, timeout). 507 * Auto restarts with adjusted timeout if interrupted by 508 * signal other than our wakeup signal. 509 */ 510 int NET_Timeout0(int s, long timeout,long currentTime) { 511 long prevtime = currentTime, newtime; 512 struct timeval t; 513 fdEntry_t *fdEntry = getFdEntry(s); 514 515 /* 516 * Check that fd hasn't been closed. 517 */ 518 if (fdEntry == NULL) { 519 errno = EBADF; 520 return -1; 521 } 522 523 for(;;) { 524 struct pollfd pfd; 525 int rv; 526 threadEntry_t self; 527 528 /* 529 * Poll the fd. If interrupted by our wakeup signal 530 * errno will be set to EBADF. 531 */ 532 pfd.fd = s; 533 pfd.events = POLLIN | POLLERR; 534 535 startOp(fdEntry, &self); 536 rv = poll(&pfd, 1, timeout); 537 endOp(fdEntry, &self); 538 539 /* 540 * If interrupted then adjust timeout. If timeout 541 * has expired return 0 (indicating timeout expired). 542 */ 543 if (rv < 0 && errno == EINTR) { 544 if (timeout > 0) { 545 gettimeofday(&t, NULL); 546 newtime = t.tv_sec * 1000 + t.tv_usec / 1000; 547 timeout -= newtime - prevtime; 548 if (timeout <= 0) { 549 return 0; 550 } 551 prevtime = newtime; 552 } 553 } else { 554 return rv; 555 } 556 557 } 558 } 559 560 int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime) { 561 return NET_Timeout0(s, timeout, currentTime); 562 } 563 564 int NET_Timeout(int s, long timeout) { 565 long currentTime = 0; 566 struct timeval t; 567 if (timeout > 0) { 568 gettimeofday(&t, NULL); 569 currentTime = t.tv_sec * 1000 + t.tv_usec / 1000; 570 } 571 return NET_Timeout0(s, timeout, currentTime); 572 } |