Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbatch committed Mar 26, 2023
1 parent 5f12203 commit 623178e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 43 deletions.
9 changes: 4 additions & 5 deletions sln/punchnat/punchnat.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<ClInclude Include="..\..\src\networks\modes.hpp" />
<ClInclude Include="..\..\src\networks\connections.hpp" />
<ClInclude Include="..\..\src\networks\stun.hpp" />
<ClInclude Include="..\..\src\shares\dynarray.hpp" />
<ClInclude Include="..\..\src\shares\share_defines.hpp" />
<ClInclude Include="..\..\src\shares\string_utils.hpp" />
</ItemGroup>
Expand All @@ -45,28 +44,28 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
3 changes: 0 additions & 3 deletions sln/punchnat/punchnat.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\shares\dynarray.hpp">
<Filter>Header Files\shares</Filter>
</ClInclude>
<ClInclude Include="..\..\src\shares\share_defines.hpp">
<Filter>Header Files\shares</Filter>
</ClInclude>
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_mode> udp_sessions;
std::vector<tcp_mode> tcp_sessions;

Expand Down
51 changes: 26 additions & 25 deletions src/networks/connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ void tcp_session::async_read_data()
if (!stopped.load() && connection_socket.is_open())
{
std::unique_ptr<uint8_t[]> buffer_cache = std::make_unique<uint8_t[]>(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);
Expand All @@ -161,8 +162,8 @@ void tcp_session::async_send_data(std::unique_ptr<uint8_t[]> 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);
Expand Down Expand Up @@ -332,15 +333,15 @@ void udp_server::continue_receive()

void udp_server::async_send_out(std::unique_ptr<std::vector<uint8_t>> data, udp::endpoint client_endpoint)
{
std::vector<uint8_t> &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<uint8_t[]> 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) {});
}

Expand All @@ -362,8 +363,8 @@ void udp_server::initialise(udp::endpoint ep)
void udp_server::start_receive()
{
std::unique_ptr<uint8_t[]> buffer_cache = std::make_unique<uint8_t[]>(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);
Expand All @@ -381,11 +382,11 @@ void udp_server::handle_receive(std::unique_ptr<uint8_t[]> 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()
Expand Down Expand Up @@ -477,8 +478,8 @@ void udp_client::async_send_out(std::unique_ptr<std::vector<uint8_t>> data, udp:
if (stopped.load())
return;

std::vector<uint8_t> &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());
}
Expand All @@ -488,8 +489,8 @@ void udp_client::async_send_out(std::unique_ptr<uint8_t[]> 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());
}
Expand Down Expand Up @@ -535,8 +536,8 @@ void udp_client::start_receive()
return;

std::unique_ptr<uint8_t[]> buffer_cache = std::make_unique<uint8_t[]>(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);
Expand All @@ -563,9 +564,9 @@ void udp_client::handle_receive(std::unique_ptr<uint8_t[]> 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);
//});
}
1 change: 0 additions & 1 deletion src/networks/connections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/networks/modes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class udp_mode
const std::array<uint8_t, 16> zero_value_array;

std::shared_mutex mutex_udp_session_map_to_wrapper;
std::unordered_map<udp::endpoint, std::unique_ptr<udp_client>> udp_session_map_to_wrapper;
std::unordered_map<udp::endpoint, std::shared_ptr<udp_client>> udp_session_map_to_wrapper;
std::shared_mutex mutex_wrapper_session_map_to_udp;
std::unordered_map<asio::ip::port_type, udp::endpoint> wrapper_session_map_to_udp;

Expand Down
8 changes: 5 additions & 3 deletions src/networks/udp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<udp_client> &client_ptr = iter->second;
if (client_ptr->time_gap_of_receive() >= TIMEOUT && client_ptr->time_gap_of_send() >= TIMEOUT)
std::shared_ptr<udp_client> 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);
Expand All @@ -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);
}
Expand Down
19 changes: 16 additions & 3 deletions src/shares/share_defines.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <climits>
#include <limits>
#include <stdexcept>
#include <cstdlib>
Expand All @@ -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<std::string> &args, std::vector<std::string> &error_msg)
{
using namespace str_utils;
Expand Down Expand Up @@ -65,6 +68,13 @@ user_settings parse_from_args(const std::vector<std::string> &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<uint16_t>(time_interval);
break;

default:
error_msg.emplace_back("unknow option: " + arg);
}
Expand All @@ -80,18 +90,21 @@ void check_settings(user_settings &current_user_settings, std::vector<std::strin
if (current_user_settings.destination_address.empty())
error_msg.emplace_back("invalid destination_address setting");

if (0 == current_user_settings.listen_port)
if (current_user_settings.listen_port == 0)
error_msg.emplace_back("listen_port is not set");

if (0 == current_user_settings.destination_port)
if (current_user_settings.destination_port == 0)
error_msg.emplace_back("destination_port is not set");

if (!current_user_settings.stun_server.empty())
{
if (0 == current_user_settings.listen_port)
if (current_user_settings.listen_port == 0)
error_msg.emplace_back("do not specify multiple listen ports when STUN Server is set");
}

if (current_user_settings.udp_timeout == 0)
current_user_settings.udp_timeout = TIMEOUT;

if (!current_user_settings.log_directory.empty())
{
if (std::filesystem::exists(current_user_settings.log_directory))
Expand Down
1 change: 1 addition & 0 deletions src/shares/share_defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct user_settings
{
uint16_t listen_port = 0;
uint16_t destination_port = 0;
uint16_t udp_timeout = 0;
std::string listen_on;
std::string destination_address;
std::string stun_server;
Expand Down

0 comments on commit 623178e

Please sign in to comment.