source: flair-src/branches/sanscv/lib/FlairCore/src/UdpSocket_impl.cpp@ 324

Last change on this file since 324 was 324, checked in by Sanahuja Guillaume, 5 years ago

removing opencv dependency

File size: 10.7 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2013/11/17
6// filename: Socket_impl.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: classe pour une Socket_impl
14//
15//
16/*********************************************************************/
17#include "UdpSocket_impl.h"
18#include "UdpSocket.h"
19#include "FrameworkManager.h"
20#include <fcntl.h>
21#include <cstdlib>
22#include <unistd.h>
23#include <string.h>
24#ifdef __XENO__
25#include "config.h"
26#endif
27
28using std::string;
29using namespace flair::core;
30
31UdpSocket_impl::UdpSocket_impl(const UdpSocket *self, string name, uint16_t port) {
32 this->self = self;
33 this->port = port;
34 this->address = "";
35 this->broadcast = false;
36 Init();
37}
38
39UdpSocket_impl::UdpSocket_impl(const UdpSocket *self, string name, string address,
40 bool broadcast) {
41 this->self = self;
42 int pos = address.find(":");
43 this->address = address.substr(0, pos);
44 port = atoi(address.substr(pos + 1).c_str());
45 this->broadcast = broadcast;
46
47 if (pos == 0 || address == "") {
48 self->Err("address %s is not correct\n", address.c_str());
49 }
50 Init();
51}
52
53void UdpSocket_impl::Init(void) {
54 int yes = 1;
55
56 fd = socket(AF_INET, SOCK_DGRAM, 0); // UDP
57
58 if (broadcast == true) {
59 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int)) != 0)
60 self->Err("Setsockopt error\n");
61 }
62
63 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
64 self->Err("Setsockopt error\n");
65
66 if (address == "" || broadcast == true) {
67 sockaddr_in sin = {0};
68
69 if (broadcast == true) {
70 struct hostent *hostinfo;
71
72 hostinfo = gethostbyname(this->address.c_str());
73 if (hostinfo == NULL) {
74 self->Err("hostinfo error\n");
75 }
76 sin.sin_addr = *(in_addr *)hostinfo->h_addr;
77 } else {
78 sin.sin_addr.s_addr = INADDR_ANY;
79 }
80
81 sin.sin_port = htons(port);
82 sin.sin_family = AF_INET;
83 if (bind(fd, (sockaddr *)&sin, sizeof sin) == -1) {
84 self->Err("bind error\n");
85 }
86 }
87
88#ifdef __XENO__
89 string tmp_name;
90 int status;
91 is_running = true;
92
93 // pipe
94 tmp_name =
95 getFrameworkManager()->ObjectName() + "-" + self->ObjectName() + "-pipe";
96 // xenomai limitation
97 if (tmp_name.size() > 31)
98 self->Err("rt_pipe_create error (%s is too long)\n", tmp_name.c_str());
99#ifdef RT_PIPE_SIZE
100 status = rt_pipe_create(&pipe, tmp_name.c_str(), P_MINOR_AUTO, RT_PIPE_SIZE);
101#else
102 status = rt_pipe_create(&pipe, tmp_name.c_str(), P_MINOR_AUTO, 0);
103#endif
104 if (status != 0) {
105 char errorMsg[256];
106 self->Err("rt_pipe_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
107 }
108
109// start user side thread
110#ifdef NRT_STACK_SIZE
111 // Initialize thread creation attributes
112 pthread_attr_t attr;
113 if (pthread_attr_init(&attr) != 0) {
114 self->Err("pthread_attr_init error\n");
115 }
116 if (pthread_attr_setstacksize(&attr, NRT_STACK_SIZE) != 0) {
117 self->Err("pthread_attr_setstacksize error\n");
118 }
119 if (pthread_create(&user_thread, &attr, user, (void *)this) < 0) {
120 self->Err("pthread_create error\n");
121 }
122 if (pthread_attr_destroy(&attr) != 0) {
123 self->Err("pthread_attr_destroy error\n");
124 }
125#else // NRT_STACK_SIZE
126 if (pthread_create(&user_thread, NULL, user, (void *)this) < 0) {
127 self->Err("pthread_create error\n");
128 }
129#endif // NRT_STACK_SIZE
130#endif //__XENO__
131
132 if (address != "") {
133 struct hostent *hostinfo;
134 hostinfo = gethostbyname(address.c_str());
135 if (hostinfo == NULL) {
136 self->Err("gethostbyname\n");
137 }
138
139 sock_in.sin_addr = *(in_addr *)hostinfo->h_addr;
140 sock_in.sin_port = htons(port);
141 sock_in.sin_family = AF_INET;
142 }
143}
144
145UdpSocket_impl::~UdpSocket_impl() {
146#ifdef __XENO__
147 is_running = false;
148
149 pthread_join(user_thread, NULL);
150 int status = rt_pipe_delete(&pipe);
151 if (status != 0) {
152 char errorMsg[256];
153 self->Err("rt_pipe_delete error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
154 }
155#endif
156 close(fd);
157}
158
159void UdpSocket_impl::SendMessage(const char *src, size_t src_len) {
160 ssize_t written;
161 string to_send;
162
163 if (broadcast == true) {
164 to_send = getFrameworkManager()->ObjectName() + ":" + string(src, src_len);
165 src_len = to_send.size();
166 src = (char *)to_send.c_str();
167 }
168
169#ifdef __XENO__
170 // Printf("send pipe %s\n",src);
171 written = rt_pipe_write(&pipe, src, src_len, P_NORMAL);
172
173 if (written < 0) {
174 char errorMsg[256];
175 self->Err("rt_pipe_write error (%s)\n", strerror_r(-written, errorMsg, sizeof(errorMsg)));
176 } else if (written != (ssize_t)src_len) {
177 self->Err("rt_pipe_write error %i/%i\n", written, to_send.size());
178 }
179#else
180 written =
181 sendto(fd, src, src_len, 0, (struct sockaddr *)&sock_in, sizeof(sock_in));
182 if(written==-1) {
183 char errorMsg[256];
184 self->Err("sendto error (%s)\n",strerror_r(errno, errorMsg, sizeof(errorMsg)));
185 } else if (written != (ssize_t)src_len) {
186 self->Err("sendto error %i/%i\n",written,src_len);
187 }
188#endif
189}
190
191void UdpSocket_impl::SendMessage(string message) {
192 ssize_t written;
193
194 if (broadcast == true)
195 message = self->Parent()->ObjectName() + ":" + message;
196// Printf("SendMessage %s\n",message.c_str());
197#ifdef __XENO__
198 written = rt_pipe_write(&pipe, message.c_str(), message.size(), P_NORMAL);
199
200 if (written < 0) {
201 char errorMsg[256];
202 self->Err("rt_pipe_write error (%s)\n", strerror_r(-written, errorMsg, sizeof(errorMsg)));
203 } else if (written != (ssize_t)message.size()) {
204 self->Err("rt_pipe_write error %i/%i\n", written, message.size());
205 }
206#else
207 written = sendto(fd, message.c_str(), message.size(), 0,
208 (struct sockaddr *)&sock_in, sizeof(sock_in));
209 if (written != (ssize_t)message.size()) {
210 self->Err("sendto error\n");
211 }
212
213#endif
214}
215
216ssize_t UdpSocket_impl::RecvMessage(char *msg, size_t msg_len, Time timeout,
217 char *src, size_t *src_len) {
218 ssize_t nb_read;
219 char buffer[128];
220#ifdef __XENO__
221 nb_read = rt_pipe_read(&pipe, &buffer, sizeof(buffer), timeout);
222#else
223 socklen_t sinsize = sizeof(sock_in);
224 struct timeval tv;
225
226 if (timeout != TIME_NONBLOCK) {
227 int attr = fcntl(fd, F_GETFL, 0);
228 fcntl(fd, F_SETFL, attr & (~O_NONBLOCK));
229
230 tv.tv_sec = timeout / 1000000000;
231 timeout = timeout - (timeout / 1000000000) * 1000000000;
232 tv.tv_usec = timeout / 1000;
233 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
234 self->Err("setsockopt SO_RCVTIMEO failed\n");
235 }
236 } else {
237 fcntl(fd, F_SETFL, O_NONBLOCK);
238 }
239
240 if (broadcast == false) {
241 nb_read =
242 recvfrom(fd, buffer, sizeof(buffer), 0, (sockaddr *)&sock_in, &sinsize);
243 } else {
244 nb_read = recvfrom(fd, buffer, sizeof(buffer), 0, NULL, NULL);
245 }
246#endif
247 if (nb_read <= 0) {
248 return nb_read;
249 } else {
250 // printf("%s\n",buffer);
251 if (broadcast == true) {
252 int index = -1;
253 for (int i = 0; i < nb_read; i++) {
254 if (buffer[i] == ':') {
255 index = i;
256 break;
257 }
258 }
259 if (index < 0) {
260 self->Warn("Malformed message\n");
261 return -1;
262 } else if (src_len != NULL && src != NULL) {
263 if (index + 1 > (int)(*src_len) &&
264 src != NULL) { //+1 pour inserer un 0)
265 self->Warn("insufficent src size\n");
266 return -1;
267 }
268 } else if (nb_read - index - 1 + 1 >
269 (int)msg_len) { //+1 pour inserer un 0
270 self->Warn("insufficent msg size (%i/%i)\n", nb_read - index - 1 + 1,
271 msg_len);
272 return -1;
273 }
274 if (src != NULL) {
275 memcpy(src, buffer, index);
276 src[index] = 0;
277 }
278 memcpy(msg, &buffer[index + 1], nb_read - index - 1);
279 msg[nb_read - index - 1] = 0;
280 return nb_read - index;
281 } else {
282 if (nb_read > (int)msg_len) {
283 self->Warn("insufficent msg size (%i/%i)\n", nb_read, msg_len);
284 return -1;
285 }
286 memcpy(msg, buffer, nb_read);
287 return nb_read;
288 }
289 }
290}
291
292#ifdef __XENO__
293void *UdpSocket_impl::user(void *arg) {
294 UdpSocket_impl *caller = (UdpSocket_impl *)arg;
295 int pipe_fd = -1;
296 string devname;
297
298 devname = NRT_PIPE_PATH + getFrameworkManager()->ObjectName() + "-" +
299 caller->self->ObjectName() + "-pipe";
300 while (pipe_fd < 0) {
301 pipe_fd = open(devname.c_str(), O_RDWR);
302 if (pipe_fd < 0 && errno != ENOENT) {
303 char errorMsg[256];
304 caller->self->Err("open pipe_fd error (%s)\n", strerror_r(errno, errorMsg, sizeof(errorMsg)));
305 }
306 usleep(1000);
307 }
308
309 while (caller->is_running == true) {
310 fd_set set;
311 struct timeval timeout;
312 int rv;
313
314 FD_ZERO(&set); // clear the set
315 FD_SET(caller->fd, &set); // add our file descriptor to the set
316 FD_SET(pipe_fd, &set); // add our file descriptor to the set
317
318 timeout.tv_sec = 0;
319 timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
320 rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
321
322 if (rv == -1) {
323 caller->self->Err("select error\n"); // an error occured
324 } else if (rv == 0) {
325 // printf("timeout\n");
326 } else {
327 ssize_t nb_read, nb_write;
328 char buffer[1024];
329
330 if (FD_ISSET(caller->fd, &set)) {
331 socklen_t sinsize = sizeof(caller->sock_in);
332 if (caller->broadcast == false) {
333 nb_read = recvfrom(caller->fd, buffer, sizeof(buffer), 0,
334 (sockaddr *)&(caller->sock_in), &sinsize);
335 } else {
336 nb_read = recvfrom(caller->fd, buffer, sizeof(buffer), 0, NULL, NULL);
337 }
338 if (nb_read < 0) {
339 caller->self->Err("recvfrom error\n");
340 }
341 // printf("user %s\n",buffer);
342 // on ne garde que les messages venant pas de soi meme
343 if (caller->broadcast == false ||
344 (caller->broadcast == true &&
345 getFrameworkManager()->ObjectName().compare(
346 0, getFrameworkManager()->ObjectName().size(), buffer,
347 getFrameworkManager()->ObjectName().size()) != 0)) {
348 nb_write = write(pipe_fd, buffer, nb_read);
349 if (nb_write != nb_read) {
350 caller->self->Err("write error\n");
351 }
352 } else {
353 // printf("self %s\n",buffer);
354 }
355 }
356 if (FD_ISSET(pipe_fd, &set)) {
357 nb_read = read(pipe_fd, buffer, sizeof(buffer));
358 // printf("read pipe %i %s\n",nb_read,buffer);
359 if (nb_read > 0) {
360 // printf("send %s\n",buffer);
361 nb_write = sendto(caller->fd, buffer, nb_read, 0,
362 (struct sockaddr *)&(caller->sock_in),
363 sizeof(caller->sock_in));
364 if (nb_write != nb_read) {
365 caller->self->Err("sendto error\n");
366 }
367 }
368 }
369 }
370 }
371
372 close(pipe_fd);
373 pthread_exit(0);
374}
375#endif
Note: See TracBrowser for help on using the repository browser.