1 /* Copyright (c) 2015-2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_MC_CHANNEL_HPP
8 #define SIMGRID_MC_CHANNEL_HPP
12 #include <type_traits>
14 #include "src/mc/remote/mc_protocol.h"
19 /** A channel for exchanging messages between model-checker and model-checked
21 * This abstracts away the way the messages are transferred. Currently, they
22 * are sent over a (connected) `SOCK_DGRAM` socket.
26 template <class M> static constexpr bool messageType()
28 return std::is_class<M>::value && std::is_trivial<M>::value;
33 explicit Channel(int sock) : socket_(sock) {}
37 Channel(Channel const&) = delete;
38 Channel& operator=(Channel const&) = delete;
41 Channel(Channel&& that) : socket_(that.socket_) { that.socket_ = -1; }
42 Channel& operator=(Channel&& that)
44 this->socket_ = that.socket_;
50 int send(const void* message, size_t size) const;
51 int send(e_mc_message_type type) const
53 s_mc_message message = {type};
54 return this->send(&message, sizeof(message));
56 /** @brief Send a message; returns 0 on success or errno on failure */
57 template <class M> typename std::enable_if<messageType<M>(), int>::type send(M const& m) const
59 return this->send(&m, sizeof(M));
63 ssize_t receive(void* message, size_t size, bool block = true) const;
64 template <class M> typename std::enable_if<messageType<M>(), ssize_t>::type receive(M& m) const
66 return this->receive(&m, sizeof(M));
69 int getSocket() const { return socket_; }