# HG changeset patch # User Daniele Nicolodi # Date 1434460163 -7200 # Node ID a03df7dc98f8c64c3c6b8686df70a3d1ecb1c73f # Parent 3622e24a443f5dd4248af953753751bc21496e3d Add xsocket wrappers diff -r 3622e24a443f -r a03df7dc98f8 xsocket.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xsocket.c Tue Jun 16 15:09:23 2015 +0200 @@ -0,0 +1,157 @@ +#ifdef _CVI_ +#include +#include +#define strdup(s) StrDup(s) +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include "xsocket.h" + + +struct xsocket * xsocket(const char *hostname, const int port) +{ + struct xsocket *s = malloc(sizeof(struct xsocket)); + if (! s) + return NULL; + + s->hostname = strdup(hostname); + s->port = port; + s->fd = -1; + + return s; +} + + +int xconnect(struct xsocket *s) +{ + int r, sock; + struct sockaddr_in addr; + struct hostent* host; + +#ifdef _CVI_ + WSADATA wsdata; + WSAStartup(MAKEWORD(2,2), &wsdata); +#endif + + if (s->fd >= 0) + return 0; + + host = gethostbyname(s->hostname); + if (host == NULL) + return -errno; + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) + return -errno; + + addr.sin_family = host->h_addrtype; + memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); + addr.sin_port = htons(s->port); + + r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); + if (r < 0) + return -errno; + + s->fd = sock; + return 0; +} + + +int xsend(struct xsocket *s, const char *data, size_t len) +{ + int r, retry = 2; + + while (--retry) { + + r = xconnect(s); + if (r < 0) + return r; + + r = send(s->fd, data, len, 0); + if (r < 0) { + close(s->fd); + s->fd = -1; + continue; + } + + break; + } + + return (r < 0) ? -errno : 0; +} + + +int xrecv(struct xsocket *s, char *buffer, size_t len) +{ + return recv(s->fd, buffer, len, 0); +} + + +int msend(struct xsocket *s, const char *data, size_t len) +{ + char *buffer = malloc(len + 2); + memcpy(buffer, data, len); + + buffer[len++] = '\r'; + buffer[len++] = '\n'; + + return xsend(s, buffer, len); +} + + +int mrecv(struct xsocket *s, char *buffer, size_t len) +{ + int n; + + n = xrecv(s, buffer, len); + if (n < 0) + return n; + + if ((buffer[--n] != '\n') || (buffer[--n] != '\r')) + return -EINVAL; + + buffer[n] = '\0'; + + return n; +} + + +int command(struct xsocket *s, 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); + + buffer[n++] = '\r'; + buffer[n++] = '\n'; + + r = xsend(s, buffer, n); + if (r < 0) + return r; + + r = mrecv(s, buffer, sizeof(buffer)); + if (r < 0) + return r; + + if (strcmp(buffer, "OK") != 0) + return -EINVAL; + + return 0; +} diff -r 3622e24a443f -r a03df7dc98f8 xsocket.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xsocket.h Tue Jun 16 15:09:23 2015 +0200 @@ -0,0 +1,26 @@ +#ifndef __XSOCKET_H__ +#define __XSOCKET_H__ + +#ifdef _CVI_ +#include +#else +#include +#endif + +struct xsocket { + char *hostname; + int port; + int fd; +}; + +struct xsocket * xsocket(const char *hostname, const int port); +int xconnect(struct xsocket *s); +int xsend(struct xsocket *s, const char *data, size_t len); +int xrecv(struct xsocket *s, char *buffer, size_t len); + +int msend(struct xsocket *s, const char *data, size_t len); +int mrecv(struct xsocket *s, char *buffer, size_t len); + +int command(struct xsocket *s, char *frmt, ...); + +#endif