-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpclientresolver.cpp
More file actions
86 lines (72 loc) · 2.33 KB
/
Copy pathtcpclientresolver.cpp
File metadata and controls
86 lines (72 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "tcpclientresolver.h"
#include "tcpclient.h"
#include <iostream>
#include <string>
#include <boost/bind.hpp>
TcpClientResolver::TcpClientResolver(std::string& Host,
unsigned int Port,
boost::asio::io_service& IOService,
TcpClient& Client) :
m_Host(Host),
m_Port(Port),
m_IOService(IOService),
m_Resolver(IOService),
m_ClientSharedLock(Client.GetMutex()),
m_Client(Client)
{
}
void TcpClientResolver::AsyncResolve(std::string& Host,
unsigned int Port,
boost::asio::io_service& IOService,
TcpClient& Client)
{
auto Resolver = std::make_shared<TcpClientResolver>(Host, Port, IOService, Client);
Resolver->Resolve();
}
// TODO Remove Args
void TcpClientResolver::Resolve()
{
boost::asio::ip::tcp::resolver::query query(m_Host, std::to_string(m_Port));
m_Resolver.async_resolve(query,
boost::bind(&TcpClientResolver::HandleResolve,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::iterator)
);
}
void TcpClientResolver::HandleResolve(const boost::system::error_code& ErrorCode,
boost::asio::ip::tcp::resolver::iterator Endpoints)
{
if(ErrorCode)
{
m_Client.OnTcpResolveError(ErrorCode.message());
}
else
{
TryAndConnect(Endpoints);
}
}
void TcpClientResolver::TryAndConnect(boost::asio::ip::tcp::resolver::iterator Endpoints)
{
boost::system::error_code HostError = boost::asio::error::host_not_found;
boost::asio::ip::tcp::resolver::iterator end;
boost::asio::ip::tcp::socket* Socket = new boost::asio::ip::tcp::socket(m_IOService);
while(HostError && Endpoints != end)
{
Socket->close();
Socket->connect(*Endpoints++, HostError);
if(HostError)
{
Socket->close();
}
}
if(!Socket->is_open())
{
m_Client.OnTcpResolveError(HostError.message());
delete Socket;
}
else
{
m_Client.OnTcpResolveConnect(Socket);
}
}