Skip to content

Commit a8d5d85

Browse files
committed
Vutils
1 parent 4b1289b commit a8d5d85

File tree

4 files changed

+78
-102
lines changed

4 files changed

+78
-102
lines changed

Test/Sample.AsyncSocket.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void example_binding(const vu::Endpoint& endpoint)
3838
server.bind(endpoint);
3939
server.listen();
4040
server.run();
41-
server.close();
41+
// server.stop();
4242
}
4343

4444
void example_inheritance(const vu::Endpoint& endpoint)
@@ -77,7 +77,7 @@ void example_inheritance(const vu::Endpoint& endpoint)
7777
server.bind(endpoint);
7878
server.listen();
7979
server.run();
80-
server.close();
80+
// server.stop();
8181
}
8282

8383
#endif // VU_INET_ENABLED

include/Vutils.h

+14-16
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
#include <vector>
110110
#include <thread>
111111
#include <memory>
112+
#include <atomic>
112113
#include <numeric>
113114
#include <sstream>
114115
#include <cassert>
@@ -1295,7 +1296,6 @@ class Socket : public LastError
12951296
const address_family_t af = AF_INET,
12961297
const type_t type = SOCK_STREAM,
12971298
const protocol_t proto = IPPROTO_IP,
1298-
const bool wsa = true,
12991299
const Options* options = nullptr
13001300
);
13011301
Socket(const Socket& right);
@@ -1306,7 +1306,6 @@ class Socket : public LastError
13061306
const Socket& operator=(const Socket& right);
13071307

13081308
const SOCKET& vuapi handle() const;
1309-
const WSADATA& vuapi wsa_data() const;
13101309
const address_family_t vuapi af() const;
13111310
const type_t vuapi type() const;
13121311
const protocol_t vuapi protocol() const;
@@ -1342,9 +1341,9 @@ class Socket : public LastError
13421341

13431342
IResult vuapi close();
13441343

1345-
const sockaddr_in vuapi get_local_sai() const;
1346-
const sockaddr_in vuapi get_remote_sai() const;
1347-
std::string vuapi get_host_name() const;
1344+
const sockaddr_in vuapi get_local_sai();
1345+
const sockaddr_in vuapi get_remote_sai();
1346+
std::string vuapi get_host_name();
13481347

13491348
Options& options();
13501349

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

13601359
private:
1361-
bool m_wsa;
13621360
type_t m_type;
13631361
WSADATA m_wsa_data;
13641362
address_family_t m_af;
13651363
protocol_t m_proto;
13661364
sockaddr_in m_sai;
13671365
SOCKET m_socket;
13681366
Options m_options;
1369-
bool m_self;
1367+
bool m_attached;
13701368
};
13711369

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

14261424
VUResult vuapi run(const bool in_worker_thread = false);
1427-
1428-
VUResult vuapi stop();
1429-
IResult vuapi close(const Socket::shutdowns_t flags = SD_BOTH, const bool cleanup = false);
1425+
VUResult vuapi stop(const Socket::shutdowns_t flags = SD_BOTH, const bool cleanup = false);
14301426

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

14561452
protected:
1453+
HANDLE m_thread;
1454+
std::atomic<bool> m_running;
1455+
1456+
side_type m_side;
14571457
vu::Socket m_socket;
1458-
side_type m_side;
1459-
bool m_running;
1460-
DWORD m_n_events;
1458+
1459+
DWORD m_n_events;
14611460
SOCKET m_connections[WSA_MAXIMUM_WAIT_EVENTS];
1462-
std::recursive_mutex m_mutex_client_list;
14631461
WSAEVENT m_events[WSA_MAXIMUM_WAIT_EVENTS];
1462+
std::recursive_mutex m_mutex_client_list;
1463+
14641464
fn_prototype_t m_functions[function::UNDEFINED];
1465-
std::mutex m_mutex;
1466-
HANDLE m_thread;
14671465
};
14681466

14691467
#endif // VU_INET_ENABLED

src/details/asyncsocket.cpp

