Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The creation of the pimpl needs no simcall
[simgrid.git] / src / mc / remote / Channel.hpp
1 /* Copyright (c) 2015-2019. 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 {
14 namespace mc {
15
16 /** A channel for exchanging messages between model-checker and model-checked
17  *
18  *  This abstracts away the way the messages are transferred. Currently, they
19  *  are sent over a (connected) `SOCK_SEQPACKET` socket.
20  */
21 class Channel {
22   int socket_ = -1;
23   template <class M> static constexpr bool messageType()
24   {
25     return std::is_class<M>::value && std::is_trivial<M>::value;
26   }
27
28 public:
29   Channel() = default;
30   explicit Channel(int sock) : socket_(sock) {}
31   ~Channel();
32
33   // No copy:
34   Channel(Channel const&) = delete;
35   Channel& operator=(Channel const&) = delete;
36
37   // Move:
38   Channel(Channel&& that) : socket_(that.socket_) { that.socket_ = -1; }
39   Channel& operator=(Channel&& that)
40   {
41     this->socket_ = that.socket_;
42     that.socket_  = -1;
43     return *this;
44   }
45
46   // Send
47   int send(const void* message, size_t size) const;
48   int send(e_mc_message_type type) const
49   {
50     s_mc_message_t message = {type};
51     return this->send(&message, sizeof(message));
52   }
53   /** @brief Send a message; returns 0 on success or errno on failure */
54   template <class M> typename std::enable_if<messageType<M>(), int>::type send(M const& m) const
55   {
56     return this->send(&m, sizeof(M));
57   }
58
59   // Receive
60   ssize_t receive(void* message, size_t size, bool block = true) const;
61   template <class M> typename std::enable_if<messageType<M>(), ssize_t>::type receive(M& m) const
62   {
63     return this->receive(&m, sizeof(M));
64   }
65
66   int getSocket() const { return socket_; }
67 };
68 }
69 }
70
71 #endif