vrmodel

changeset 2:be91b72ce3f9

foobar
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Aug 2014 21:46:37 +0300
parents 76e75cbcb758
children a1784a4290c2
files .clang_complete Makefile include/proto.h inptools/test/Makefile inptools/test/src/main.c src/inpclient.cc src/inpclient.h
diffstat 7 files changed, 178 insertions(+), 72 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.clang_complete	Sat Aug 30 21:46:37 2014 +0300
     1.3 @@ -0,0 +1,1 @@
     1.4 +-Iinclude
     2.1 --- a/Makefile	Fri Aug 29 23:07:59 2014 +0300
     2.2 +++ b/Makefile	Sat Aug 30 21:46:37 2014 +0300
     2.3 @@ -3,7 +3,10 @@
     2.4  dep = $(obj:.o=.d)
     2.5  bin = vrmodel
     2.6  
     2.7 -CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl2`
     2.8 +inc = -Iinclude
     2.9 +
    2.10 +CFLAGS = -pedantic -Wall -g $(inc) `pkg-config --cflags sdl2`
    2.11 +CXXFLAGS = $(CFLAGS)
    2.12  LDFLAGS = $(libgl) `pkg-config --libs sdl2`
    2.13  
    2.14  ifeq ($(shell uname -s), Darwin)
    2.15 @@ -13,12 +16,15 @@
    2.16  endif
    2.17  
    2.18  $(bin): $(obj)
    2.19 -	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.20 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    2.21  
    2.22  -include $(dep)
    2.23  
    2.24 +%.d: %.c
    2.25 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.26 +
    2.27  %.d: %.cc
    2.28 -	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.29 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.30  
    2.31  .PHONY: clean
    2.32  clean:
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/include/proto.h	Sat Aug 30 21:46:37 2014 +0300
     3.3 @@ -0,0 +1,23 @@
     3.4 +#ifndef PROTO_H_
     3.5 +#define PROTO_H_
     3.6 +
     3.7 +#define DEF_PORT	0x6d0f
     3.8 +
     3.9 +#define MAGIC		0x6d0f
    3.10 +/* requests */
    3.11 +#define REQ_DISCOVER	1
    3.12 +#define REQ_START		2
    3.13 +#define REQ_STOP		3
    3.14 +/* responses */
    3.15 +#define RSP_OK			0
    3.16 +#define RSP_ERR			255
    3.17 +
    3.18 +
    3.19 +struct message {
    3.20 +	int magic;
    3.21 +	int type;
    3.22 +	char data[24];
    3.23 +};
    3.24 +
    3.25 +
    3.26 +#endif	/* PROTO_H_ */
     4.1 --- a/inptools/test/Makefile	Fri Aug 29 23:07:59 2014 +0300
     4.2 +++ b/inptools/test/Makefile	Sat Aug 30 21:46:37 2014 +0300
     4.3 @@ -2,7 +2,7 @@
     4.4  obj = $(src:.c=.o)
     4.5  bin = test
     4.6  
     4.7 -CFLAGS = -pedantic -Wall -g
     4.8 +CFLAGS = -pedantic -Wall -g -I../../include
     4.9  
    4.10  $(bin): $(obj)
    4.11  	$(CC) -o $@ $(obj) $(LDFLAGS)
     5.1 --- a/inptools/test/src/main.c	Fri Aug 29 23:07:59 2014 +0300
     5.2 +++ b/inptools/test/src/main.c	Sat Aug 30 21:46:37 2014 +0300
     5.3 @@ -1,73 +1,5 @@
     5.4  #include <stdio.h>
     5.5 -#include <stdlib.h>
     5.6 -#include <string.h>
     5.7 -#include <unistd.h>
     5.8 -#include <sys/types.h>
     5.9 -#include <sys/socket.h>
    5.10 -#include <arpa/inet.h>
    5.11 -#include <sys/time.h>
    5.12 -#include <sys/select.h>
    5.13 -
    5.14 -#define MAGIC		0x6d0f
    5.15 -#define DISCOVER	1
    5.16 -#define DREPLY		2
    5.17 -
    5.18 -struct message {
    5.19 -	int magic;
    5.20 -	int type;
    5.21 -	char data[24];
    5.22 -};
    5.23 -
    5.24 -int discover(struct sockaddr_in *client_sa);
    5.25 -
    5.26 -int opt_dport = 0x6d0f;	/* discover broadcast port */
    5.27 -unsigned int opt_timeout = 5000;	/* 10 sec discovery timeout */
    5.28  
    5.29  int main(int argc, char **argv)
    5.30  {
    5.31  }
    5.32 -
    5.33 -int discover(struct sockaddr_in *client_sa)
    5.34 -{
    5.35 -	int s, true = 1;
    5.36 -	struct sockaddr_in sa;
    5.37 -	unsigned int timeout = opt_timeout;
    5.38 -	struct message msg;
    5.39 -	struct timeval tv, tv_timeout;
    5.40 -
    5.41 -	if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
    5.42 -		perror("failed to create discover datagram socket");
    5.43 -		return -1;
    5.44 -	}
    5.45 -	setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true, sizeof true);
    5.46 -
    5.47 -	memset(&sa, 0, sizeof sa);
    5.48 -	sa.sin_family = AF_INET;
    5.49 -	sa.sin_port = htons(opt_dport);
    5.50 -	sa.sin_addr.s_addr = htonl(INADDR_ANY);
    5.51 -
    5.52 -	if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
    5.53 -		perror("failed to bind datagram socket");
    5.54 -		close(s);
    5.55 -		return -1;
    5.56 -	}
    5.57 -
    5.58 -	do {
    5.59 -		msg.magic = MAGIC;
    5.60 -		msg.type = DISCOVER;
    5.61 -
    5.62 -		memset(&sa, 0, sizeof sa);
    5.63 -		sa.sin_family = AF_INET;
    5.64 -		sa.sin_port = htons(opt_dport);
    5.65 -		sa.sin_addr.s_addr = 0xffffffff;
    5.66 -
    5.67 -		if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) {
    5.68 -			perror("failed to send discovery bcast dgram");
    5.69 -			close(s);
    5.70 -			return -1;
    5.71 -		}
    5.72 -	} while(0);
    5.73 -
    5.74 -	close(s);
    5.75 -	return 0;
    5.76 -}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/inpclient.cc	Sat Aug 30 21:46:37 2014 +0300
     6.3 @@ -0,0 +1,134 @@
     6.4 +#include <stdio.h>
     6.5 +#include <stdlib.h>
     6.6 +#include <string.h>
     6.7 +#include <errno.h>
     6.8 +#include <unistd.h>
     6.9 +#include <sys/types.h>
    6.10 +#include <sys/socket.h>
    6.11 +#include <arpa/inet.h>
    6.12 +#include <sys/time.h>
    6.13 +#include <sys/select.h>
    6.14 +#include "inpclient.h"
    6.15 +#include "proto.h"
    6.16 +
    6.17 +static int discover(struct sockaddr_in *srv_sa);
    6.18 +
    6.19 +static int opt_dport = DEF_PORT;	/* discover broadcast port */
    6.20 +static long opt_timeout = 5000;	/* 10 sec discovery timeout */
    6.21 +
    6.22 +static int sock = -1;
    6.23 +
    6.24 +
    6.25 +int netinp_start()
    6.26 +{
    6.27 +	struct message msg;
    6.28 +	struct sockaddr_in srv_sa;
    6.29 +
    6.30 +	if(sock == -1) {
    6.31 +		if((sock = discover(&srv_sa)) == -1) {
    6.32 +			return -1;
    6.33 +		}
    6.34 +
    6.35 +		if(connect(sock, (struct sockaddr*)&srv_sa, sizeof srv_sa) == -1) {
    6.36 +			perror("failed to connect socket");
    6.37 +			close(sock);
    6.38 +			return -1;
    6.39 +		}
    6.40 +	}
    6.41 +
    6.42 +	memset(&msg, 0, sizeof msg);
    6.43 +	msg.magic = MAGIC;
    6.44 +	msg.type = START;
    6.45 +
    6.46 +	if(send(sock, &msg, sizeof msg, 0) == -1) {
    6.47 +		perror("failed to send start command");
    6.48 +	}
    6.49 +	return 0;
    6.50 +}
    6.51 +
    6.52 +int netinp_stop()
    6.53 +{
    6.54 +}
    6.55 +
    6.56 +static int discover(struct sockaddr_in *srv_sa)
    6.57 +{
    6.58 +	int s, true_val = 1;
    6.59 +	struct sockaddr_in sa;
    6.60 +	long timeout = opt_timeout;
    6.61 +	struct message msg;
    6.62 +	struct timeval tv0, tv, tv_timeout;
    6.63 +
    6.64 +	if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
    6.65 +		perror("failed to create discover datagram socket");
    6.66 +		return -1;
    6.67 +	}
    6.68 +	setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true_val, sizeof true_val);
    6.69 +
    6.70 +	memset(&sa, 0, sizeof sa);
    6.71 +	sa.sin_family = AF_INET;
    6.72 +	sa.sin_port = htons(opt_dport);
    6.73 +	sa.sin_addr.s_addr = htonl(INADDR_ANY);
    6.74 +
    6.75 +	if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
    6.76 +		perror("failed to bind datagram socket");
    6.77 +		close(s);
    6.78 +		return -1;
    6.79 +	}
    6.80 +
    6.81 +	do {
    6.82 +		fd_set rdset;
    6.83 +
    6.84 +		msg.magic = MAGIC;
    6.85 +		msg.type = REQ_DISCOVER;
    6.86 +
    6.87 +		memset(&sa, 0, sizeof sa);
    6.88 +		sa.sin_family = AF_INET;
    6.89 +		sa.sin_port = htons(opt_dport);
    6.90 +		sa.sin_addr.s_addr = 0xffffffff;
    6.91 +
    6.92 +		if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) {
    6.93 +			perror("failed to send discovery bcast dgram");
    6.94 +			close(s);
    6.95 +			return -1;
    6.96 +		}
    6.97 +
    6.98 +		tv_timeout.tv_sec = timeout / 1000;
    6.99 +		tv_timeout.tv_usec = (timeout % 1000) * 1000;
   6.100 +
   6.101 +		FD_ZERO(&rdset);
   6.102 +		FD_SET(s, &rdset);
   6.103 +
   6.104 +		gettimeofday(&tv0, 0);
   6.105 +
   6.106 +		while(select(s + 1, &rdset, 0, 0, &tv_timeout) == -1 && errno == EINTR);
   6.107 +
   6.108 +		gettimeofday(&tv, 0);
   6.109 +		timeout -= (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
   6.110 +
   6.111 +		if(FD_ISSET(s, &rdset)) {
   6.112 +			socklen_t sa_size = sizeof *srv_sa;
   6.113 +			if(recvfrom(s, &msg, sizeof msg, 0, (struct sockaddr*)srv_sa, &sa_size) == -1) {
   6.114 +				perror("failed to receive datagram\n");
   6.115 +			}
   6.116 +			if(msg.magic == MAGIC && msg.type == RSP_OK) {
   6.117 +				printf("found input server \"%s\" at: %s:%d\n", msg.data,
   6.118 +						inet_ntoa(srv_sa->sin_addr), ntohs(srv_sa->sin_port));
   6.119 +				return s;
   6.120 +			}
   6.121 +		}
   6.122 +
   6.123 +	} while(timeout > 0);
   6.124 +
   6.125 +	close(s);
   6.126 +	return -1;
   6.127 +}
   6.128 +
   6.129 +static int send_request(int s, struct message *msg, unsigned int timeout)
   6.130 +{
   6.131 +	if(send(s, msg, sizeof *msg, 0) == -1) {
   6.132 +		perror("failed to send message");
   6.133 +		return -1;
   6.134 +	}
   6.135 +
   6.136 +	// TODO cont.
   6.137 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/inpclient.h	Sat Aug 30 21:46:37 2014 +0300
     7.3 @@ -0,0 +1,10 @@
     7.4 +#ifndef INPCLIENT_H_
     7.5 +#define INPCLIENT_H_
     7.6 +
     7.7 +int netinp_start();
     7.8 +int netinp_stop();
     7.9 +
    7.10 +int netinp_read(float *pos);
    7.11 +int netinp_reset();
    7.12 +
    7.13 +#endif	// INPCLIENT_H_