Samchon Framework for CPP  1.0.0
ServerConnector.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <samchon/protocol/Communicator.hpp>
5 
6 namespace samchon
7 {
8 namespace protocol
9 {
38  : public virtual Communicator
39  {
40  protected:
41  std::unique_ptr<boost::asio::io_service> io_service;
42 
43  std::unique_ptr<boost::asio::ip::tcp::endpoint> endpoint;
44 
45  public:
46  /* -----------------------------------------------------------
47  CONSTRUCTORS
48  ----------------------------------------------------------- */
56  {
57  this->listener = listener;
58  };
59 
63  virtual ~ServerConnector() = default;
64 
65  /* -----------------------------------------------------------
66  CONNECTOR
67  ----------------------------------------------------------- */
87  virtual void connect(const std::string &ip, int port)
88  {
89  _Connect(ip, port);
90 
91  listen_message();
92  };
93 
94  protected:
95  void _Connect(const std::string &ip, int port)
96  {
97  if (socket != nullptr && socket->is_open() == true)
98  throw std::logic_error("Already connected");
99 
100  io_service.reset(new boost::asio::io_service());
101  endpoint.reset(new boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(ip), port));
102 
103  socket.reset(new boost::asio::ip::tcp::socket(*io_service, boost::asio::ip::tcp::v4()));
104  socket->connect(*endpoint);
105  };
106  };
107 };
108 };
virtual void connect(const std::string &ip, int port)