diff --git a/sln/punchnat/punchnat.vcxproj b/sln/punchnat/punchnat.vcxproj index fd9f9a9..ce3bb3f 100644 --- a/sln/punchnat/punchnat.vcxproj +++ b/sln/punchnat/punchnat.vcxproj @@ -30,7 +30,6 @@ - @@ -45,28 +44,28 @@ Application true - v142 Unicode + v143 Application false - v142 true Unicode + v143 Application true - v142 Unicode + v143 Application false - v142 true Unicode + v143 diff --git a/sln/punchnat/punchnat.vcxproj.filters b/sln/punchnat/punchnat.vcxproj.filters index 6167d4f..b0ad284 100644 --- a/sln/punchnat/punchnat.vcxproj.filters +++ b/sln/punchnat/punchnat.vcxproj.filters @@ -47,9 +47,6 @@ - - Header Files\shares - Header Files\shares diff --git a/src/main.cpp b/src/main.cpp index e45882f..a4ec54d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,8 +18,8 @@ int main(int argc, char *argv[]) return 0; } - asio::io_context ioc; - asio::io_context network_io{ 1 }; + asio::io_context ioc {1}; + asio::io_context network_io; std::vector udp_sessions; std::vector tcp_sessions; diff --git a/src/networks/connections.cpp b/src/networks/connections.cpp index f847b56..47682fa 100644 --- a/src/networks/connections.cpp +++ b/src/networks/connections.cpp @@ -134,7 +134,8 @@ void tcp_session::async_read_data() if (!stopped.load() && connection_socket.is_open()) { std::unique_ptr buffer_cache = std::make_unique(BUFFER_SIZE); - asio::async_read(connection_socket, asio::buffer(buffer_cache.get(), BUFFER_SIZE), asio::transfer_at_least(1), + auto asio_buffer = asio::buffer(buffer_cache.get(), BUFFER_SIZE); + asio::async_read(connection_socket, asio_buffer, asio::transfer_at_least(1), [this, data = std::move(buffer_cache)](const asio::error_code &error, size_t bytes_transferred) mutable { after_read_completed(std::move(data), error, bytes_transferred); @@ -161,8 +162,8 @@ void tcp_session::async_send_data(std::unique_ptr input_data, size_t { if (stopped.load()) return; - uint8_t *data_ptr = input_data.get(); - asio::async_write(connection_socket, asio::buffer(data_ptr, data_size), + auto asio_buffer = asio::buffer(input_data.get(), data_size); + asio::async_write(connection_socket, asio_buffer, [this, data = std::move(input_data)](const asio::error_code &error, size_t bytes_transferred) { after_write_completed(error, bytes_transferred); @@ -332,15 +333,15 @@ void udp_server::continue_receive() void udp_server::async_send_out(std::unique_ptr> data, udp::endpoint client_endpoint) { - std::vector &data_vector = *data; - connection_socket.async_send_to(asio::buffer(data_vector), client_endpoint, + auto asio_buffer = asio::buffer(*data); + connection_socket.async_send_to(asio_buffer, client_endpoint, [data_ = std::move(data)](const asio::error_code &error, size_t bytes_transferred) {}); } void udp_server::async_send_out(std::unique_ptr data, size_t data_size, udp::endpoint client_endpoint) { - uint8_t *data_ptr = data.get(); - connection_socket.async_send_to(asio::buffer(data_ptr, data_size), client_endpoint, + auto asio_buffer = asio::buffer(data.get(), data_size); + connection_socket.async_send_to(asio_buffer, client_endpoint, [data_ = std::move(data)](const asio::error_code &error, size_t bytes_transferred) {}); } @@ -362,8 +363,8 @@ void udp_server::initialise(udp::endpoint ep) void udp_server::start_receive() { std::unique_ptr buffer_cache = std::make_unique(BUFFER_SIZE); - uint8_t *buffer_ptr = buffer_cache.get(); - connection_socket.async_receive_from(asio::buffer(buffer_ptr, BUFFER_SIZE), incoming_endpoint, + auto asio_buffer = asio::buffer(buffer_cache.get(), BUFFER_SIZE); + connection_socket.async_receive_from(asio_buffer, incoming_endpoint, [data = std::move(buffer_cache), this](const asio::error_code &error, std::size_t bytes_transferred) mutable { handle_receive(std::move(data), error, bytes_transferred); @@ -381,11 +382,11 @@ void udp_server::handle_receive(std::unique_ptr buffer_cache, const a udp::endpoint copy_of_incoming_endpoint = incoming_endpoint; start_receive(); - //callback(buffer_cache, bytes_transferred, std::move(copy_of_incoming_endpoint), port_number); - asio::post(task_assigner, [this, data = std::move(buffer_cache), bytes_transferred, peer_ep = std::move(copy_of_incoming_endpoint)]() mutable - { - callback(std::move(data), bytes_transferred, std::move(peer_ep), port_number); - }); + callback(std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, port_number); + //asio::post(task_assigner, [this, data = std::move(buffer_cache), bytes_transferred, peer_ep = std::move(copy_of_incoming_endpoint)]() mutable + //{ + // callback(std::move(data), bytes_transferred, std::move(peer_ep), port_number); + //}); } asio::ip::port_type udp_server::get_port_number() @@ -477,8 +478,8 @@ void udp_client::async_send_out(std::unique_ptr> data, udp: if (stopped.load()) return; - std::vector &data_vector = *data; - connection_socket.async_send_to(asio::buffer(data_vector), peer_endpoint, + auto asio_buffer = asio::buffer(*data); + connection_socket.async_send_to(asio_buffer, peer_endpoint, [data_ = std::move(data)](const asio::error_code &error, size_t bytes_transferred) {}); last_send_time.store(right_now()); } @@ -488,8 +489,8 @@ void udp_client::async_send_out(std::unique_ptr data, size_t data_siz if (stopped.load()) return; - uint8_t *data_ptr = data.get(); - connection_socket.async_send_to(asio::buffer(data_ptr, data_size), peer_endpoint, + auto asio_buffer = asio::buffer(data.get(), data_size); + connection_socket.async_send_to(asio_buffer, peer_endpoint, [data_ = std::move(data)](const asio::error_code &error, size_t bytes_transferred) {}); last_send_time.store(right_now()); } @@ -535,8 +536,8 @@ void udp_client::start_receive() return; std::unique_ptr buffer_cache = std::make_unique(BUFFER_SIZE); - uint8_t *buffer_ptr = buffer_cache.get(); - connection_socket.async_receive_from(asio::buffer(buffer_ptr, BUFFER_SIZE), incoming_endpoint, + auto asio_buffer = asio::buffer(buffer_cache.get(), BUFFER_SIZE); + connection_socket.async_receive_from(asio_buffer, incoming_endpoint, [buffer_ptr = std::move(buffer_cache), this](const asio::error_code &error, std::size_t bytes_transferred) mutable { handle_receive(std::move(buffer_ptr), error, bytes_transferred); @@ -563,9 +564,9 @@ void udp_client::handle_receive(std::unique_ptr buffer_cache, const a auto local_port = local_ep.port(); udp::endpoint copy_of_incoming_endpoint = incoming_endpoint; start_receive(); - //callback(buffer_cache, bytes_transferred, std::move(copy_of_incoming_endpoint), local_port); - asio::post(task_assigner, [this, data_ptr = std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, local_port]() mutable - { - callback(std::move(data_ptr), bytes_transferred, copy_of_incoming_endpoint, local_port); - }); + callback(std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, local_port); + //asio::post(task_assigner, [this, data_ptr = std::move(buffer_cache), bytes_transferred, copy_of_incoming_endpoint, local_port]() mutable + //{ + // callback(std::move(data_ptr), bytes_transferred, copy_of_incoming_endpoint, local_port); + //}); } diff --git a/src/networks/connections.hpp b/src/networks/connections.hpp index b5ebe01..c946ac6 100644 --- a/src/networks/connections.hpp +++ b/src/networks/connections.hpp @@ -28,7 +28,6 @@ constexpr size_t BUFFER_SIZE = 4096u; constexpr size_t EMPTY_PACKET_SIZE = 1430u; constexpr size_t RETRY_TIMES = 5u; constexpr size_t RETRY_WAITS = 3u; -constexpr size_t TIMEOUT = 180; // second constexpr size_t CLEANUP_WAITS = 10; // second constexpr auto STUN_RESEND = std::chrono::seconds(30); constexpr auto FINDER_TIMEOUT_INTERVAL = std::chrono::seconds(1); diff --git a/src/networks/modes.hpp b/src/networks/modes.hpp index 5179c31..e53ed09 100644 --- a/src/networks/modes.hpp +++ b/src/networks/modes.hpp @@ -85,7 +85,7 @@ class udp_mode const std::array zero_value_array; std::shared_mutex mutex_udp_session_map_to_wrapper; - std::unordered_map> udp_session_map_to_wrapper; + std::unordered_map> udp_session_map_to_wrapper; std::shared_mutex mutex_wrapper_session_map_to_udp; std::unordered_map wrapper_session_map_to_udp; diff --git a/src/networks/udp_mode.cpp b/src/networks/udp_mode.cpp index 8527936..a0cf825 100644 --- a/src/networks/udp_mode.cpp +++ b/src/networks/udp_mode.cpp @@ -195,8 +195,9 @@ void udp_mode::loop_timeout_sessions() for (auto iter = udp_session_map_to_wrapper.begin(), next_iter = iter; iter != udp_session_map_to_wrapper.end(); iter = next_iter) { ++next_iter; - std::unique_ptr &client_ptr = iter->second; - if (client_ptr->time_gap_of_receive() >= TIMEOUT && client_ptr->time_gap_of_send() >= TIMEOUT) + std::shared_ptr client_ptr = iter->second; + if (client_ptr->time_gap_of_receive() >= current_settings.udp_timeout && + client_ptr->time_gap_of_send() >= current_settings.udp_timeout) { asio::ip::port_type port_number = client_ptr->local_port_number(); client_ptr->pause(true); @@ -205,7 +206,8 @@ void udp_mode::loop_timeout_sessions() wrapper_session_map_to_udp.erase(port_number); } - if (client_ptr->time_gap_of_receive() > TIMEOUT + 5 && client_ptr->time_gap_of_send() > TIMEOUT + 5) + if (client_ptr->time_gap_of_receive() > (int64_t)current_settings.udp_timeout + 5 && + client_ptr->time_gap_of_send() > (int64_t)current_settings.udp_timeout + 5) { udp_session_map_to_wrapper.erase(iter); } diff --git a/src/shares/share_defines.cpp b/src/shares/share_defines.cpp index ffe4f1c..1d11cfe 100644 --- a/src/shares/share_defines.cpp +++ b/src/shares/share_defines.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,6 +7,8 @@ #include "share_defines.hpp" #include "string_utils.hpp" +constexpr size_t TIMEOUT = 180; // second + user_settings parse_from_args(const std::vector &args, std::vector &error_msg) { using namespace str_utils; @@ -65,6 +68,13 @@ user_settings parse_from_args(const std::vector &args, std::vector< current_user_settings.log_directory = original_value; break; + case strhash("udp_timeout"): + if (auto time_interval = std::stoi(value); time_interval <= 0 || time_interval > USHRT_MAX) + current_user_settings.udp_timeout = 0; + else + current_user_settings.udp_timeout = static_cast(time_interval); + break; + default: error_msg.emplace_back("unknow option: " + arg); } @@ -80,18 +90,21 @@ void check_settings(user_settings ¤t_user_settings, std::vector