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>; }
23 std::vector<char> buffer_;
27 explicit Channel(int sock) : socket_(sock) {}
28 Channel(int sock, Channel const& other);
32 Channel(Channel const&) = delete;
33 Channel& operator=(Channel const&) = delete;
36 int send(const void* message, size_t size) const;
37 int send(MessageType type) const
39 s_mc_message_t message = {type};
40 return this->send(&message, sizeof(message));
42 /** @brief Send a message; returns 0 on success or errno on failure */
43 template <class M> typename std::enable_if_t<messageType<M>(), int> send(M const& m) const
45 return this->send(&m, sizeof(M));
49 ssize_t receive(void* message, size_t size, int flags = 0);
50 template <class M> typename std::enable_if_t<messageType<M>(), ssize_t> receive(M& m)
52 return this->receive(&m, sizeof(M), 0);
54 void reinject(const char* data, size_t size);
55 bool has_pending_data() const { return not buffer_.empty(); }
58 int get_socket() const { return socket_; }
59 void reset_socket(int socket) { socket_ = socket; }
61 } // namespace simgrid::mc