# HG changeset patch # User John Tsiombikas # Date 1409424397 -10800 # Node ID be91b72ce3f9d82fe45d6c9d76395b25ef04e31c # Parent 76e75cbcb758f978d915dc518611a8631c871c18 foobar diff -r 76e75cbcb758 -r be91b72ce3f9 .clang_complete --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.clang_complete Sat Aug 30 21:46:37 2014 +0300 @@ -0,0 +1,1 @@ +-Iinclude diff -r 76e75cbcb758 -r be91b72ce3f9 Makefile --- a/Makefile Fri Aug 29 23:07:59 2014 +0300 +++ b/Makefile Sat Aug 30 21:46:37 2014 +0300 @@ -3,7 +3,10 @@ dep = $(obj:.o=.d) bin = vrmodel -CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl2` +inc = -Iinclude + +CFLAGS = -pedantic -Wall -g $(inc) `pkg-config --cflags sdl2` +CXXFLAGS = $(CFLAGS) LDFLAGS = $(libgl) `pkg-config --libs sdl2` ifeq ($(shell uname -s), Darwin) @@ -13,12 +16,15 @@ endif $(bin): $(obj) - $(CC) -o $@ $(obj) $(LDFLAGS) + $(CXX) -o $@ $(obj) $(LDFLAGS) -include $(dep) +%.d: %.c + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + %.d: %.cc - @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@ .PHONY: clean clean: diff -r 76e75cbcb758 -r be91b72ce3f9 include/proto.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/proto.h Sat Aug 30 21:46:37 2014 +0300 @@ -0,0 +1,23 @@ +#ifndef PROTO_H_ +#define PROTO_H_ + +#define DEF_PORT 0x6d0f + +#define MAGIC 0x6d0f +/* requests */ +#define REQ_DISCOVER 1 +#define REQ_START 2 +#define REQ_STOP 3 +/* responses */ +#define RSP_OK 0 +#define RSP_ERR 255 + + +struct message { + int magic; + int type; + char data[24]; +}; + + +#endif /* PROTO_H_ */ diff -r 76e75cbcb758 -r be91b72ce3f9 inptools/test/Makefile --- a/inptools/test/Makefile Fri Aug 29 23:07:59 2014 +0300 +++ b/inptools/test/Makefile Sat Aug 30 21:46:37 2014 +0300 @@ -2,7 +2,7 @@ obj = $(src:.c=.o) bin = test -CFLAGS = -pedantic -Wall -g +CFLAGS = -pedantic -Wall -g -I../../include $(bin): $(obj) $(CC) -o $@ $(obj) $(LDFLAGS) diff -r 76e75cbcb758 -r be91b72ce3f9 inptools/test/src/main.c --- a/inptools/test/src/main.c Fri Aug 29 23:07:59 2014 +0300 +++ b/inptools/test/src/main.c Sat Aug 30 21:46:37 2014 +0300 @@ -1,73 +1,5 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAGIC 0x6d0f -#define DISCOVER 1 -#define DREPLY 2 - -struct message { - int magic; - int type; - char data[24]; -}; - -int discover(struct sockaddr_in *client_sa); - -int opt_dport = 0x6d0f; /* discover broadcast port */ -unsigned int opt_timeout = 5000; /* 10 sec discovery timeout */ int main(int argc, char **argv) { } - -int discover(struct sockaddr_in *client_sa) -{ - int s, true = 1; - struct sockaddr_in sa; - unsigned int timeout = opt_timeout; - struct message msg; - struct timeval tv, tv_timeout; - - if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { - perror("failed to create discover datagram socket"); - return -1; - } - setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true, sizeof true); - - memset(&sa, 0, sizeof sa); - sa.sin_family = AF_INET; - sa.sin_port = htons(opt_dport); - sa.sin_addr.s_addr = htonl(INADDR_ANY); - - if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) { - perror("failed to bind datagram socket"); - close(s); - return -1; - } - - do { - msg.magic = MAGIC; - msg.type = DISCOVER; - - memset(&sa, 0, sizeof sa); - sa.sin_family = AF_INET; - sa.sin_port = htons(opt_dport); - sa.sin_addr.s_addr = 0xffffffff; - - if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) { - perror("failed to send discovery bcast dgram"); - close(s); - return -1; - } - } while(0); - - close(s); - return 0; -} diff -r 76e75cbcb758 -r be91b72ce3f9 src/inpclient.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/inpclient.cc Sat Aug 30 21:46:37 2014 +0300 @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "inpclient.h" +#include "proto.h" + +static int discover(struct sockaddr_in *srv_sa); + +static int opt_dport = DEF_PORT; /* discover broadcast port */ +static long opt_timeout = 5000; /* 10 sec discovery timeout */ + +static int sock = -1; + + +int netinp_start() +{ + struct message msg; + struct sockaddr_in srv_sa; + + if(sock == -1) { + if((sock = discover(&srv_sa)) == -1) { + return -1; + } + + if(connect(sock, (struct sockaddr*)&srv_sa, sizeof srv_sa) == -1) { + perror("failed to connect socket"); + close(sock); + return -1; + } + } + + memset(&msg, 0, sizeof msg); + msg.magic = MAGIC; + msg.type = START; + + if(send(sock, &msg, sizeof msg, 0) == -1) { + perror("failed to send start command"); + } + return 0; +} + +int netinp_stop() +{ +} + +static int discover(struct sockaddr_in *srv_sa) +{ + int s, true_val = 1; + struct sockaddr_in sa; + long timeout = opt_timeout; + struct message msg; + struct timeval tv0, tv, tv_timeout; + + if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { + perror("failed to create discover datagram socket"); + return -1; + } + setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true_val, sizeof true_val); + + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(opt_dport); + sa.sin_addr.s_addr = htonl(INADDR_ANY); + + if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) { + perror("failed to bind datagram socket"); + close(s); + return -1; + } + + do { + fd_set rdset; + + msg.magic = MAGIC; + msg.type = REQ_DISCOVER; + + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(opt_dport); + sa.sin_addr.s_addr = 0xffffffff; + + if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) { + perror("failed to send discovery bcast dgram"); + close(s); + return -1; + } + + tv_timeout.tv_sec = timeout / 1000; + tv_timeout.tv_usec = (timeout % 1000) * 1000; + + FD_ZERO(&rdset); + FD_SET(s, &rdset); + + gettimeofday(&tv0, 0); + + while(select(s + 1, &rdset, 0, 0, &tv_timeout) == -1 && errno == EINTR); + + gettimeofday(&tv, 0); + timeout -= (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; + + if(FD_ISSET(s, &rdset)) { + socklen_t sa_size = sizeof *srv_sa; + if(recvfrom(s, &msg, sizeof msg, 0, (struct sockaddr*)srv_sa, &sa_size) == -1) { + perror("failed to receive datagram\n"); + } + if(msg.magic == MAGIC && msg.type == RSP_OK) { + printf("found input server \"%s\" at: %s:%d\n", msg.data, + inet_ntoa(srv_sa->sin_addr), ntohs(srv_sa->sin_port)); + return s; + } + } + + } while(timeout > 0); + + close(s); + return -1; +} + +static int send_request(int s, struct message *msg, unsigned int timeout) +{ + if(send(s, msg, sizeof *msg, 0) == -1) { + perror("failed to send message"); + return -1; + } + + // TODO cont. +} diff -r 76e75cbcb758 -r be91b72ce3f9 src/inpclient.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/inpclient.h Sat Aug 30 21:46:37 2014 +0300 @@ -0,0 +1,10 @@ +#ifndef INPCLIENT_H_ +#define INPCLIENT_H_ + +int netinp_start(); +int netinp_stop(); + +int netinp_read(float *pos); +int netinp_reset(); + +#endif // INPCLIENT_H_