diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-07-02 18:44:05 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-07-02 18:44:05 +0000 |
| commit | b1a4ad2b1996936744c679e6ae563986ff701a3a (patch) | |
| tree | c446d3bf2fe5bb0452027ee93388e505c2dfa53d /src/usocket.c | |
| parent | 63807d647624df155a81a2b323f370e2c36192f6 (diff) | |
| download | luasocket-b1a4ad2b1996936744c679e6ae563986ff701a3a.tar.gz luasocket-b1a4ad2b1996936744c679e6ae563986ff701a3a.tar.bz2 luasocket-b1a4ad2b1996936744c679e6ae563986ff701a3a.zip | |
Compiles and runs on windows.
Diffstat (limited to 'src/usocket.c')
| -rw-r--r-- | src/usocket.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/src/usocket.c b/src/usocket.c index b99eaa8..0e3d30c 100644 --- a/src/usocket.c +++ b/src/usocket.c | |||
| @@ -2,20 +2,13 @@ | |||
| 2 | * Socket compatibilization module for Unix | 2 | * Socket compatibilization module for Unix |
| 3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
| 4 | * | 4 | * |
| 5 | * We are now treating EINTRs, but if an interrupt happens in the middle of | 5 | * The code is now interrupt-safe. |
| 6 | * a select function call, we don't guarantee values timeouts anymore. | 6 | * The penalty of calling select to avoid busy-wait is only paid when |
| 7 | * It's not a big deal, since we are not real-time anyways. | 7 | * the I/O call fail in the first place. |
| 8 | * | ||
| 9 | * We also exchanged the order of the calls to send/recv and select. | ||
| 10 | * The idea is that the outer loop (whoever is calling sock_send/recv) | ||
| 11 | * will call the function again if we didn't time out, so we can | ||
| 12 | * call write and then select only if it fails. This moves the penalty | ||
| 13 | * to when data is not available, maximizing the bandwidth if data is | ||
| 14 | * always available. | ||
| 15 | * | 8 | * |
| 16 | * RCS ID: $Id$ | 9 | * RCS ID: $Id$ |
| 17 | \*=========================================================================*/ | 10 | \*=========================================================================*/ |
| 18 | #include <string.h> | 11 | #include <string.h> |
| 19 | #include <signal.h> | 12 | #include <signal.h> |
| 20 | 13 | ||
| 21 | #include "socket.h" | 14 | #include "socket.h" |
| @@ -177,9 +170,9 @@ const char *sock_accept(p_sock ps, p_sock pa, SA *addr, | |||
| 177 | FD_SET(sock, &fds); | 170 | FD_SET(sock, &fds); |
| 178 | err = sock_select(sock+1, &fds, NULL, NULL, tm); | 171 | err = sock_select(sock+1, &fds, NULL, NULL, tm); |
| 179 | if (err == 0) return io_strerror(IO_TIMEOUT); | 172 | if (err == 0) return io_strerror(IO_TIMEOUT); |
| 180 | else if (err < 0) return sock_strerror(); | 173 | else if (err < 0) break; |
| 181 | } | 174 | } |
| 182 | return io_strerror(IO_TIMEOUT); /* can't get here */ | 175 | return sock_strerror(); |
| 183 | } | 176 | } |
| 184 | 177 | ||
| 185 | /*-------------------------------------------------------------------------*\ | 178 | /*-------------------------------------------------------------------------*\ |
| @@ -217,9 +210,10 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) | |||
| 217 | FD_SET(sock, &fds); | 210 | FD_SET(sock, &fds); |
| 218 | ret = sock_select(sock+1, NULL, &fds, NULL, tm); | 211 | ret = sock_select(sock+1, NULL, &fds, NULL, tm); |
| 219 | if (ret == 0) return IO_TIMEOUT; | 212 | if (ret == 0) return IO_TIMEOUT; |
| 220 | if (ret < 0) return IO_USER; | 213 | else if (ret < 0) break; |
| 221 | /* otherwise, try sending again */ | 214 | /* otherwise, try sending again */ |
| 222 | } | 215 | } |
| 216 | return IO_USER; | ||
| 223 | } | 217 | } |
| 224 | 218 | ||
| 225 | /*-------------------------------------------------------------------------*\ | 219 | /*-------------------------------------------------------------------------*\ |
| @@ -250,8 +244,9 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, | |||
| 250 | FD_SET(sock, &fds); | 244 | FD_SET(sock, &fds); |
| 251 | ret = sock_select(sock+1, NULL, &fds, NULL, tm); | 245 | ret = sock_select(sock+1, NULL, &fds, NULL, tm); |
| 252 | if (ret == 0) return IO_TIMEOUT; | 246 | if (ret == 0) return IO_TIMEOUT; |
| 253 | if (ret < 0) return IO_USER; | 247 | else if (ret < 0) break; |
| 254 | } | 248 | } |
| 249 | return IO_USER; | ||
| 255 | } | 250 | } |
| 256 | 251 | ||
| 257 | /*-------------------------------------------------------------------------*\ | 252 | /*-------------------------------------------------------------------------*\ |
| @@ -278,8 +273,9 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { | |||
| 278 | FD_SET(sock, &fds); | 273 | FD_SET(sock, &fds); |
| 279 | ret = sock_select(sock+1, &fds, NULL, NULL, tm); | 274 | ret = sock_select(sock+1, &fds, NULL, NULL, tm); |
| 280 | if (ret == 0) return IO_TIMEOUT; | 275 | if (ret == 0) return IO_TIMEOUT; |
| 281 | if (ret < 0) return IO_USER; | 276 | else if (ret < 0) break; |
| 282 | } | 277 | } |
| 278 | return IO_USER; | ||
| 283 | } | 279 | } |
| 284 | 280 | ||
| 285 | /*-------------------------------------------------------------------------*\ | 281 | /*-------------------------------------------------------------------------*\ |
| @@ -307,8 +303,9 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, | |||
| 307 | FD_SET(sock, &fds); | 303 | FD_SET(sock, &fds); |
| 308 | ret = sock_select(sock+1, &fds, NULL, NULL, tm); | 304 | ret = sock_select(sock+1, &fds, NULL, NULL, tm); |
| 309 | if (ret == 0) return IO_TIMEOUT; | 305 | if (ret == 0) return IO_TIMEOUT; |
| 310 | if (ret < 0) return IO_USER; | 306 | else if (ret < 0) break; |
| 311 | } | 307 | } |
| 308 | return IO_USER; | ||
| 312 | } | 309 | } |
| 313 | 310 | ||
| 314 | /*-------------------------------------------------------------------------*\ | 311 | /*-------------------------------------------------------------------------*\ |
| @@ -333,7 +330,12 @@ void sock_setnonblocking(p_sock ps) { | |||
| 333 | * Error translation functions | 330 | * Error translation functions |
| 334 | \*-------------------------------------------------------------------------*/ | 331 | \*-------------------------------------------------------------------------*/ |
| 335 | const char *sock_hoststrerror(void) { | 332 | const char *sock_hoststrerror(void) { |
| 336 | return hstrerror(h_errno); | 333 | switch (h_errno) { |
| 334 | case HOST_NOT_FOUND: | ||
| 335 | return "host not found"; | ||
| 336 | default: | ||
| 337 | return hstrerror(h_errno); | ||
| 338 | } | ||
| 337 | } | 339 | } |
| 338 | 340 | ||
| 339 | /* make sure important error messages are standard */ | 341 | /* make sure important error messages are standard */ |
