1 /* Copyright (c) 2015-2023. 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>
13 namespace simgrid::mc {
15 /** A channel for exchanging messages between model-checker and model-checked app
17 * This abstracts away the way the messages are transferred. Currently, they
18 * are sent over a (connected) `SOCK_SEQPACKET` socket.
22 template <class M> static constexpr bool messageType() { return std::is_class_v<M> && std::is_trivial_v<M>; }
25 explicit Channel(int sock) : socket_(sock) {}
29 Channel(Channel const&) = delete;
30 Channel& operator=(Channel const&) = delete;
33 int send(const void* message, size_t size) const;
34 int send(MessageType type) const
36 s_mc_message_t message = {type};
37 return this->send(&message, sizeof(message));
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
42 return this->send(&m, sizeof(M));
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
49 return this->receive(&m, sizeof(M));
52 int get_socket() const { return socket_; }
54 } // namespace simgrid::mc