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 4b1289b commit a8d5d85
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 102 deletions.
4 changes: 2 additions & 2 deletions Test/Sample.AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void example_binding(const vu::Endpoint& endpoint)
server.bind(endpoint);
server.listen();
server.run();
server.close();
// server.stop();
}

void example_inheritance(const vu::Endpoint& endpoint)
Expand Down Expand Up @@ -77,7 +77,7 @@ void example_inheritance(const vu::Endpoint& endpoint)
server.bind(endpoint);
server.listen();
server.run();
server.close();
// server.stop();
}

#endif // VU_INET_ENABLED
Expand Down
30 changes: 14 additions & 16 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#include <vector>
#include <thread>
#include <memory>
#include <atomic>
#include <numeric>
#include <sstream>
#include <cassert>
Expand Down Expand Up @@ -1295,7 +1296,6 @@ class Socket : public LastError
const address_family_t af = AF_INET,
const type_t type = SOCK_STREAM,
const protocol_t proto = IPPROTO_IP,
const bool wsa = true,
const Options* options = nullptr
);
Socket(const Socket& right);
Expand All @@ -1306,7 +1306,6 @@ class Socket : public LastError
const Socket& operator=(const Socket& right);

const SOCKET& vuapi handle() const;
const WSADATA& vuapi wsa_data() const;
const address_family_t vuapi af() const;
const type_t vuapi type() const;
const protocol_t vuapi protocol() const;
Expand Down Expand Up @@ -1342,9 +1341,9 @@ class Socket : public LastError

IResult vuapi close();

const sockaddr_in vuapi get_local_sai() const;
const sockaddr_in vuapi get_remote_sai() const;
std::string vuapi get_host_name() const;
const sockaddr_in vuapi get_local_sai();
const sockaddr_in vuapi get_remote_sai();
std::string vuapi get_host_name();

Options& options();

Expand All @@ -1358,15 +1357,14 @@ class Socket : public LastError
std::string vuapi get_host_address(const std::string& name) const;

private:
bool m_wsa;
type_t m_type;
WSADATA m_wsa_data;
address_family_t m_af;
protocol_t m_proto;
sockaddr_in m_sai;
SOCKET m_socket;
Options m_options;
bool m_self;
bool m_attached;
};

class AsyncSocket : public LastError
Expand Down Expand Up @@ -1424,9 +1422,7 @@ class AsyncSocket : public LastError
VUResult vuapi listen(const int maxcon = SOMAXCONN);

VUResult vuapi run(const bool in_worker_thread = false);

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

void vuapi get_connections(std::set<SOCKET>& connections);
VUResult vuapi disconnect_connections(const Socket::shutdowns_t flags = SD_BOTH, const bool cleanup = false);
Expand Down Expand Up @@ -1454,16 +1450,18 @@ class AsyncSocket : public LastError
IResult vuapi do_close(WSANETWORKEVENTS& events, SOCKET& connection);

protected:
HANDLE m_thread;
std::atomic<bool> m_running;

side_type m_side;
vu::Socket m_socket;
side_type m_side;
bool m_running;
DWORD m_n_events;

DWORD m_n_events;
SOCKET m_connections[WSA_MAXIMUM_WAIT_EVENTS];
std::recursive_mutex m_mutex_client_list;
WSAEVENT m_events[WSA_MAXIMUM_WAIT_EVENTS];
std::recursive_mutex m_mutex_client_list;

fn_prototype_t m_functions[function::UNDEFINED];
std::mutex m_mutex;
HANDLE m_thread;
};

