OSSIA
Open Scenario System for Interactive Application
writers.hpp
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <boost/asio/buffer.hpp>
5 #include <boost/asio/error.hpp>
6 
7 #include <cinttypes>
8 #include <vector>
9 
10 namespace ossia::net
11 {
12 template <typename T>
13 struct socket_writer
14 {
15  T& socket;
16  void operator()(const char* data, std::size_t sz) const { socket.write(data, sz); }
17 };
18 
19 template <typename Socket>
20 struct multi_socket_writer
21 {
22  std::vector<Socket>& sockets;
23  void write(const boost::asio::const_buffer& buf)
24  {
25  for(auto& sock : sockets)
26  {
27  sock->write(buf);
28  }
29  }
30  void write_some(const boost::asio::const_buffer& buf)
31  {
32  for(auto& sock : sockets)
33  {
34  sock->write(buf);
35  }
36  }
37 };
38 
39 template <typename T, typename F>
40 struct stream_processor
41 {
42  T& self;
43  F on_message;
44  template <typename... Args>
45  void operator()(Args&&... args) const
46  {
47  this->on_message(std::forward<Args>(args)...);
48  }
49 
50  bool validate_stream(boost::system::error_code ec) const
51  {
52  if(ec == boost::asio::error::operation_aborted)
53  {
54  self.on_fail();
55  return false;
56  }
57 
58  if(ec == boost::asio::error::eof)
59  {
60  self.on_close();
61  return false;
62  }
63 
64  return true;
65  }
66 };
67 }