Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Nov 1, 2023
1 parent a7aa816 commit 5024db8
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Test/Sample.AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#if defined(VU_INET_ENABLED)

void example_binding(const vu::Socket::Endpoint& endpoint)
void example_binding(const vu::Endpoint& endpoint)
{
vu::AsyncSocket server;

Expand Down Expand Up @@ -41,7 +41,7 @@ void example_binding(const vu::Socket::Endpoint& endpoint)
server.close();
}

void example_inheritance(const vu::Socket::Endpoint& endpoint)
void example_inheritance(const vu::Endpoint& endpoint)
{
class CAsyncSocketServer : public vu::AsyncSocket
{
Expand Down
2 changes: 1 addition & 1 deletion Test/Sample.Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ DEF_SAMPLE(Socket)

vu::Socket socket;

if (socket.connect("ipv4.download.thinkbroadband.com", 80) != vu::VU_OK)
if (socket.connect(vu::Endpoint("ipv4.download.thinkbroadband.com", 80)) != vu::VU_OK)
{
std::tcout << ts("Socket -> Connect -> Failed") << std::endl;
return 1;
Expand Down
33 changes: 20 additions & 13 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,23 @@ class LibraryW : public LastError
#define VU_DEFAULT_SEND_RECV_TIMEOUT 3 // 3 seconds
#define VU_DEFAULT_SEND_RECV_BLOCK_SIZE KiB // 1 KiB

struct Endpoint
{
std::string m_host;
ushort m_port;

Endpoint(const std::string& endpoint);
Endpoint(const std::wstring& endpoint);
Endpoint(const std::string& host, const ushort port);
Endpoint(const std::wstring& host, const ushort port);

bool operator==(const Endpoint& right);
bool operator!=(const Endpoint& right);
const Endpoint& operator=(const Endpoint& right);
const Endpoint& operator=(const std::string& right);
const Endpoint& operator=(const std::wstring& right);
};

class Socket : public LastError
{
public:
Expand All @@ -1216,14 +1233,6 @@ class Socket : public LastError
char ip[15];
};

struct Endpoint
{
std::string host;
ushort port;

Endpoint(const std::string& host, const ushort port) : host(host), port(port) {}
};

struct Options
{
struct
Expand All @@ -1247,7 +1256,7 @@ class Socket : public LastError
);
virtual ~Socket();

SOCKET& vuapi handle();
const SOCKET& vuapi handle() const;
const WSADATA& vuapi wsa_data() const;
const address_family_t vuapi af() const;
const type_t vuapi type() const;
Expand All @@ -1266,8 +1275,6 @@ class Socket : public LastError
VUResult vuapi accept(Handle& socket);

VUResult vuapi connect(const Endpoint& endpoint);
VUResult vuapi connect(const std::string& address, const ushort port);

VUResult vuapi disconnect(const shutdowns_t flags = SD_BOTH);

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

VUResult vuapi bind(const Socket::Endpoint& endpoint);
VUResult vuapi bind(const Endpoint& endpoint);
VUResult vuapi bind(const std::string& address, const ushort port);

/**
Expand Down
8 changes: 4 additions & 4 deletions src/details/asyncsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ bool vuapi AsyncSocket::running()
return m_running;
}

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

VUResult vuapi AsyncSocket::bind(const std::string& address, const ushort port)
Expand Down Expand Up @@ -132,7 +132,7 @@ VUResult vuapi AsyncSocket::stop()
return VU_OK;
}

VUResult vuapi AsyncSocket::connect(const Socket::Endpoint& endpoint)
VUResult vuapi AsyncSocket::connect(const Endpoint& endpoint)
{
if (!m_socket.available())
{
Expand Down Expand Up @@ -165,7 +165,7 @@ VUResult vuapi AsyncSocket::connect(const Socket::Endpoint& endpoint)

VUResult vuapi AsyncSocket::connect(const std::string& address, const ushort port)
{
Socket::Endpoint endpoint(address, port);
Endpoint endpoint(address, port);
return this->connect(endpoint);
}

Expand Down
94 changes: 83 additions & 11 deletions src/details/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,83 @@ namespace vu

#ifdef VU_INET_ENABLED

/**
* Endpoint
*/

Endpoint::Endpoint(const std::string& endpoint)
{
auto l = split_string_A(endpoint, ":");
if (l.size() != 2)
{
throw "invalid endpoint";
}
else
{
m_host = l[0];
m_port = ushort(std::stoul(l[1]));
}
}

Endpoint::Endpoint(const std::wstring& endpoint)
{
auto temp = to_string_A(endpoint);
auto l = split_string_A(temp, ":");
if (l.size() != 2)
{
throw "invalid endpoint";
}
else
{
m_host = l[0];
m_port = ushort(std::stoul(l[1]));
}
}

Endpoint::Endpoint(const std::wstring& host, const ushort port) : m_port(port)
{
m_host = to_string_A(host);
}

Endpoint::Endpoint(const std::string& host, const ushort port) : m_host(host), m_port(port)
{
}

bool Endpoint::operator==(const Endpoint& right)
{
return m_host == right.m_host && m_port == right.m_port;
}

bool Endpoint::operator!=(const Endpoint& right)
{
return !(*this == right);
}

const vu::Endpoint& Endpoint::operator=(const Endpoint& right)
{
m_host = right.m_host;
m_port = right.m_port;
return *this;
}

const vu::Endpoint& Endpoint::operator=(const std::string& right)
{
Endpoint endpoint(right);
*this = endpoint;
return *this;
}

const vu::Endpoint& Endpoint::operator=(const std::wstring& right)
{
Endpoint endpoint(right);
*this = endpoint;
return *this;
}

/**
* Socket
*/

Socket::Socket(
const address_family_t af,
const type_t type,
Expand Down Expand Up @@ -117,7 +194,7 @@ const Socket::protocol_t vuapi Socket::protocol() const
return m_proto;
}

SOCKET& vuapi Socket::handle()
const SOCKET& vuapi Socket::handle() const
{
return m_socket;
}
Expand Down Expand Up @@ -186,7 +263,7 @@ VUResult vuapi Socket::enable_non_blocking(bool state)

VUResult vuapi Socket::bind(const Endpoint& endpoint)
{
return this->bind(endpoint.host, endpoint.port);
return this->bind(endpoint.m_host, endpoint.m_port);
}

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

VUResult vuapi Socket::connect(const Endpoint& endpoint)
{
return this->connect(endpoint.host, endpoint.port);
}

VUResult vuapi Socket::connect(const std::string& address, ushort port)
{
std::string ip;

if (this->is_host_name(address) == true)
if (this->is_host_name(endpoint.m_host) == true)
{
ip = this->get_host_address(address);
ip = this->get_host_address(endpoint.m_host);
}
else
{
ip = address;
ip = endpoint.m_host;
}

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

m_sai.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
m_sai.sin_port = htons(port);
m_sai.sin_port = htons(endpoint.m_port);

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

0 comments on commit 5024db8

Please sign in to comment.