Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Misc sonar issues.
[simgrid.git] / src / mc / remote / Channel.cpp
1 /* Copyright (c) 2015-2023. The SimGrid Team.  All rights reserved.         */
2
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. */
5
6 #include "src/mc/remote/Channel.hpp"
7 #include <xbt/log.h>
8
9 #include <cerrno>
10 #include <cstring>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_channel, mc, "MC interprocess communication");
16
17 namespace simgrid::mc {
18 Channel::Channel(int sock, Channel const& other) : socket_(sock), buffer_(other.buffer_)
19 {
20   XBT_DEBUG("Adopt %zu bytes buffered by father channel.", buffer_.size());
21 }
22
23 Channel::~Channel()
24 {
25   if (this->socket_ >= 0)
26     close(this->socket_);
27 }
28
29 /** @brief Send a message; returns 0 on success or errno on failure */
30 int Channel::send(const void* message, size_t size) const
31 {
32   if (size >= sizeof(int) && is_valid_MessageType(*static_cast<const int*>(message))) {
33     XBT_DEBUG("Sending %s (%zu bytes sent)", to_c_str(*static_cast<const MessageType*>(message)), size);
34   } else {
35     XBT_DEBUG("Sending bytes directly (from address %p) (%zu bytes sent)", message, size);
36     if (size == 0)
37       XBT_WARN("Request to send a 0-sized message! Proceeding anyway.");
38   }
39
40   while (::send(this->socket_, message, size, 0) == -1) {
41     if (errno != EINTR) {
42       XBT_ERROR("Channel::send failure: %s", strerror(errno));
43       return errno;
44     }
45   }
46   return 0;
47 }
48
49 ssize_t Channel::receive(void* message, size_t size, int flags)
50 {
51   size_t bufsize = buffer_.size();
52   ssize_t copied = 0;
53   auto* whereto  = static_cast<char*>(message);
54   size_t todo    = size;
55   if (bufsize > 0) {
56     XBT_DEBUG("%d %zu bytes (of %zu expected) are already in buffer", getpid(), bufsize, size);
57     copied = std::min(size, bufsize);
58     std::copy_n(begin(buffer_), copied, whereto);
59     buffer_.erase(begin(buffer_), begin(buffer_) + copied);
60     todo -= copied;
61     whereto += copied;
62   }
63   ssize_t res = 0;
64   if (todo > 0) {
65     errno = 0;
66     res   = recv(this->socket_, whereto, todo, flags);
67     xbt_assert(res != -1 || errno == EAGAIN, "Channel::receive failure: %s", strerror(errno));
68     if (res == -1) {
69       res = 0;
70     }
71   }
72   XBT_DEBUG("%d Wanted %zu; Got %zd from buffer and %zd from network", getpid(), size, copied, res);
73   res += copied;
74   if (static_cast<size_t>(res) >= sizeof(int) && is_valid_MessageType(*static_cast<const int*>(message))) {
75     XBT_DEBUG("%d Receive %s (requested %zu; received %zd at %p)", getpid(),
76               to_c_str(*static_cast<const MessageType*>(message)), size, res, message);
77   } else {
78     XBT_DEBUG("Receive %zd bytes", res);
79   }
80   return res;
81 }
82
83 void Channel::reinject(const char* data, size_t size)
84 {
85   xbt_assert(size > 0, "Cannot reinject less than one char (size: %lu)", size);
86   XBT_DEBUG("%d Reinject %zu bytes on top of %zu pre-existing bytes", getpid(), size, buffer_.size());
87   buffer_.insert(end(buffer_), data, data + size);
88 }
89 } // namespace simgrid::mc