-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudposcendpoint.cpp
More file actions
113 lines (97 loc) · 3.13 KB
/
Copy pathudposcendpoint.cpp
File metadata and controls
113 lines (97 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
#include "udposcendpoint.h"
// Constructor setup local ip and port, allow local ip
UdpOscEndpoint::UdpOscEndpoint(const std::shared_ptr<boost::asio::io_service>& _ptrIo)
: ptrIo(_ptrIo), currentState(STATE::STOPPED)
{
LOG(__PRETTY_FUNCTION__);
}
// Setup called when added to io service handler, assign ioservice, open socket
void UdpOscEndpoint::start(uint16_t _port)
{
if(_port < 1024){
LOG(__PRETTY_FUNCTION__, " invalid port");
setState(STATE::ERROR);
return;
}
if(getState() == STATE::RUNNING){
LOG(__PRETTY_FUNCTION__, " already running");
return;
}
try{
socket.reset(new boost::asio::ip::udp::socket(
*ptrIo, boost::asio::ip::udp::endpoint(
boost::asio::ip::udp::v4(), _port )));
} catch (boost::system::system_error err) {
LOG_ERR(__PRETTY_FUNCTION__, " caught exception ", err.what());
setState(STATE::ERROR);
return;
}
LOG(__PRETTY_FUNCTION__, " port: ", _port);
setState(STATE::RUNNING);
run();
}
//run call recieve from to fill buffer
void UdpOscEndpoint::run()
{
LOG(__PRETTY_FUNCTION__);
if(getState() != STATE::RUNNING)
return;
socket->async_receive_from( boost::asio::buffer(recieveBuffer), remoteEndpoint,
boost::bind(&UdpOscEndpoint::handleReceive, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
//If socket is open, shutdown and close
void UdpOscEndpoint::stop()
{
if(!socket){
LOG(__PRETTY_FUNCTION__, " no socket");
setState(STATE::STOPPED);
return;
}
if(!socket->is_open()){
LOG(__PRETTY_FUNCTION__," socket already closed");
setState(STATE::STOPPED);
return;
}
LOG(__PRETTY_FUNCTION__);
socket->close();
setState(STATE::STOPPED);
}
//recieve handler, calls run at end
void UdpOscEndpoint::handleReceive(const boost::system::error_code& error,
std::size_t how_many)
{
LOG(__PRETTY_FUNCTION__, " ", std::this_thread::get_id());
if(getState() == STATE::STOPPED){
return;
}
if (error && error != boost::asio::error::message_size && error != boost::asio::error::operation_aborted){
LOG_ERR(__PRETTY_FUNCTION__, " ",error.category().name(), " ", error.message().c_str());
setState(STATE::ERROR);
return;
}
BufferMessage msg(recieveBuffer,how_many);
messaging::send<BufferMessage>(this,msg);
run();
}
void UdpOscEndpoint::setState(const UdpOscEndpoint::STATE &_state)
{
if(currentState == _state){
LOG(__PRETTY_FUNCTION__, " no change ", _state);
return;
}
LOG(__PRETTY_FUNCTION__, " ", _state);
currentState = _state;
messaging::send<Server::STATE>(this,currentState);
}
const UdpOscEndpoint::STATE& UdpOscEndpoint::getState()
{
LOG(__PRETTY_FUNCTION__, " ", currentState);
return currentState;
}
UdpOscEndpoint::~UdpOscEndpoint()
{
LOG(__PRETTY_FUNCTION__);
stop();
}