Explicitly set SO_SNDBUF size to fix drops on Windows and address other PR feedback

This commit is contained in:
Chris Love 2021-09-04 13:18:06 -07:00
parent 2d1217006b
commit 497fa60f57
3 changed files with 22 additions and 5 deletions

View File

@ -26,6 +26,7 @@ namespace spdlog {
namespace details {
class udp_client
{
const int TX_BUFFER_SIZE = 10240;
SOCKET socket_ = INVALID_SOCKET;
sockaddr_in addr_ = { 0 };
@ -92,8 +93,16 @@ public:
int last_error = ::WSAGetLastError();
WSACleanup();
throw_winsock_error_("error: Create Socket failed", last_error);
return false;
}
int option_value = TX_BUFFER_SIZE;
if (setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&option_value, sizeof(option_value)) < 0)
{
int last_error = ::WSAGetLastError();
close();
throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error);
}
return true;
}
@ -119,8 +128,8 @@ public:
socklen_t tolen = sizeof(struct sockaddr);
if (sendto(socket_, data, static_cast<int>(n_bytes), 0, (struct sockaddr *)&addr_, tolen) == -1)
{
throw_spdlog_ex("sendto(2) failed", errno);
close();
throw_spdlog_ex("sendto(2) failed", errno);
}
}
};

View File

@ -21,8 +21,10 @@
namespace spdlog {
namespace details {
class udp_client
{
const int TX_BUFFER_SIZE = 10240;
int socket_ = -1;
struct sockaddr_in sockAddr_;
public:
@ -33,7 +35,13 @@ public:
if (socket_ < 0)
{
throw_spdlog_ex("error: Create Socket Failed!");
return false;
}
int option_value = TX_BUFFER_SIZE;
if (setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, (char*)&option_value, sizeof(option_value)) < 0)
{
close();
throw_spdlog_ex("error: setsockopt(SO_SNDBUF) Failed!");
}
sockAddr_.sin_family = AF_INET;
@ -76,8 +84,8 @@ public:
socklen_t tolen = sizeof(struct sockaddr);
if (( toslen = sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == -1)
{
throw_spdlog_ex("sendto(2) failed", errno);
close();
throw_spdlog_ex("sendto(2) failed", errno);
}
}
};

View File

@ -43,7 +43,7 @@ public:
explicit udp_sink(udp_sink_config sink_config)
: config_{std::move(sink_config)}
{
client_.init(config_.server_host, config_.server_port);
}
~udp_sink() override = default;