doorbell
changeset 6:026156ea8801
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 16 Mar 2016 00:00:06 +0200 |
parents | f21ae31ef0e7 |
children | dc4018af3a40 |
files | doorbelld/src/srv.c |
diffstat | 1 files changed, 47 insertions(+), 20 deletions(-) [+] |
line diff
1.1 --- a/doorbelld/src/srv.c Tue Mar 15 08:35:21 2016 +0200 1.2 +++ b/doorbelld/src/srv.c Wed Mar 16 00:00:06 2016 +0200 1.3 @@ -14,11 +14,14 @@ 1.4 1.5 /* TODO convert this whole thing to multicast */ 1.6 1.7 +static int bsock = -1; 1.8 +static struct sockaddr_in baddr; 1.9 static int lis_sock = -1, max_socket = -1; 1.10 static int *csock; 1.11 1.12 int srv_init(int port) 1.13 { 1.14 + int val; 1.15 struct sockaddr_in addr; 1.16 1.17 if(lis_sock != -1) { 1.18 @@ -51,6 +54,19 @@ 1.19 } 1.20 listen(lis_sock, 8); 1.21 1.22 + /* then create the video broadcast socket */ 1.23 + if((bsock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { 1.24 + fprintf(stderr, "%s: failed to create video broadcast socket\n", __func__); 1.25 + return -1; 1.26 + } 1.27 + val = 1; 1.28 + setsockopt(bsock, SOL_SOCKET, SO_BROADCAST, &val, sizeof val); 1.29 + 1.30 + memset(&baddr, 0, sizeof baddr); 1.31 + baddr.sin_family = AF_INET; 1.32 + baddr.sin_port = htons(port + 1); 1.33 + baddr.sin_addr.s_addr = 0xffffffff; 1.34 + 1.35 return 0; 1.36 } 1.37 1.38 @@ -81,30 +97,32 @@ 1.39 return max_socket; 1.40 } 1.41 1.42 -int srv_handle(int s) 1.43 +static int handle_new_client(void) 1.44 +{ 1.45 + int s; 1.46 + /* incoming connection */ 1.47 + struct sockaddr_in addr; 1.48 + socklen_t addr_size = sizeof addr; 1.49 + if((s = accept(lis_sock, (struct sockaddr*)&addr, &addr_size)) == -1) { 1.50 + fprintf(stderr, "%s: failed to accept incoming connection: %s\n", __func__, strerror(errno)); 1.51 + return -1; 1.52 + } 1.53 + printf("%s: incoming connection from %s:%d\n", __func__, 1.54 + inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); 1.55 + csock = dynarr_push(csock, &s); 1.56 + assert(csock); 1.57 + if(s > max_socket) { 1.58 + max_socket = s; 1.59 + } 1.60 + fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK); 1.61 + return 0; 1.62 +} 1.63 + 1.64 +static int handle_client(int s) 1.65 { 1.66 static char buf[1024]; 1.67 int sz; 1.68 1.69 - if(s == lis_sock) { 1.70 - /* incoming connection */ 1.71 - struct sockaddr_in addr; 1.72 - socklen_t addr_size = sizeof addr; 1.73 - if((s = accept(lis_sock, (struct sockaddr*)&addr, &addr_size)) == -1) { 1.74 - fprintf(stderr, "%s: failed to accept incoming connection: %s\n", __func__, strerror(errno)); 1.75 - return -1; 1.76 - } 1.77 - printf("%s: incoming connection from %s:%d\n", __func__, 1.78 - inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); 1.79 - csock = dynarr_push(csock, &s); 1.80 - assert(csock); 1.81 - if(s > max_socket) { 1.82 - max_socket = s; 1.83 - } 1.84 - fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK); 1.85 - return 0; 1.86 - } 1.87 - 1.88 /* handle client */ 1.89 while((sz = read(s, buf, sizeof buf - 1)) > 0) { 1.90 printf("%s: got data: %s\n", __func__, buf); 1.91 @@ -130,6 +148,15 @@ 1.92 return 0; 1.93 } 1.94 1.95 +int srv_handle(int s) 1.96 +{ 1.97 + if(s == lis_sock) { 1.98 + return handle_new_client(); 1.99 + } 1.100 + 1.101 + return handle_client(s); 1.102 +} 1.103 + 1.104 struct video_block { 1.105 unsigned int magic; 1.106 int frame;