Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
factory.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include "message.hh"
4 #include <boost/asio/streambuf.hpp>
5 #include <boost/asio.hpp>
6 #include <string>
7 #include <memory>
8 
9 namespace eclipse {
10 namespace messages {
11 
12 Message* load_message (boost::asio::streambuf&);
13 Message* load_message (std::string&);
14 std::string* save_message (Message*);
15 
16 void send_message(boost::asio::ip::tcp::socket*, eclipse::messages::Message*);
17 
18 template <typename T>
19 auto read_reply(boost::asio::ip::tcp::socket* socket) {
20  using namespace boost::asio;
21  char header[17] = {0};
22  header[16] = '\0';
23  boost::asio::streambuf buf;
24 
25  read(*socket, buffer(header, 16));
26  size_t size_of_msg = atoi(header);
27 
28  size_t l = read(*socket, buf.prepare(size_of_msg));
29 
30  Message* msg = nullptr;
31  buf.commit(l);
32  msg = load_message(buf);
33  buf.consume(l);
34  T* m = dynamic_cast<T*>(msg);
35  return std::unique_ptr<T>(m);
36 }
37 
38 } /* messages */
39 }
Message * load_message(std::string &str)
Definition: factory.cc:18
auto read_reply(boost::asio::ip::tcp::socket *socket)
Definition: factory.hh:19
Definition: message.hh:15
std::string * save_message(Message *m)
Definition: factory.cc:46
void send_message(boost::asio::ip::tcp::socket *socket, eclipse::messages::Message *msg)
Definition: factory.cc:69