forked from securesocketfunneling/udt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (70 loc) · 2.89 KB
/
main.cpp
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
#include <boost/asio/io_service.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>
#include <boost/system/error_code.hpp>
#include <boost/thread.hpp>
#include "udt/connected_protocol/logger/file_log.h"
#include "udt/ip/udt.h"
int main(int argc, char* argv[]) {
if (argc != 2) {
BOOST_LOG_TRIVIAL(error) << "Command help : ./udt_server [port]";
return 1;
}
using udt_protocol = ip::udt<>;
using Buffer = std::array<uint8_t, 150000>;
using SocketPtr = std::shared_ptr<udt_protocol::socket>;
using ReceiveHandler = std::function<void(const boost::system::error_code&, std::size_t,
SocketPtr)>;
using AcceptHandler = std::function<void(const boost::system::error_code&, SocketPtr)>
;
boost::asio::io_service io_service;
boost::system::error_code resolve_ec;
Buffer r_buffer2;
SocketPtr p_socket(std::make_shared<udt_protocol::socket>(io_service));
udt_protocol::acceptor acceptor(io_service);
udt_protocol::resolver resolver(io_service);
udt_protocol::resolver::query acceptor_udt_query(boost::asio::ip::udp::v4(),
argv[1]);
auto acceptor_endpoint_it = resolver.resolve(acceptor_udt_query, resolve_ec);
if (resolve_ec) {
BOOST_LOG_TRIVIAL(error) << "Wrong argument provided" << std::endl;
return 1;
}
udt_protocol::endpoint acceptor_endpoint(*acceptor_endpoint_it);
AcceptHandler accepted;
ReceiveHandler received_handler;
accepted = [&](const boost::system::error_code& ec, SocketPtr p_socket) {
if (ec) {
BOOST_LOG_TRIVIAL(trace) << "Error on accept : " << ec.value() << " "
<< ec.message();
return;
}
BOOST_LOG_TRIVIAL(trace) << "Accepted";
boost::asio::async_read(*p_socket, boost::asio::buffer(r_buffer2),
boost::bind(received_handler, _1, _2, p_socket));
SocketPtr p_new_socket(
std::make_shared<udt_protocol::socket>(io_service));
acceptor.async_accept(*p_new_socket,
boost::bind(accepted, _1, p_new_socket));
};
received_handler = [&](const boost::system::error_code& ec,
std::size_t length, SocketPtr p_socket) {
if (ec) {
BOOST_LOG_TRIVIAL(trace) << "Error on receive ec : " << ec.value() << " "
<< ec.message();
return;
}
boost::asio::async_read(*p_socket, boost::asio::buffer(r_buffer2),
boost::bind(received_handler, _1, _2, p_socket));
};
boost::system::error_code ec;
acceptor.open();
acceptor.bind(acceptor_endpoint, ec);
acceptor.listen(100, ec);
acceptor.async_accept(*p_socket, boost::bind(accepted, _1, p_socket));
boost::thread_group threads;
for (uint16_t i = 1; i <= boost::thread::hardware_concurrency(); ++i) {
threads.create_thread([&io_service]() { io_service.run(); });
}
threads.join_all();
}