2
0

sendrecv.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_private.h"
  17. #if BEOS_BONE /* BONE uses the unix code - woohoo */
  18. #include "../unix/sendrecv.c"
  19. #else
  20. #include "fspr_arch_networkio.h"
  21. #include "fspr_time.h"
  22. static fspr_status_t wait_for_io_or_timeout(fspr_socket_t *sock, int for_read)
  23. {
  24. struct timeval tv, *tvptr;
  25. fd_set fdset;
  26. int srv;
  27. do {
  28. FD_ZERO(&fdset);
  29. FD_SET(sock->socketdes, &fdset);
  30. if (sock->timeout < 0) {
  31. tvptr = NULL;
  32. }
  33. else {
  34. tv.tv_sec = sock->timeout / APR_USEC_PER_SEC;
  35. tv.tv_usec = sock->timeout % APR_USEC_PER_SEC;
  36. tvptr = &tv;
  37. }
  38. srv = select(sock->socketdes + 1,
  39. for_read ? &fdset : NULL,
  40. for_read ? NULL : &fdset,
  41. NULL,
  42. tvptr);
  43. /* TODO - timeout should be smaller on repeats of this loop */
  44. } while (srv == -1 && errno == EINTR);
  45. if (srv == 0) {
  46. return APR_TIMEUP;
  47. }
  48. else if (srv < 0) {
  49. return errno;
  50. }
  51. return APR_SUCCESS;
  52. }
  53. #define SEND_WAIT APR_USEC_PER_SEC / 10
  54. APR_DECLARE(fspr_status_t) fspr_socket_send(fspr_socket_t *sock, const char *buf,
  55. fspr_size_t *len)
  56. {
  57. fspr_ssize_t rv;
  58. do {
  59. rv = send(sock->socketdes, buf, (*len), 0);
  60. } while (rv == -1 && errno == EINTR);
  61. if (rv == -1 && errno == EWOULDBLOCK && sock->timeout > 0) {
  62. fspr_int32_t snooze_val = SEND_WAIT;
  63. fspr_int32_t zzz = 0;
  64. do {
  65. rv = send(sock->socketdes, buf, (*len), 0);
  66. if (rv == -1 && errno == EWOULDBLOCK){
  67. fspr_sleep (snooze_val);
  68. zzz += snooze_val;
  69. snooze_val += SEND_WAIT;
  70. /* have we passed our timeout value */
  71. if (zzz > (sock->timeout * APR_USEC_PER_SEC))
  72. break;
  73. }
  74. } while (rv == -1 && (errno == EINTR || errno == EWOULDBLOCK));
  75. }
  76. if (rv == -1) {
  77. *len = 0;
  78. return errno;
  79. }
  80. (*len) = rv;
  81. return APR_SUCCESS;
  82. }
  83. APR_DECLARE(fspr_status_t) fspr_socket_recv(fspr_socket_t *sock, char *buf,
  84. fspr_size_t *len)
  85. {
  86. fspr_ssize_t rv;
  87. do {
  88. rv = recv(sock->socketdes, buf, (*len), 0);
  89. } while (rv == -1 && errno == EINTR);
  90. if (rv == -1 && errno == EWOULDBLOCK && sock->timeout > 0) {
  91. fspr_status_t arv = wait_for_io_or_timeout(sock, 1);
  92. if (arv != APR_SUCCESS) {
  93. *len = 0;
  94. return arv;
  95. }
  96. else {
  97. do {
  98. rv = recv(sock->socketdes, buf, (*len), 0);
  99. } while (rv == -1 && errno == EINTR);
  100. }
  101. }
  102. if (rv == -1) {
  103. (*len) = 0;
  104. return errno;
  105. }
  106. (*len) = rv;
  107. if (rv == 0)
  108. return APR_EOF;
  109. return APR_SUCCESS;
  110. }
  111. /* BeOS doesn't have writev for sockets so we use the following instead...
  112. */
  113. APR_DECLARE(fspr_status_t) fspr_socket_sendv(fspr_socket_t * sock,
  114. const struct iovec *vec,
  115. fspr_int32_t nvec, fspr_size_t *len)
  116. {
  117. *len = vec[0].iov_len;
  118. return fspr_socket_send(sock, vec[0].iov_base, len);
  119. }
  120. APR_DECLARE(fspr_status_t) fspr_socket_sendto(fspr_socket_t *sock,
  121. fspr_sockaddr_t *where,
  122. fspr_int32_t flags, const char *buf,
  123. fspr_size_t *len)
  124. {
  125. fspr_ssize_t rv;
  126. do {
  127. rv = sendto(sock->socketdes, buf, (*len), flags,
  128. (const struct sockaddr*)&where->sa,
  129. where->salen);
  130. } while (rv == -1 && errno == EINTR);
  131. if (rv == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)
  132. && sock->timeout != 0) {
  133. fspr_status_t arv = wait_for_io_or_timeout(sock, 0);
  134. if (arv != APR_SUCCESS) {
  135. *len = 0;
  136. return arv;
  137. } else {
  138. do {
  139. rv = sendto(sock->socketdes, buf, (*len), flags,
  140. (const struct sockaddr*)&where->sa,
  141. where->salen);
  142. } while (rv == -1 && errno == EINTR);
  143. }
  144. }
  145. if (rv == -1) {
  146. *len = 0;
  147. return errno;
  148. }
  149. *len = rv;
  150. return APR_SUCCESS;
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_socket_recvfrom(fspr_sockaddr_t *from,
  153. fspr_socket_t *sock,
  154. fspr_int32_t flags, char *buf,
  155. fspr_size_t *len)
  156. {
  157. fspr_ssize_t rv;
  158. if (from == NULL){
  159. return APR_ENOMEM;
  160. /* Not sure if this is correct. Maybe we should just allocate
  161. the memory??
  162. */
  163. }
  164. do {
  165. rv = recvfrom(sock->socketdes, buf, (*len), flags,
  166. (struct sockaddr*)&from->sa, &from->salen);
  167. } while (rv == -1 && errno == EINTR);
  168. if (rv == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
  169. sock->timeout != 0) {
  170. fspr_status_t arv = wait_for_io_or_timeout(sock, 1);
  171. if (arv != APR_SUCCESS) {
  172. *len = 0;
  173. return arv;
  174. } else {
  175. do {
  176. rv = recvfrom(sock->socketdes, buf, (*len), flags,
  177. (struct sockaddr*)&from->sa, &from->salen);
  178. } while (rv == -1 && errno == EINTR);
  179. }
  180. }
  181. if (rv == -1) {
  182. (*len) = 0;
  183. return errno;
  184. }
  185. (*len) = rv;
  186. if (rv == 0)
  187. return APR_EOF;
  188. return APR_SUCCESS;
  189. }
  190. #endif /* ! BEOS_BONE */