-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpdevice.cpp
More file actions
118 lines (96 loc) · 3.13 KB
/
Copy pathtcpdevice.cpp
File metadata and controls
118 lines (96 loc) · 3.13 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
#include "tcpdevice.h"
TcpDevice::TcpDevice(std::shared_ptr<boost::asio::io_service> _ptrIo,const std::string &_address, uint16_t _port) :
ptrIo(_ptrIo),
resolver(*_ptrIo),
socket(*_ptrIo),
address(_address),
port(_port)
{
LOG(__PRETTY_FUNCTION__);
}
void TcpDevice::start()
{
LOG(__PRETTY_FUNCTION__);
if(getState() == DEVICE_STATE::CONNECTED ||
getState() == DEVICE_STATE::CONNECTING)
return;
boost::asio::ip::tcp::resolver::query query(address, std::to_string(port));
resolver.async_resolve(query,
boost::bind(&TcpDevice::handleResolve, this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
void TcpDevice::handleResolve(const boost::system::error_code& _ec,
boost::asio::ip::tcp::resolver::iterator _endpointIterator)
{
LOG(__PRETTY_FUNCTION__);
if(_ec){
LOG_ERR(__PRETTY_FUNCTION__, " error ", _ec.message());
setState(DEVICE_STATE::ERROR, _ec.message());
return;
}
boost::system::error_code hostError = boost::asio::error::host_not_found;
boost::asio::ip::tcp::resolver::iterator end;
setState(DEVICE_STATE::CONNECTING, "Resolving host");
while(hostError && _endpointIterator != end){
socket.close();
socket.connect(*_endpointIterator++, hostError);
if(hostError){
socket.close();
}
}
if(!socket.is_open()){
LOG_ERR(__PRETTY_FUNCTION__, " error ", hostError.message());
setState(DEVICE_STATE::ERROR, hostError.message());
return;
}
setState(DEVICE_STATE::CONNECTED, "Ready");
startReceive();
}
void TcpDevice::startReceive()
{
LOG(__PRETTY_FUNCTION__);
socket.async_receive(boost::asio::buffer(recieveBuffer),
boost::bind(&TcpDevice::handleReceive, shared_from_this(),
boost::asio::placeholders::error(),
boost::asio::placeholders::bytes_transferred()));
}
void TcpDevice::handleReceive(const boost::system::error_code &_ec, std::size_t _howMany)
{
LOG(__PRETTY_FUNCTION__);
if(isStopped())
return;
if (_ec && _ec != boost::asio::error::message_size){
LOG_ERR(__PRETTY_FUNCTION__, " ",_ec.category().name(), " ", _ec.message().c_str());
setState(DEVICE_STATE::ERROR, _ec.message());
return;
}
std::string s(recieveBuffer.data(),_howMany);
LOG(s);
startReceive();
}
bool TcpDevice::isStopped()
{
LOG(__PRETTY_FUNCTION__);
return !socket.is_open();
}
void TcpDevice::stop()
{
LOG(__PRETTY_FUNCTION__);
if(isStopped() )
return;
socket.close();
setState(DEVICE_STATE::DISCONNECTED, "Halted");
}
void TcpDevice::send(const std::string &_deviceMessage)
{
LOG(__PRETTY_FUNCTION__);
if(isStopped() || getState() != DEVICE_STATE::CONNECTED)
return;
socket.send(boost::asio::buffer(_deviceMessage.data(),_deviceMessage.size()));
}
TcpDevice::~TcpDevice()
{
LOG(__PRETTY_FUNCTION__);
stop();
}