mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-23 13:41:02 +00:00
Fix clang-tidy warnings
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "address.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "../utils/finally.hpp"
|
||||
@@ -125,7 +126,7 @@ namespace network
|
||||
}
|
||||
}
|
||||
|
||||
void address::set_port(const unsigned short port)
|
||||
void address::set_port(const uint16_t port)
|
||||
{
|
||||
switch (this->get_family())
|
||||
{
|
||||
@@ -140,7 +141,7 @@ namespace network
|
||||
}
|
||||
}
|
||||
|
||||
unsigned short address::get_port() const
|
||||
uint16_t address::get_port() const
|
||||
{
|
||||
switch (this->get_family())
|
||||
{
|
||||
@@ -155,27 +156,27 @@ namespace network
|
||||
|
||||
std::string address::to_string() const
|
||||
{
|
||||
char buffer[1000] = {};
|
||||
std::string addr;
|
||||
std::string addr{};
|
||||
std::array<char, 1000> buffer{};
|
||||
|
||||
switch (this->get_family())
|
||||
{
|
||||
case AF_INET:
|
||||
inet_ntop(this->get_family(), &this->address4_.sin_addr, buffer, sizeof(buffer));
|
||||
addr = std::string(buffer);
|
||||
inet_ntop(this->get_family(), &this->address4_.sin_addr, buffer.data(), buffer.size());
|
||||
addr = std::string(buffer.data());
|
||||
break;
|
||||
case AF_INET6:
|
||||
inet_ntop(this->get_family(), &this->address6_.sin6_addr, buffer, sizeof(buffer));
|
||||
addr = "[" + std::string(buffer) + "]";
|
||||
inet_ntop(this->get_family(), &this->address6_.sin6_addr, buffer.data(), buffer.size());
|
||||
addr = "[" + std::string(buffer.data()) + "]";
|
||||
break;
|
||||
default:
|
||||
buffer[0] = '?';
|
||||
buffer[1] = 0;
|
||||
addr = std::string(buffer);
|
||||
addr = std::string(buffer.data());
|
||||
break;
|
||||
}
|
||||
|
||||
return addr + ":"s + std::to_string(this->get_port());
|
||||
return addr + ":" + std::to_string(this->get_port());
|
||||
}
|
||||
|
||||
bool address::is_local() const
|
||||
@@ -187,8 +188,8 @@ namespace network
|
||||
|
||||
// According to: https://en.wikipedia.org/wiki/Private_network
|
||||
|
||||
uint8_t bytes[4];
|
||||
memcpy(bytes, &this->address4_.sin_addr.s_addr, sizeof(bytes));
|
||||
std::array<uint8_t, 4> bytes{};
|
||||
memcpy(bytes.data(), &this->address4_.sin_addr.s_addr, bytes.size());
|
||||
|
||||
// 10.X.X.X
|
||||
if (bytes[0] == 10)
|
||||
@@ -346,7 +347,7 @@ namespace network
|
||||
{
|
||||
address a{};
|
||||
a.set_address(i->ai_addr, static_cast<socklen_t>(i->ai_addrlen));
|
||||
results.emplace_back(std::move(a));
|
||||
results.emplace_back(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -372,6 +373,8 @@ std::size_t std::hash<network::address>::operator()(const network::address& a) c
|
||||
std::string_view{reinterpret_cast<const char*>(a.get_in6_addr().sin6_addr.s6_addr),
|
||||
sizeof(a.get_in6_addr().sin6_addr.s6_addr)});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return hash;
|
||||
|
||||
@@ -63,8 +63,8 @@ namespace network
|
||||
void set_ipv6(const in6_addr& addr);
|
||||
void set_address(const sockaddr* addr, socklen_t length);
|
||||
|
||||
void set_port(unsigned short port);
|
||||
[[nodiscard]] unsigned short get_port() const;
|
||||
void set_port(uint16_t port);
|
||||
[[nodiscard]] uint16_t get_port() const;
|
||||
|
||||
sockaddr& get_addr();
|
||||
sockaddr_in& get_in_addr();
|
||||
|
||||
@@ -71,11 +71,13 @@ namespace network
|
||||
}
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(readability-make-member-function-const)
|
||||
bool socket::bind(const address& target)
|
||||
{
|
||||
return ::bind(this->socket_, &target.get_addr(), target.get_size()) == 0;
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(readability-make-member-function-const)
|
||||
bool socket::set_blocking(const bool blocking)
|
||||
{
|
||||
return socket::set_blocking(this->socket_, blocking);
|
||||
@@ -89,7 +91,10 @@ namespace network
|
||||
#else
|
||||
int flags = fcntl(s, F_GETFL, 0);
|
||||
if (flags == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
|
||||
return fcntl(s, F_SETFL, flags) == 0;
|
||||
#endif
|
||||
@@ -97,30 +102,6 @@ namespace network
|
||||
|
||||
bool socket::sleep(const std::chrono::milliseconds timeout, const bool in_poll) const
|
||||
{
|
||||
/*fd_set fdr;
|
||||
FD_ZERO(&fdr);
|
||||
FD_SET(this->socket_, &fdr);
|
||||
|
||||
const auto msec = timeout.count();
|
||||
|
||||
timeval tv{};
|
||||
tv.tv_sec = static_cast<long>(msec / 1000ll);
|
||||
tv.tv_usec = static_cast<long>((msec % 1000) * 1000);
|
||||
|
||||
const auto retval = select(static_cast<int>(this->socket_) + 1, &fdr, nullptr, nullptr, &tv);
|
||||
if (retval == SOCKET_ERROR)
|
||||
{
|
||||
std::this_thread::sleep_for(1ms);
|
||||
return socket_is_ready;
|
||||
}
|
||||
|
||||
if (retval > 0)
|
||||
{
|
||||
return socket_is_ready;
|
||||
}
|
||||
|
||||
return !socket_is_ready;*/
|
||||
|
||||
std::vector<const socket*> sockets{};
|
||||
sockets.push_back(this);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "tcp_client_socket.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
|
||||
namespace network
|
||||
@@ -58,13 +59,13 @@ namespace network
|
||||
|
||||
std::optional<std::string> tcp_client_socket::receive(const std::optional<size_t> max_size)
|
||||
{
|
||||
char buffer[0x2000];
|
||||
const auto size = std::min(sizeof(buffer), max_size.value_or(sizeof(buffer)));
|
||||
std::array<char, 0x2000> buffer{};
|
||||
const auto size = std::min(buffer.size(), max_size.value_or(buffer.size()));
|
||||
|
||||
const auto result = recv(this->get_socket(), buffer, static_cast<int>(size), 0);
|
||||
const auto result = recv(this->get_socket(), buffer.data(), static_cast<int>(size), 0);
|
||||
if (result > 0)
|
||||
{
|
||||
return std::string(buffer, result);
|
||||
return std::string(buffer.data(), static_cast<size_t>(result));
|
||||
}
|
||||
|
||||
if (result == 0 || (result < 0 && GET_SOCKET_ERROR() == SERR(ECONNRESET)))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "udp_socket.hpp"
|
||||
#include <array>
|
||||
|
||||
namespace network
|
||||
{
|
||||
@@ -31,17 +32,17 @@ namespace network
|
||||
|
||||
std::optional<std::pair<address, std::string>> udp_socket::receive() const
|
||||
{
|
||||
char buffer[0x2000];
|
||||
std::array<char, 0x2000> buffer{};
|
||||
address source{};
|
||||
auto len = source.get_max_size();
|
||||
|
||||
const auto result =
|
||||
recvfrom(this->get_socket(), buffer, static_cast<int>(sizeof(buffer)), 0, &source.get_addr(), &len);
|
||||
recvfrom(this->get_socket(), buffer.data(), static_cast<int>(buffer.size()), 0, &source.get_addr(), &len);
|
||||
if (result == SOCKET_ERROR)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return {{source, std::string(buffer, result)}};
|
||||
return {{source, std::string(buffer.data(), static_cast<size_t>(result))}};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user