Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
30ccdcd74ee7bd6be6f431f220475aa60a81e3bd
[simgrid.git] / src / mc / remote / Channel.cpp
1 /* Copyright (c) 2015-2016. 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 <errno.h>
8 #include <unistd.h>
9
10 #include <sys/socket.h>
11 #include <sys/types.h>
12
13 #include <xbt/log.h>
14
15 #include "src/mc/remote/Channel.hpp"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Channel, mc, "MC interprocess communication");
18
19 namespace simgrid {
20 namespace mc {
21
22 Channel::~Channel()
23 {
24   if (this->socket_ >= 0)
25     close(this->socket_);
26 }
27
28 int Channel::send(const void* message, size_t size) const
29 {
30   XBT_DEBUG("Send %s", MC_message_type_name(*(e_mc_message_type*)message));
31   while (::send(this->socket_, message, size, 0) == -1)
32     if (errno == EINTR)
33       continue;
34     else
35       return errno;
36   return 0;
37 }
38
39 ssize_t Channel::receive(void* message, size_t size, bool block) const
40 {
41   int res = recv(this->socket_, message, size, block ? 0 : MSG_DONTWAIT);
42   if (res != -1)
43     XBT_DEBUG("Receive %s", MC_message_type_name(*(e_mc_message_type*)message));
44   return res;
45 }
46 }
47 }