vrmodel

changeset 3:a1784a4290c2

client-side networking
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Sep 2014 05:59:31 +0300
parents be91b72ce3f9
children a32b151fb3c6
files src/inpclient.cc
diffstat 1 files changed, 74 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/src/inpclient.cc	Sat Aug 30 21:46:37 2014 +0300
     1.2 +++ b/src/inpclient.cc	Mon Sep 01 05:59:31 2014 +0300
     1.3 @@ -12,6 +12,7 @@
     1.4  #include "proto.h"
     1.5  
     1.6  static int discover(struct sockaddr_in *srv_sa);
     1.7 +static int send_request(int s, struct message *msg, long timeout);
     1.8  
     1.9  static int opt_dport = DEF_PORT;	/* discover broadcast port */
    1.10  static long opt_timeout = 5000;	/* 10 sec discovery timeout */
    1.11 @@ -38,16 +39,46 @@
    1.12  
    1.13  	memset(&msg, 0, sizeof msg);
    1.14  	msg.magic = MAGIC;
    1.15 -	msg.type = START;
    1.16 -
    1.17 -	if(send(sock, &msg, sizeof msg, 0) == -1) {
    1.18 -		perror("failed to send start command");
    1.19 -	}
    1.20 -	return 0;
    1.21 +	msg.type = REQ_START;
    1.22 +	return send_request(sock, &msg, opt_timeout);
    1.23  }
    1.24  
    1.25  int netinp_stop()
    1.26  {
    1.27 +	struct message msg;
    1.28 +
    1.29 +	if(sock == -1) {
    1.30 +		return -1;
    1.31 +	}
    1.32 +
    1.33 +	memset(&msg, 0, sizeof msg);
    1.34 +	msg.magic = MAGIC;
    1.35 +	msg.type = REQ_STOP;
    1.36 +	return send_request(sock, &msg, opt_timeout);
    1.37 +}
    1.38 +
    1.39 +int netinp_read(float *pos)
    1.40 +{
    1.41 +	struct message msg;
    1.42 +
    1.43 +	if(sock == -1) {
    1.44 +		return -1;
    1.45 +	}
    1.46 +
    1.47 +	if(recv(sock, &msg, sizeof msg, 0) == -1) {
    1.48 +		perror("netinp_read failed");
    1.49 +		return -1;
    1.50 +	}
    1.51 +
    1.52 +	pos[0] = ((float*)msg.data)[0];
    1.53 +	pos[1] = ((float*)msg.data)[1];
    1.54 +	pos[2] = ((float*)msg.data)[3];
    1.55 +	return 0;
    1.56 +}
    1.57 +
    1.58 +int netinp_reset()
    1.59 +{
    1.60 +	return -1;	// TODO
    1.61  }
    1.62  
    1.63  static int discover(struct sockaddr_in *srv_sa)
    1.64 @@ -123,12 +154,43 @@
    1.65  	return -1;
    1.66  }
    1.67  
    1.68 -static int send_request(int s, struct message *msg, unsigned int timeout)
    1.69 +static int send_request(int s, struct message *msg, long timeout)
    1.70  {
    1.71 -	if(send(s, msg, sizeof *msg, 0) == -1) {
    1.72 -		perror("failed to send message");
    1.73 -		return -1;
    1.74 -	}
    1.75 +	fd_set rdset;
    1.76 +	struct timeval tv, tv0, tv_timeout;
    1.77 +	struct message resp;
    1.78  
    1.79 -	// TODO cont.
    1.80 +	do {
    1.81 +		if(send(s, msg, sizeof *msg, 0) == -1) {
    1.82 +			perror("failed to send message");
    1.83 +			return -1;
    1.84 +		}
    1.85 +
    1.86 +		tv_timeout.tv_sec = timeout / 1000;
    1.87 +		tv_timeout.tv_usec = (timeout % 1000) * 1000;
    1.88 +
    1.89 +		FD_ZERO(&rdset);
    1.90 +		FD_SET(s, &rdset);
    1.91 +
    1.92 +		gettimeofday(&tv0, 0);
    1.93 +		while(select(s + 1, &rdset, 0, 0, &tv_timeout) == -1 && errno == EINTR);
    1.94 +		gettimeofday(&tv, 0);
    1.95 +
    1.96 +		if(FD_ISSET(s, &rdset)) {
    1.97 +			if(recv(s, &resp, sizeof resp, 0) == -1) {
    1.98 +				perror("failed to recieve message");
    1.99 +				return -1;
   1.100 +			}
   1.101 +			if(resp.magic != MAGIC) {
   1.102 +				fprintf(stderr, "got invalid response\n");
   1.103 +				return -1;
   1.104 +			}
   1.105 +			memcpy(msg, &resp, sizeof resp);
   1.106 +			return 0;
   1.107 +		}
   1.108 +
   1.109 +		timeout -= (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
   1.110 +	} while(timeout > 0);
   1.111 +
   1.112 +	return -1;
   1.113  }