# HG changeset patch # User Daniele Nicolodi # Date 1434457895 -7200 # Node ID 5296f3bcd1609fa83d85e9fff32853379f452353 # Parent 8cbfce046d410625eca147d916fdb44b13d65e36 Implement DDS clients reconnect On network send() failures try to reconnect to the server before returning an error. This allows to restart the network servers controlling the DDSes wiothout having to restart the clients. diff -r 8cbfce046d41 -r 5296f3bcd160 ad9912.c --- a/ad9912.c Fri Jun 05 21:51:25 2015 +0200 +++ b/ad9912.c Tue Jun 16 14:31:35 2015 +0200 @@ -1,67 +1,36 @@ #ifdef _CVI_ -#include #include #include +#define usleep(t) Delay((t) / 1000000.0) #else #include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include #endif #include "dds.h" +#include "xsocket.h" #include "ad9912.h" int ad9912_init(struct ad9912 *d, const char *hostname, double clock) { - int n, r; + int r; char buffer[256]; - int sock; - struct sockaddr_in addr; - struct hostent* host; -#ifdef _CVI_ - WSADATA wsdata; - WSAStartup(MAKEWORD(2,2), &wsdata); -#endif - - d->hostname = strdup(hostname); - d->port = 1234; d->clock = clock; + d->sock = xsocket(hostname, 1234); - host = gethostbyname(hostname); - if (host == NULL) - return -1; - - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) - return -2; - - addr.sin_family = host->h_addrtype; - memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); - addr.sin_port = htons(d->port); - - r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); - if (r < 0) - return -3; - - d->fd = sock; - - /* check compatibility */ - n = snprintf(buffer, sizeof(buffer), "REVISION"); - r = msend(d->fd, buffer, n); + r = xconnect(d->sock); if (r < 0) return r; - r = mrecv(d->fd, buffer, sizeof(buffer)); + + /* check compatibility */ + r = msend(d->sock, "REVISION", strlen("REVISION")); + if (r < 0) + return r; + r = mrecv(d->sock, buffer, sizeof(buffer)); if (r < 0) return r; if (atoi(buffer) != REVISION) @@ -79,7 +48,7 @@ /* ad9912 ignores ftw last bit */ value = value & ~1ULL; - r = command(d->fd, "SET CH%d:FTW0 0x%llX", channel, value); + r = command(d->sock, "SET CH%d:FTW0 0x%llX", channel, value); if (r < 0) return r; @@ -97,11 +66,11 @@ n = snprintf(buffer, sizeof(buffer), "GET CH%d:FTW0", channel); - r = msend(d->fd, buffer, n); + r = msend(d->sock, buffer, n); if (r < 0) return r; - r = mrecv(d->fd, buffer, sizeof(buffer)); + r = mrecv(d->sock, buffer, sizeof(buffer)); if (r < 0) return r; @@ -141,6 +110,7 @@ /* final adjustment */ ad9912_set_frequency(d, channel, f2); + return 0; } diff -r 8cbfce046d41 -r 5296f3bcd160 ad9912.h --- a/ad9912.h Fri Jun 05 21:51:25 2015 +0200 +++ b/ad9912.h Tue Jun 16 14:31:35 2015 +0200 @@ -1,12 +1,12 @@ #ifndef __AD9912_H__ #define __AD9912_H__ +#include "xsocket.h" + #define REVISION 1 struct ad9912 { - char *hostname; - int port; - int fd; + struct xsocket *sock; double clock; double frequency[4]; }; diff -r 8cbfce046d41 -r 5296f3bcd160 ad9956.c --- a/ad9956.c Fri Jun 05 21:51:25 2015 +0200 +++ b/ad9956.c Tue Jun 16 14:31:35 2015 +0200 @@ -1,86 +1,61 @@ #ifdef _CVI_ -#include #include #include #else #include #include #include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include +#include #endif #include "dds.h" +#include "xsocket.h" #include "ad9956.h" int ad9956_init(struct ad9956 *d, const char *hostname, double clock) { - int sock, n, r; + int r; char buffer[256]; - struct sockaddr_in addr; - struct hostent* host; uint64 value; #ifdef _CVI_ WSADATA wsdata; WSAStartup(MAKEWORD(2,2), &wsdata); #endif - - d->hostname = strdup(hostname); - d->port = 1234; + + d->sock = xsocket(hostname, 1234); d->profile = 0; d->clock = clock; - host = gethostbyname(hostname); - if (host == NULL) - return -1; - - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) - return -2; - - addr.sin_family = host->h_addrtype; - memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); - addr.sin_port = htons(d->port); - - r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); + r = xconnect(d->sock); if (r < 0) - return -3; - - d->fd = sock; + return r; /* check compatibility */ - n = snprintf(buffer, sizeof(buffer), "REVISION"); - r = msend(d->fd, buffer, n); + r = msend(d->sock, "REVISION", strlen("REVISION")); if (r < 0) return r; - r = mrecv(d->fd, buffer, sizeof(buffer)); + r = mrecv(d->sock, buffer, sizeof(buffer)); if (r < 0) return r; if (atoi(buffer) != REVISION) return -EINVAL; /* disable clock divider */ - n = snprintf(buffer, sizeof(buffer), "GET CFR2"); - r = msend(d->fd, buffer, n); + r = msend(d->sock, "GET CFR2", strlen("GET CFR2")); if (r < 0) return r; - r = mrecv(d->fd, buffer, sizeof(buffer)); + r = mrecv(d->sock, buffer, sizeof(buffer)); if (r < 0) return r; r = strtouint64(buffer, &value); if (r < 0) return r; value = value | (1ULL << 16); - r = command(d->fd, "SET CFR2 0x%X", value); + r = command(d->sock, "SET CFR2 0x%X", value); if (r < 0) return r; @@ -93,7 +68,7 @@ int r; uint64 value = ftw(d->clock, f); - r = command(d->fd, "SET PCR%d 0x%llX", d->profile, value); + r = command(d->sock, "SET PCR%d 0x%llX", d->profile, value); if (r < 0) return r; @@ -110,12 +85,11 @@ char buffer[256]; n = snprintf(buffer, sizeof(buffer), "GET PCR%d", d->profile); - - r = msend(d->fd, buffer, n); + r = msend(d->sock, buffer, n); if (r < 0) return r; - r = mrecv(d->fd, buffer, sizeof(buffer)); + r = mrecv(d->sock, buffer, sizeof(buffer)); if (r < 0) return r; @@ -136,17 +110,18 @@ int64 value = (int64)ftw(d->clock, fabs(rate * 0.01)); if (rate < 0.0) value = -value; - return command(d->fd, "SWEEP SET DFTW %lld", value); + + return command(d->sock, "SWEEP SET DFTW %lld", value); } int ad9956_sweep_start(struct ad9956 *d) { - return command(d->fd, "SWEEP START"); + return command(d->sock, "SWEEP START"); } int ad9956_sweep_stop(struct ad9956 *d) { - return command(d->fd, "SWEEP STOP"); + return command(d->sock, "SWEEP STOP"); } diff -r 8cbfce046d41 -r 5296f3bcd160 ad9956.h --- a/ad9956.h Fri Jun 05 21:51:25 2015 +0200 +++ b/ad9956.h Tue Jun 16 14:31:35 2015 +0200 @@ -1,11 +1,12 @@ #ifndef __AD9956_H__ #define __AD9956_H__ +#include "xsocket.h" + #define REVISION 1 struct ad9956 { - char *hostname; - int port; + struct xsocket *sock; int profile; int fd; double clock; diff -r 8cbfce046d41 -r 5296f3bcd160 dds.c --- a/dds.c Fri Jun 05 21:51:25 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -#ifdef _CVI_ -#include -#include -#else -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif - - -int msend(int fd, char *buffer, int n) -{ - buffer[n++] = '\r'; - buffer[n++] = '\n'; - - return send(fd, buffer, n, 0); -} - - -int mrecv(int fd, char *buffer, int len) -{ - int n; - - n = recv(fd, buffer, len, 0); - if (n < 0) - return n; - - if ((buffer[--n] != '\n') || (buffer[--n] != '\r')) - return -1; - - buffer[n] = '\0'; - - return n; -} - - -int command(int fd, char *frmt, ...) -{ - int r, n; - char buffer[1024]; - va_list v; - - va_start(v, frmt); - n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); - va_end(v); - - r = msend(fd, buffer, n); - if (r < 0) - return r; - - r = mrecv(fd, buffer, sizeof(buffer)); - if (r < 0) - return r; - - if (strcmp(buffer, "OK") != 0) - return -1; - - return 0; -} diff -r 8cbfce046d41 -r 5296f3bcd160 dds.h --- a/dds.h Fri Jun 05 21:51:25 2015 +0200 +++ b/dds.h Tue Jun 16 14:31:35 2015 +0200 @@ -1,10 +1,6 @@ #ifndef __FXANALYSE_DDS_H__ #define __FXANALYSE_DDS_H__ -#define usleep(t) Delay((t) / 1000000.0) -#define strdup(s) StrDup(s) - - typedef long long int64; typedef unsigned long long uint64; @@ -29,9 +25,5 @@ return 0; } -int msend(int fd, char *buffer, int n); -int mrecv(int fd, char *buffer, int len); -int command(int fd, char *frmt, ...); - #endif