# HG changeset patch # User John Tsiombikas # Date 1458079206 -7200 # Node ID 026156ea880167a764c0b43fa8e5fa66b6656084 # Parent f21ae31ef0e763a6db6ff829d9be735514b52ab2 foo diff -r f21ae31ef0e7 -r 026156ea8801 doorbelld/src/srv.c --- a/doorbelld/src/srv.c Tue Mar 15 08:35:21 2016 +0200 +++ b/doorbelld/src/srv.c Wed Mar 16 00:00:06 2016 +0200 @@ -14,11 +14,14 @@ /* TODO convert this whole thing to multicast */ +static int bsock = -1; +static struct sockaddr_in baddr; static int lis_sock = -1, max_socket = -1; static int *csock; int srv_init(int port) { + int val; struct sockaddr_in addr; if(lis_sock != -1) { @@ -51,6 +54,19 @@ } listen(lis_sock, 8); + /* then create the video broadcast socket */ + if((bsock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { + fprintf(stderr, "%s: failed to create video broadcast socket\n", __func__); + return -1; + } + val = 1; + setsockopt(bsock, SOL_SOCKET, SO_BROADCAST, &val, sizeof val); + + memset(&baddr, 0, sizeof baddr); + baddr.sin_family = AF_INET; + baddr.sin_port = htons(port + 1); + baddr.sin_addr.s_addr = 0xffffffff; + return 0; } @@ -81,30 +97,32 @@ return max_socket; } -int srv_handle(int s) +static int handle_new_client(void) +{ + int s; + /* incoming connection */ + struct sockaddr_in addr; + socklen_t addr_size = sizeof addr; + if((s = accept(lis_sock, (struct sockaddr*)&addr, &addr_size)) == -1) { + fprintf(stderr, "%s: failed to accept incoming connection: %s\n", __func__, strerror(errno)); + return -1; + } + printf("%s: incoming connection from %s:%d\n", __func__, + inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); + csock = dynarr_push(csock, &s); + assert(csock); + if(s > max_socket) { + max_socket = s; + } + fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK); + return 0; +} + +static int handle_client(int s) { static char buf[1024]; int sz; - if(s == lis_sock) { - /* incoming connection */ - struct sockaddr_in addr; - socklen_t addr_size = sizeof addr; - if((s = accept(lis_sock, (struct sockaddr*)&addr, &addr_size)) == -1) { - fprintf(stderr, "%s: failed to accept incoming connection: %s\n", __func__, strerror(errno)); - return -1; - } - printf("%s: incoming connection from %s:%d\n", __func__, - inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); - csock = dynarr_push(csock, &s); - assert(csock); - if(s > max_socket) { - max_socket = s; - } - fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK); - return 0; - } - /* handle client */ while((sz = read(s, buf, sizeof buf - 1)) > 0) { printf("%s: got data: %s\n", __func__, buf); @@ -130,6 +148,15 @@ return 0; } +int srv_handle(int s) +{ + if(s == lis_sock) { + return handle_new_client(); + } + + return handle_client(s); +} + struct video_block { unsigned int magic; int frame;