From c8183c702e8421961c5ff79fd1edaed6b951fcc2 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Thu, 1 Aug 2024 14:53:18 -0400 Subject: [PATCH] Detecting if remote connection just vanish --- lib/gestcp.c | 31 ++++++++++++++++++++++++------- lib/gestcp.h | 5 +---- lib/modrec.c | 15 ++++++++++++--- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/gestcp.c b/lib/gestcp.c index a4a3db6..d6cd049 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -29,17 +29,34 @@ static _Bool modopen; //boolean module open/close /* unable to send char return -1; */ /* */ /********************************************************/ -PUBLIC int tcp_fprintf(CONTYP *contact,const char *format,...) +PUBLIC int tcp_write(CONTYP *contact,char *buffer,int parnum) { int sent; -va_list args; +int got; +char zone[10]; -sent=0; -va_start(args,format); -sent=vdprintf(contact->channel,format,args); -//(void) fdatasync(contact->channel); -va_end(args); +sent=-1; +(void) strcpy(zone,"??"); +got=recv(contact->channel,zone,0,MSG_PEEK); +switch (got) { + case -1 : + if (errno==EWOULDBLOCK) + errno=EAGAIN; + switch (errno) { + case EAGAIN : + sent=send(contact->channel,buffer,parnum,MSG_DONTWAIT); + break; + default : + (void) fprintf(stderr,"Got '%02d' error=<%s> (errno='%d')\n", + got,strerror(errno),errno); + break; + } + break; + default : + (void) fprintf(stderr,"Got '%02d' zone=<%s>\n",got,zone); + break; + } return sent; } /* diff --git a/lib/gestcp.h b/lib/gestcp.h index 9ce9577..4010265 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -18,10 +18,7 @@ typedef struct { }CONTYP; //Transmit formated data to the contact channel -extern int tcp_fprintf(CONTYP *contact,const char *format,...); - -//Wait for an input coming from the contact channel -extern int *tcp_fscanf(CONTYP *contact,const char *format,...); +extern int tcp_write(CONTYP *contact,char *buffer,int parnum); //wait for an incoming contact extern CONTYP *tcp_getcontact(SOCTYP *binding); diff --git a/lib/modrec.c b/lib/modrec.c index 92168fe..ec667e8 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -32,7 +32,7 @@ void docontact(SOCTYP *binding,int pos) { #define OPEP "modrec.c:contact" -#define TESTL 4 +#define TESTL 30 CONTYP *contact; int phase; @@ -57,8 +57,17 @@ while (proceed==true) { case 3 : //do contact printf("Client channel=%d (pid=%d)\n",contact->channel,getpid()); for (int i=0;ichannel,"Remote pid=%d iter=%d/%d\n", - getpid(),i,TESTL); + int sent; + char buffer[100]; + + printf("send string\n"); + sprintf(buffer,"Remote pid=%d iter=%d/%d\n",getpid(),i,TESTL); + sent=tcp_write(contact,buffer,strlen(buffer)); + printf("string sent '%d'\n",sent); + if (sent<0) { + (void) rou_alert(0,"%s, Unable to send data to remote",OPEP); + break; + } (void) sleep(1); } break; -- 2.47.3