]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Improved soc_received procedure
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sun, 30 Mar 2025 20:04:06 +0000 (16:04 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sun, 30 Mar 2025 20:04:06 +0000 (16:04 -0400)
lib/devsoc.c
lib/subrou.c

index 058152366b2fb37b9352384580d402c6265c9ef5..caa23ffb8039a900b6feb911c0f5054258328992 100644 (file)
@@ -1043,69 +1043,88 @@ PUBLIC int soc_receive(SOCPTR *socptr)
 
 int got;
 SOCTYP *soc;
+int limit;
+char *buffer;
+int phase;
+_Bool proceed;
 
 got=0;
 soc=(SOCTYP *)socptr;
-if (soc!=(SOCTYP *)0) {
-  int limit;
-  char *buffer;
-
-  buffer=soc->carpile+soc->carin;
-  limit=(soc->maxcarin-soc->carin);
-  (void) memset(buffer,'\000',limit);
-  limit--;
-  switch (soc->modtls) {
-    case true   :
-      got=tls_read(soc->tls,buffer,limit);
+limit=0;
+buffer=(char *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase);
+  switch (phase) {
+    case 0      :       //is socket available
+      if (soc==(SOCTYP *)0) {
+        (void) rou_alert(0,"%s socket pointer is null (Bug?)",OPEP);
+        phase=999;      //No need to go further
+        }
       break;
-    case false  :
-      got=recv(soc->handle,buffer,limit,MSG_DONTWAIT);
-      (void) rou_alert(0,"%s, got=%d char",OPEP,got);
+    case 1      :       //Is the socket conneted
+      if (soc->connected==false) {
+        (void) rou_alert(0,"%s socket Already disconnect",OPEP);
+        phase=999;      //No need to go further
+        }
+      break;
+    case 2      :       //reading data
+      buffer=soc->carpile+soc->carin;
+      limit=(soc->maxcarin-soc->carin);
+      (void) memset(buffer,'\000',limit);
+      limit--;
+      switch (soc->modtls) {
+        case true   :
+          got=tls_read(soc->tls,buffer,limit);
+          break;
+        case false  :
+          got=recv(soc->handle,buffer,limit,MSG_DONTWAIT);
+          break;
+        }
+      (void) rou_alert(0,"%s, JMPDBG got=%d char buffer=<%s>",OPEP,got,buffer);
+      break;
+    case 3      :       //check about recieved data
       switch (got) {
         case -1     :             //do not block
           if (errno==EWOULDBLOCK)
             errno=EAGAIN;
           switch (errno) {
-            case EAGAIN     :    //no char available yet
+            case EAGAIN     :   //no char available yet
               break;
-            case ECONNRESET :    //Connection reset by peer
-              (void) rou_alert(0,"%s JMPDBG connection reset by peer",OPEP);
+            case ECONNRESET :   //Connection reset by peer
+              (void) rou_alert(0,"%s connection reset by peer",OPEP);
               break;
             default         :
-              foreground=false; //JMPDBG?
               (void) rou_alert(0,"%s Unexpected error=%d <%s> (Bug)",
                                   OPEP,errno,strerror(errno));
-              (void) exit(1);   //JMPDBG!
               break;
             }
           break;
-        case 0      :          //Premature EOF?
+        case  0     :           //No char available?
           (void) rou_alert(0,"%s, detected EOF error=<%s:%d>",
                                   OPEP,strerror(errno),errno);
-          if (close(soc->handle)<0) {
+          if (close(soc->handle)<0)
             (void) rou_alert(0,"%s, after close error=<%s:%d>",
                                   OPEP,strerror(errno),errno);
-            }
-          soc->handle=-1;
+          soc->handle=-1;      //Socket is now disconnected
           soc->connected=false;
           break;
         default     :          //we got some char from remote
+          for (int i=0;i<got;i++,buffer++) {
+            if (*buffer=='\000')
+              *buffer='?';
+            }
+          soc->carin+=got;      //managing carpile
+          soc->carpile[soc->carin]='\000';
           break;
         }
       break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
     }
-  if (got>0) {                 //we have recived some character
-    char *ptr;
-    //removing NULL char if received 
-    ptr=buffer;
-    for (int i=0;i<got;i++,ptr++) {
-      if (*ptr=='\000')
-        *ptr='?';
-      }
-    soc->carin+=got;           //managing carpile
-    soc->carpile[soc->carin]='\000';
-    }
+  phase++;
   }
 return got;
 #undef  OPEP
index eb0b687d10f0027cfaa6bad0f87a0406ff80a8ce..86016a0033f5770d66e115146d833975a8360517 100644 (file)
@@ -21,7 +21,7 @@
 
 //version definition 
 #define VERSION "0.6"
-#define RELEASE "29"
+#define RELEASE "30"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level