source: titan/titan/rguid.h @ 15272

Last change on this file since 15272 was 15143, checked in by nit, 12 years ago

[titan] plugin optimize, plugin scriptexec, rcgui stanby fix, optimize transponder, optimize channel, optimize png render

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