Questo era rimasto indietro da un bel po'
authorSimone Piccardi <piccardi@gnulinux.it>
Tue, 17 Feb 2004 22:20:13 +0000 (22:20 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Tue, 17 Feb 2004 22:20:13 +0000 (22:20 +0000)
listati/poll_echod.c [new file with mode: 0644]

diff --git a/listati/poll_echod.c b/listati/poll_echod.c
new file mode 100644 (file)
index 0000000..91b53ce
--- /dev/null
@@ -0,0 +1,60 @@
+    /* initialize all needed variables */
+    poll_set = (struct pollfd *) malloc(n * sizeof(struct pollfd));
+    max_fd = list_fd;                 /* maximum now is listening socket */
+    for (i=0; i<n; i++) {
+       poll_set[i].fd = -1;
+       poll_set[i].events = POLLRDNORM;
+    }
+    poll_set[max_fd].fd = list_fd;
+    /* main loop, wait for connection and data inside a select */
+    while (1) {    
+       while ( ((n = poll(poll_set, max_fd + 1, -1)) < 0) 
+               && (errno == EINTR));         /* wait for data or connection */
+       if (n < 0) {                          /* on real error exit */
+           PrintErr("poll error");
+           exit(1);
+       }
+       /* on activity */
+       if (poll_set[list_fd].revents & POLLRDNORM) {  /* if new connection */
+           n--;                              /* decrement active */
+           len = sizeof(c_addr);             /* and call accept */
+           if ((fd = accept(list_fd, (struct sockaddr *)&c_addr, &len)) < 0) {
+               PrintErr("accept error");
+               exit(1);
+           }
+           poll_set[fd].fd = fd;             /* set new connection socket */
+           if (max_fd < fd) max_fd = fd;     /* if needed set new maximum */
+       }
+       /* loop on open connections */
+       i = list_fd;                  /* first socket to look */
+       while (n != 0) {              /* loop until active */
+           i++;                      /* start after listening socket */
+           if (poll_set[i].fd == -1) continue;   /* closed, go next */
+           if (poll_set[i].revents & (POLLRDNORM|POLLERR)) {
+               n--;                         /* decrease active */
+               nread = read(i, buffer, MAXLINE);     /* read operations */
+               if (nread < 0) {
+                   PrintErr("Errore in lettura");
+                   exit(1);
+               }
+               if (nread == 0) {            /* if closed connection */
+                   close(i);                /* close file */
+                   poll_set[i].fd = -1;          /* mark as closed in table */
+                   if (max_fd == i) {       /* if was the maximum */
+                       while (poll_set[--i].fd == -1);    /* loop down */
+                       max_fd = i;          /* set new maximum */
+                       break;               /* and go back to select */
+                   }
+                   continue;                /* continue loop on open */
+               }
+               nwrite = FullWrite(i, buffer, nread); /* write data */
+               if (nwrite) {
+                   PrintErr("Errore in scrittura");
+                   exit(1);
+               }
+           }
+       }
+    }
+    /* normal exit, never reached */
+    exit(0);
+}