Questo era rimasto indietro da un bel po'
[gapil.git] / listati / poll_echod.c
1     /* initialize all needed variables */
2     poll_set = (struct pollfd *) malloc(n * sizeof(struct pollfd));
3     max_fd = list_fd;                 /* maximum now is listening socket */
4     for (i=0; i<n; i++) {
5         poll_set[i].fd = -1;
6         poll_set[i].events = POLLRDNORM;
7     }
8     poll_set[max_fd].fd = list_fd;
9     /* main loop, wait for connection and data inside a select */
10     while (1) {    
11         while ( ((n = poll(poll_set, max_fd + 1, -1)) < 0) 
12                 && (errno == EINTR));         /* wait for data or connection */
13         if (n < 0) {                          /* on real error exit */
14             PrintErr("poll error");
15             exit(1);
16         }
17         /* on activity */
18         if (poll_set[list_fd].revents & POLLRDNORM) {  /* if new connection */
19             n--;                              /* decrement active */
20             len = sizeof(c_addr);             /* and call accept */
21             if ((fd = accept(list_fd, (struct sockaddr *)&c_addr, &len)) < 0) {
22                 PrintErr("accept error");
23                 exit(1);
24             }
25             poll_set[fd].fd = fd;             /* set new connection socket */
26             if (max_fd < fd) max_fd = fd;     /* if needed set new maximum */
27         }
28         /* loop on open connections */
29         i = list_fd;                  /* first socket to look */
30         while (n != 0) {              /* loop until active */
31             i++;                      /* start after listening socket */
32             if (poll_set[i].fd == -1) continue;   /* closed, go next */
33             if (poll_set[i].revents & (POLLRDNORM|POLLERR)) {
34                 n--;                         /* decrease active */
35                 nread = read(i, buffer, MAXLINE);     /* read operations */
36                 if (nread < 0) {
37                     PrintErr("Errore in lettura");
38                     exit(1);
39                 }
40                 if (nread == 0) {            /* if closed connection */
41                     close(i);                /* close file */
42                     poll_set[i].fd = -1;          /* mark as closed in table */
43                     if (max_fd == i) {       /* if was the maximum */
44                         while (poll_set[--i].fd == -1);    /* loop down */
45                         max_fd = i;          /* set new maximum */
46                         break;               /* and go back to select */
47                     }
48                     continue;                /* continue loop on open */
49                 }
50                 nwrite = FullWrite(i, buffer, nread); /* write data */
51                 if (nwrite) {
52                     PrintErr("Errore in scrittura");
53                     exit(1);
54                 }
55             }
56         }
57     }
58     /* normal exit, never reached */
59     exit(0);
60 }