-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpclient.cpp
More file actions
121 lines (97 loc) · 2.79 KB
/
Copy pathtcpclient.cpp
File metadata and controls
121 lines (97 loc) · 2.79 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "tcpclient.h"
#include "tcpsocketreader.h"
#include "tcpsocketwriter.h"
#include "tcpclientresolver.h"
#include "deviceconfiguration.h"
#include <iostream>
TcpClient::TcpClient(boost::asio::io_service& IOService,
const std::string& Host,
unsigned int Port,
StateChangeListener<ClientState> &Listener) :
Client(Listener),
m_IOService(IOService),
m_Host(Host),
m_Port(Port)
{
SetState(ClientState::READY);
}
TcpClient::TcpClient(boost::asio::io_service& IOService,
const DeviceConfiguration& Configuration,
StateChangeListener<ClientState> &Listener) :
Client(Listener),
m_IOService(IOService)
{
Configuration.Get("Host", m_Host);
Configuration.Get("Port", m_Port);
SetState(ClientState::READY);
}
TcpClient::~TcpClient()
{
Reset();
}
// Client
void TcpClient::Start()
{
if(GetState() != ClientState::READY) { return; }
SetState(ClientState::CONNECTING);
TcpClientResolver::AsyncResolve(m_Host, m_Port, m_IOService, *this);
}
void TcpClient::Reset()
{
if (m_Socket)
{
boost::system::error_code ec;
m_Socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
m_Socket->close();
boost::unique_lock<boost::shared_mutex> Lock(m_DestructorMutex);
delete m_Socket;
m_Socket = nullptr;
}
SetState(ClientState::READY);
}
bool TcpClient::ReadyToStart() const
{
return !m_Socket;
}
void TcpClient::Write(const std::string &Message)
{
if(!m_Socket) { return; }
TcpSocketWriter::AsyncWrite(Message, *m_Socket, *this);
}
boost::shared_mutex& TcpClient::GetMutex()
{
return m_DestructorMutex;
}
// Resolver
void TcpClient::OnTcpResolveConnect(boost::asio::ip::tcp::socket* Socket)
{
m_Socket = Socket;
SetState(ClientState::CONNECTED);
TcpSocketReader::AsyncRead(*m_Socket, *this);
}
void TcpClient::OnTcpResolveError(const std::string& Message)
{
std::cerr << "[" << m_Host << "] Resolve Error: " << Message << std::endl;
SetState(ClientState::ERROR);
}
// Reader
void TcpClient::OnTcpReadSuccess(const std::string& Message)
{
std::cout << "[" << m_Host << "] Received: " << Message << std::endl;
TcpSocketReader::AsyncRead(*m_Socket, *this);
}
void TcpClient::OnTcpReadError(const std::string& Message)
{
std::cerr << "[" << m_Host << "] Read Error: " << Message << std::endl;
SetState(ClientState::ERROR);
}
// Writer
void TcpClient::OnTcpWriteSuccess(std::size_t BytesTransferred)
{
std::cout << "[" << m_Host << "] Sent: " << BytesTransferred << " Bytes" << std::endl;
}
void TcpClient::OnTcpWriteError(const std::string& Message)
{
std::cerr << "[" << m_Host << "] Write Error:" << Message << std::endl;
SetState(ClientState::ERROR);
}