Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Sep 3, 2024
1 parent a8d5d85 commit aa7148e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
20 changes: 10 additions & 10 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,13 @@ class Socket : public LastError
typedef int flags_t;
typedef int shutdowns_t;

enum class side_type : uint
{
SERVER,
CLIENT,
UNDEFINED,
};

struct Handle
{
SOCKET s;
Expand Down Expand Up @@ -1305,6 +1312,7 @@ class Socket : public LastError
bool operator!=(const Socket& right);
const Socket& operator=(const Socket& right);

side_type vuapi side() const;
const SOCKET& vuapi handle() const;
const address_family_t vuapi af() const;
const type_t vuapi type() const;
Expand Down Expand Up @@ -1357,6 +1365,7 @@ class Socket : public LastError
std::string vuapi get_host_address(const std::string& name) const;

private:
side_type m_side;
type_t m_type;
WSADATA m_wsa_data;
address_family_t m_af;
Expand All @@ -1382,12 +1391,6 @@ class AsyncSocket : public LastError
UNDEFINED,
};

enum class side_type : uint
{
SERVER,
CLIENT,
};

AsyncSocket(
const vu::Socket::address_family_t af = AF_INET,
const vu::Socket::type_t type = SOCK_STREAM,
Expand All @@ -1396,8 +1399,7 @@ class AsyncSocket : public LastError
);
virtual ~AsyncSocket();

side_type vuapi side() const;

Socket::side_type vuapi side() const;
bool vuapi available() const;
bool vuapi running() const;

Expand Down Expand Up @@ -1453,9 +1455,7 @@ class AsyncSocket : public LastError
HANDLE m_thread;
std::atomic<bool> m_running;

side_type m_side;
vu::Socket m_socket;

DWORD m_n_events;
SOCKET m_connections[WSA_MAXIMUM_WAIT_EVENTS];
WSAEVENT m_events[WSA_MAXIMUM_WAIT_EVENTS];
Expand Down
16 changes: 4 additions & 12 deletions src/details/asyncsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ AsyncSocket::AsyncSocket(
const vu::Socket::Options* options
) : m_socket(af, type, proto, options), m_thread(INVALID_HANDLE_VALUE), LastError()
{
UNREFERENCED_PARAMETER(m_side);

this->initialze();

for (uint i = 0; i < function::UNDEFINED; i++)
Expand All @@ -45,9 +43,10 @@ void vuapi AsyncSocket::initialze()
m_running = false;
}

AsyncSocket::side_type vuapi AsyncSocket::side() const

Socket::side_type vuapi AsyncSocket::side() const
{
return m_side;
return m_socket.side();
}

bool vuapi AsyncSocket::available() const
Expand All @@ -68,13 +67,7 @@ VUResult vuapi AsyncSocket::bind(const Endpoint& endpoint)
VUResult vuapi AsyncSocket::bind(const std::string& address, const ushort port)
{
auto result = m_socket.bind(address, port);
if (result == VU_OK)
{
m_side = side_type::SERVER;
}

this->set_last_error_code(m_socket.get_last_error_code());

return result;
}

Expand Down Expand Up @@ -144,7 +137,6 @@ VUResult vuapi AsyncSocket::connect(const Endpoint& endpoint)
if (result == VU_OK)
{
m_last_error_code = ERROR_SUCCESS;
m_side = side_type::CLIENT;
m_connections[m_n_events] = m_socket.handle();
m_events[m_n_events] = event;
m_n_events++;
Expand Down Expand Up @@ -177,7 +169,7 @@ void vuapi AsyncSocket::get_connections(std::set<SOCKET>& connections)
continue;
}

if (m_side == side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle
if (m_socket.side() == Socket::side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle
{
continue;
}
Expand Down
30 changes: 22 additions & 8 deletions src/details/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ Socket::Socket(
const type_t type,
const protocol_t proto,
const Options* options
) : LastError(), m_af(af), m_type(type), m_proto(proto), m_attached(false)
) : LastError(), m_af(af), m_type(type), m_proto(proto)
, m_attached(false), m_side(Socket::side_type::UNDEFINED)
{
ZeroMemory(&m_wsa_data, sizeof(m_wsa_data));
if (WSAStartup(MAKEWORD(2, 2), &m_wsa_data) == INVALID_SOCKET)
Expand Down Expand Up @@ -141,11 +142,14 @@ Socket::~Socket()
return; // ignore if the connection is attached from outside
}

if (::closesocket(m_socket) == INVALID_SOCKET)
if (this->available())
{
assert("close socket failed.");
m_last_error_code = GetLastError();
m_socket = INVALID_SOCKET;
if (::closesocket(m_socket) == INVALID_SOCKET)
{
assert("close socket failed.");
m_last_error_code = GetLastError();
m_socket = INVALID_SOCKET;
}
}

if (WSACleanup() == INVALID_SOCKET)
Expand Down Expand Up @@ -183,6 +187,11 @@ const vu::Socket& Socket::operator=(const Socket& right)
return *this;
}

Socket::side_type vuapi Socket::side() const
{
return m_side;
}

bool vuapi Socket::valid(const SOCKET& socket) const
{
return !(socket == 0 || socket == INVALID_SOCKET);
Expand Down Expand Up @@ -330,6 +339,8 @@ VUResult vuapi Socket::bind(const std::string& address, const ushort port)
return 3;
}

m_side = side_type::SERVER;

return VU_OK;
}

Expand Down Expand Up @@ -393,13 +404,16 @@ VUResult vuapi Socket::connect(const Endpoint& endpoint)
m_sai.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
m_sai.sin_port = htons(endpoint.m_port);

VUResult result = VU_OK;

if (::connect(m_socket, (const struct sockaddr*)&m_sai, sizeof(m_sai)) == SOCKET_ERROR)
{
m_last_error_code = GetLastError();
return m_last_error_code == WSAEWOULDBLOCK ? VU_OK : 2;
result = GetLastError() == WSAEWOULDBLOCK ? VU_OK : 2; // ignore 'A non-blocking socket operation could not be completed immediately.'
}

return VU_OK;
m_side = side_type::CLIENT;

return result;
}

IResult vuapi Socket::send(const char* ptr_data, int size, const flags_t flags)
Expand Down

0 comments on commit aa7148e

Please sign in to comment.