diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-11-20 07:20:26 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-11-20 07:20:26 +0000 |
| commit | f20f4889bfe5a02cd9b77868b90cc8042352176a (patch) | |
| tree | 339e085e385190ea5f3a9246c3a790c45036c61c /src/wsocket.c | |
| parent | 087b4f865d861162eb32a4081bb7c2087688919a (diff) | |
| download | luasocket-f20f4889bfe5a02cd9b77868b90cc8042352176a.tar.gz luasocket-f20f4889bfe5a02cd9b77868b90cc8042352176a.tar.bz2 luasocket-f20f4889bfe5a02cd9b77868b90cc8042352176a.zip | |
Changed prefix of function names to match module names.
Removed some warnings and useless code.
Diffstat (limited to 'src/wsocket.c')
| -rw-r--r-- | src/wsocket.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/wsocket.c b/src/wsocket.c index a690dad..dce333b 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
| @@ -46,16 +46,20 @@ int socket_close(void) { | |||
| 46 | #define WAITFD_E 4 | 46 | #define WAITFD_E 4 |
| 47 | #define WAITFD_C (WAITFD_E|WAITFD_W) | 47 | #define WAITFD_C (WAITFD_E|WAITFD_W) |
| 48 | 48 | ||
| 49 | int socket_waitfd(p_socket ps, int sw, p_tm tm) { | 49 | int socket_waitfd(p_socket ps, int sw, p_timeout tm) { |
| 50 | int ret; | 50 | int ret; |
| 51 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; | 51 | fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; |
| 52 | struct timeval tv, *tp = NULL; | 52 | struct timeval tv, *tp = NULL; |
| 53 | double t; | 53 | double t; |
| 54 | if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ | 54 | if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ |
| 55 | if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; } | 55 | if (sw & WAITFD_R) { |
| 56 | FD_ZERO(&rfds); | ||
| 57 | FD_SET(*ps, &rfds); | ||
| 58 | rp = &rfds; | ||
| 59 | } | ||
| 56 | if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } | 60 | if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } |
| 57 | if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } | 61 | if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } |
| 58 | if ((t = tm_get(tm)) >= 0.0) { | 62 | if ((t = timeout_get(tm)) >= 0.0) { |
| 59 | tv.tv_sec = (int) t; | 63 | tv.tv_sec = (int) t; |
| 60 | tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6); | 64 | tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6); |
| 61 | tp = &tv; | 65 | tp = &tv; |
| @@ -70,9 +74,10 @@ int socket_waitfd(p_socket ps, int sw, p_tm tm) { | |||
| 70 | /*-------------------------------------------------------------------------*\ | 74 | /*-------------------------------------------------------------------------*\ |
| 71 | * Select with int timeout in ms | 75 | * Select with int timeout in ms |
| 72 | \*-------------------------------------------------------------------------*/ | 76 | \*-------------------------------------------------------------------------*/ |
| 73 | int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { | 77 | int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, |
| 78 | p_timeout tm) { | ||
| 74 | struct timeval tv; | 79 | struct timeval tv; |
| 75 | double t = tm_get(tm); | 80 | double t = timeout_get(tm); |
| 76 | tv.tv_sec = (int) t; | 81 | tv.tv_sec = (int) t; |
| 77 | tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); | 82 | tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); |
| 78 | if (n <= 0) { | 83 | if (n <= 0) { |
| @@ -113,7 +118,7 @@ int socket_create(p_socket ps, int domain, int type, int protocol) { | |||
| 113 | /*-------------------------------------------------------------------------*\ | 118 | /*-------------------------------------------------------------------------*\ |
| 114 | * Connects or returns error message | 119 | * Connects or returns error message |
| 115 | \*-------------------------------------------------------------------------*/ | 120 | \*-------------------------------------------------------------------------*/ |
| 116 | int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { | 121 | int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { |
| 117 | int err; | 122 | int err; |
| 118 | /* don't call on closed socket */ | 123 | /* don't call on closed socket */ |
| 119 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 124 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| @@ -123,7 +128,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { | |||
| 123 | err = WSAGetLastError(); | 128 | err = WSAGetLastError(); |
| 124 | if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; | 129 | if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; |
| 125 | /* zero timeout case optimization */ | 130 | /* zero timeout case optimization */ |
| 126 | if (tm_iszero(tm)) return IO_TIMEOUT; | 131 | if (timeout_iszero(tm)) return IO_TIMEOUT; |
| 127 | /* we wait until something happens */ | 132 | /* we wait until something happens */ |
| 128 | err = socket_waitfd(ps, WAITFD_C, tm); | 133 | err = socket_waitfd(ps, WAITFD_C, tm); |
| 129 | if (err == IO_CLOSED) { | 134 | if (err == IO_CLOSED) { |
| @@ -131,7 +136,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { | |||
| 131 | /* give windows time to set the error (yes, disgusting) */ | 136 | /* give windows time to set the error (yes, disgusting) */ |
| 132 | Sleep(10); | 137 | Sleep(10); |
| 133 | /* find out why we failed */ | 138 | /* find out why we failed */ |
| 134 | getsockopt(*ps, SOL_SOCKETET, SO_ERROR, (char *)&err, &len); | 139 | getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); |
| 135 | /* we KNOW there was an error. if 'why' is 0, we will return | 140 | /* we KNOW there was an error. if 'why' is 0, we will return |
| 136 | * "unknown error", but it's not really our fault */ | 141 | * "unknown error", but it's not really our fault */ |
| 137 | return err > 0? err: IO_UNKNOWN; | 142 | return err > 0? err: IO_UNKNOWN; |
| @@ -164,7 +169,8 @@ int socket_listen(p_socket ps, int backlog) { | |||
| 164 | /*-------------------------------------------------------------------------*\ | 169 | /*-------------------------------------------------------------------------*\ |
| 165 | * Accept with timeout | 170 | * Accept with timeout |
| 166 | \*-------------------------------------------------------------------------*/ | 171 | \*-------------------------------------------------------------------------*/ |
| 167 | int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_tm tm) { | 172 | int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, |
| 173 | p_timeout tm) { | ||
| 168 | SA daddr; | 174 | SA daddr; |
| 169 | socklen_t dlen = sizeof(daddr); | 175 | socklen_t dlen = sizeof(daddr); |
| 170 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 176 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| @@ -192,7 +198,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_tm tm) { | |||
| 192 | * Therefore, whoever calls this function should not pass a huge buffer. | 198 | * Therefore, whoever calls this function should not pass a huge buffer. |
| 193 | \*-------------------------------------------------------------------------*/ | 199 | \*-------------------------------------------------------------------------*/ |
| 194 | int socket_send(p_socket ps, const char *data, size_t count, | 200 | int socket_send(p_socket ps, const char *data, size_t count, |
| 195 | size_t *sent, p_tm tm) | 201 | size_t *sent, p_timeout tm) |
| 196 | { | 202 | { |
| 197 | int err; | 203 | int err; |
| 198 | /* avoid making system calls on closed sockets */ | 204 | /* avoid making system calls on closed sockets */ |
| @@ -222,7 +228,7 @@ int socket_send(p_socket ps, const char *data, size_t count, | |||
| 222 | * Sendto with timeout | 228 | * Sendto with timeout |
| 223 | \*-------------------------------------------------------------------------*/ | 229 | \*-------------------------------------------------------------------------*/ |
| 224 | int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | 230 | int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, |
| 225 | SA *addr, socklen_t len, p_tm tm) | 231 | SA *addr, socklen_t len, p_timeout tm) |
| 226 | { | 232 | { |
| 227 | int err; | 233 | int err; |
| 228 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 234 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| @@ -243,7 +249,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | |||
| 243 | /*-------------------------------------------------------------------------*\ | 249 | /*-------------------------------------------------------------------------*\ |
| 244 | * Receive with timeout | 250 | * Receive with timeout |
| 245 | \*-------------------------------------------------------------------------*/ | 251 | \*-------------------------------------------------------------------------*/ |
| 246 | int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_tm tm) { | 252 | int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { |
| 247 | int err; | 253 | int err; |
| 248 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 254 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| 249 | *got = 0; | 255 | *got = 0; |
| @@ -265,7 +271,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_tm tm) { | |||
| 265 | * Recvfrom with timeout | 271 | * Recvfrom with timeout |
| 266 | \*-------------------------------------------------------------------------*/ | 272 | \*-------------------------------------------------------------------------*/ |
| 267 | int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, | 273 | int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, |
| 268 | SA *addr, socklen_t *len, p_tm tm) { | 274 | SA *addr, socklen_t *len, p_timeout tm) { |
| 269 | int err; | 275 | int err; |
| 270 | if (*ps == SOCKET_INVALID) return IO_CLOSED; | 276 | if (*ps == SOCKET_INVALID) return IO_CLOSED; |
| 271 | *got = 0; | 277 | *got = 0; |
