vrmodel

view src/inpclient.cc @ 5:9e260c091f75

wrote some server code in the test input tool
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 15 Sep 2014 10:57:31 +0300
parents a32b151fb3c6
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <arpa/inet.h>
9 #include <sys/time.h>
10 #include <sys/select.h>
11 #include "inpclient.h"
12 #include "proto.h"
14 static int discover(struct sockaddr_in *srv_sa);
15 static int send_request(int s, struct message *msg, long timeout);
17 static int opt_dport = DEF_PORT; /* discover broadcast port */
18 static long opt_timeout = 5000; /* 10 sec discovery timeout */
20 static int sock = -1;
23 int netinp_start()
24 {
25 struct message msg;
26 struct sockaddr_in srv_sa;
28 if(sock == -1) {
29 if((sock = discover(&srv_sa)) == -1) {
30 return -1;
31 }
33 if(connect(sock, (struct sockaddr*)&srv_sa, sizeof srv_sa) == -1) {
34 perror("failed to connect socket");
35 close(sock);
36 return -1;
37 }
38 }
40 memset(&msg, 0, sizeof msg);
41 msg.magic = MAGIC;
42 msg.type = REQ_START;
43 if(send_request(sock, &msg, opt_timeout) == -1) {
44 return -1;
45 }
46 return msg.magic == MAGIC && msg.type == RSP_OK ? 0 : -1;
47 }
49 int netinp_stop()
50 {
51 struct message msg;
53 if(sock == -1) {
54 return -1;
55 }
57 memset(&msg, 0, sizeof msg);
58 msg.magic = MAGIC;
59 msg.type = REQ_STOP;
61 if(send_request(sock, &msg, opt_timeout) == -1) {
62 return -1;
63 }
64 return msg.magic == MAGIC && msg.type == RSP_OK ? 0 : -1;
65 }
67 int netinp_read(float *pos, unsigned int *bnmask)
68 {
69 struct message msg;
71 if(sock == -1) {
72 return -1;
73 }
75 if(recv(sock, &msg, sizeof msg, 0) == -1) {
76 perror("netinp_read failed");
77 return -1;
78 }
80 if(msg.magic != MAGIC || msg.type != MAGIC) {
81 fprintf(stderr, "not enough magic in the message\n");
82 return -1;
83 }
85 pos[0] = msg.data.event.x;
86 pos[1] = msg.data.event.y;
87 pos[2] = msg.data.event.z;
88 *bnmask = msg.data.event.bnmask;
89 return 0;
90 }
92 int netinp_reset()
93 {
94 struct message msg;
96 if(sock == -1) {
97 return -1;
98 }
100 memset(&msg, 0, sizeof msg);
101 msg.magic = MAGIC;
102 msg.type = REQ_STOP;
104 if(send_request(sock, &msg, opt_timeout) == -1) {
105 return -1;
106 }
107 return msg.magic == MAGIC && msg.type == RSP_OK ? 0 : -1;
108 }
110 static int discover(struct sockaddr_in *srv_sa)
111 {
112 int s, true_val = 1;
113 struct sockaddr_in sa;
114 long timeout = opt_timeout;
115 struct message msg;
116 struct timeval tv0, tv, tv_timeout;
118 if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
119 perror("failed to create discover datagram socket");
120 return -1;
121 }
122 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true_val, sizeof true_val);
124 do {
125 fd_set rdset;
127 msg.magic = MAGIC;
128 msg.type = REQ_DISCOVER;
130 memset(&sa, 0, sizeof sa);
131 sa.sin_family = AF_INET;
132 sa.sin_port = htons(opt_dport);
133 sa.sin_addr.s_addr = 0xffffffff;
135 if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)&sa, sizeof sa) == -1) {
136 perror("failed to send discovery bcast dgram");
137 close(s);
138 return -1;
139 }
141 tv_timeout.tv_sec = timeout / 1000;
142 tv_timeout.tv_usec = (timeout % 1000) * 1000;
144 FD_ZERO(&rdset);
145 FD_SET(s, &rdset);
147 gettimeofday(&tv0, 0);
149 while(select(s + 1, &rdset, 0, 0, &tv_timeout) == -1 && errno == EINTR);
151 gettimeofday(&tv, 0);
152 timeout -= (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
154 if(FD_ISSET(s, &rdset)) {
155 socklen_t sa_size = sizeof *srv_sa;
156 if(recvfrom(s, &msg, sizeof msg, 0, (struct sockaddr*)srv_sa, &sa_size) == -1) {
157 perror("failed to receive datagram\n");
158 }
159 if(msg.magic == MAGIC && msg.type == RSP_OK) {
160 printf("found input server \"%s\" at: %s:%d\n", msg.data.raw,
161 inet_ntoa(srv_sa->sin_addr), ntohs(srv_sa->sin_port));
162 return s;
163 }
164 }
166 } while(timeout > 0);
168 close(s);
169 return -1;
170 }
172 static int send_request(int s, struct message *msg, long timeout)
173 {
174 fd_set rdset;
175 struct timeval tv, tv0, tv_timeout;
176 struct message resp;
178 do {
179 if(send(s, msg, sizeof *msg, 0) == -1) {
180 perror("failed to send message");
181 return -1;
182 }
184 tv_timeout.tv_sec = timeout / 1000;
185 tv_timeout.tv_usec = (timeout % 1000) * 1000;
187 FD_ZERO(&rdset);
188 FD_SET(s, &rdset);
190 gettimeofday(&tv0, 0);
191 while(select(s + 1, &rdset, 0, 0, &tv_timeout) == -1 && errno == EINTR);
192 gettimeofday(&tv, 0);
194 if(FD_ISSET(s, &rdset)) {
195 if(recv(s, &resp, sizeof resp, 0) == -1) {
196 perror("failed to recieve message");
197 return -1;
198 }
199 if(resp.magic != MAGIC) {
200 fprintf(stderr, "got invalid response\n");
201 return -1;
202 }
203 memcpy(msg, &resp, sizeof resp);
204 return 0;
205 }
207 timeout -= (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
208 } while(timeout > 0);
210 return -1;
211 }