forked from larsmm/pixelflut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
435 lines (393 loc) · 13.1 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#define BUFSIZE 2048
#define XSTR(a) #a
#define STR(a) XSTR(a)
static int bytesPerPixel;
static uint8_t* pixels;
static volatile int running = 1;
static volatile int client_thread_count;
static volatile int server_sock;
static volatile unsigned int histogram[8][8][8];
static volatile unsigned int total;
static uint8_t* index_html = 0;
static int index_html_len = 0;
void * handle_client(void *);
void * handle_clients(void *);
void set_pixel(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
if(x < PIXEL_WIDTH && y < PIXEL_HEIGHT){
uint8_t *pixel = ((uint8_t*)pixels) + (y * PIXEL_WIDTH + x) * bytesPerPixel; // RGB(A)
histogram[r >> 5][g >> 5][b >> 5]++; // color statistics
total++;
if(a == 255){ // fast & usual path
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
}
else{
int alpha = a * 65793;
int nalpha = (255 - a) * 65793;
pixel[0] = (uint8_t)(r * alpha + pixel[0] * nalpha) >> 16;
pixel[1] = (uint8_t)(g * alpha + pixel[1] * nalpha) >> 16;
pixel[2] = (uint8_t)(b * alpha + pixel[2] * nalpha) >> 16;
}
}
}
void update_pixels()
{
static int frame = 0;
if (frame % 4 == 0)
{
// fade out
for (int y = 0; y < PIXEL_HEIGHT; y++)
{
for (int x = 0; x < PIXEL_WIDTH; x++)
{
uint8_t *pixel = ((uint8_t*)pixels) + (y * PIXEL_WIDTH + x) * bytesPerPixel; // RGB(A)
pixel[0] = pixel[0] ? pixel[0] - 1 : pixel[0];
pixel[1] = pixel[1] ? pixel[1] - 1 : pixel[1];
pixel[2] = pixel[2] ? pixel[2] - 1 : pixel[2];
}
}
// update histogram
for (int r = 0; r < 8; r++)
for (int g = 0; g < 8; g++)
for (int b = 0; b < 8; b++)
histogram[r][g][b] *= 0.99;
}
frame++;
}
static void loadIndexHtml()
{
const char http_ok[] = "HTTP/1.1 200 OK\r\n\r\n";
FILE *file = fopen("index.html", "rt");
if (!file)
{
fprintf(stderr, "Could not open index.html!\n");
return;
}
fseek(file, 0, SEEK_END);
long len = ftell(file);
fseek(file, 0, SEEK_SET);
index_html_len = len + sizeof(http_ok) - 1;
index_html = malloc(index_html_len);
memcpy(index_html, http_ok, sizeof(http_ok) - 1);
int read = fread(index_html + sizeof(http_ok) - 1, 1, len, file);
if (!read)
fprintf(stderr, "Could not read index.html!\n");
fclose(file);
}
char * itoa(int n, char *s)
{
int i, j, l, sign;
char c;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
l = i;
for (i = 0, j = l-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
return s + l;
}
void * handle_client(void *s){
client_thread_count++;
int sock = *(int*)s;
char buf[BUFSIZE];
int read_size, read_pos = 0;
uint32_t x,y,c, hi;
int32_t offset_x = 0, offset_y = 0;
while(running && (read_size = recv(sock , buf + read_pos, sizeof(buf) - read_pos , 0)) > 0){
read_pos += read_size;
int found;
do{
found = 0;
for (int i = 0; i < read_pos; i++){
if (buf[i] == '\n'){
buf[i] = 0;
if(!strncmp(buf, "PX ", 3)){ // ...don't ask :D...
char *pos1 = buf + 3;
x = strtoul(buf + 3, &pos1, 10);
if(buf != pos1){
pos1++;
char *pos2 = pos1;
y = strtoul(pos1, &pos2, 10);
if(pos1 != pos2){
x += offset_x;
y += offset_y;
pos2++;
pos1 = pos2;
c = strtoul(pos2, &pos1, 16);
if(pos2 != pos1){
uint8_t r, g, b, a;
int codelen = pos1 - pos2;
if(codelen > 6){ // rgba
r = c >> 24;
g = c >> 16;
b = c >> 8;
a = c;
}
else if(codelen > 2){ // rgb
r = c >> 16;
g = c >> 8;
b = c;
a = 255;
}
else{ // gray
r = c;
g = c;
b = c;
a = 255;
}
set_pixel(x, y, r, g, b, a);
}
else if((x < PIXEL_WIDTH) && (y < PIXEL_HEIGHT)){
char colorout[30];
snprintf(colorout, sizeof(colorout), "PX %d %d %06x\n",x,y, pixels[y * PIXEL_WIDTH + x] & 0xffffff);
send(sock, colorout, sizeof(colorout) - 1, MSG_DONTWAIT | MSG_NOSIGNAL);
}
}
}
} else if(!strncmp(buf, "OFFSET ", 7)){
int32_t x, y;
if (sscanf(buf + 7, "%i %i", &x,&y) == 2){
offset_x = x;
offset_y = y;
}
} else if(!strncmp(buf, "SIZE", 4)){
static const char out[] = "SIZE " STR(PIXEL_WIDTH) " " STR(PIXEL_HEIGHT) "\n";
send(sock, out, sizeof(out) - 1, MSG_DONTWAIT | MSG_NOSIGNAL);
}
else if(!strncmp(buf, "CONNECTIONS", 11)){
char out[32];
sprintf(out, "CONNECTIONS %d\n", client_thread_count);
send(sock, out, strlen(out), MSG_DONTWAIT | MSG_NOSIGNAL);
}
else if(!strncmp(buf, "HELP", 4)){
static const char out[] =
"send pixel: 'PX {x} {y} {GG or RRGGBB or RRGGBBAA as HEX}\\n'; "
"request pixel: 'PX {x} {y}\\n'; "
"request resolution: 'SIZE\\n'; "
"request client connection count: 'CONNECTIONS\\n'; "
"request this help message: 'HELP\\n';\n";
send(sock, out, sizeof(out) - 1, MSG_DONTWAIT | MSG_NOSIGNAL);
}
else if(!strncmp(buf, "GET", 3)){ // obviously totally HTTP compliant!
char out[16384];
if (!strncmp(buf + 4, "/data.json", 10)){
strcpy(out, "HTTP/1.1 200 OK\r\n\r\n[");
char *hp = out + sizeof("HTTP/1.1 200 OK\r\n\r\n[") - 1;
for (hi = 0; hi < 8 * 8 * 8; hi++){
hp = itoa(((unsigned int*)&histogram[0][0][0])[hi], hp);
*hp++ = ',';
}
hp[-1] = ']';
hp[0] = 0;
//printf(out);
send(sock, out, strlen(out), MSG_DONTWAIT | MSG_NOSIGNAL);
}
else{
free(index_html);
loadIndexHtml(); // debug
send(sock, index_html, index_html_len, MSG_DONTWAIT | MSG_NOSIGNAL);
}
goto disconnect;
}
else{
/*printf("BULLSHIT[%i]: ", i);
int j;
for (j = 0; j < i; j++)
printf("%c", buf[j]);
printf("\n");*/
break;
}
int offset = i + 1;
int count = read_pos - offset;
if (count > 0)
memmove(buf, buf + offset, count); // TODO: ring buffer?
read_pos -= offset;
found = 1;
break;
}
}
if (sizeof(buf) - read_pos == 0){ // received only garbage for a whole buffer. start over!
//buf[sizeof(buf) - 1] = 0;
//printf("BULLSHIT BUFFER: %s\n", buf);
read_pos = 0;
}
}while (found);
}
disconnect:
close(sock);
//printf("Client disconnected\n");
fflush(stdout);
client_thread_count--;
return 0;
}
void * handle_clients(void * foobar){
(void)foobar;
pthread_t thread_id;
int client_sock;
socklen_t addr_len;
struct sockaddr_in addr;
addr_len = sizeof(addr);
struct timeval tv;
printf("Starting Server...\n");
server_sock = socket(PF_INET, SOCK_STREAM, 0);
tv.tv_sec = 5;
tv.tv_usec = 0;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
if (server_sock == -1){
perror("socket() failed");
return 0;
}
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
printf("setsockopt(SO_REUSEADDR) failed\n");
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEPORT, &(int){ 1 }, sizeof(int)) < 0)
printf("setsockopt(SO_REUSEPORT) failed\n");
int retries;
for (retries = 0; bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) == -1 && retries < 10; retries++){
perror("bind() failed ...retry in 5s");
usleep(5000000);
}
if (retries == 10)
return 0;
if (listen(server_sock, 32) == -1){
perror("listen() failed");
return 0;
}
printf("Listening...\n");
setsockopt(server_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
setsockopt(server_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
while(running){
client_sock = accept(server_sock, (struct sockaddr*)&addr, &addr_len);
if(client_sock > 0){
//printf("Client %s connected\n", inet_ntoa(addr.sin_addr));
if( pthread_create( &thread_id , NULL , handle_client , (void*) &client_sock) < 0)
{
close(client_sock);
perror("could not create thread");
}
}
}
close(server_sock);
return 0;
}
void * handle_udp(void * foobar){
(void)foobar;
int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0){
perror("udp socket() failed");
return 0;
}
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
printf("udp setsockopt(SO_REUSEADDR) failed\n");
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
printf("Error setting socket timeout\n");
}
struct sockaddr_in si_me = {0};
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) < 0){
perror("udp bind() failed");
return 0;
}
#define UDP_BUFFER_SIZE 65507
while(running){
uint8_t buf[UDP_BUFFER_SIZE];
int n = recv(s, buf, UDP_BUFFER_SIZE, 0);
if (n < 0){
if(errno != EAGAIN)
perror("udp recv() failed");
continue;
}
if (n > 6)
{
uint16_t *p = (uint16_t*)buf;
int x = p[0], y = p[1], stride = p[2];
uint8_t *data = buf + 6;
int pixelCount = (n - 6) / 3;
if (x + stride <= PIXEL_WIDTH)
{
while(y < PIXEL_HEIGHT && pixelCount)
{
int linePixelCount = pixelCount > stride ? stride : pixelCount;
uint8_t *pixel = pixels + (y * PIXEL_WIDTH + x) * bytesPerPixel;
for (int i = 0; i < linePixelCount; i++)
{
pixel[0] = data[0];
pixel[1] = data[1];
pixel[2] = data[2];
pixel += bytesPerPixel;
data += 3;
}
y++;
pixelCount -= linePixelCount;
}
}
}
}
return 0;
}
pthread_t thread_id;
pthread_t udp_thread_id;
int server_start()
{
loadIndexHtml();
pixels = calloc(PIXEL_WIDTH * PIXEL_HEIGHT, bytesPerPixel);
if(pthread_create(&thread_id , NULL, handle_clients , NULL) < 0){
perror("could not create tcp thread");
running = 0;
free(pixels);
return 0;
}
if(pthread_create(&udp_thread_id , NULL, handle_udp , NULL) < 0){
perror("could not create udp thread");
running = 0;
free(pixels);
return 0;
}
return 1;
}
void server_stop()
{
running = 0;
printf("Shutting Down %d childs ...\n", client_thread_count);
while (client_thread_count)
usleep(100000);
printf("Shutting Down socket ...\n");
close(server_sock);
printf("Joining threads ... ");
printf("TCP ... ");
pthread_join(thread_id, NULL);
printf("UDP ... ");
pthread_join(udp_thread_id, NULL);
printf("done\n");
printf("Cleaning memory\n");
free(pixels);
}