1 /* Copyright (c) 2015-2019. 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;
30 explicit Channel(int sock) : socket_(sock) {}
34 Channel(Channel const&) = delete;
35 Channel& operator=(Channel const&) = delete;
38 Channel(Channel&& that) : socket_(that.socket_) { that.socket_ = -1; }
39 Channel& operator=(Channel&& that)
41 std::swap(this->socket_, that.socket_);
46 int send(const void* message, size_t size) const;
47 int send(e_mc_message_type type) const
49 s_mc_message_t message = {type};
50 return this->send(&message, sizeof(message));
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
55 return this->send(&m, sizeof(M));
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
62 return this->receive(&m, sizeof(M));
65 int get_socket() const { return socket_; }