source: titan/titan/rguid.h @ 38972

Last change on this file since 38972 was 18039, checked in by nit, 12 years ago

[titan] fix rguid

File size: 3.2 KB
Line 
1#ifndef RGUID_H
2#define RGUID_H
3
4void rguidgotdata(struct stimerthread* timernode, int* connfd)
5{
6        int ret = 0, keycode = 0;
7        unsigned char* buf = NULL;
8
9        if(timernode == NULL)
10        {
11                sockclose(connfd);
12                return;
13        }
14
15        buf = malloc(MINMALLOC);
16        if(buf == NULL)
17        {
18                err("no mem");
19                sockclose(connfd);
20                return;
21        }
22        memset(buf, 0, 6);
23
24        debug(250, "get client data");
25
26        status.rguidfd = *connfd;
27        blitfb(0);
28        while(timernode->aktion != STOP)
29        {
30                ret = sockread(*connfd, buf, 0, 6, 1000 * 1000, 1);
31                if(ret < 0)
32                {
33                        err("lost connection");
34                        break;
35                }
36
37                if(buf != NULL && buf[0] != '\0')
38                {
39                        keycode = atoi((char*)buf);
40      memset(buf, 0, 6);
41                        debug(250, "got keycode %d\n", keycode);
42                       
43                        if(status.standby == 0 && keycode == 98989)
44                        {
45                                int i = 0;
46                               
47                                //leave all menüs and screens
48                                for(i = 0; i < 10; i++)
49                                {
50                                        writerc(getrcconfigint("rcexit", NULL));
51                                        usleep(5000);
52                                }
53                               
54                                //send box into standby
55                                int tmpaktion = getconfigint("poweraktion", NULL);
56                                addconfigint("poweraktion", 2);
57                                writerc(getrcconfigint("rcpower", NULL));
58                                usleep(500000);
59                                addconfigint("poweraktion", tmpaktion);
60                                continue;
61                        }
62                       
63                        if(status.standby == 0 && keycode == getrcconfigint("rcpower", NULL))
64                                continue;
65
66                        writerc(keycode);
67                }
68        }
69
70        status.rguidfd = -1;
71        sockclose(connfd);
72        free(buf); buf = NULL;
73}
74
75int rguidnewconn(int* streamfd, int* connfd)
76{
77        int i, fd = -1;
78
79        fd = sockaccept(streamfd, 1);
80        if(fd < 0) return -1;
81
82        for(i = 0; i < MAXRGUIDCONN; i++)
83        {
84                if(connfd[i] < 0)
85                {
86                        connfd[i] = fd;
87                        debug(250, "accept rguid connection fd=%d", fd);
88                        fd = -1;
89                        break;
90                }
91        }
92
93        if(fd != -1)
94        {
95                debug(250, "all connections in use");
96                sockclose(&fd);
97                return -1;
98        }
99
100        return 0;
101}
102
103void rguidthreadfunc(struct stimerthread* timernode)
104{
105        struct timeval timeout;
106        fd_set rfds;
107        int i, ret = 0, streamfd = -1, connfd[MAXRGUIDCONN], maxfd = -1;
108
109        debug(250, "Starting rguid thread");
110
111        for(i = 0; i < MAXRGUIDCONN; i++)
112                connfd[i] = -1;
113
114        while(timernode->aktion != STOP)
115        {
116                if(streamfd < 0)
117                {
118                        sockportcreate(&streamfd, getconfigint("rguidport", NULL), MAXHTTPDCONN);
119                        if(streamfd < 0) break;
120                        maxfd = streamfd;
121
122                        //set nonblocking
123                        fcntl(streamfd, F_SETFL, fcntl(streamfd, F_GETFL) | O_NONBLOCK);
124                }
125
126                timeout.tv_sec = 1;
127                timeout.tv_usec = 0;
128
129                FD_ZERO(&rfds);
130                FD_SET(streamfd, &rfds);
131
132                for(i = 0; i < MAXRGUIDCONN; i++)
133                {
134                        if(connfd[i] > -1)
135                        {
136                                FD_SET(connfd[i], &rfds);
137                                if(connfd[i] > maxfd) maxfd = connfd[i];
138                        }
139                }
140
141                ret = TEMP_FAILURE_RETRY(select(maxfd + 1, &rfds , NULL, NULL, &timeout));
142
143                if(ret < 0)
144                {
145                        perr("rguid listen socket fd=%d", maxfd);
146                        sleep(1);
147                        continue;
148                }
149                if(ret == 0) continue; //timeout
150
151                if(FD_ISSET(streamfd, &rfds))
152                {
153                        ret = rguidnewconn(&streamfd, connfd);
154                        if(ret < 0) continue;
155                }
156
157                for(i = 0; i < MAXRGUIDCONN; i++)
158                {
159                        if(connfd[i] > -1 && FD_ISSET(connfd[i], &rfds))
160                                rguidgotdata(timernode, &connfd[i]);
161                }
162        }
163
164        debug(250, "Stopping rguid thread");
165        sockclose(&streamfd);
166        for(i = 0; i < MAXRGUIDCONN; i++)
167                sockclose(&connfd[i]);
168
169        return;
170}
171
172#endif
Note: See TracBrowser for help on using the repository browser.