nbeng

a non-blocking client/server engine
git clone git://git.2f30.org/nbeng.git
Log | Files | Refs | README

commit 6c4769c337712a30c1617b006d89d91ee5122135
parent 82e1b2743bbd196111d0a5c93741f0feed2e02e0
Author: dsp <dsp@2f30.org>
Date:   Thu Jun 14 14:47:09 +0300

styled according to style(9) police (aka lostd@)

Diffstat:
nbeng.c | 48+++++++++++++++++++++++-------------------------
1 file changed, 23 insertions(+), 25 deletions(-)
diff --git a/nbeng.c b/nbeng.c @@ -13,7 +13,7 @@ #include <err.h> /*SSL flag*/ -int fssl=0; +int fssl = 0; /*connection context */ typedef struct concontxt_t { @@ -34,15 +34,14 @@ usage(const char *s) /*connect to in port port and store the fds in the context*/ static void -connectit(concontxt *c,char *to,char *port){ +connectit(concontxt *c, char *to, char *port){ struct addrinfo cli_hints, *cli_servinfo, *p0; - int rv,cli_sockfd; - if( (c==NULL) || (to == NULL) || (port == NULL)) - errx(1,"conectit was passed a null arg"); + int rv, cli_sockfd; + if( (c == NULL) || (to == NULL) || (port == NULL)) + errx(1, "conectit was passed a null arg"); memset(&cli_hints, 0, sizeof(cli_hints)); cli_hints.ai_family = AF_INET; cli_hints.ai_socktype = SOCK_DGRAM; - rv = getaddrinfo(to, port, &cli_hints, &cli_servinfo); if (rv) errx(1, "getaddrinfo: %s", gai_strerror(rv)); @@ -57,23 +56,23 @@ connectit(concontxt *c,char *to,char *port){ if (!p0) errx(1, "failed to bind socket"); /*all was ok , so we register the socket to the context*/ - c->clifd=cli_sockfd; - c->clinfo=cli_servinfo; + c->clifd = cli_sockfd; + c->clinfo = cli_servinfo; } int main(int argc,char *argv[]) { /*our input is the stdin for now, we record lines and send them*/ - int recfd=STDIN_FILENO; + int recfd = STDIN_FILENO; char *prog = *argv; int c; fd_set rsocks; - int highsock,readsocks; + int highsock, readsocks; struct timeval timeout; - char *inbuf=NULL,*linbuf=NULL; + char *inbuf=NULL, *linbuf=NULL; ssize_t inbufln; - concontxt *con = (concontxt*) malloc(sizeof(concontxt)); + concontxt *con = (concontxt *) malloc(sizeof(concontxt)); while ((c = getopt(argc, argv, "hs")) != -1) { switch (c) { case 'h': @@ -81,7 +80,7 @@ main(int argc,char *argv[]) goto freex; break; case 's': - fssl=1; + fssl = 1; break; case '?': default: @@ -96,29 +95,28 @@ main(int argc,char *argv[]) goto freex; } /*connect to the host*/ - connectit(con,argv[0],argv[1]); + connectit(con, argv[0], argv[1]); /*XXX: this should change when we add the srv fd*/ highsock = recfd; /*these are the sockets for reading (stdin + server fd)*/ + FD_ZERO(&rsocks); + FD_SET(recfd, &rsocks); while (1) { FD_ZERO(&rsocks); - FD_SET(recfd,&rsocks); + FD_SET(recfd, &rsocks); timeout.tv_sec = 1; timeout.tv_usec = 0; - readsocks = select(highsock+1, &rsocks, (fd_set *) 0, - (fd_set *) 0, &timeout); - if (readsocks < 0) { - warnx("select error"); + readsocks = select(highsock + 1, &rsocks, (fd_set *) 0, (fd_set *) 0, &timeout); + if(readsocks < 0) goto freex; - } if (readsocks == 0) { /*show that we are alive*/ printf("."); fflush(stdout); } else { - if (FD_ISSET(recfd,&rsocks)){ + if (FD_ISSET(recfd, &rsocks)){ /*deal with input data*/ - inbuf = fgetln(stdin,&inbufln); + inbuf = fgetln(stdin, &inbufln); if (inbuf[inbufln - 1] == '\n') inbuf[inbufln - 1] = '\0'; else { @@ -131,10 +129,10 @@ main(int argc,char *argv[]) linbuf[inbufln] = '\0'; inbuf = linbuf; } - if ((strncmp(inbuf,"Q",2)==0)) + if ((strncmp(inbuf, "Q", 2) == 0)) goto freex; - printf("got msg: %s , sending it \n",inbuf); - sendto(con->clifd,inbuf,inbufln,0,con->clinfo->ai_addr,con->clinfo->ai_addrlen); + printf("got msg: %s , sending it \n", inbuf); + sendto(con->clifd, inbuf, inbufln, 0, con->clinfo->ai_addr, con->clinfo->ai_addrlen); fflush(stdout); /*free the buf*/ if(linbuf != NULL)