#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"
*/
/********************************************************/
/* */
+/* 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;
*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
/* 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"
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
#include "subrou.h"
#include "uniprc.h"
+#include "unieml.h"
#include "unisig.h"
#include "devsoc.h"
#include "gestcp.h"
#define TESTL 8
CONTYP *contact;
+
int phase;
_Bool proceed;
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