view dds.c @ 241:65ab87e5a8d6

Add ZMQ library
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 12 Feb 2015 22:43:46 +0100
parents fcc988c6f841
children
line wrap: on
line source

#ifdef _CVI_
#include <winsock2.h>
#include <ansi_c.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#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;
}