aboutsummaryrefslogtreecommitdiff
path: root/src/usocket.c
diff options
context:
space:
mode:
authorSam Roberts <vieuxtech@gmail.com>2012-04-11 14:18:20 -0700
committerSam Roberts <vieuxtech@gmail.com>2012-04-11 14:18:20 -0700
commit4b671f4551e98ac9e1d9a7407d3dffdd7eb1d3dc (patch)
treeba92aa753ae1b145760cb1c5e69c886d3bf11328 /src/usocket.c
parentf399ab25fcecad2ff96a5977e8eaf069bb45473c (diff)
parent195b2a74bb3f368b1f31f9c8bbc1ce0f54de2035 (diff)
downloadluasocket-4b671f4551e98ac9e1d9a7407d3dffdd7eb1d3dc.tar.gz
luasocket-4b671f4551e98ac9e1d9a7407d3dffdd7eb1d3dc.tar.bz2
luasocket-4b671f4551e98ac9e1d9a7407d3dffdd7eb1d3dc.zip
Merge branch 'git-sam' into diego-sam-mwild-integration
Conflicts in options.c were just due to independent small functions being close to each other. unix.c in mwild was broken, it wasn't using LUASOCKET_API. serial.c needed luaL_reg renamed, and to use LUASOCKET_API. makefile didn't respect standard DESTDIR and prefix makefile variables, and didn't allow LUAV variable to select lua version to build against. I've tested the top-level install-both target builds and installs against both lua5.1 and lua5.2, but not done further testing. Conflicts: README config gem/ltn012.tex makefile src/makefile src/options.c src/options.h src/tcp.c src/usocket.c
Diffstat (limited to 'src/usocket.c')
-rw-r--r--src/usocket.c76
1 files changed, 68 insertions, 8 deletions
diff --git a/src/usocket.c b/src/usocket.c
index fdab123..bf2d19c 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -16,7 +16,7 @@
16/*-------------------------------------------------------------------------*\ 16/*-------------------------------------------------------------------------*\
17* Wait for readable/writable/connected socket with timeout 17* Wait for readable/writable/connected socket with timeout
18\*-------------------------------------------------------------------------*/ 18\*-------------------------------------------------------------------------*/
19#ifdef SOCKET_POLL 19#ifndef SOCKET_SELECT
20#include <sys/poll.h> 20#include <sys/poll.h>
21 21
22#define WAITFD_R POLLIN 22#define WAITFD_R POLLIN
@@ -49,6 +49,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
49 fd_set rfds, wfds, *rp, *wp; 49 fd_set rfds, wfds, *rp, *wp;
50 struct timeval tv, *tp; 50 struct timeval tv, *tp;
51 double t; 51 double t;
52 if (*ps >= FD_SETSIZE) return EINVAL;
52 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ 53 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
53 do { 54 do {
54 /* must set bits within loop, because select may have modifed them */ 55 /* must set bits within loop, because select may have modifed them */
@@ -213,14 +214,13 @@ int socket_send(p_socket ps, const char *data, size_t count,
213 for ( ;; ) { 214 for ( ;; ) {
214 long put = (long) send(*ps, data, count, 0); 215 long put = (long) send(*ps, data, count, 0);
215 /* if we sent anything, we are done */ 216 /* if we sent anything, we are done */
216 if (put > 0) { 217 if (put >= 0) {
217 *sent = put; 218 *sent = put;
218 return IO_DONE; 219 return IO_DONE;
219 } 220 }
220 err = errno; 221 err = errno;
221 /* send can't really return 0, but EPIPE means the connection was 222 /* EPIPE means the connection was closed */
222 closed */ 223 if (err == EPIPE) return IO_CLOSED;
223 if (put == 0 || err == EPIPE) return IO_CLOSED;
224 /* we call was interrupted, just try again */ 224 /* we call was interrupted, just try again */
225 if (err == EINTR) continue; 225 if (err == EINTR) continue;
226 /* if failed fatal reason, report error */ 226 /* if failed fatal reason, report error */
@@ -243,12 +243,12 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
243 if (*ps == SOCKET_INVALID) return IO_CLOSED; 243 if (*ps == SOCKET_INVALID) return IO_CLOSED;
244 for ( ;; ) { 244 for ( ;; ) {
245 long put = (long) sendto(*ps, data, count, 0, addr, len); 245 long put = (long) sendto(*ps, data, count, 0, addr, len);
246 if (put > 0) { 246 if (put >= 0) {
247 *sent = put; 247 *sent = put;
248 return IO_DONE; 248 return IO_DONE;
249 } 249 }
250 err = errno; 250 err = errno;
251 if (put == 0 || err == EPIPE) return IO_CLOSED; 251 if (err == EPIPE) return IO_CLOSED;
252 if (err == EINTR) continue; 252 if (err == EINTR) continue;
253 if (err != EAGAIN) return err; 253 if (err != EAGAIN) return err;
254 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; 254 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
@@ -301,6 +301,66 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
301 return IO_UNKNOWN; 301 return IO_UNKNOWN;
302} 302}
303 303
304
305/*-------------------------------------------------------------------------*\
306* Write with timeout
307*
308* socket_read and socket_write are cut-n-paste of socket_send and socket_recv,
309* with send/recv replaced with write/read. We can't just use write/read
310* in the socket version, because behaviour when size is zero is different.
311\*-------------------------------------------------------------------------*/
312int socket_write(p_socket ps, const char *data, size_t count,
313 size_t *sent, p_timeout tm)
314{
315 int err;
316 *sent = 0;
317 /* avoid making system calls on closed sockets */
318 if (*ps == SOCKET_INVALID) return IO_CLOSED;
319 /* loop until we send something or we give up on error */
320 for ( ;; ) {
321 long put = (long) write(*ps, data, count);
322 /* if we sent anything, we are done */
323 if (put >= 0) {
324 *sent = put;
325 return IO_DONE;
326 }
327 err = errno;
328 /* EPIPE means the connection was closed */
329 if (err == EPIPE) return IO_CLOSED;
330 /* we call was interrupted, just try again */
331 if (err == EINTR) continue;
332 /* if failed fatal reason, report error */
333 if (err != EAGAIN) return err;
334 /* wait until we can send something or we timeout */
335 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
336 }
337 /* can't reach here */
338 return IO_UNKNOWN;
339}
340
341/*-------------------------------------------------------------------------*\
342* Read with timeout
343* See note for socket_write
344\*-------------------------------------------------------------------------*/
345int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
346 int err;
347 *got = 0;
348 if (*ps == SOCKET_INVALID) return IO_CLOSED;
349 for ( ;; ) {
350 long taken = (long) read(*ps, data, count);
351 if (taken > 0) {
352 *got = taken;
353 return IO_DONE;
354 }
355 err = errno;
356 if (taken == 0) return IO_CLOSED;
357 if (err == EINTR) continue;
358 if (err != EAGAIN) return err;
359 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
360 }
361 return IO_UNKNOWN;
362}
363
304/*-------------------------------------------------------------------------*\ 364/*-------------------------------------------------------------------------*\
305* Put socket into blocking mode 365* Put socket into blocking mode
306\*-------------------------------------------------------------------------*/ 366\*-------------------------------------------------------------------------*/
@@ -360,7 +420,7 @@ const char *socket_strerror(int err) {
360 case ECONNABORTED: return "closed"; 420 case ECONNABORTED: return "closed";
361 case ECONNRESET: return "closed"; 421 case ECONNRESET: return "closed";
362 case ETIMEDOUT: return "timeout"; 422 case ETIMEDOUT: return "timeout";
363 default: return strerror(errno); 423 default: return strerror(err);
364 } 424 }
365} 425}
366 426