]> SAFE projects GIT repository - jmp/mailleur/commitdiff
readline is working using a timeout
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 3 Aug 2024 13:15:09 +0000 (09:15 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 3 Aug 2024 13:15:09 +0000 (09:15 -0400)
lib/Makefile
lib/gestcp.c
lib/gestcp.h
lib/modrec.c
lib/subrou.c
lib/subrou.h

index eec4efbaaba14e585a59c59c0565b6b14e65f8ec..34f5988d981346875d85e4337b489f407aaca0c1 100644 (file)
@@ -37,7 +37,6 @@ modrec.o:                                     \
           modrec.h modrec.c
 
 gestcp.o:                                      \
-          subrou.h                             \
           unieml.h                             \
           uniprc.h                             \
           unisig.h                             \
@@ -67,6 +66,7 @@ subrou.o:                                     \
           subrou.h subrou.c
 
 gestcp.h:                                      \
+          subrou.h                             \
           devsoc.h                             \
 
 uniprc.h:                                      \
index 20bb4ee34f9470b2dfc1282ffe5ecd070c43547a..59e62035fc93b1222027adcb370f17a19d8a72b8 100644 (file)
@@ -9,12 +9,12 @@
 #include        <errno.h>
 #include        <fcntl.h>
 #include        <netdb.h>
+#include        <poll.h>
 #include        <stdlib.h>
 #include        <stdio.h>
 #include        <string.h>
 #include        <unistd.h>
 
-#include        "subrou.h"
 #include        "unieml.h"
 #include        "unisig.h"
 #include        "uniprc.h"
@@ -111,11 +111,72 @@ return contact;
 */
 /********************************************************/
 /*                                                      */
+/*     Procedure to check if char are available on     */
+/*      soc interface.                                  */
+/*      comming from the remote end.                    */
+/*                                                      */
+/********************************************************/
+static int waitforchar(CONTYP *tact,TIMESPEC *attend)
+
+{
+struct pollfd polling[1];
+
+polling[0].events=POLLIN|POLLPRI;
+polling[0].revents=(short)0;
+polling[0].fd=tact->channel;
+return ppoll(polling,1,attend,(sigset_t *)0);
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
+/*     Procedure to read the maximun of characters     */
+/*      comming from the remote end.                    */
+/*                                                      */
+/********************************************************/
+static void readmaxchar(CONTYP *tact)
+
+{
+int limit;
+
+limit=(tact->maxcarin-tact->carin)-1;
+if (limit>0) {
+  int got;
+
+  got=recv(tact->channel,tact->carpile+tact->carin,limit,MSG_DONTWAIT);
+  (void) printf("readmax got '%d' char\n",got);
+  switch (got) {
+    case -1     :               //do not block
+      if (errno==EWOULDBLOCK)
+        errno=EAGAIN;
+      switch (errno) {
+        case EAGAIN     :       //no char available
+        break;
+      default           :
+        (void) fprintf(stderr,"JMPDBG readmax error=<%s>\n",strerror(errno));
+        break;
+        }
+      break;
+    case 0      :               //EOL?
+      break;
+    default     :               //we got som char from remote
+      tact->carin+=got;         //managing carpile
+      tact->carpile[tact->carin]='\000';
+      break;
+    }
+  }
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
 /*     Procedure to locate CRLF within the current line*/
 /*      assigne memory and store carpile contents       */
 /*                                                      */
 /********************************************************/
-PUBLIC int getnextline(CONTYP *tact,char **lineptr)
+static int getnextline(CONTYP *tact,char **lineptr)
 
 {
 int got;
@@ -144,11 +205,12 @@ while (proceed==true) {
       *eol='\000';
       (void) strcpy(*lineptr,tact->carpile);
       (void) strcat(*lineptr,tact->EOL);
+      got=strlen(*lineptr);
       break;
     case 3      :       //managing carpile
       tact->carin-=strlen(*lineptr);
       if (tact->carin>0) 
-        (void) memove(tact->carpile,tact->carpile+strlen(*lineptr),tact->carin);
+        (void) memmove(tact->carpile,tact->carpile+strlen(*lineptr),tact->carin);
       tact->carpile[tact->carin]='\000';
       break;
     default     :       //SAFE guard
@@ -168,7 +230,7 @@ return got;
 /*      for a CRLF AS EOL.                              */
 /*                                                      */
 /********************************************************/
-PUBLIC int tcp_getline(CONTYP *contact,u_long usec,char **lineptr)
+PUBLIC int tcp_getline(CONTYP *contact,TIMESPEC *attend,char **lineptr)
 
 {
 #define OPEP    "gestcp.c:tcp_getline"
@@ -193,7 +255,7 @@ while (proceed==true) {
         phase=999;      //we got a line.
       break;
     case 2      :       //waiting for new character presence
-      switch (waitforchar(contact,&usec)) {
+      switch (waitforchar(contact,attend)) {
         case -1         :       //trouble? signal?
         case  0         :       //normal time out
           break;                //no need to read line
index c06b30023cee76618817231f7a94c4db6e6656e8..219106239b2e2c0face52d683f4a3e88b0c5121b 100644 (file)
@@ -9,6 +9,7 @@
 
 #include        <stdbool.h>
 
+#include        "subrou.h"
 #include        "devsoc.h"
 
 typedef struct  {
@@ -23,7 +24,7 @@ typedef struct  {
         }CONTYP;
 
 //read a line from contact up to CRLF
-extern int tcp_getline(CONTYP *contact,u_long usec,char **lineptr);
+extern int tcp_getline(CONTYP *contact,TIMESPEC *attend,char **lineptr);
 
 //Transmit formated data to the contact channel
 extern int tcp_write(CONTYP *contact,char *buffer,int parnum);
index 7f672205a92ea1b540173cc6f6af968da3d861b7..162c0888a445d9c322a5f992b43b71adbf8581d8 100644 (file)
@@ -13,6 +13,7 @@
 
 #include        "subrou.h"
 #include        "uniprc.h"
+#include        "unieml.h"
 #include        "unisig.h"
 #include        "devsoc.h"
 #include        "gestcp.h"
@@ -35,6 +36,7 @@ void docontact(SOCPTR *socptr,int pos)
 #define TESTL   8
 
 CONTYP *contact;
+
 int phase;
 _Bool proceed;
 
@@ -53,18 +55,25 @@ while (proceed==true) {
     case 2      :       //do contact
       printf("Client channel=%d (pid=%d)\n",contact->channel,getpid());
       for (int i=0;i<TESTL;i++) {
-        int sent;
-        char buffer[100];
-        
-        if ((hangup==true)||(reload==true))
-          break;
-        sprintf(buffer,"Remote pid=%d iter=%d/%d\n",getpid(),i,TESTL);
-        sent=tcp_write(contact,buffer,strlen(buffer));
-        if (sent<0) {
-          (void) rou_alert(0,"%s, Unable to send data to remote",OPEP);
-          break;
+        TIMESPEC attend;
+        char *line;
+        int got;
+
+        attend.tv_sec=20;
+        attend.tv_nsec=0;
+        (void) printf("attend %d seconds\n",(int)attend.tv_sec);
+        got=tcp_getline(contact,&attend,&line);
+        if (got==0) {
+          (void) printf("remote timeout\n");
+          break;        //remote disconnect
+          }
+        (void) printf("Read '%d' char =<%s>\n",got,line);
+        if (strcasecmp(line,"QUIT"CRLF)==0) {
+          (void) printf("remote quit\n");
+          break;        //remote disconnect
           }
-        (void) sleep(1);
+        (void) tcp_write(contact,line,strlen(line));
+        (void) free(line);
         }
       break;
     case 3      :       //connection terminated
index 290f7e44570a8fdd51de4aff3aa28787eec65da3..ceb27515aae002d5c7690de71d530797512ccd58 100644 (file)
@@ -20,7 +20,7 @@
 
 //version definition 
 #define VERSION "0.3"
-#define RELEASE "17"
+#define RELEASE "18"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level
index 96d425a4d471a6daa52ef0c864048f3f1e69bb2b..7efcd65e7c8c974f35c35f70e895f8023d6d23a1 100644 (file)
@@ -16,6 +16,8 @@
 
 typedef void (*freehandler_t)(void *);
 
+typedef struct timespec TIMESPEC;
+
 //---   Variables defined within subrou.c       ---------
 extern int             debug;  //application debug mode
 extern _Bool       foreground;  //process is in foreground mode