Skip to content

Commit

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

enum class side_type : uint
enum class side_type : int
{
UNDEFINED = -1,
SERVER,
CLIENT,
UNDEFINED,
};

struct Handle
Expand Down Expand Up @@ -1347,7 +1347,7 @@ class Socket : public LastError
IResult vuapi recv_from(Buffer& data, const Handle& socket);
IResult vuapi recv_all_from(Buffer& data, const Handle& socket);

IResult vuapi close();
IResult vuapi close(const Socket::shutdowns_t flags = SD_BOTH, const bool cleanup = false);

const sockaddr_in vuapi get_local_sai();
const sockaddr_in vuapi get_remote_sai();
Expand Down
22 changes: 11 additions & 11 deletions src/details/asyncsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ IResult vuapi AsyncSocket::stop(const Socket::shutdowns_t flags, const bool clea

if (m_thread != INVALID_HANDLE_VALUE)
{
TerminateThread(m_thread, 0); // CloseHandle(m_thread);
// TerminateThread(m_thread, 0); // CloseHandle(m_thread);
// Using atomic for `m_running = false`. So, the thread will be closed after set `m_running = false` to exit looping
}

return VU_OK;
Expand Down Expand Up @@ -184,7 +185,7 @@ VUResult vuapi AsyncSocket::disconnect_connections(const Socket::shutdowns_t fla
this->get_connections(connections);
for (const auto& connection : connections)
{
Socket socket(m_socket);
Socket socket;
socket.attach(connection);
socket.disconnect(flags, cleanup);
}
Expand Down Expand Up @@ -326,7 +327,7 @@ IResult vuapi AsyncSocket::do_connect(WSANETWORKEVENTS& events, SOCKET& connecti
return events.iErrorCode[FD_CONNECT_BIT];
}

Socket socket(m_socket);
Socket socket;
socket.attach(connection);
this->on_connect(socket);
socket.detach();
Expand Down Expand Up @@ -357,7 +358,7 @@ IResult vuapi AsyncSocket::do_open(WSANETWORKEVENTS& events, SOCKET& connection)
m_connections[m_n_events] = obj.s;
m_n_events++;

Socket socket(m_socket);
Socket socket;
socket.attach(obj);
this->on_open(socket);
socket.detach();
Expand All @@ -372,7 +373,7 @@ IResult vuapi AsyncSocket::do_recv(WSANETWORKEVENTS& events, SOCKET& connection)
return events.iErrorCode[FD_READ_BIT];
}

Socket socket(m_socket);
Socket socket;
socket.attach(connection);
this->on_recv(socket);
socket.detach();
Expand All @@ -387,7 +388,7 @@ IResult vuapi AsyncSocket::do_send(WSANETWORKEVENTS& events, SOCKET& connection)
return events.iErrorCode[FD_WRITE_BIT];
}

Socket socket(m_socket);
Socket socket;
socket.attach(connection);
this->on_send(socket);
socket.detach();
Expand Down Expand Up @@ -433,13 +434,12 @@ IResult vuapi AsyncSocket::do_close(WSANETWORKEVENTS& events, SOCKET& connection
m_n_events++;
}

Socket socket(m_socket);
Socket socket;
socket.attach(connection);
this->on_close(socket);
socket.close();
socket.detach();

::closesocket(connection);

connection = INVALID_SOCKET;

// CompressArrays(m_Events, m_Sockets, &m_nEvents);
Expand Down Expand Up @@ -499,7 +499,7 @@ IResult vuapi AsyncSocket::send(
int size,
const Socket::flags_t flags)
{
Socket socket(m_socket);
Socket socket;
socket.attach(connection);
return socket.send(ptr_data, size, flags);
}
Expand All @@ -509,7 +509,7 @@ IResult vuapi AsyncSocket::send(
const Buffer& data,
const Socket::flags_t flags)
{
Socket socket(m_socket);
Socket socket;
socket.attach(connection);
return socket.send(data, flags);
}
Expand Down
46 changes: 21 additions & 25 deletions src/details/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,7 @@ Socket::~Socket()

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

if (WSACleanup() == INVALID_SOCKET)
Expand Down Expand Up @@ -460,7 +455,7 @@ IResult vuapi Socket::recv(char* ptr_data, int size, const flags_t flags)

timeval timeout = { 0 };
timeout.tv_usec = 0;
timeout.tv_sec = m_options.timeout.recv;
timeout.tv_sec = m_options.timeout.recv;

int status = ::select(0, &fds_read, nullptr, nullptr, &timeout);
if (status == SOCKET_ERROR)
Expand Down Expand Up @@ -586,7 +581,7 @@ IResult vuapi Socket::recv_from(char* ptr_data, int size, const Handle& socket)
}

int n = sizeof(socket.sai);
IResult z = ::recvfrom(m_socket, ptr_data, size, 0, (struct sockaddr *)&socket.sai, &n);
IResult z = ::recvfrom(m_socket, ptr_data, size, 0, (struct sockaddr*)&socket.sai, &n);
if (z == SOCKET_ERROR)
{
m_last_error_code = GetLastError();
Expand Down Expand Up @@ -630,21 +625,7 @@ IResult vuapi Socket::recv_all_from(Buffer& buffer, const Handle& socket)
return IResult(buffer.size());
}

VUResult vuapi Socket::close()
{
if (!this->available())
{
return 1;
}

::closesocket(m_socket);

m_socket = INVALID_SOCKET;

return VU_OK;
}

VUResult vuapi Socket::disconnect(const shutdowns_t flags, const bool cleanup)
VUResult vuapi Socket::close(const shutdowns_t flags, const bool cleanup)
{
if (!this->available())
{
Expand All @@ -659,8 +640,8 @@ VUResult vuapi Socket::disconnect(const shutdowns_t flags, const bool cleanup)

if (::shutdown(m_socket, flags) == SOCKET_ERROR)
{
m_last_error_code = GetLastError();
return 2;
// m_last_error_code = GetLastError();
// return 2;
}

if (::closesocket(m_socket) == SOCKET_ERROR)
Expand All @@ -674,6 +655,21 @@ VUResult vuapi Socket::disconnect(const shutdowns_t flags, const bool cleanup)
return VU_OK;
}

VUResult vuapi Socket::disconnect(const shutdowns_t flags, const bool cleanup)
{
if (!this->available())
{
return 1;
}

if (this->close(flags, cleanup) != VU_OK)
{
return 2;
}

return VU_OK;
}

std::string vuapi Socket::get_host_name()
{
std::string result = "";
Expand Down

0 comments on commit 1ccd728

Please sign in to comment.