view KKFX80E.c @ 258:5296f3bcd160

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.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 14:31:35 +0200
parents 3427013e4f70
children
line wrap: on
line source

#include <ctype.h>
#include <windows.h>
#include <utility.h>
#include <ansi_c.h>


int (_stdcall *FX_OpenPort)(char **Port) = NULL;
void (_stdcall *FX_ClosePort)(void) = NULL;
int (_stdcall *FX_SendCommand)(char **Command) = NULL;
int (_stdcall *FX_GetReport)(char **Data) = NULL;
void (_stdcall *FX_Debug)(BOOL DbgFlag) = NULL;
// int (_stdcall *GetFXM)(char **Data) = NULL;
// void (_stdcall *Log)(int DbgNum) = NULL;
// int (_stdcall *ReadMonitor)(char **Data) = NULL;
// BOOL (_stdcall *EnumeratePorts)(char **PortName, char *PortNamePrefix) = NULL;

static HMODULE module;
static char *errormsg;


char * FX_Error(void)
{
	return errormsg;
}


int FX_Init(void)
{
	FARPROC funcp = NULL;
	errormsg = "";
	
	module = LoadLibrary("KK_FX80E.dll");
	if (! module)
		return 0;
	
	funcp = GetProcAddress(module, "FX_OpenPort");
  	if (! funcp)
		goto error;
	FX_OpenPort = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_ClosePort");
  	if (! funcp)
		goto error;
	FX_ClosePort = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_SendCommand");
  	if (! funcp)
		goto error;
	FX_SendCommand = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_GetReport");
  	if (! funcp)
		goto error;
	FX_GetReport = (void *)funcp;
	
	funcp = GetProcAddress(module, "FX_Debug");
  	if (! funcp)
		goto error;
	FX_Debug = (void *)funcp;
	
	return 1;
	
error:
	FreeLibrary(module);
	return 0;
}


void FX_Free(void)
{
	FreeLibrary(module);	
}


int FX_Open(char *port)
{
	int rv = FX_OpenPort(&port);
	if (! rv)
		errormsg = port;
	return rv;
}


void FX_Close(void)
{
	FX_ClosePort();
}


int FX_Send(char cmd)
{
	char *p = &cmd;
	int rv = FX_SendCommand(&p);
	if (! rv)
		errormsg = p;
	return rv;
}


int FX_Recv(char **ret, int timeout)
{
	int rv;
	char *data;
	double mark = Timer();
	do {
		data = ".";
		rv = FX_GetReport(&data);
		if ((Timer() - mark) > timeout) {
			rv = 0;
			data = "Function FX_Recv: Timeout";
		}
		Delay(0.01);
	} while ((! data) && (rv == 1));
	
	if (! rv) {
		errormsg = data;
		data = NULL;
	}
	
	if (ret)
		*ret = data;
	
	return rv;
}