# HG changeset patch # User Daniele Nicolodi # Date 1434467955 -7200 # Node ID 8d9a4c5eb7a48086196f98fae5fd71d1107c970c # Parent ebbe0f198322b8f3d89313188a4bc975d89577f2 Fixes to make last changes working in CVI diff -r ebbe0f198322 -r 8d9a4c5eb7a4 ad9956.c --- a/ad9956.c Tue Jun 16 17:16:11 2015 +0200 +++ b/ad9956.c Tue Jun 16 17:19:15 2015 +0200 @@ -21,11 +21,6 @@ char buffer[256]; uint64 value; -#ifdef _CVI_ - WSADATA wsdata; - WSAStartup(MAKEWORD(2,2), &wsdata); -#endif - d->sock = xsocket(hostname, 1234); d->profile = 0; d->clock = clock; diff -r ebbe0f198322 -r 8d9a4c5eb7a4 xsocket.c --- 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 +#include +#include #include #define strdup(s) StrDup(s) +#define ERRNO 1 #else #include #include @@ -16,22 +19,11 @@ #include #include #include +#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;