#endif // VU_INET_ENABLED
Expand Down
68 changes: 22 additions & 46 deletions src/details/asyncsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AsyncSocket::AsyncSocket(
const vu::Socket::type_t type,
const vu::Socket::protocol_t proto,
const vu::Socket::Options* options
) : m_socket(af, type, proto, true, options), m_thread(INVALID_HANDLE_VALUE), LastError()
) : m_socket(af, type, proto, options), m_thread(INVALID_HANDLE_VALUE), LastError()
{
UNREFERENCED_PARAMETER(m_side);

Expand All @@ -33,15 +33,12 @@ AsyncSocket::AsyncSocket(

AsyncSocket::~AsyncSocket()
{
this->close();
}

void vuapi AsyncSocket::initialze()
{
m_n_events = 0;

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

memset(m_connections, int(INVALID_SOCKET), sizeof(m_connections));
memset(m_events, int(0), sizeof(m_events));

Expand Down Expand Up @@ -97,8 +94,6 @@ VUResult vuapi AsyncSocket::listen(const int maxcon)
return 2;
}

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

m_connections[m_n_events] = m_socket.handle();
m_events[m_n_events] = event;
m_n_events++;
Expand All @@ -110,9 +105,14 @@ VUResult vuapi AsyncSocket::listen(const int maxcon)
return result;
}

IResult vuapi AsyncSocket::close(const Socket::shutdowns_t flags, const bool cleanup)
IResult vuapi AsyncSocket::stop(const Socket::shutdowns_t flags, const bool cleanup)
{
this->stop();
if (!m_socket.available())
{
return 1;
}

m_running = false;

this->disconnect_connections(flags, cleanup);

Expand All @@ -121,18 +121,6 @@ IResult vuapi AsyncSocket::close(const Socket::shutdowns_t flags, const bool cle
TerminateThread(m_thread, 0); // CloseHandle(m_thread);
}

auto result = m_socket.close();

m_last_error_code = GetLastError();

return result;
}

VUResult vuapi AsyncSocket::stop()
{
m_mutex.lock();
m_running = false;
m_mutex.unlock();
return VU_OK;
}

Expand All @@ -152,8 +140,6 @@ VUResult vuapi AsyncSocket::connect(const Endpoint& endpoint)
return 2;
}

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

auto result = m_socket.connect(endpoint);
if (result == VU_OK)
{
Expand All @@ -179,31 +165,29 @@ void vuapi AsyncSocket::get_connections(std::set<SOCKET>& connections)
{
connections.clear();

if (m_socket.available())
if (!m_socket.available())
{
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
return;
}

for (auto& socket : m_connections)
for (auto& socket : m_connections)
{
if (socket == INVALID_SOCKET) // ignore invalid socket handle
{
if (socket == INVALID_SOCKET) // ignore invalid socket handle
{
continue;
}

if (m_side == side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle
{
continue;
}
continue;
}

connections.insert(socket);
if (m_side == side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle
{
continue;
}

connections.insert(socket);
}
}

VUResult vuapi AsyncSocket::disconnect_connections(const Socket::shutdowns_t flags, const bool cleanup)
{
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

std::set<SOCKET> connections;
this->get_connections(connections);
for (const auto& connection : connections)
Expand Down Expand Up @@ -365,8 +349,6 @@ IResult vuapi AsyncSocket::do_open(WSANETWORKEVENTS& events, SOCKET& connection)
return events.iErrorCode[FD_ACCEPT_BIT];
}

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

Socket::Handle obj = { 0 };
int n = static_cast<int>(sizeof(obj.sai));

Expand Down Expand Up @@ -398,8 +380,6 @@ IResult vuapi AsyncSocket::do_recv(WSANETWORKEVENTS& events, SOCKET& connection)
return events.iErrorCode[FD_READ_BIT];
}

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

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

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

Socket socket(m_socket);
socket.attach(connection);
this->on_send(socket);
Expand All @@ -427,16 +405,14 @@ IResult vuapi AsyncSocket::do_send(WSANETWORKEVENTS& events, SOCKET& connection)

IResult vuapi AsyncSocket::do_close(WSANETWORKEVENTS& events, SOCKET& connection)
{
// TODO: In certain cases(e.g., user - mode drivers), it crashes.
// TODO: Vic. In certain cases(e.g., user - mode drivers), it crashes.
// I'm not sure why, so temporarily comment out these codes.
//
// if (events.iErrorCode[FD_CLOSE_BIT] != 0)
// {
// return events.iErrorCode[FD_CLOSE_BIT];
// }

std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);

std::vector<std::pair<SOCKET, HANDLE>> in_used_connections;

for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; i++)
Expand Down
Loading

0 comments on commit a8d5d85

Please sign in to comment.