Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
da1f649ae1dca577d80ecb1d9b32bf42d76576f9
[simgrid.git] / src / mc / remote / Channel.hpp
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 #ifndef SIMGRID_MC_CHANNEL_HPP
7 #define SIMGRID_MC_CHANNEL_HPP
8
9 #include "src/mc/remote/mc_protocol.h"
10
11 #include <type_traits>
12
13 namespace simgrid::mc {
14
15 /** A channel for exchanging messages between model-checker and model-checked app
16  *
17  *  This abstracts away the way the messages are transferred. Currently, they
18  *  are sent over a (connected) `SOCK_SEQPACKET` socket.
19  */
20 class Channel {
21   int socket_ = -1;
22   template <class M> static constexpr bool messageType() { return std::is_class_v<M> && std::is_trivial_v<M>; }
23
24 public:
25   explicit Channel(int sock) : socket_(sock) {}
26   ~Channel();
27
28   // No copy:
29   Channel(Channel const&) = delete;
30   Channel& operator=(Channel const&) = delete;
31
32   // Send
33   int send(const void* message, size_t size) const;
34   int send(MessageType type) const
35   {
36     s_mc_message_t message = {type};
37     return this->send(&message, sizeof(message));
38   }
39   /** @brief Send a message; returns 0 on success or errno on failure */
40   template <class M> typename std::enable_if_t<messageType<M>(), int> send(M const& m) const
41   {
42     return this->send(&m, sizeof(M));
43   }
44
45   // Receive
46   ssize_t receive(void* message, size_t size) const;
47   template <class M> typename std::enable_if_t<messageType<M>(), ssize_t> receive(M& m) const
48   {
49     return this->receive(&m, sizeof(M));
50   }
51
52   int get_socket() const { return socket_; }
53 };
54 } // namespace simgrid::mc
55
56 #endif