From 868e9daea6477412530a8c47b22e254de575a176 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Wed, 15 May 2024 11:39:00 +0300 Subject: [PATCH] fixed socket read bug --- client.c | 23 +++++++++++++++-------- cmdlnopts.c | 2 ++ cmdlnopts.h | 1 + main.c | 3 +++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/client.c b/client.c index 266d97e..b488dfd 100644 --- a/client.c +++ b/client.c @@ -55,18 +55,24 @@ static size_t imbufsz = 0; // image buffer for allocated `ima` static uint8_t *imbuf = NULL; #endif +// read message from queue or file descriptor static char *readmsg(int fd){ static cc_strbuff *buf = NULL; - if(!buf) buf = cc_strbufnew(BUFSIZ, 255); + if(!buf) buf = cc_strbufnew(BUFSIZ, 256); + int test(){ + size_t got = cc_getline(buf); + if(got > 255){ + DBG("Client fd=%d gave buffer overflow", fd); + LOGMSG("SERVER client fd=%d buffer overflow", fd); + }else if(got){ + return TRUE; + } + return FALSE; + } + if(test()) return buf->string; if(1 == cc_canberead(fd)){ if(cc_read2buf(fd, buf)){ - size_t got = cc_getline(buf); - if(got > 255){ - DBG("Client fd=%d gave buffer overflow", fd); - LOGMSG("SERVER client fd=%d buffer overflow", fd); - }else if(got){ - return buf->string; - } + if(test()) return buf->string; }else ERRX("Server disconnected"); } return NULL; @@ -120,6 +126,7 @@ DBG("1 msg-> %s, ans -> %s", msg, ans); DBG("BREAK"); break; } + } return ((ans) ? TRUE : FALSE); } diff --git a/cmdlnopts.c b/cmdlnopts.c index d0608c5..d274137 100644 --- a/cmdlnopts.c +++ b/cmdlnopts.c @@ -31,6 +31,7 @@ static glob_pars G = { .setwheel = -1, .fanspeed = -1, .shmkey = 7777777, + .anstmout = -1, .infty = -1 }; @@ -109,6 +110,7 @@ myoption cmdlnopts[] = { {"client", NO_ARGS, &G.client,1, arg_none, NULL, N_("run as client")}, {"viewer", NO_ARGS, &G.viewer,1, arg_none, NULL, N_("passive viewer (only get last images)")}, {"restart", NO_ARGS, &G.restart,1, arg_none, NULL, N_("restart image server")}, + {"timeout", NEED_ARG, NULL, '0', arg_double, APTR(&G.anstmout), N_("network answer timeout (default: 0.1s)")}, {"shmkey", NEED_ARG, NULL, 'k', arg_int, APTR(&G.shmkey), N_("shared memory (with image data) key (default: 7777777)")}, {"forceimsock",NO_ARGS, &G.forceimsock,1, arg_none, NULL, N_("force using image through socket transition even if can use SHM")}, diff --git a/cmdlnopts.h b/cmdlnopts.h index 66207fc..6ad6fbc 100644 --- a/cmdlnopts.h +++ b/cmdlnopts.h @@ -75,6 +75,7 @@ typedef struct{ int infty; // run (==1) or stop (==0) infinity loop float gain; // gain level (only for CMOS) float brightness; // brightness (only for CMOS) + double anstmout; // answer timeout by socket double exptime; // time of exposition in seconds double temperature; // temperature of CCD double gotopos; // move stepper motor of focuser to absolute position diff --git a/main.c b/main.c index f0943ed..57dbdb3 100644 --- a/main.c +++ b/main.c @@ -91,6 +91,9 @@ int main(int argc, char **argv){ struct stat filestat; if(0 == stat(GP->outfile, &filestat)) ERRX("File %s exists!", GP->outfile); } + if(GP->anstmout > 0.){ + if(!cc_setAnsTmout(GP->anstmout)) ERRX("Can't set answer timeout to %g", GP->anstmout); + } if(GP->port){ if(GP->path){ WARNX("Options `port` and `path` can't be used together! Point `port` for TCP socket or `path` for UNIX.");