11
11
#include <netdb.h>
12
12
#include <errno.h>
13
13
14
- static const unsigned short commandPort = 4000 ;
14
+ static const unsigned short CommandPort = 4000 ;
15
15
16
- int video_enable = 0 ;
17
- int audio_enable = 0 ;
18
- int jpeg_capture = 0 ;
19
- static int SelfPipe [2 ];
20
-
21
- static char * token_ptr ;
16
+ int VideoCaptureEnable = 0 ;
17
+ int AudioCaptureEnable = 0 ;
18
+ int JpegCaptureTriggler = 0 ;
22
19
23
- struct commandTableSt {
24
- const char * cmd ;
25
- char * (* func )(int );
26
- };
20
+ static int SelfPipe [2 ];
21
+ static char * TokenPtr ;
27
22
28
- void commandResponse (int fd , const char * res ) {
23
+ void CommandResponse (int fd , const char * res ) {
29
24
30
25
unsigned char buf [256 ];
31
- buf [0 ] = fd ;
32
- strcpy ((char * )buf + 1 , res );
33
- write (SelfPipe [1 ], & buf , strlen (res ) + 2 );
26
+ buf [0 ] = strlen (res ) + 1 ;
27
+ buf [1 ] = fd ;
28
+ strncpy ((char * )buf + 2 , res , 253 );
29
+ write (SelfPipe [1 ], & buf , buf [0 ] + 2 );
34
30
}
35
31
36
32
char * VideoCapture (int fd ) {
37
33
38
- char * p = strtok_r (NULL , " \t\r\n" , & token_ptr );
34
+ char * p = strtok_r (NULL , " \t\r\n" , & TokenPtr );
39
35
if (!p ) return "error" ;
40
36
if (!strcmp (p , "on" )) {
41
- video_enable = 1 ;
37
+ VideoCaptureEnable = 1 ;
42
38
fprintf (stderr , "[command] video capute on\n" , p );
43
39
return "ok" ;
44
40
}
45
41
if (!strcmp (p , "off" )) {
46
- video_enable = 0 ;
42
+ VideoCaptureEnable = 0 ;
47
43
fprintf (stderr , "[command] video capute off\n" , p );
48
44
return "ok" ;
49
45
}
@@ -52,15 +48,15 @@ char *VideoCapture(int fd) {
52
48
53
49
char * AudioCapture (int fd ) {
54
50
55
- char * p = strtok_r (NULL , " \t\r\n" , & token_ptr );
51
+ char * p = strtok_r (NULL , " \t\r\n" , & TokenPtr );
56
52
if (!p ) return "error" ;
57
53
if (!strcmp (p , "on" )) {
58
- audio_enable = 1 ;
54
+ AudioCaptureEnable = 1 ;
59
55
fprintf (stderr , "[command] audio capute on\n" , p );
60
56
return "ok" ;
61
57
}
62
58
if (!strcmp (p , "off" )) {
63
- audio_enable = 0 ;
59
+ AudioCaptureEnable = 0 ;
64
60
fprintf (stderr , "[command] audio capute off\n" , p );
65
61
return "ok" ;
66
62
}
@@ -69,23 +65,30 @@ char *AudioCapture(int fd) {
69
65
70
66
char * JpegCapture (int fd ) {
71
67
72
- if (jpeg_capture ) commandResponse (jpeg_capture , "error" );
73
- jpeg_capture = fd ;
68
+ if (JpegCaptureTriggler ) {
69
+ fprintf (stderr , "[command] jpeg error %d\n" , JpegCaptureTriggler );
70
+ CommandResponse (JpegCaptureTriggler , "error" );
71
+ }
72
+ JpegCaptureTriggler = fd ;
74
73
return NULL ;
75
74
}
76
75
77
- struct commandTableSt commandTable [] = {
76
+ struct CommandTableSt {
77
+ const char * cmd ;
78
+ char * (* func )(int );
79
+ };
80
+
81
+ struct CommandTableSt CommandTable [] = {
78
82
{ "video" , & VideoCapture },
79
83
{ "audio" , & AudioCapture },
80
84
{ "jpeg" , & JpegCapture },
81
85
};
82
86
83
- static void * command_thread (void * arg ) {
87
+ static void * CommandThread (void * arg ) {
84
88
85
89
static const int MaxConnect = 255 ;
86
- int MaxFd = 0 ;
87
- int PortTable [MaxConnect ];
88
- fd_set TargetFd ;
90
+ int maxFd = 0 ;
91
+ fd_set targetFd ;
89
92
90
93
int listenSocket = socket (AF_INET , SOCK_STREAM , 0 );
91
94
if (listenSocket < 0 ) {
@@ -102,7 +105,7 @@ static void *command_thread(void *arg) {
102
105
103
106
struct sockaddr_in saddr ;
104
107
saddr .sin_family = AF_INET ;
105
- saddr .sin_port = htons (commandPort );
108
+ saddr .sin_port = htons (CommandPort );
106
109
saddr .sin_addr .s_addr = htonl (INADDR_ANY );
107
110
if (bind (listenSocket , (struct sockaddr * )& saddr , sizeof (saddr )) < 0 ) {
108
111
fprintf (stderr , "bind : %s\n" , strerror (errno ));
@@ -116,36 +119,36 @@ static void *command_thread(void *arg) {
116
119
return NULL ;
117
120
}
118
121
119
- FD_ZERO (& TargetFd );
120
- for (int i = 0 ; i < MaxConnect ; i ++ ) {
121
- PortTable [i ] = 0 ;
122
- }
123
-
124
- PortTable [listenSocket ] = 0 ;
125
- FD_SET (listenSocket , & TargetFd );
126
- MaxFd = listenSocket ;
127
- FD_SET (SelfPipe [0 ], & TargetFd );
128
- MaxFd = (SelfPipe [0 ] > MaxFd ) ? SelfPipe [0 ] : MaxFd ;
129
- if (MaxFd >= MaxConnect ) MaxFd = MaxConnect - 1 ;
122
+ FD_ZERO (& targetFd );
123
+ FD_SET (listenSocket , & targetFd );
124
+ maxFd = listenSocket ;
125
+ FD_SET (SelfPipe [0 ], & targetFd );
126
+ maxFd = (SelfPipe [0 ] > maxFd ) ? SelfPipe [0 ] : maxFd ;
127
+ if (maxFd >= MaxConnect ) maxFd = MaxConnect - 1 ;
130
128
131
129
while (1 ) {
132
130
fd_set checkFDs ;
133
- memcpy (& checkFDs , & TargetFd , sizeof (TargetFd ));
134
- if (select (MaxFd + 1 , & checkFDs , NULL , NULL , NULL ) == -1 ) {
131
+ memcpy (& checkFDs , & targetFd , sizeof (targetFd ));
132
+ if (select (maxFd + 1 , & checkFDs , NULL , NULL , NULL ) == -1 ) {
135
133
fprintf (stderr , "select error : %s\n" , strerror (errno ));
136
134
} else {
137
- for (int fd = MaxFd ; fd >= 0 ; fd -- ) {
135
+ for (int fd = maxFd ; fd >= 0 ; fd -- ) {
138
136
if (FD_ISSET (fd , & checkFDs )) {
139
137
if (fd == SelfPipe [0 ]) {
140
- unsigned char buf [256 ];
141
- read (SelfPipe [0 ], buf , 256 );
142
- int resFd = buf [0 ];
143
- char * res = (char * )buf + 1 ;
144
- strcat (res , "\n" );
145
- send (resFd , res , strlen (res ) + 1 , 0 );
146
- close (resFd );
147
- FD_CLR (resFd , & TargetFd );
148
- PortTable [resFd ] = 0 ;
138
+ while (1 ) {
139
+ unsigned char buf [256 ];
140
+ int length = read (SelfPipe [0 ], buf , 2 );
141
+ if (length <= 1 ) break ;
142
+ int resSize = buf [0 ];
143
+ int resFd = buf [1 ];
144
+ length = read (SelfPipe [0 ], buf , resSize );
145
+ if (length < resSize ) break ;
146
+ char * res = (char * )buf ;
147
+ strcat (res , "\n" );
148
+ send (resFd , res , strlen (res ) + 1 , 0 );
149
+ close (resFd );
150
+ FD_CLR (resFd , & targetFd );
151
+ }
149
152
} else if (fd == listenSocket ) {
150
153
struct sockaddr_in dstAddr ;
151
154
int len = sizeof (dstAddr );
@@ -161,35 +164,32 @@ static void *command_thread(void *arg) {
161
164
}
162
165
int flag = fcntl (newSocket , F_GETFL , 0 );
163
166
fcntl (newSocket , F_SETFL , O_NONBLOCK |flag );
164
- PortTable [newSocket ] = 1 ;
165
- FD_SET (newSocket , & TargetFd );
166
- MaxFd = (newSocket > MaxFd ) ? newSocket : MaxFd ;
167
- if (MaxFd >= MaxConnect ) MaxFd = MaxConnect - 1 ;
168
- } else if (PortTable [fd ] > 0 ) {
167
+ FD_SET (newSocket , & targetFd );
168
+ maxFd = (newSocket > maxFd ) ? newSocket : maxFd ;
169
+ if (maxFd >= MaxConnect ) maxFd = MaxConnect - 1 ;
170
+ } else {
169
171
char buf [256 ];
170
172
int size = recv (fd , buf , 255 , 0 );
171
173
if (!size ) {
172
- FD_CLR (fd , & TargetFd );
174
+ FD_CLR (fd , & targetFd );
173
175
break ;
174
176
}
175
177
if (size < 0 ) {
176
178
close (fd );
177
- FD_CLR (fd , & TargetFd );
178
- PortTable [fd ] = 0 ;
179
+ FD_CLR (fd , & targetFd );
179
180
break ;
180
181
}
181
182
buf [size ] = 0 ;
182
- char * p = strtok_r (buf , " \t\r\n" , & token_ptr );
183
+ char * p = strtok_r (buf , " \t\r\n" , & TokenPtr );
183
184
if (!p ) continue ;
184
185
int executed = 0 ;
185
- for (int i = 0 ; i < sizeof (commandTable ) / sizeof (struct commandTableSt ); i ++ ) {
186
- if (!strcmp (p , commandTable [i ].cmd )) {
187
- char * res = (* commandTable [i ].func )(fd );
186
+ for (int i = 0 ; i < sizeof (CommandTable ) / sizeof (struct CommandTableSt ); i ++ ) {
187
+ if (!strcmp (p , CommandTable [i ].cmd )) {
188
+ char * res = (* CommandTable [i ].func )(fd );
188
189
if (res ) {
189
190
send (fd , res , strlen (res ) + 1 , 0 );
190
191
close (fd );
191
- FD_CLR (fd , & TargetFd );
192
- PortTable [fd ] = 0 ;
192
+ FD_CLR (fd , & targetFd );
193
193
}
194
194
executed = 1 ;
195
195
break ;
@@ -199,8 +199,7 @@ static void *command_thread(void *arg) {
199
199
char * res = "error" ;
200
200
send (fd , res , strlen (res ) + 1 , 0 );
201
201
close (fd );
202
- FD_CLR (fd , & TargetFd );
203
- PortTable [fd ] = 0 ;
202
+ FD_CLR (fd , & targetFd );
204
203
fprintf (stderr , "command error : %s\n" , p );
205
204
}
206
205
}
@@ -222,5 +221,5 @@ static void __attribute ((constructor)) command_init(void) {
222
221
fcntl (SelfPipe [1 ], F_SETFL , O_NONBLOCK |flag );
223
222
224
223
pthread_t thread ;
225
- pthread_create (& thread , NULL , command_thread , NULL );
224
+ pthread_create (& thread , NULL , CommandThread , NULL );
226
225
}
0 commit comments