[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 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 | /*********************************************************************/
|
---|
[178] | 17 | #include "UdpSocket_impl.h"
|
---|
| 18 | #include "UdpSocket.h"
|
---|
[2] | 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 |
|
---|
| 28 | using std::string;
|
---|
| 29 | using namespace flair::core;
|
---|
| 30 |
|
---|
[178] | 31 | UdpSocket_impl::UdpSocket_impl(const UdpSocket *self, string name, uint16_t port) {
|
---|
[15] | 32 | this->self = self;
|
---|
| 33 | this->port = port;
|
---|
| 34 | this->address = "";
|
---|
| 35 | this->broadcast = false;
|
---|
| 36 | Init();
|
---|
[2] | 37 | }
|
---|
| 38 |
|
---|
[178] | 39 | UdpSocket_impl::UdpSocket_impl(const UdpSocket *self, string name, string address,
|
---|
[15] | 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;
|
---|
[2] | 46 |
|
---|
[15] | 47 | if (pos == 0 || address == "") {
|
---|
| 48 | self->Err("address %s is not correct\n", address.c_str());
|
---|
| 49 | }
|
---|
| 50 | Init();
|
---|
[2] | 51 | }
|
---|
| 52 |
|
---|
[178] | 53 | void UdpSocket_impl::Init(void) {
|
---|
[15] | 54 | int yes = 1;
|
---|
[2] | 55 |
|
---|
[15] | 56 | fd = socket(AF_INET, SOCK_DGRAM, 0); // UDP
|
---|
[2] | 57 |
|
---|
[15] | 58 | if (broadcast == true) {
|
---|
| 59 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int)) != 0)
|
---|
| 60 | self->Err("Setsockopt error\n");
|
---|
| 61 | }
|
---|
[2] | 62 |
|
---|
[15] | 63 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
|
---|
| 64 | self->Err("Setsockopt error\n");
|
---|
[2] | 65 |
|
---|
[15] | 66 | if (address == "" || broadcast == true) {
|
---|
| 67 | sockaddr_in sin = {0};
|
---|
[2] | 68 |
|
---|
[15] | 69 | if (broadcast == true) {
|
---|
| 70 | struct hostent *hostinfo;
|
---|
[2] | 71 |
|
---|
[15] | 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 | }
|
---|
[2] | 80 |
|
---|
[15] | 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");
|
---|
[2] | 85 | }
|
---|
[15] | 86 | }
|
---|
[2] | 87 |
|
---|
| 88 | #ifdef __XENO__
|
---|
[15] | 89 | string tmp_name;
|
---|
| 90 | int status;
|
---|
| 91 | is_running = true;
|
---|
[2] | 92 |
|
---|
[15] | 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());
|
---|
[2] | 99 | #ifdef RT_PIPE_SIZE
|
---|
[15] | 100 | status = rt_pipe_create(&pipe, tmp_name.c_str(), P_MINOR_AUTO, RT_PIPE_SIZE);
|
---|
[2] | 101 | #else
|
---|
[15] | 102 | status = rt_pipe_create(&pipe, tmp_name.c_str(), P_MINOR_AUTO, 0);
|
---|
[2] | 103 | #endif
|
---|
[15] | 104 | if (status != 0) {
|
---|
[133] | 105 | char errorMsg[256];
|
---|
| 106 | self->Err("rt_pipe_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 107 | }
|
---|
[2] | 108 |
|
---|
[15] | 109 | // start user side thread
|
---|
[2] | 110 | #ifdef NRT_STACK_SIZE
|
---|
[15] | 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
|
---|
[2] | 130 | #endif //__XENO__
|
---|
| 131 |
|
---|
[15] | 132 | if (address != "") {
|
---|
| 133 | struct hostent *hostinfo;
|
---|
| 134 | hostinfo = gethostbyname(address.c_str());
|
---|
| 135 | if (hostinfo == NULL) {
|
---|
| 136 | self->Err("gethostbyname\n");
|
---|
| 137 | }
|
---|
[2] | 138 |
|
---|
[15] | 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 | }
|
---|
[2] | 143 | }
|
---|
| 144 |
|
---|
[178] | 145 | UdpSocket_impl::~UdpSocket_impl() {
|
---|
[2] | 146 | #ifdef __XENO__
|
---|
[15] | 147 | is_running = false;
|
---|
[2] | 148 |
|
---|
[15] | 149 | pthread_join(user_thread, NULL);
|
---|
| 150 | int status = rt_pipe_delete(&pipe);
|
---|
[133] | 151 | if (status != 0) {
|
---|
| 152 | char errorMsg[256];
|
---|
| 153 | self->Err("rt_pipe_delete error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 154 | }
|
---|
[2] | 155 | #endif
|
---|
[15] | 156 | close(fd);
|
---|
[2] | 157 | }
|
---|
| 158 |
|
---|
[178] | 159 | void UdpSocket_impl::SendMessage(const char *src, size_t src_len) {
|
---|
[15] | 160 | ssize_t written;
|
---|
| 161 | string to_send;
|
---|
[133] | 162 | char errorMsg[256];
|
---|
[2] | 163 |
|
---|
[15] | 164 | if (broadcast == true) {
|
---|
| 165 | to_send = getFrameworkManager()->ObjectName() + ":" + string(src, src_len);
|
---|
| 166 | src_len = to_send.size();
|
---|
| 167 | src = (char *)to_send.c_str();
|
---|
| 168 | }
|
---|
[2] | 169 |
|
---|
| 170 | #ifdef __XENO__
|
---|
[15] | 171 | // Printf("send pipe %s\n",src);
|
---|
| 172 | written = rt_pipe_write(&pipe, src, src_len, P_NORMAL);
|
---|
[2] | 173 |
|
---|
[15] | 174 | if (written < 0) {
|
---|
[133] | 175 | self->Err("rt_pipe_write error (%s)\n", strerror_r(-written, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 176 | } else if (written != (ssize_t)src_len) {
|
---|
| 177 | self->Err("rt_pipe_write error %i/%i\n", written, to_send.size());
|
---|
| 178 | }
|
---|
[2] | 179 | #else
|
---|
[15] | 180 | written =
|
---|
| 181 | sendto(fd, src, src_len, 0, (struct sockaddr *)&sock_in, sizeof(sock_in));
|
---|
[93] | 182 | if(written==-1) {
|
---|
[133] | 183 | char errorMsg[256];
|
---|
| 184 | self->Err("sendto error (%s)\n",strerror_r(errno, errorMsg, sizeof(errorMsg)));
|
---|
[93] | 185 | } else if (written != (ssize_t)src_len) {
|
---|
[92] | 186 | self->Err("sendto error %i/%i\n",written,src_len);
|
---|
[15] | 187 | }
|
---|
[2] | 188 | #endif
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[178] | 191 | void UdpSocket_impl::SendMessage(string message) {
|
---|
[15] | 192 | ssize_t written;
|
---|
[2] | 193 |
|
---|
[15] | 194 | if (broadcast == true)
|
---|
| 195 | message = self->Parent()->ObjectName() + ":" + message;
|
---|
| 196 | // Printf("SendMessage %s\n",message.c_str());
|
---|
[2] | 197 | #ifdef __XENO__
|
---|
[15] | 198 | written = rt_pipe_write(&pipe, message.c_str(), message.size(), P_NORMAL);
|
---|
[2] | 199 |
|
---|
[15] | 200 | if (written < 0) {
|
---|
[133] | 201 | char errorMsg[256];
|
---|
| 202 | self->Err("rt_pipe_write error (%s)\n", strerror_r(-written, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 203 | } else if (written != (ssize_t)message.size()) {
|
---|
| 204 | self->Err("rt_pipe_write error %i/%i\n", written, message.size());
|
---|
| 205 | }
|
---|
[2] | 206 | #else
|
---|
[15] | 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 | }
|
---|
[2] | 212 |
|
---|
| 213 | #endif
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[178] | 216 | ssize_t UdpSocket_impl::RecvMessage(char *msg, size_t msg_len, Time timeout,
|
---|
[15] | 217 | char *src, size_t *src_len) {
|
---|
| 218 | ssize_t nb_read;
|
---|
| 219 | char buffer[128];
|
---|
[2] | 220 | #ifdef __XENO__
|
---|
[15] | 221 | nb_read = rt_pipe_read(&pipe, &buffer, sizeof(buffer), timeout);
|
---|
[2] | 222 | #else
|
---|
[15] | 223 | socklen_t sinsize = sizeof(sock_in);
|
---|
| 224 | struct timeval tv;
|
---|
[2] | 225 |
|
---|
[15] | 226 | if (timeout != TIME_NONBLOCK) {
|
---|
| 227 | int attr = fcntl(fd, F_GETFL, 0);
|
---|
| 228 | fcntl(fd, F_SETFL, attr & (~O_NONBLOCK));
|
---|
[2] | 229 |
|
---|
[15] | 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");
|
---|
[2] | 235 | }
|
---|
[15] | 236 | } else {
|
---|
| 237 | fcntl(fd, F_SETFL, O_NONBLOCK);
|
---|
| 238 | }
|
---|
[2] | 239 |
|
---|
[15] | 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 | }
|
---|
[2] | 246 | #endif
|
---|
[15] | 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;
|
---|
[2] | 281 | } else {
|
---|
[15] | 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;
|
---|
[2] | 288 | }
|
---|
[15] | 289 | }
|
---|
[2] | 290 | }
|
---|
| 291 |
|
---|
| 292 | #ifdef __XENO__
|
---|
[178] | 293 | void *UdpSocket_impl::user(void *arg) {
|
---|
| 294 | UdpSocket_impl *caller = (UdpSocket_impl *)arg;
|
---|
[15] | 295 | int pipe_fd = -1;
|
---|
| 296 | string devname;
|
---|
[2] | 297 |
|
---|
[15] | 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);
|
---|
[133] | 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 | }
|
---|
[15] | 306 | usleep(1000);
|
---|
| 307 | }
|
---|
[2] | 308 |
|
---|
[15] | 309 | while (caller->is_running == true) {
|
---|
| 310 | fd_set set;
|
---|
| 311 | struct timeval timeout;
|
---|
| 312 | int rv;
|
---|
[2] | 313 |
|
---|
[15] | 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
|
---|
[2] | 317 |
|
---|
[15] | 318 | timeout.tv_sec = 0;
|
---|
| 319 | timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
|
---|
| 320 | rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
|
---|
[2] | 321 |
|
---|
[15] | 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);
|
---|
[2] | 337 | }
|
---|
[15] | 338 | if (nb_read < 0) {
|
---|
| 339 | caller->self->Err("recvfrom error\n");
|
---|
[2] | 340 | }
|
---|
[15] | 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);
|
---|
[2] | 354 | }
|
---|
[15] | 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 | }
|
---|
[2] | 369 | }
|
---|
[15] | 370 | }
|
---|
[2] | 371 |
|
---|
[15] | 372 | close(pipe_fd);
|
---|
| 373 | pthread_exit(0);
|
---|
[2] | 374 | }
|
---|
| 375 | #endif
|
---|