Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Initialize data members with class initializers, or initialization lists.
[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 app
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     std::swap(this->socket_, that.socket_);
42     return *this;
43   }
44
45   // Send
46   int send(const void* message, size_t size) const;
47   int send(e_mc_message_type type) const
48   {
49     s_mc_message_t message = {type};
50     return this->send(&message, sizeof(message));
51   }
52   /** @brief Send a message; returns 0 on success or errno on failure */
53   template <class M> typename std::enable_if<messageType<M>(), int>::type send(M const& m) const
54   {
55     return this->send(&m, sizeof(M));
56   }
57
58   // Receive
59   ssize_t receive(void* message, size_t size, bool block = true) const;
60   template <class M> typename std::enable_if<messageType<M>(), ssize_t>::type receive(M& m) const
61   {
62     return this->receive(&m, sizeof(M));
63   }
64
65   int get_socket() const { return socket_; }
66 };
67 }
68 }
69
70 #endif