[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 | /*********************************************************************/
|
---|
| 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 |
|
---|
| 28 | using std::string;
|
---|
| 29 | using namespace flair::core;
|
---|
| 30 |
|
---|
[15] | 31 | Socket_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();
|
---|
[2] | 37 | }
|
---|
| 38 |
|
---|
[15] | 39 | Socket_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;
|
---|
[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 |
|
---|
[15] | 53 | void Socket_impl::Init(void) {
|
---|
| 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) {
|
---|
| 105 | self->Err("rt_pipe_create error (%s)\n", strerror(-status));
|
---|
| 106 | }
|
---|
[2] | 107 |
|
---|
[15] | 108 | // start user side thread
|
---|
[2] | 109 | #ifdef NRT_STACK_SIZE
|
---|
[15] | 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
|
---|
[2] | 129 | #endif //__XENO__
|
---|
| 130 |
|
---|
[15] | 131 | if (address != "") {
|
---|
| 132 | struct hostent *hostinfo;
|
---|
| 133 | hostinfo = gethostbyname(address.c_str());
|
---|
| 134 | if (hostinfo == NULL) {
|
---|
| 135 | self->Err("gethostbyname\n");
|
---|
| 136 | }
|
---|
[2] | 137 |
|
---|
[15] | 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 | }
|
---|
[2] | 142 | }
|
---|
| 143 |
|
---|
[15] | 144 | Socket_impl::~Socket_impl() {
|
---|
[2] | 145 | #ifdef __XENO__
|
---|
[15] | 146 | is_running = false;
|
---|
[2] | 147 |
|
---|
[15] | 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));
|
---|
[2] | 152 |
|
---|
| 153 | #endif
|
---|
[15] | 154 | close(fd);
|
---|
[2] | 155 | }
|
---|
| 156 |
|
---|
[15] | 157 | void Socket_impl::SendMessage(const char *src, size_t src_len) {
|
---|
| 158 | ssize_t written;
|
---|
| 159 | string to_send;
|
---|
[2] | 160 |
|
---|
[15] | 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 | }
|
---|
[2] | 166 |
|
---|
| 167 | #ifdef __XENO__
|
---|
[15] | 168 | // Printf("send pipe %s\n",src);
|
---|
| 169 | written = rt_pipe_write(&pipe, src, src_len, P_NORMAL);
|
---|
[2] | 170 |
|
---|
[15] | 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 | }
|
---|
[2] | 176 | #else
|
---|
[15] | 177 | written =
|
---|
| 178 | sendto(fd, src, src_len, 0, (struct sockaddr *)&sock_in, sizeof(sock_in));
|
---|
[2] | 179 |
|
---|
[15] | 180 | if (written != (ssize_t)src_len) {
|
---|
| 181 | self->Err("sendto error\n");
|
---|
| 182 | }
|
---|
[2] | 183 | #endif
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[15] | 186 | void Socket_impl::SendMessage(string message) {
|
---|
| 187 | ssize_t written;
|
---|
[2] | 188 |
|
---|
[15] | 189 | if (broadcast == true)
|
---|
| 190 | message = self->Parent()->ObjectName() + ":" + message;
|
---|
| 191 | // Printf("SendMessage %s\n",message.c_str());
|
---|
[2] | 192 | #ifdef __XENO__
|
---|
[15] | 193 | written = rt_pipe_write(&pipe, message.c_str(), message.size(), P_NORMAL);
|
---|
[2] | 194 |
|
---|
[15] | 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 | }
|
---|
[2] | 200 | #else
|
---|
[15] | 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 | }
|
---|
[2] | 206 |
|
---|
| 207 | #endif
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[15] | 210 | ssize_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];
|
---|
[2] | 214 | #ifdef __XENO__
|
---|
[15] | 215 | nb_read = rt_pipe_read(&pipe, &buffer, sizeof(buffer), timeout);
|
---|
[2] | 216 | #else
|
---|
[15] | 217 | socklen_t sinsize = sizeof(sock_in);
|
---|
| 218 | struct timeval tv;
|
---|
[2] | 219 |
|
---|
[15] | 220 | if (timeout != TIME_NONBLOCK) {
|
---|
| 221 | int attr = fcntl(fd, F_GETFL, 0);
|
---|
| 222 | fcntl(fd, F_SETFL, attr & (~O_NONBLOCK));
|
---|
[2] | 223 |
|
---|
[15] | 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");
|
---|
[2] | 229 | }
|
---|
[15] | 230 | } else {
|
---|
| 231 | fcntl(fd, F_SETFL, O_NONBLOCK);
|
---|
| 232 | }
|
---|
[2] | 233 |
|
---|
[15] | 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 | }
|
---|
[2] | 240 | #endif
|
---|
[15] | 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;
|
---|
[2] | 275 | } else {
|
---|
[15] | 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;
|
---|
[2] | 282 | }
|
---|
[15] | 283 | }
|
---|
[2] | 284 | }
|
---|
| 285 |
|
---|
| 286 | #ifdef __XENO__
|
---|
[15] | 287 | void *Socket_impl::user(void *arg) {
|
---|
| 288 | Socket_impl *caller = (Socket_impl *)arg;
|
---|
| 289 | int pipe_fd = -1;
|
---|
| 290 | string devname;
|
---|
[2] | 291 |
|
---|
[15] | 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 | }
|
---|
[2] | 300 |
|
---|
[15] | 301 | while (caller->is_running == true) {
|
---|
| 302 | fd_set set;
|
---|
| 303 | struct timeval timeout;
|
---|
| 304 | int rv;
|
---|
[2] | 305 |
|
---|
[15] | 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
|
---|
[2] | 309 |
|
---|
[15] | 310 | timeout.tv_sec = 0;
|
---|
| 311 | timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
|
---|
| 312 | rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
|
---|
[2] | 313 |
|
---|
[15] | 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);
|
---|
[2] | 329 | }
|
---|
[15] | 330 | if (nb_read < 0) {
|
---|
| 331 | caller->self->Err("recvfrom error\n");
|
---|
[2] | 332 | }
|
---|
[15] | 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);
|
---|
[2] | 346 | }
|
---|
[15] | 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 | }
|
---|
[2] | 361 | }
|
---|
[15] | 362 | }
|
---|
[2] | 363 |
|
---|
[15] | 364 | close(pipe_fd);
|
---|
| 365 | pthread_exit(0);
|
---|
[2] | 366 | }
|
---|
| 367 | #endif
|
---|