+22-46
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ AsyncSocket::AsyncSocket(
1919
const vu::Socket::type_t type,
2020
const vu::Socket::protocol_t proto,
2121
const vu::Socket::Options* options
22-
) : m_socket(af, type, proto, true, options), m_thread(INVALID_HANDLE_VALUE), LastError()
22+
) : m_socket(af, type, proto, options), m_thread(INVALID_HANDLE_VALUE), LastError()
2323
{
2424
UNREFERENCED_PARAMETER(m_side);
2525

@@ -33,15 +33,12 @@ AsyncSocket::AsyncSocket(
3333

3434
AsyncSocket::~AsyncSocket()
3535
{
36-
this->close();
3736
}
3837

3938
void vuapi AsyncSocket::initialze()
4039
{
4140
m_n_events = 0;
4241

43-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
44-
4542
memset(m_connections, int(INVALID_SOCKET), sizeof(m_connections));
4643
memset(m_events, int(0), sizeof(m_events));
4744

@@ -97,8 +94,6 @@ VUResult vuapi AsyncSocket::listen(const int maxcon)
9794
return 2;
9895
}
9996

100-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
101-
10297
m_connections[m_n_events] = m_socket.handle();
10398
m_events[m_n_events] = event;
10499
m_n_events++;
@@ -110,9 +105,14 @@ VUResult vuapi AsyncSocket::listen(const int maxcon)
110105
return result;
111106
}
112107

113-
IResult vuapi AsyncSocket::close(const Socket::shutdowns_t flags, const bool cleanup)
108+
IResult vuapi AsyncSocket::stop(const Socket::shutdowns_t flags, const bool cleanup)
114109
{
115-
this->stop();
110+
if (!m_socket.available())
111+
{
112+
return 1;
113+
}
114+
115+
m_running = false;
116116

117117
this->disconnect_connections(flags, cleanup);
118118

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

124-
auto result = m_socket.close();
125-
126-
m_last_error_code = GetLastError();
127-
128-
return result;
129-
}
130-
131-
VUResult vuapi AsyncSocket::stop()
132-
{
133-
m_mutex.lock();
134-
m_running = false;
135-
m_mutex.unlock();
136124
return VU_OK;
137125
}
138126

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

155-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
156-
157143
auto result = m_socket.connect(endpoint);
158144
if (result == VU_OK)
159145
{
@@ -179,31 +165,29 @@ void vuapi AsyncSocket::get_connections(std::set<SOCKET>& connections)
179165
{
180166
connections.clear();
181167

182-
if (m_socket.available())
168+
if (!m_socket.available())
183169
{
184-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
170+
return;
171+
}
185172

186-
for (auto& socket : m_connections)
173+
for (auto& socket : m_connections)
174+
{
175+
if (socket == INVALID_SOCKET) // ignore invalid socket handle
187176
{
188-
if (socket == INVALID_SOCKET) // ignore invalid socket handle
189-
{
190-
continue;
191-
}
192-
193-
if (m_side == side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle
194-
{
195-
continue;
196-
}
177+
continue;
178+
}
197179

198-
connections.insert(socket);
180+
if (m_side == side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle
181+
{
182+
continue;
199183
}
184+
185+
connections.insert(socket);
200186
}
201187
}
202188

203189
VUResult vuapi AsyncSocket::disconnect_connections(const Socket::shutdowns_t flags, const bool cleanup)
204190
{
205-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
206-
207191
std::set<SOCKET> connections;
208192
this->get_connections(connections);
209193
for (const auto& connection : connections)
@@ -365,8 +349,6 @@ IResult vuapi AsyncSocket::do_open(WSANETWORKEVENTS& events, SOCKET& connection)
365349
return events.iErrorCode[FD_ACCEPT_BIT];
366350
}
367351

368-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
369-
370352
Socket::Handle obj = { 0 };
371353
int n = static_cast<int>(sizeof(obj.sai));
372354

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

401-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
402-
403383
Socket socket(m_socket);
404384
socket.attach(connection);
405385
this->on_recv(socket);
@@ -415,8 +395,6 @@ IResult vuapi AsyncSocket::do_send(WSANETWORKEVENTS& events, SOCKET& connection)
415395
return events.iErrorCode[FD_WRITE_BIT];
416396
}
417397

418-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
419-
420398
Socket socket(m_socket);
421399
socket.attach(connection);
422400
this->on_send(socket);
@@ -427,16 +405,14 @@ IResult vuapi AsyncSocket::do_send(WSANETWORKEVENTS& events, SOCKET& connection)
427405

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

438-
std::lock_guard<std::recursive_mutex> lg(m_mutex_client_list);
439-
440416
std::vector<std::pair<SOCKET, HANDLE>> in_used_connections;
441417

442418
for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; i++)

0 commit comments

Comments
 (0)