From f07267f7a8a4734ca18567e88572b717d662b0dd Mon Sep 17 00:00:00 2001 From: "N.N." Date: Sat, 16 May 2009 23:44:56 +0000 Subject: reformat svn path=/trunk/; revision=11402 --- desiredata/src/u_pdreceive.c | 260 +++++++++++++++---------------------------- 1 file changed, 89 insertions(+), 171 deletions(-) (limited to 'desiredata') diff --git a/desiredata/src/u_pdreceive.c b/desiredata/src/u_pdreceive.c index 8898e3a3..619d2f93 100644 --- a/desiredata/src/u_pdreceive.c +++ b/desiredata/src/u_pdreceive.c @@ -23,13 +23,12 @@ standard output. */ #define SOCKET_ERROR -1 #endif -typedef struct _fdpoll -{ - int fdp_fd; - char *fdp_inbuf; - int fdp_inhead; - int fdp_intail; - int fdp_udp; +typedef struct _fdpoll { + int fd; + char *inbuf; + int inhead; + int intail; + int udp; } t_fdpoll; static int nfdpoll; @@ -38,36 +37,29 @@ static int maxfd; static int sockfd; static int protocol; -static void sockerror(char *s); +static void sockerror(const char *s); static void x_closesocket(int fd); -static void dopoll(void); +static void dopoll(); #define BUFSIZE 4096 -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { int portno; struct sockaddr_in server; #ifdef MSW short version = MAKEWORD(2, 0); WSADATA nobby; #endif - if (argc < 2 || sscanf(argv[1], "%d", &portno) < 1 || portno <= 0) - goto usage; - if (argc >= 3) - { - if (!strcmp(argv[2], "tcp")) - protocol = SOCK_STREAM; - else if (!strcmp(argv[2], "udp")) - protocol = SOCK_DGRAM; + if (argc < 2 || sscanf(argv[1],"%d",&portno)<1 || portno<=0) goto usage; + if (argc >= 3) { + if (!strcmp(argv[2],"tcp")) protocol = SOCK_STREAM; + else if (!strcmp(argv[2],"udp")) protocol = SOCK_DGRAM; else goto usage; - } - else protocol = SOCK_STREAM; + } else protocol = SOCK_STREAM; #ifdef MSW if (WSAStartup(version, &nobby)) sockerror("WSAstartup"); #endif sockfd = socket(AF_INET, protocol, 0); - if (sockfd < 0) - { + if (sockfd < 0) { sockerror("socket()"); exit(1); } @@ -76,76 +68,56 @@ int main(int argc, char **argv) server.sin_addr.s_addr = INADDR_ANY; #ifdef IRIX - /* this seems to work only in IRIX but is unnecessary in - Linux. Not sure what MSW needs in place of this. */ - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0) - fprintf(stderr, "setsockopt failed\n"); + /* this seems to work only in IRIX but is unnecessary in Linux. Not sure what MSW needs in place of this. */ + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0) fprintf(stderr, "setsockopt failed\n"); #endif - /* assign client port number */ + /* assign client port number */ server.sin_port = htons((unsigned short)portno); - /* name the socket */ - if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) - { + /* name the socket */ + if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) { sockerror("bind"); x_closesocket(sockfd); - return (0); + return 0; } - if (protocol == SOCK_STREAM) - { - if (listen(sockfd, 5) < 0) - { + if (protocol == SOCK_STREAM) { + if (listen(sockfd, 5) < 0) { sockerror("listen"); x_closesocket(sockfd); - exit(1); + return 1; } } - /* now loop forever selecting on sockets */ - while (1) - dopoll(); - + /* now loop forever selecting on sockets */ + while (1) dopoll(); usage: fprintf(stderr, "usage: pdreceive [udp|tcp]\n"); fprintf(stderr, "(default is tcp)\n"); - exit(1); + return 1; } -static void addport(int fd) -{ - t_fdpoll *fp; - fdpoll = (t_fdpoll *)realloc(fdpoll, - (nfdpoll+1) * sizeof(t_fdpoll)); - fp = fdpoll + nfdpoll; - fp->fdp_fd = fd; +static void addport(int fd) { + fdpoll = (t_fdpoll *)realloc(fdpoll, (nfdpoll+1)*sizeof(t_fdpoll)); + t_fdpoll *fp = fdpoll + nfdpoll; + fp->fd = fd; nfdpoll++; if (fd >= maxfd) maxfd = fd + 1; - fp->fdp_inhead = fp->fdp_intail = 0; - if (!(fp->fdp_inbuf = (char *) malloc(BUFSIZE))) - { - fprintf(stderr, "out of memory"); - exit(1); - } + fp->inhead = fp->intail = 0; + if (!(fp->inbuf = (char *) malloc(BUFSIZE))) {fprintf(stderr, "out of memory"); exit(1);} printf("number_connected %d;\n", nfdpoll); } -static void rmport(t_fdpoll *x) -{ - int i; - t_fdpoll *fp; - for (i = nfdpoll, fp = fdpoll; i--; fp++) - { - if (fp == x) - { - x_closesocket(fp->fdp_fd); - free(fp->fdp_inbuf); - while (i--) - { +static void rmport(t_fdpoll *x) { + t_fdpoll *fp = fdpoll; + for (int i=nfdpoll; i--; fp++) { + if (fp == x) { + x_closesocket(fp->fd); + free(fp->inbuf); + while (i--) { fp[0] = fp[1]; fp++; } - fdpoll = (t_fdpoll *)realloc(fdpoll, - (nfdpoll-1) * sizeof(t_fdpoll)); + fdpoll = (t_fdpoll *)realloc(fdpoll, (nfdpoll-1)*sizeof(t_fdpoll)); nfdpoll--; printf("number_connected %d;\n", nfdpoll); return; @@ -154,33 +126,24 @@ static void rmport(t_fdpoll *x) fprintf(stderr, "warning: item removed from poll list but not found"); } -static void doconnect(void) -{ +static void doconnect() { int fd = accept(sockfd, 0, 0); - if (fd < 0) - perror("accept"); + if (fd < 0) perror("accept"); else addport(fd); } -static void udpread(void) -{ +static void udpread() { char buf[BUFSIZE]; int ret = recv(sockfd, buf, BUFSIZE, 0); - if (ret < 0) - { + if (ret<0) { sockerror("recv (udp)"); x_closesocket(sockfd); exit(1); - } - else if (ret > 0) - { + } else if (ret>0) { #ifdef MSW - int j; - for (j = 0; j < ret; j++) - putchar(buf[j]); + for (int j=0; jfdp_inhead; - int intail = x->fdp_intail; - char *inbuf = x->fdp_inbuf; - if (intail == inhead) - return (0); - for (indx = intail; indx != inhead; indx = (indx+1)&(BUFSIZE-1)) - { - /* search for a semicolon. */ + int inhead = x->inhead; + int intail = x->intail; + char *inbuf = x->inbuf; + if (intail == inhead) return 0; + for (int indx = intail; indx != inhead; indx = (indx+1)&(BUFSIZE-1)) { + /* search for a semicolon. */ char c = *bp++ = inbuf[indx]; - if (c == ';') - { + if (c == ';') { intail = (indx+1)&(BUFSIZE-1); - if (inbuf[intail] == '\n') - intail = (intail+1)&(BUFSIZE-1); + if (inbuf[intail] == '\n') intail = (intail+1)&(BUFSIZE-1); *bp++ = '\n'; #ifdef MSW - { - int j; - for (j = 0; j < bp - messbuf; j++) - putchar(messbuf[j]); - } + for (int j=0; jfdp_inhead = inhead; - x->fdp_intail = intail; - return (1); + x->inhead = inhead; + x->intail = intail; + return 1; } } - return (0); + return 0; } -static void tcpread(t_fdpoll *x) -{ - int readto = - (x->fdp_inhead >= x->fdp_intail ? BUFSIZE : x->fdp_intail-1); +static void tcpread(t_fdpoll *x) { + int readto = x->inhead >= x->intail ? BUFSIZE : x->intail-1; int ret; - - /* the input buffer might be full. If so, drop the whole thing */ - if (readto == x->fdp_inhead) - { + /* the input buffer might be full. If so, drop the whole thing */ + if (readto == x->inhead) { fprintf(stderr, "pd: dropped message from gui\n"); - x->fdp_inhead = x->fdp_intail = 0; + x->inhead = x->intail = 0; readto = BUFSIZE; - } - else - { - ret = recv(x->fdp_fd, x->fdp_inbuf + x->fdp_inhead, - readto - x->fdp_inhead, 0); - if (ret < 0) - { + } else { + ret = recv(x->fd, x->inbuf + x->inhead, readto - x->inhead, 0); + if (ret < 0) { sockerror("recv (tcp)"); rmport(x); - } - else if (ret == 0) - rmport(x); - else - { - x->fdp_inhead += ret; - if (x->fdp_inhead >= BUFSIZE) - x->fdp_inhead = 0; - while (tcpmakeoutput(x)) - ; + } else if (ret == 0) rmport(x); + else { + x->inhead += ret; + if (x->inhead >= BUFSIZE) x->inhead = 0; + while (tcpmakeoutput(x)) {} } } } -static void dopoll(void) -{ - int i; +static void dopoll() { t_fdpoll *fp; fd_set readset, writeset, exceptset; FD_ZERO(&writeset); FD_ZERO(&readset); FD_ZERO(&exceptset); - FD_SET(sockfd, &readset); - if (protocol == SOCK_STREAM) - { - for (fp = fdpoll, i = nfdpoll; i--; fp++) - FD_SET(fp->fdp_fd, &readset); + if (protocol == SOCK_STREAM) { + fp = fdpoll; + for (int i=nfdpoll; i--; fp++) FD_SET(fp->fd, &readset); } - if (select(maxfd+1, &readset, &writeset, &exceptset, 0) < 0) - { - perror("select"); - exit(1); - } - if (protocol == SOCK_STREAM) - { - for (i = 0; i < nfdpoll; i++) - if (FD_ISSET(fdpoll[i].fdp_fd, &readset)) - tcpread(&fdpoll[i]); - if (FD_ISSET(sockfd, &readset)) - doconnect(); - } - else - { - if (FD_ISSET(sockfd, &readset)) - udpread(); + if (select(maxfd+1, &readset, &writeset, &exceptset, 0) < 0) {perror("select"); exit(1);} + if (protocol == SOCK_STREAM) { + for (int i=0; i