Finish tcp client socket

This commit is contained in:
momo5502
2025-01-12 08:23:47 +01:00
parent 21e2f6f999
commit 8333c25f2c
9 changed files with 57 additions and 30 deletions

View File

@@ -2,12 +2,24 @@
namespace network
{
tcp_client_socket::tcp_client_socket(SOCKET s, const address& target)
: socket(s),
target_(target)
tcp_client_socket::tcp_client_socket(const int af)
: socket(af, SOCK_STREAM, IPPROTO_TCP)
{
}
tcp_client_socket::tcp_client_socket(SOCKET s)
: socket(s)
{
}
tcp_client_socket::~tcp_client_socket()
{
if (*this && this->get_target())
{
::shutdown(this->get_socket(), SHUT_RDWR);
}
}
bool tcp_client_socket::send(const void* data, const size_t size) const
{
const auto res = ::send(this->get_socket(), static_cast<const char*>(data), static_cast<send_size>(size), 0);
@@ -33,8 +45,26 @@ namespace network
return true;
}
address tcp_client_socket::get_target() const
std::optional<address> tcp_client_socket::get_target() const
{
return this->target_;
address a{};
auto len = a.get_max_size();
if (getpeername(this->get_socket(), &a.get_addr(), &len) == SOCKET_ERROR)
{
return std::nullopt;
}
return a;
}
bool tcp_client_socket::connect(const address& target)
{
if (::connect(this->get_socket(), &target.get_addr(), target.get_size()) != SOCKET_ERROR)
{
return true;
}
const auto error = GET_SOCKET_ERROR();
return error == SOCK_WOULDBLOCK;
}
}