Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: move some files related to ELF, DWARF or unwind reading into their own directory
[simgrid.git] / src / mc / remote / Channel.cpp
1 /* Copyright (c) 2015-2019. 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 <unistd.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Channel, mc, "MC interprocess communication");
16
17 namespace simgrid {
18 namespace mc {
19
20 Channel::~Channel()
21 {
22   if (this->socket_ >= 0)
23     close(this->socket_);
24 }
25
26 /** @brief Send a message; returns 0 on success or errno on failure */
27 int Channel::send(const void* message, size_t size) const
28 {
29   XBT_DEBUG("Send %s", MC_message_type_name(*(e_mc_message_type*)message));
30   while (::send(this->socket_, message, size, 0) == -1) {
31     if (errno != EINTR)
32       return errno;
33   }
34   return 0;
35 }
36
37 ssize_t Channel::receive(void* message, size_t size, bool block) const
38 {
39   ssize_t res = recv(this->socket_, message, size, block ? 0 : MSG_DONTWAIT);
40   if (res != -1)
41     XBT_DEBUG("Receive %s", MC_message_type_name(*(e_mc_message_type*)message));
42   return res;
43 }
44 }
45 }