Samchon Framework for CPP  1.0.0
ServerConnector.cpp
1 #include <samchon/protocol/ServerConnector.hpp>
2 
3 #include <boost/asio.hpp>
4 #include <thread>
5 
6 using namespace samchon;
7 using namespace samchon::protocol;
8 
9 using namespace std;
10 using namespace boost;
11 using namespace boost::asio;
12 using namespace boost::asio::ip;
13 
15  : super()
16 {
17  ioService = nullptr;
18  endPoint = nullptr;
19  localEndPoint = nullptr;
20 }
21 ServerConnector::~ServerConnector()
22 {
23  delete ioService;
24  delete endPoint;
25  delete localEndPoint;
26 }
27 
28 auto ServerConnector::getMyIP() const -> std::string
29 {
30  return "";
31 }
32 
34 {
35  std::string &ip = getIP();
36  std::string &myIP = getMyIP();
37 
38  if (super::socket != nullptr && super::socket->is_open() == true)
39  return;
40 
41  ioService = new io_service();
42  super::socket = new tcp::socket(*ioService, tcp::v4());
43  endPoint = new tcp::endpoint(address::from_string(ip), getPort());
44 
45  if (getMyIP().empty() == false && localEndPoint == nullptr)
46  {
47  localEndPoint = new tcp::endpoint(address::from_string(myIP), NULL);
49  }
50 
51  super::socket->connect(*endPoint);
52 }
Definition: RWMutex.hpp:4
boost::asio::io_service * ioService
An io_service of Boost.Asio&#39;s own.
Package of network protocol and libraries.
Definition: protocol.hpp:185
An interface for a client.
Definition: IClient.hpp:53
virtual auto getMyIP() const -> std::string
(optional) My IP, if you want to bind
ServerConnector()
Default Constructor.
Socket * socket
Socket for network I/O.
Definition: IClient.hpp:60
EndPoint * endPoint
An endpoint directing a server.
virtual auto getIP() const -> std::string=0
Destinatio IP.
EndPoint * localEndPoint
(Optional) An local endpoint of the client (my system)
virtual void connect()
Connect to a server.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
virtual auto getPort() const -> int=0
Destination port.