1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/mc/remote/Channel.hpp"
12 #include <sys/socket.h>
13 #include <sys/types.h>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_channel, mc, "MC interprocess communication");
18 namespace simgrid::mc {
19 Channel::Channel(int sock, Channel const& other) : socket_(sock), buffer_(other.buffer_)
21 XBT_DEBUG("Adopt %zu bytes buffered by father channel.", buffer_.size());
26 if (this->socket_ >= 0)
30 /** @brief Send a message; returns 0 on success or errno on failure */
31 int Channel::send(const void* message, size_t size) const
33 if (size >= sizeof(int) && is_valid_MessageType(*static_cast<const int*>(message))) {
34 XBT_DEBUG("Sending %s (%zu bytes sent)", to_c_str(*static_cast<const MessageType*>(message)), size);
36 XBT_DEBUG("Sending bytes directly (from address %p) (%zu bytes sent)", message, size);
38 XBT_WARN("Request to send a 0-sized message! Proceeding anyway.");
41 while (::send(this->socket_, message, size, 0) == -1) {
43 XBT_ERROR("Channel::send failure: %s", strerror(errno));
50 ssize_t Channel::receive(void* message, size_t size, int flags)
52 size_t bufsize = buffer_.size();
54 auto* whereto = static_cast<char*>(message);
57 XBT_DEBUG("%d %zu bytes (of %zu expected) are already in buffer", getpid(), bufsize, size);
58 copied = std::min(size, bufsize);
59 std::copy_n(begin(buffer_), copied, whereto);
60 buffer_.erase(begin(buffer_), begin(buffer_) + copied);
67 res = recv(this->socket_, whereto, todo, flags);
68 xbt_assert(res != -1 || errno == EAGAIN, "Channel::receive failure: %s", strerror(errno));
73 XBT_DEBUG("%d Wanted %zu; Got %zd from buffer and %zd from network", getpid(), size, copied, res);
75 if (static_cast<size_t>(res) >= sizeof(int) && is_valid_MessageType(*static_cast<const int*>(message))) {
76 XBT_DEBUG("%d Receive %s (requested %zu; received %zd at %p)", getpid(),
77 to_c_str(*static_cast<const MessageType*>(message)), size, res, message);
79 XBT_DEBUG("Receive %zd bytes", res);
84 void Channel::reinject(const char* data, size_t size)
86 xbt_assert(size > 0, "Cannot reinject less than one char (size: %lu)", size);
87 XBT_DEBUG("%d Reinject %zu bytes on top of %zu pre-existing bytes", getpid(), size, buffer_.size());
88 buffer_.insert(end(buffer_), data, data + size);
90 } // namespace simgrid::mc