Skip to content

Commit 5024db8

Browse files
committed
Vutils
1 parent a7aa816 commit 5024db8

File tree

5 files changed

+110
-31
lines changed

5 files changed

+110
-31
lines changed

Test/Sample.AsyncSocket.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#if defined(VU_INET_ENABLED)
88

9-
void example_binding(const vu::Socket::Endpoint& endpoint)
9+
void example_binding(const vu::Endpoint& endpoint)
1010
{
1111
vu::AsyncSocket server;
1212

@@ -41,7 +41,7 @@ void example_binding(const vu::Socket::Endpoint& endpoint)
4141
server.close();
4242
}
4343

44-
void example_inheritance(const vu::Socket::Endpoint& endpoint)
44+
void example_inheritance(const vu::Endpoint& endpoint)
4545
{
4646
class CAsyncSocketServer : public vu::AsyncSocket
4747
{

Test/Sample.Socket.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ DEF_SAMPLE(Socket)
1818

1919
vu::Socket socket;
2020

21-
if (socket.connect("ipv4.download.thinkbroadband.com", 80) != vu::VU_OK)
21+
if (socket.connect(vu::Endpoint("ipv4.download.thinkbroadband.com", 80)) != vu::VU_OK)
2222
{
2323
std::tcout << ts("Socket -> Connect -> Failed") << std::endl;
2424
return 1;

include/Vutils.h

+20-13
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,23 @@ class LibraryW : public LastError
12001200
#define VU_DEFAULT_SEND_RECV_TIMEOUT 3 // 3 seconds
12011201
#define VU_DEFAULT_SEND_RECV_BLOCK_SIZE KiB // 1 KiB
12021202

1203+
struct Endpoint
1204+
{
1205+
std::string m_host;
1206+
ushort m_port;
1207+
1208+
Endpoint(const std::string& endpoint);
1209+
Endpoint(const std::wstring& endpoint);
1210+
Endpoint(const std::string& host, const ushort port);
1211+
Endpoint(const std::wstring& host, const ushort port);
1212+
1213+
bool operator==(const Endpoint& right);
1214+
bool operator!=(const Endpoint& right);
1215+
const Endpoint& operator=(const Endpoint& right);
1216+
const Endpoint& operator=(const std::string& right);
1217+
const Endpoint& operator=(const std::wstring& right);
1218+
};
1219+
12031220
class Socket : public LastError
12041221
{
12051222
public:
@@ -1216,14 +1233,6 @@ class Socket : public LastError
12161233
char ip[15];
12171234
};
12181235

1219-
struct Endpoint
1220-
{
1221-
std::string host;
1222-
ushort port;
1223-
1224-
Endpoint(const std::string& host, const ushort port) : host(host), port(port) {}
1225-
};
1226-
12271236
struct Options
12281237
{
12291238
struct
@@ -1247,7 +1256,7 @@ class Socket : public LastError
12471256
);
12481257
virtual ~Socket();
12491258

1250-
SOCKET& vuapi handle();
1259+
const SOCKET& vuapi handle() const;
12511260
const WSADATA& vuapi wsa_data() const;
12521261
const address_family_t vuapi af() const;
12531262
const type_t vuapi type() const;
@@ -1266,8 +1275,6 @@ class Socket : public LastError
12661275
VUResult vuapi accept(Handle& socket);
12671276

12681277
VUResult vuapi connect(const Endpoint& endpoint);
1269-
VUResult vuapi connect(const std::string& address, const ushort port);
1270-
12711278
VUResult vuapi disconnect(const shutdowns_t flags = SD_BOTH);
12721279

12731280
IResult vuapi send(const char* ptr_data, int size, const flags_t flags = MSG_NONE);
@@ -1351,10 +1358,10 @@ class AsyncSocket : public LastError
13511358
* https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaeventselect?redirectedfrom=MSDN#return-value
13521359
* Note: After connected (FD_CONNECT), client will be auto generated first event FD_WRITE.
13531360
*/
1354-
VUResult vuapi connect(const Socket::Endpoint& endpoint);
1361+
VUResult vuapi connect(const Endpoint& endpoint);
13551362
VUResult vuapi connect(const std::string& address, const ushort port);
13561363

1357-
VUResult vuapi bind(const Socket::Endpoint& endpoint);
1364+
VUResult vuapi bind(const Endpoint& endpoint);
13581365
VUResult vuapi bind(const std::string& address, const ushort port);
13591366

13601367
/**

src/details/asyncsocket.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ bool vuapi AsyncSocket::running()
6161
return m_running;
6262
}
6363

64-
VUResult vuapi AsyncSocket::bind(const Socket::Endpoint& endpoint)
64+
VUResult vuapi AsyncSocket::bind(const Endpoint& endpoint)
6565
{
66-
return this->bind(endpoint.host, endpoint.port);
66+
return this->bind(endpoint.m_host, endpoint.m_port);
6767
}
6868

6969
VUResult vuapi AsyncSocket::bind(const std::string& address, const ushort port)
@@ -132,7 +132,7 @@ VUResult vuapi AsyncSocket::stop()
132132
return VU_OK;
133133
}
134134

135-
VUResult vuapi AsyncSocket::connect(const Socket::Endpoint& endpoint)
135+
VUResult vuapi AsyncSocket::connect(const Endpoint& endpoint)
136136
{
137137
if (!m_socket.available())
138138
{
@@ -165,7 +165,7 @@ VUResult vuapi AsyncSocket::connect(const Socket::Endpoint& endpoint)
165165

166166
VUResult vuapi AsyncSocket::connect(const std::string& address, const ushort port)
167167
{
168-
Socket::Endpoint endpoint(address, port);
168+
Endpoint endpoint(address, port);
169169
return this->connect(endpoint);
170170
}
171171

src/details/socket.cpp

+83-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,83 @@ namespace vu
2222

2323
#ifdef VU_INET_ENABLED
2424

25+
/**
26+
* Endpoint
27+
*/
28+
29+
Endpoint::Endpoint(const std::string& endpoint)
30+
{
31+
auto l = split_string_A(endpoint, ":");
32+
if (l.size() != 2)
33+
{
34+
throw "invalid endpoint";
35+
}
36+
else
37+
{
38+
m_host = l[0];
39+
m_port = ushort(std::stoul(l[1]));
40+
}
41+
}
42+
43+
Endpoint::Endpoint(const std::wstring& endpoint)
44+
{
45+
auto temp = to_string_A(endpoint);
46+
auto l = split_string_A(temp, ":");
47+
if (l.size() != 2)
48+
{
49+
throw "invalid endpoint";
50+
}
51+
else
52+
{
53+
m_host = l[0];
54+
m_port = ushort(std::stoul(l[1]));
55+
}
56+
}
57+
58+
Endpoint::Endpoint(const std::wstring& host, const ushort port) : m_port(port)
59+
{
60+
m_host = to_string_A(host);
61+
}
62+
63+
Endpoint::Endpoint(const std::string& host, const ushort port) : m_host(host), m_port(port)
64+
{
65+
}
66+
67+
bool Endpoint::operator==(const Endpoint& right)
68+
{
69+
return m_host == right.m_host && m_port == right.m_port;
70+
}
71+
72+
bool Endpoint::operator!=(const Endpoint& right)
73+
{
74+
return !(*this == right);
75+
}
76+
77+
const vu::Endpoint& Endpoint::operator=(const Endpoint& right)
78+
{
79+
m_host = right.m_host;
80+
m_port = right.m_port;
81+
return *this;
82+
}
83+
84+
const vu::Endpoint& Endpoint::operator=(const std::string& right)
85+
{
86+
Endpoint endpoint(right);
87+
*this = endpoint;
88+
return *this;
89+
}
90+
91+
const vu::Endpoint& Endpoint::operator=(const std::wstring& right)
92+
{
93+
Endpoint endpoint(right);
94+
*this = endpoint;
95+
return *this;
96+
}
97+
98+
/**
99+
* Socket
100+
*/
101+
25102
Socket::Socket(
26103
const address_family_t af,
27104
const type_t type,
@@ -117,7 +194,7 @@ const Socket::protocol_t vuapi Socket::protocol() const
117194
return m_proto;
118195
}
119196

120-
SOCKET& vuapi Socket::handle()
197+
const SOCKET& vuapi Socket::handle() const
121198
{
122199
return m_socket;
123200
}
@@ -186,7 +263,7 @@ VUResult vuapi Socket::enable_non_blocking(bool state)
186263

187264
VUResult vuapi Socket::bind(const Endpoint& endpoint)
188265
{
189-
return this->bind(endpoint.host, endpoint.port);
266+
return this->bind(endpoint.m_host, endpoint.m_port);
190267
}
191268

192269
VUResult vuapi Socket::bind(const std::string& address, const ushort port)
@@ -254,21 +331,16 @@ VUResult vuapi Socket::accept(Handle& socket)
254331
}
255332

256333
VUResult vuapi Socket::connect(const Endpoint& endpoint)
257-
{
258-
return this->connect(endpoint.host, endpoint.port);
259-
}
260-
261-
VUResult vuapi Socket::connect(const std::string& address, ushort port)
262334
{
263335
std::string ip;
264336

265-
if (this->is_host_name(address) == true)
337+
if (this->is_host_name(endpoint.m_host) == true)
266338
{
267-
ip = this->get_host_address(address);
339+
ip = this->get_host_address(endpoint.m_host);
268340
}
269341
else
270342
{
271-
ip = address;
343+
ip = endpoint.m_host;
272344
}
273345

274346
if (ip.empty())
@@ -277,7 +349,7 @@ VUResult vuapi Socket::connect(const std::string& address, ushort port)
277349
}
278350

279351
m_sai.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
280-
m_sai.sin_port = htons(port);
352+
m_sai.sin_port = htons(endpoint.m_port);
281353

282354
if (::connect(m_socket, (const struct sockaddr*)&m_sai, sizeof(m_sai)) == SOCKET_ERROR)
283355
{

0 commit comments

Comments
 (0)