Fix most conversion warnings

This commit is contained in:
momo5502
2025-01-05 20:34:54 +01:00
parent eeac915a55
commit 17860edc4c
9 changed files with 34 additions and 39 deletions

View File

@@ -57,7 +57,7 @@ namespace network
this->address4_ = addr;
}
address::address(const sockaddr* addr, const int length)
address::address(const sockaddr* addr, const socklen_t length)
: address()
{
this->set_address(addr, length);
@@ -109,7 +109,7 @@ namespace network
this->address6_.sin6_addr = addr;
}
void address::set_address(const sockaddr* addr, const int length)
void address::set_address(const sockaddr* addr, const socklen_t length)
{
if (static_cast<size_t>(length) >= sizeof(sockaddr_in) && addr->sa_family == AF_INET)
{
@@ -250,29 +250,29 @@ namespace network
return this->address6_;
}
int address::get_size() const
socklen_t address::get_size() const
{
switch (this->address_.sa_family)
{
case AF_INET:
return static_cast<int>(sizeof(this->address4_));
return static_cast<socklen_t>(sizeof(this->address4_));
case AF_INET6:
return static_cast<int>(sizeof(this->address6_));
return static_cast<socklen_t>(sizeof(this->address6_));
default:
return static_cast<int>(sizeof(this->address_));
return static_cast<socklen_t>(sizeof(this->address_));
}
}
int address::get_max_size() const
socklen_t address::get_max_size() const
{
const auto s = sizeof(this->address_);
const auto s4 = sizeof(this->address4_);
const auto s6 = sizeof(this->address6_);
const auto sstore = sizeof(this->storage_);
const auto max_size = std::max(sstore, std::max(s, std::max(s4, s6)));
constexpr auto s = sizeof(this->address_);
constexpr auto s4 = sizeof(this->address4_);
constexpr auto s6 = sizeof(this->address6_);
constexpr auto sstore = sizeof(this->storage_);
constexpr auto max_size = std::max(sstore, std::max(s, std::max(s4, s6)));
static_assert(max_size == sstore);
return max_size;
return static_cast<socklen_t>(max_size);
}
bool address::is_ipv4() const
@@ -349,7 +349,7 @@ namespace network
if (i->ai_family == AF_INET || i->ai_family == AF_INET6)
{
address a{};
a.set_address(i->ai_addr, static_cast<int>(i->ai_addrlen));
a.set_address(i->ai_addr, static_cast<socklen_t>(i->ai_addrlen));
results.emplace_back(std::move(a));
}
}

View File

@@ -21,7 +21,7 @@
#include <cstring>
#define ZeroMemory(x, y) memset(x, 0, static_cast<size_t>(y))
#define ZeroMemory(x, y) memset(x, 0, y)
#endif
@@ -30,6 +30,7 @@
#include <optional>
#ifdef _WIN32
using socklen_t = int;
#pragma comment(lib, "ws2_32.lib")
#endif
@@ -44,12 +45,12 @@ namespace network
address(const std::string& addr, const std::optional<int>& family = {});
address(const sockaddr_in& addr);
address(const sockaddr_in6& addr);
address(const sockaddr* addr, int length);
address(const sockaddr* addr, socklen_t length);
void set_ipv4(uint32_t ip);
void set_ipv4(const in_addr& addr);
void set_ipv6(const in6_addr& addr);
void set_address(const sockaddr* addr, int length);
void set_address(const sockaddr* addr, socklen_t length);
void set_port(unsigned short port);
[[nodiscard]] unsigned short get_port() const;
@@ -62,8 +63,8 @@ namespace network
const sockaddr_in& get_in_addr() const;
const sockaddr_in6& get_in6_addr() const;
int get_size() const;
int get_max_size() const;
socklen_t get_size() const;
socklen_t get_max_size() const;
bool is_ipv4() const;
bool is_ipv6() const;

View File

@@ -57,7 +57,7 @@ namespace network
bool socket::bind_port(const address& target)
{
const auto result = bind(this->socket_, &target.get_addr(), static_cast<socklen_t>(target.get_size())) == 0;
const auto result = bind(this->socket_, &target.get_addr(), target.get_size()) == 0;
if (result)
{
this->port_ = target.get_port();
@@ -70,7 +70,7 @@ namespace network
{
const auto res = sendto(this->socket_, static_cast<const char*>(data), static_cast<send_size>(size), 0,
&target.get_addr(),
static_cast<socklen_t>(target.get_size()));
target.get_size());
return static_cast<size_t>(res) == size;
}
@@ -82,7 +82,7 @@ namespace network
bool socket::receive(address& source, std::string& data) const
{
char buffer[0x2000];
auto len = static_cast<socklen_t>(source.get_max_size());
auto len = source.get_max_size();
const auto result = recvfrom(this->socket_, buffer, static_cast<int>(sizeof(buffer)), 0, &source.get_addr(),
&len);

View File

@@ -6,7 +6,6 @@
#include <chrono>
#ifdef _WIN32
using socklen_t = int;
using send_size = int;
#define GET_SOCKET_ERROR() (WSAGetLastError())
#define poll WSAPoll