fixed socket read bug

This commit is contained in:
Edward Emelianov 2024-05-15 11:39:00 +03:00
parent a0bb1890a7
commit 868e9daea6
4 changed files with 21 additions and 8 deletions

View File

@ -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(1 == cc_canberead(fd)){
if(cc_read2buf(fd, buf)){
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 buf->string;
return TRUE;
}
return FALSE;
}
if(test()) return buf->string;
if(1 == cc_canberead(fd)){
if(cc_read2buf(fd, buf)){
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);
}

View File

@ -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")},

View File

@ -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

3
main.c
View File

@ -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.");