comparison rvinterf/lowlevel/rvifmain.c @ 177:fef035264dd4

rvinterf: beginning of local socket handling
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 23 Nov 2013 20:15:02 +0000
parents 7f727aaf5cd4
children 7ab6b29e76bb
comparison
equal deleted inserted replaced
176:7f727aaf5cd4 177:fef035264dd4
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 #include <time.h> 10 #include <time.h>
11 #include "../pktmux.h" 11 #include "../pktmux.h"
12 #include "../localsock.h"
13 #include "client.h"
12 14
13 extern int target_fd; 15 extern int target_fd, listener;
14 extern char *baudrate_name; 16 extern char *baudrate_name;
15 17
16 extern u_char rxpkt[]; 18 extern u_char rxpkt[];
17 extern size_t rxpkt_len; 19 extern size_t rxpkt_len;
18 20
21 struct client *client_head;
22
19 char *logfname; 23 char *logfname;
20 FILE *logF; 24 FILE *logF;
21 time_t logtime; 25 time_t logtime;
22 int background; 26 int background;
27 int max_fd;
23 28
24 main(argc, argv) 29 main(argc, argv)
25 char **argv; 30 char **argv;
26 { 31 {
27 extern char *optarg; 32 extern char *optarg;
28 extern int optind; 33 extern int optind;
29 int c; 34 int c;
30 fd_set fds; 35 fd_set fds;
36 struct client *cli, **clip;
31 37
32 while ((c = getopt(argc, argv, "bB:d:l:")) != EOF) 38 while ((c = getopt(argc, argv, "bB:d:l:")) != EOF)
33 switch (c) { 39 switch (c) {
34 case 'b': 40 case 'b':
35 background++; 41 background++;
52 if (target_fd <= 0) { 58 if (target_fd <= 0) {
53 if (argc - optind != 1) 59 if (argc - optind != 1)
54 goto usage; 60 goto usage;
55 open_target_serial(argv[optind]); 61 open_target_serial(argv[optind]);
56 } 62 }
63 max_fd = target_fd;
57 64
58 set_serial_nonblock(0); 65 set_serial_nonblock(0);
59 setlinebuf(stdout); 66 setlinebuf(stdout);
60 if (logfname) { 67 if (logfname) {
61 logF = fopen(logfname, "w"); 68 logF = fopen(logfname, "w");
64 exit(1); 71 exit(1);
65 } 72 }
66 fprintf(logF, "*** Log of rvinterf session ***\n"); 73 fprintf(logF, "*** Log of rvinterf session ***\n");
67 setlinebuf(logF); 74 setlinebuf(logF);
68 } 75 }
76 create_listener_socket();
69 if (background) { 77 if (background) {
70 c = fork(); 78 c = fork();
71 if (c < 0) { 79 if (c < 0) {
72 perror("fork"); 80 perror("fork");
73 exit(1); 81 exit(1);
78 } 86 }
79 } 87 }
80 for (;;) { 88 for (;;) {
81 FD_ZERO(&fds); 89 FD_ZERO(&fds);
82 FD_SET(target_fd, &fds); 90 FD_SET(target_fd, &fds);
83 c = select(target_fd+1, &fds, 0, 0, 0); 91 FD_SET(listener, &fds);
92 for (clip = &client_head; cli = *clip; ) {
93 if (cli->rx_state == 2) {
94 close(cli->fd);
95 *clip = cli->next;
96 free(cli);
97 continue;
98 }
99 FD_SET(cli->fd, &fds);
100 clip = &cli->next;
101 }
102 c = select(max_fd+1, &fds, 0, 0, 0);
84 time(&logtime); 103 time(&logtime);
85 if (c < 0) { 104 if (c < 0) {
86 if (errno == EINTR) 105 if (errno == EINTR)
87 continue; 106 continue;
88 perror("select"); 107 perror("select");
89 exit(1); 108 exit(1);
90 } 109 }
91 if (FD_ISSET(target_fd, &fds)) 110 if (FD_ISSET(target_fd, &fds))
92 process_serial_rx(); 111 process_serial_rx();
112 if (FD_ISSET(listener, &fds))
113 handle_listener_select();
114 for (cli = client_head; cli; cli = cli->next)
115 if (FD_ISSET(cli->fd, &fds))
116 handle_client_select(cli);
93 } 117 }
94 } 118 }
95 119
96 handle_rx_packet() 120 handle_rx_packet()
97 { 121 {