-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwebsocket_listener.cpp
More file actions
72 lines (65 loc) · 1.92 KB
/
Copy pathwebsocket_listener.cpp
File metadata and controls
72 lines (65 loc) · 1.92 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
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/http/dynamic_body.hpp>
#include <qthread.h>
#include "framework.h"
#include "websocket_listener.h"
#include "http_session.h"
namespace shelllet {
}
shelllet::inspect::WebSocketListener::WebSocketListener(shelllet::inspect::Receiver* receiver, boost::asio::io_context& ioc, boost::asio::ip::tcp::endpoint endpoint)
: receiver_(receiver)
, ioc_(ioc)
, acceptor_(ioc)
{
boost::beast::error_code ec;
acceptor_.open(endpoint.protocol(), ec);
if (ec)
{
LOG_ERROR("inspect") << "# open error: " << ec.message() << std::endl;
return;
}
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
if (ec)
{
LOG_ERROR("inspect") << "# set option error: " << ec.message() << std::endl;
return;
}
acceptor_.bind(endpoint, ec);
if (ec)
{
LOG_ERROR("inspect") << "# bind error: " << ec.message() << std::endl;
return;
}
acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
if (ec)
{
LOG_ERROR("inspect") << "# set option error: " << ec.message() << std::endl;
}
}
void shelllet::inspect::WebSocketListener::run()
{
boost::asio::dispatch(acceptor_.get_executor(), boost::beast::bind_front_handler(&WebSocketListener::doAccept, this->shared_from_this()));
}
void shelllet::inspect::WebSocketListener::doAccept()
{
acceptor_.async_accept(boost::asio::make_strand(ioc_), boost::beast::bind_front_handler(&WebSocketListener::onAccept, shared_from_this()));
}
void shelllet::inspect::WebSocketListener::onAccept(boost::beast::error_code ec, boost::asio::ip::tcp::socket socket)
{
if (ec)
{
LOG_ERROR("inspect") << "# accept error: " << ec.message() << std::endl;
}
else
{
std::make_shared<HttpSession>(receiver_, std::move(socket))->run();
}
if (QThread::currentThread()->isInterruptionRequested()) {
ioc_.stop();
return;
}
doAccept();
}