Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / mc / remote / Channel.cpp
1 /* Copyright (c) 2015-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/mc/remote/Channel.hpp"
8 #include <xbt/log.h>
9
10 #include <cerrno>
11 #include <cstring>
12 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Channel, mc, "MC interprocess communication");
17
18 namespace simgrid {
19 namespace mc {
20
21 Channel::~Channel()
22 {
23   if (this->socket_ >= 0)
24     close(this->socket_);
25 }
26
27 /** @brief Send a message; returns 0 on success or errno on failure */
28 int Channel::send(const void* message, size_t size) const
29 {
30   XBT_DEBUG("Send %s", to_c_str(*(MessageType*)message));
31   while (::send(this->socket_, message, size, 0) == -1) {
32     if (errno != EINTR) {
33       XBT_ERROR("Channel::send failure: %s", strerror(errno));
34       return errno;
35     }
36   }
37   return 0;
38 }
39
40 ssize_t Channel::receive(void* message, size_t size, bool block) const
41 {
42   ssize_t res = recv(this->socket_, message, size, block ? 0 : MSG_DONTWAIT);
43   if (res != -1)
44     XBT_DEBUG("Receive %s", to_c_str(*(MessageType*)message));
45   else
46     XBT_ERROR("Channel::receive failure: %s", strerror(errno));
47   return res;
48 }
49 }
50 }