Mercurial > hg > fxanalyse
diff xsocket.c @ 263:8d9a4c5eb7a4
Fixes to make last changes working in CVI
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Tue, 16 Jun 2015 17:19:15 +0200 |
parents | ebbe0f198322 |
children |
line wrap: on
line diff
--- a/xsocket.c Tue Jun 16 17:16:11 2015 +0200 +++ b/xsocket.c Tue Jun 16 17:19:15 2015 +0200 @@ -1,7 +1,10 @@ #ifdef _CVI_ #include <winsock2.h> +#include <toolbox.h> +#include <lowlvlio.h> #include <ansi_c.h> #define strdup(s) StrDup(s) +#define ERRNO 1 #else #include <stdio.h> #include <stdlib.h> @@ -16,22 +19,11 @@ #include <ctype.h> #include <math.h> #include <stdarg.h> +#define ERRNO errno #endif #include "xsocket.h" -#define XRETRY(code) \ - ({ \ - int __r, __n = 0; \ - while (__n++ < 2) { \ - __r = (code); \ - if (__r != -EAGAIN) \ - break; \ - } \ - __r; \ - }) - - struct xsocket * xsocket(const char *hostname, const int port) { struct xsocket *s = malloc(sizeof(struct xsocket)); @@ -62,11 +54,11 @@ host = gethostbyname(s->hostname); if (host == NULL) - return -errno; + return -ERRNO; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) - return -errno; + return -ERRNO; addr.sin_family = host->h_addrtype; memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); @@ -74,7 +66,7 @@ r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); if (r < 0) - return -errno; + return -ERRNO; s->fd = sock; return 0; @@ -91,7 +83,7 @@ r = send(s->fd, data, len, 0); if (r < 0) - return -errno; + return -ERRNO; return r; } @@ -103,7 +95,7 @@ r = recv(s->fd, buffer, len, 0); if (r < 0) - return -errno; + return -ERRNO; if (r == 0) { close(s->fd); s->fd = -1; @@ -114,18 +106,20 @@ } -static inline int __exchange(struct xsocket *s, const char *out, size_t outlen, char *in, size_t inlen) +static inline int __talk(struct xsocket *s, const char *out, size_t outlen, char *in, size_t inlen) { - int r; - - r = xsend(s, out, outlen); - if (r < 0) - return r; + int r = 0, n = 0; + + while (n++ < 2) { + r = xsend(s, out, outlen); + if (r < 0) + return r; - r = xrecv(s, in, inlen); - if (r < 0) - return r; - + r = xrecv(s, in, inlen); + if ((r < 0) && (r != -EAGAIN)) + return r; + } + return r; } @@ -140,7 +134,7 @@ data[len++] = '\r'; data[len++] = '\n'; - r = XRETRY(__exchange(s, data, len, buffer, bufferlen)); + r = __talk(s, data, len, buffer, bufferlen); if (r < 0) return r; @@ -164,7 +158,7 @@ buffer[len++] = '\r'; buffer[len++] = '\n'; - r = XRETRY(__exchange(s, buffer, len, buffer, sizeof(buffer))); + r = __talk(s, buffer, len, buffer, sizeof(buffer)); if (r < 0) return r;