source: flair-src/trunk/lib/FlairCore/src/Socket_impl.cpp@ 15

Last change on this file since 15 was 15, checked in by Bayard Gildas, 6 years ago

sources reformatted with flair-format-dir script

File size: 10.2 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 "Socket_impl.h"
18#include "Socket.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
31Socket_impl::Socket_impl(const Socket *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
39Socket_impl::Socket_impl(const Socket *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 Socket_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 self->Err("rt_pipe_create error (%s)\n", strerror(-status));
106 }
107
108// start user side thread
109#ifdef NRT_STACK_SIZE
110 // Initialize thread creation attributes
111 pthread_attr_t attr;
112 if (pthread_attr_init(&attr) != 0) {
113 self->Err("pthread_attr_init error\n");
114 }
115 if (pthread_attr_setstacksize(&attr, NRT_STACK_SIZE) != 0) {
116 self->Err("pthread_attr_setstacksize error\n");
117 }
118 if (pthread_create(&user_thread, &attr, user, (void *)this) < 0) {
119 self->Err("pthread_create error\n");
120 }
121 if (pthread_attr_destroy(&attr) != 0) {
122 self->Err("pthread_attr_destroy error\n");
123 }
124#else // NRT_STACK_SIZE
125 if (pthread_create(&user_thread, NULL, user, (void *)this) < 0) {
126 self->Err("pthread_create error\n");
127 }
128#endif // NRT_STACK_SIZE
129#endif //__XENO__
130
131 if (address != "") {
132 struct hostent *hostinfo;
133 hostinfo = gethostbyname(address.c_str());
134 if (hostinfo == NULL) {
135 self->Err("gethostbyname\n");
136 }
137
138 sock_in.sin_addr = *(in_addr *)hostinfo->h_addr;
139 sock_in.sin_port = htons(port);
140 sock_in.sin_family = AF_INET;
141 }
142}
143
144Socket_impl::~Socket_impl() {
145#ifdef __XENO__
146 is_running = false;
147
148 pthread_join(user_thread, NULL);
149 int status = rt_pipe_delete(&pipe);
150 if (status != 0)
151 self->Err("rt_pipe_delete error (%s)\n", strerror(-status));
152
153#endif
154 close(fd);
155}
156
157void Socket_impl::SendMessage(const char *src, size_t src_len) {
158 ssize_t written;
159 string to_send;
160
161 if (broadcast == true) {
162 to_send = getFrameworkManager()->ObjectName() + ":" + string(src, src_len);
163 src_len = to_send.size();
164 src = (char *)to_send.c_str();
165 }
166
167#ifdef __XENO__
168 // Printf("send pipe %s\n",src);
169 written = rt_pipe_write(&pipe, src, src_len, P_NORMAL);
170
171 if (written < 0) {
172 self->Err("rt_pipe_write error (%s)\n", strerror(-written));
173 } else if (written != (ssize_t)src_len) {
174 self->Err("rt_pipe_write error %i/%i\n", written, to_send.size());
175 }
176#else
177 written =
178 sendto(fd, src, src_len, 0, (struct sockaddr *)&sock_in, sizeof(sock_in));
179
180 if (written != (ssize_t)src_len) {
181 self->Err("sendto error\n");
182 }
183#endif
184}
185
186void Socket_impl::SendMessage(string message) {
187 ssize_t written;
188
189 if (broadcast == true)
190 message = self->Parent()->ObjectName() + ":" + message;
191// Printf("SendMessage %s\n",message.c_str());
192#ifdef __XENO__
193 written = rt_pipe_write(&pipe, message.c_str(), message.size(), P_NORMAL);
194
195 if (written < 0) {
196 self->Err("rt_pipe_write error (%s)\n", strerror(-written));
197 } else if (written != (ssize_t)message.size()) {
198 self->Err("rt_pipe_write error %i/%i\n", written, message.size());
199 }
200#else
201 written = sendto(fd, message.c_str(), message.size(), 0,
202 (struct sockaddr *)&sock_in, sizeof(sock_in));
203 if (written != (ssize_t)message.size()) {
204 self->Err("sendto error\n");
205 }
206
207#endif
208}
209
210ssize_t Socket_impl::RecvMessage(char *msg, size_t msg_len, Time timeout,
211 char *src, size_t *src_len) {
212 ssize_t nb_read;
213 char buffer[128];
214#ifdef __XENO__
215 nb_read = rt_pipe_read(&pipe, &buffer, sizeof(buffer), timeout);
216#else
217 socklen_t sinsize = sizeof(sock_in);
218 struct timeval tv;
219
220 if (timeout != TIME_NONBLOCK) {
221 int attr = fcntl(fd, F_GETFL, 0);
222 fcntl(fd, F_SETFL, attr & (~O_NONBLOCK));
223
224 tv.tv_sec = timeout / 1000000000;
225 timeout = timeout - (timeout / 1000000000) * 1000000000;
226 tv.tv_usec = timeout / 1000;
227 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
228 self->Err("setsockopt SO_RCVTIMEO failed\n");
229 }
230 } else {
231 fcntl(fd, F_SETFL, O_NONBLOCK);
232 }
233
234 if (broadcast == false) {
235 nb_read =
236 recvfrom(fd, buffer, sizeof(buffer), 0, (sockaddr *)&sock_in, &sinsize);
237 } else {
238 nb_read = recvfrom(fd, buffer, sizeof(buffer), 0, NULL, NULL);
239 }
240#endif
241 if (nb_read <= 0) {
242 return nb_read;
243 } else {
244 // printf("%s\n",buffer);
245 if (broadcast == true) {
246 int index = -1;
247 for (int i = 0; i < nb_read; i++) {
248 if (buffer[i] == ':') {
249 index = i;
250 break;
251 }
252 }
253 if (index < 0) {
254 self->Warn("Malformed message\n");
255 return -1;
256 } else if (src_len != NULL && src != NULL) {
257 if (index + 1 > (int)(*src_len) &&
258 src != NULL) { //+1 pour inserer un 0)
259 self->Warn("insufficent src size\n");
260 return -1;
261 }
262 } else if (nb_read - index - 1 + 1 >
263 (int)msg_len) { //+1 pour inserer un 0
264 self->Warn("insufficent msg size (%i/%i)\n", nb_read - index - 1 + 1,
265 msg_len);
266 return -1;
267 }
268 if (src != NULL) {
269 memcpy(src, buffer, index);
270 src[index] = 0;
271 }
272 memcpy(msg, &buffer[index + 1], nb_read - index - 1);
273 msg[nb_read - index - 1] = 0;
274 return nb_read - index;
275 } else {
276 if (nb_read > (int)msg_len) {
277 self->Warn("insufficent msg size (%i/%i)\n", nb_read, msg_len);
278 return -1;
279 }
280 memcpy(msg, buffer, nb_read);
281 return nb_read;
282 }
283 }
284}
285
286#ifdef __XENO__
287void *Socket_impl::user(void *arg) {
288 Socket_impl *caller = (Socket_impl *)arg;
289 int pipe_fd = -1;
290 string devname;
291
292 devname = NRT_PIPE_PATH + getFrameworkManager()->ObjectName() + "-" +
293 caller->self->ObjectName() + "-pipe";
294 while (pipe_fd < 0) {
295 pipe_fd = open(devname.c_str(), O_RDWR);
296 if (pipe_fd < 0 && errno != ENOENT)
297 caller->self->Err("open pipe_fd error (%s)\n", strerror(-errno));
298 usleep(1000);
299 }
300
301 while (caller->is_running == true) {
302 fd_set set;
303 struct timeval timeout;
304 int rv;
305
306 FD_ZERO(&set); // clear the set
307 FD_SET(caller->fd, &set); // add our file descriptor to the set
308 FD_SET(pipe_fd, &set); // add our file descriptor to the set
309
310 timeout.tv_sec = 0;
311 timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
312 rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
313
314 if (rv == -1) {
315 caller->self->Err("select error\n"); // an error occured
316 } else if (rv == 0) {
317 // printf("timeout\n");
318 } else {
319 ssize_t nb_read, nb_write;
320 char buffer[1024];
321
322 if (FD_ISSET(caller->fd, &set)) {
323 socklen_t sinsize = sizeof(caller->sock_in);
324 if (caller->broadcast == false) {
325 nb_read = recvfrom(caller->fd, buffer, sizeof(buffer), 0,
326 (sockaddr *)&(caller->sock_in), &sinsize);
327 } else {
328 nb_read = recvfrom(caller->fd, buffer, sizeof(buffer), 0, NULL, NULL);
329 }
330 if (nb_read < 0) {
331 caller->self->Err("recvfrom error\n");
332 }
333 // printf("user %s\n",buffer);
334 // on ne garde que les messages venant pas de soi meme
335 if (caller->broadcast == false ||
336 (caller->broadcast == true &&
337 getFrameworkManager()->ObjectName().compare(
338 0, getFrameworkManager()->ObjectName().size(), buffer,
339 getFrameworkManager()->ObjectName().size()) != 0)) {
340 nb_write = write(pipe_fd, buffer, nb_read);
341 if (nb_write != nb_read) {
342 caller->self->Err("write error\n");
343 }
344 } else {
345 // printf("self %s\n",buffer);
346 }
347 }
348 if (FD_ISSET(pipe_fd, &set)) {
349 nb_read = read(pipe_fd, buffer, sizeof(buffer));
350 // printf("read pipe %i %s\n",nb_read,buffer);
351 if (nb_read > 0) {
352 // printf("send %s\n",buffer);
353 nb_write = sendto(caller->fd, buffer, nb_read, 0,
354 (struct sockaddr *)&(caller->sock_in),
355 sizeof(caller->sock_in));
356 if (nb_write != nb_read) {
357 caller->self->Err("sendto error\n");
358 }
359 }
360 }
361 }
362 }
363
364 close(pipe_fd);
365 pthread_exit(0);
366}
367#endif
Note: See TracBrowser for help on using the repository browser.