|
|
@@ -6,6 +6,7 @@
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
#include <boost/asio/post.hpp>
|
|
|
+#include <boost/asio/read.hpp>
|
|
|
#include <boost/asio/write.hpp>
|
|
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
@@ -16,6 +17,7 @@ using namespace boost::asio::ip;
|
|
|
using boost::asio::buffer;
|
|
|
using boost::asio::io_context;
|
|
|
using boost::asio::post;
|
|
|
+using boost::asio::read;
|
|
|
using boost::asio::write;
|
|
|
using boost::system::error_code;
|
|
|
|
|
|
@@ -69,11 +71,50 @@ struct sender_tcp::impl {
|
|
|
frame->frame_id, frame->length);
|
|
|
}
|
|
|
|
|
|
+// void hole_punch(const tcp::endpoint &listen_ep) { // UDP hole punch
|
|
|
+// static constexpr auto fake_stun_server = "38.59.254.192"; // TODO: setting in config file
|
|
|
+// static constexpr auto fake_stun_port = 5281;
|
|
|
+// auto fake_stun_ep = tcp::endpoint{
|
|
|
+// address::from_string(fake_stun_server), fake_stun_port};
|
|
|
+//
|
|
|
+// socket = std::make_unique<tcp::socket>(*q_this->get_ctx());
|
|
|
+// socket->open(tcp::v4());
|
|
|
+// socket->set_option(boost::asio::socket_base::reuse_address{true});
|
|
|
+// socket->bind(listen_ep);
|
|
|
+// socket->connect(fake_stun_ep);
|
|
|
+//
|
|
|
+// static constexpr auto hole_punch_request_len =
|
|
|
+// sizeof(size_t) + 2 * sizeof(uint8_t);
|
|
|
+// out_buf.create(hole_punch_request_len);
|
|
|
+// auto ptr = write_binary_number(out_buf.ptr, (size_t) (2 * sizeof(uint8_t)));
|
|
|
+// ptr = write_binary_number(ptr, 'R');
|
|
|
+// ptr = write_binary_number(ptr, 'S');
|
|
|
+// auto pkt_len = ptr - out_buf.ptr;
|
|
|
+// assert(pkt_len == hole_punch_request_len);
|
|
|
+// socket->send(buffer(out_buf.ptr, pkt_len));
|
|
|
+//
|
|
|
+// smart_buffer<uint8_t> in_buf;
|
|
|
+// size_t rep_len;
|
|
|
+// in_buf.create(sizeof(rep_len));
|
|
|
+// read(*socket, buffer(in_buf.ptr, in_buf.length));
|
|
|
+// read_binary_number(in_buf.ptr, &rep_len);
|
|
|
+// in_buf.create(rep_len);
|
|
|
+// read(*socket, buffer(in_buf.ptr, in_buf.length));
|
|
|
+// assert(rep_len == 2);
|
|
|
+// assert(std::string_view((char *) in_buf.ptr, 2) == "OK");
|
|
|
+// SPDLOG_INFO("TCP hole punch succeeded.");
|
|
|
+//
|
|
|
+// socket.reset();
|
|
|
+// }
|
|
|
+
|
|
|
void async_waiting_client() {
|
|
|
+ socket = std::make_unique<tcp::socket>(*q_this->get_ctx());
|
|
|
acceptor->async_accept(*socket, [this](error_code err) {
|
|
|
if (err) {
|
|
|
- SPDLOG_ERROR("Error while accepting client: {}", err.to_string());
|
|
|
+ SPDLOG_ERROR("Error while accepting client: {}", err.what());
|
|
|
+ assert(false);
|
|
|
async_waiting_client();
|
|
|
+ return;
|
|
|
}
|
|
|
assert(socket->is_open());
|
|
|
q_this->request_idr_frame();
|
|
|
@@ -89,8 +130,13 @@ struct sender_tcp::impl {
|
|
|
auto ret = new impl;
|
|
|
ret->q_this = q_this;
|
|
|
auto listen_ep = tcp::endpoint{tcp::v4(), listen_port};
|
|
|
+// ret->hole_punch(listen_ep);
|
|
|
ret->acceptor = std::make_unique<tcp::acceptor>(*q_this->get_ctx(), listen_ep);
|
|
|
- ret->socket = std::make_unique<tcp::socket>(*q_this->get_ctx());
|
|
|
+// ret->acceptor = std::make_unique<tcp::acceptor>(*q_this->get_ctx());
|
|
|
+// ret->acceptor->open(tcp::v4());
|
|
|
+// ret->acceptor->set_option(boost::asio::socket_base::reuse_address{true});
|
|
|
+// ret->acceptor->bind(listen_ep);
|
|
|
+// ret->acceptor->listen();
|
|
|
ret->async_waiting_client();
|
|
|
return ret;
|
|
|
}
|