1 /* Copyright (c) 2015-2022. 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 #ifndef SIMGRID_MC_CHANNEL_HPP
7 #define SIMGRID_MC_CHANNEL_HPP
9 #include "src/mc/remote/mc_protocol.h"
11 #include <type_traits>
16 /** A channel for exchanging messages between model-checker and model-checked app
18 * This abstracts away the way the messages are transferred. Currently, they
19 * are sent over a (connected) `SOCK_SEQPACKET` socket.
23 template <class M> static constexpr bool messageType()
25 return std::is_class<M>::value && std::is_trivial<M>::value;
29 explicit Channel(int sock) : socket_(sock) {}
33 Channel(Channel const&) = delete;
34 Channel& operator=(Channel const&) = delete;
37 int send(const void* message, size_t size) const;
38 int send(MessageType type) const
40 s_mc_message_t message = {type};
41 return this->send(&message, sizeof(message));
43 /** @brief Send a message; returns 0 on success or errno on failure */
44 template <class M> typename std::enable_if_t<messageType<M>(), int> send(M const& m) const
46 return this->send(&m, sizeof(M));
50 ssize_t receive(void* message, size_t size, bool block = true) const;
51 template <class M> typename std::enable_if_t<messageType<M>(), ssize_t> receive(M& m) const
53 return this->receive(&m, sizeof(M));
56 int get_socket() const { return socket_; }
59 } // namespace simgrid