From cdf4507b021ea5bf878b2523470aa53b411fd031 Mon Sep 17 00:00:00 2001 From: Vic P Date: Sun, 5 Nov 2023 03:53:51 +0700 Subject: [PATCH] Vutils --- Test/Sample.AsyncSocket.h | 2 +- Test/Sample.Misc.h | 24 +++++++++++----------- Test/Sample.Socket.h | 2 +- include/Vutils.h | 41 ++++++++++++++++++++++++------------- src/details/asyncsocket.cpp | 28 ++++++++++++------------- src/details/mbuffer.cpp | 4 ++-- src/details/socket.cpp | 29 ++++++++++++++++---------- 7 files changed, 75 insertions(+), 55 deletions(-) diff --git a/Test/Sample.AsyncSocket.h b/Test/Sample.AsyncSocket.h index c66e039..d15cbb9 100644 --- a/Test/Sample.AsyncSocket.h +++ b/Test/Sample.AsyncSocket.h @@ -32,7 +32,7 @@ void example_binding(const vu::Endpoint& endpoint) { vu::Buffer data(KiB); client.recv(data); - printf("client %d recv `%s`\n", client.get_remote_sai().sin_port, data.to_string_A()->c_str()); + printf("client %d recv `%s`\n", client.get_remote_sai().sin_port, data.as_string_A()->c_str()); }); server.bind(endpoint); diff --git a/Test/Sample.Misc.h b/Test/Sample.Misc.h index bc4d4e4..3194d40 100644 --- a/Test/Sample.Misc.h +++ b/Test/Sample.Misc.h @@ -81,22 +81,22 @@ DEF_SAMPLE(Misc) std::string s = "0123456789"; vu::Buffer slicer(s.data(), s.size()); - //std::cout << slicer(0, 0)->to_string_A()->c_str() << std::endl; - //std::cout << slicer(9, 9)->to_string_A()->c_str() << std::endl; - std::cout << slicer(0, 9)->to_string_A()->c_str() << std::endl; + //std::cout << slicer(0, 0)->as_string_A()->c_str() << std::endl; + //std::cout << slicer(9, 9)->as_string_A()->c_str() << std::endl; + std::cout << slicer(0, 9)->as_string_A()->c_str() << std::endl; - std::cout << slicer(3, 7)->to_string_A()->c_str() << std::endl; + std::cout << slicer(3, 7)->as_string_A()->c_str() << std::endl; - std::cout << slicer(-7, -2)->to_string_A()->c_str() << std::endl; - std::cout << slicer(2, -2)->to_string_A()->c_str() << std::endl; + std::cout << slicer(-7, -2)->as_string_A()->c_str() << std::endl; + std::cout << slicer(2, -2)->as_string_A()->c_str() << std::endl; - std::cout << slicer(0, -8)->to_string_A()->c_str() << std::endl; - std::cout << slicer(0, -9)->to_string_A()->c_str() << std::endl; - //std::cout << slicer(0, -10)->to_string_A()->c_str() << std::endl; + std::cout << slicer(0, -8)->as_string_A()->c_str() << std::endl; + std::cout << slicer(0, -9)->as_string_A()->c_str() << std::endl; + //std::cout << slicer(0, -10)->as_string_A()->c_str() << std::endl; - std::cout << slicer(-8, 10)->to_string_A()->c_str() << std::endl; - std::cout << slicer(-9, 10)->to_string_A()->c_str() << std::endl; - std::cout << slicer(-10, 10)->to_string_A()->c_str() << std::endl; + std::cout << slicer(-8, 10)->as_string_A()->c_str() << std::endl; + std::cout << slicer(-9, 10)->as_string_A()->c_str() << std::endl; + std::cout << slicer(-10, 10)->as_string_A()->c_str() << std::endl; std::tcout << vu::undecorate_cpp_symbol(ts("?func1@a@@AAEXH@Z")) << std::endl; diff --git a/Test/Sample.Socket.h b/Test/Sample.Socket.h index 9095f91..20d112f 100644 --- a/Test/Sample.Socket.h +++ b/Test/Sample.Socket.h @@ -50,7 +50,7 @@ DEF_SAMPLE(Socket) // extract response header & body - const std::string first_response = response.to_string_A()->c_str(); + const std::string first_response = response.as_string_A()->c_str(); const std::string http_header_sep = std::crlf; const std::string http_header_end = http_header_sep + http_header_sep; diff --git a/include/Vutils.h b/include/Vutils.h index 7bd45f7..7d2e6b6 100644 --- a/include/Vutils.h +++ b/include/Vutils.h @@ -1062,8 +1062,8 @@ class Buffer bool append(const void* ptr, const size_t size); bool append(const Buffer& right); - std::unique_ptr to_string_A() const; - std::unique_ptr to_string_W() const; + std::unique_ptr as_string_A() const; + std::unique_ptr as_string_W() const; bool save_to_file(const std::string& file_path); bool save_to_file(const std::wstring& file_path); @@ -1201,6 +1201,18 @@ class LibraryW : public LastError #define VU_DEFAULT_SEND_RECV_TIMEOUT 3 // 3 seconds #define VU_DEFAULT_SEND_RECV_BLOCK_SIZE KiB // 1 KiB +template +stream_T& operator<<(stream_T& ss, const sockaddr_in& sai) +{ + auto& S_un_b = sai.sin_addr.S_un.S_un_b; + ss << int(S_un_b.s_b1) << "."; + ss << int(S_un_b.s_b2) << "."; + ss << int(S_un_b.s_b3) << "."; + ss << int(S_un_b.s_b4) << ":"; + ss << int(sai.sin_port); + return ss; +} + struct Endpoint { std::string m_host; @@ -1255,6 +1267,7 @@ class Socket : public LastError const bool wsa = true, const Options* options = nullptr ); + Socket(const Socket& right); virtual ~Socket(); bool operator==(const Socket& right); @@ -1267,7 +1280,7 @@ class Socket : public LastError const type_t vuapi type() const; const protocol_t vuapi protocol() const; - bool vuapi available(); + bool vuapi available() const; void vuapi attach(const SOCKET& socket); void vuapi attach(const Handle& socket); @@ -1298,9 +1311,9 @@ class Socket : public LastError IResult vuapi close(); - const sockaddr_in vuapi get_local_sai(); - const sockaddr_in vuapi get_remote_sai(); - std::string vuapi get_host_name(); + const sockaddr_in vuapi get_local_sai() const; + const sockaddr_in vuapi get_remote_sai() const; + std::string vuapi get_host_name() const; Options& options(); @@ -1308,10 +1321,10 @@ class Socket : public LastError VUResult vuapi enable_non_blocking(bool state = true); private: - bool vuapi valid(const SOCKET& socket); - bool vuapi parse(const Handle& socket); - bool vuapi is_host_name(const std::string& s); - std::string vuapi get_host_address(const std::string& name); + bool vuapi valid(const SOCKET& socket) const; + bool vuapi parse(const Handle& socket) const; + bool vuapi is_host_name(const std::string& s) const; + std::string vuapi get_host_address(const std::string& name) const; private: bool m_wsa; @@ -1354,10 +1367,10 @@ class AsyncSocket : public LastError ); virtual ~AsyncSocket(); - side_type vuapi side(); + side_type vuapi side() const; - bool vuapi available(); - bool vuapi running(); + bool vuapi available() const; + bool vuapi running() const; /** * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaeventselect?redirectedfrom=MSDN#return-value @@ -1381,7 +1394,7 @@ class AsyncSocket : public LastError VUResult vuapi stop(); IResult vuapi close(); - std::set vuapi get_connections(); + std::set vuapi get_connections() const; VUResult vuapi disconnect_connections(const Socket::shutdowns_t flags = SD_BOTH, const bool cleanup = true); IResult vuapi send(const SOCKET& connection, const char* ptr_data, int size, const Socket::flags_t flags = MSG_NONE); diff --git a/src/details/asyncsocket.cpp b/src/details/asyncsocket.cpp index 36ecc93..a017d4b 100644 --- a/src/details/asyncsocket.cpp +++ b/src/details/asyncsocket.cpp @@ -46,17 +46,17 @@ void vuapi AsyncSocket::initialze() m_running = false; } -AsyncSocket::side_type vuapi AsyncSocket::side() +AsyncSocket::side_type vuapi AsyncSocket::side() const { return m_side; } -bool vuapi AsyncSocket::available() +bool vuapi AsyncSocket::available() const { return m_socket.available(); } -bool vuapi AsyncSocket::running() +bool vuapi AsyncSocket::running() const { return m_running; } @@ -169,7 +169,7 @@ VUResult vuapi AsyncSocket::connect(const std::string& address, const ushort por return this->connect(endpoint); } -std::set vuapi AsyncSocket::get_connections() +std::set vuapi AsyncSocket::get_connections() const { std::set result; @@ -197,10 +197,10 @@ std::set vuapi AsyncSocket::get_connections() VUResult vuapi AsyncSocket::disconnect_connections(const Socket::shutdowns_t flags, const bool cleanup) { auto connections = this->get_connections(); - for (const auto& e : connections) + for (const auto& connection : connections) { - vu::Socket socket; - socket.attach(e); + Socket socket(m_socket); + socket.attach(connection); socket.disconnect(flags, cleanup); } @@ -334,7 +334,7 @@ IResult vuapi AsyncSocket::do_connect(WSANETWORKEVENTS& events, SOCKET& connecti return events.iErrorCode[FD_CONNECT_BIT]; } - Socket socket(m_socket.af(), m_socket.type(), m_socket.protocol(), false); + Socket socket(m_socket); socket.attach(connection); this->on_connect(socket); socket.detach(); @@ -365,7 +365,7 @@ IResult vuapi AsyncSocket::do_open(WSANETWORKEVENTS& events, SOCKET& connection) m_connections[m_n_events] = obj.s; m_n_events++; - Socket socket(m_socket.af(), m_socket.type(), m_socket.protocol(), false); + Socket socket(m_socket); socket.attach(obj); this->on_open(socket); socket.detach(); @@ -380,7 +380,7 @@ IResult vuapi AsyncSocket::do_recv(WSANETWORKEVENTS& events, SOCKET& connection) return events.iErrorCode[FD_READ_BIT]; } - Socket socket(m_socket.af(), m_socket.type(), m_socket.protocol(), false); + Socket socket(m_socket); socket.attach(connection); this->on_recv(socket); socket.detach(); @@ -395,7 +395,7 @@ IResult vuapi AsyncSocket::do_send(WSANETWORKEVENTS& events, SOCKET& connection) return events.iErrorCode[FD_WRITE_BIT]; } - Socket socket(m_socket.af(), m_socket.type(), m_socket.protocol(), false); + Socket socket(m_socket); socket.attach(connection); this->on_send(socket); socket.detach(); @@ -438,7 +438,7 @@ IResult vuapi AsyncSocket::do_close(WSANETWORKEVENTS& events, SOCKET& connection m_n_events++; } - Socket socket(m_socket.af(), m_socket.type(), m_socket.protocol(), false); + Socket socket(m_socket); socket.attach(connection); this->on_close(socket); socket.detach(); @@ -504,7 +504,7 @@ IResult vuapi AsyncSocket::send( int size, const Socket::flags_t flags) { - vu::Socket socket; + Socket socket(m_socket); socket.attach(connection); return socket.send(ptr_data, size, flags); } @@ -514,7 +514,7 @@ IResult vuapi AsyncSocket::send( const Buffer& data, const Socket::flags_t flags) { - vu::Socket socket; + Socket socket(m_socket); socket.attach(connection); return socket.send(data, flags); } diff --git a/src/details/mbuffer.cpp b/src/details/mbuffer.cpp index 20db7ae..bed826d 100644 --- a/src/details/mbuffer.cpp +++ b/src/details/mbuffer.cpp @@ -294,7 +294,7 @@ bool Buffer::append(const Buffer& right) return this->append(right.pointer(), right.size()); } -std::unique_ptr Buffer::to_string_A() const +std::unique_ptr Buffer::as_string_A() const { std::unique_ptr result(new std::string); if (m_ptr == nullptr || m_size == 0) @@ -306,7 +306,7 @@ std::unique_ptr Buffer::to_string_A() const return result; } -std::unique_ptr Buffer::to_string_W() const +std::unique_ptr Buffer::as_string_W() const { std::unique_ptr result(new std::wstring); if (m_ptr == nullptr || m_size == 0) diff --git a/src/details/socket.cpp b/src/details/socket.cpp index 39509fa..a4eb1c8 100644 --- a/src/details/socket.cpp +++ b/src/details/socket.cpp @@ -128,6 +128,11 @@ Socket::Socket( m_sai.sin_family = m_af; } +Socket::Socket(const Socket& right) +{ + *this = right; +} + Socket::~Socket() { this->close(); @@ -157,9 +162,11 @@ const vu::Socket& Socket::operator=(const Socket& right) return *this; } - m_wsa = right.m_wsa; + m_wsa = false; + // m_wsa = right.m_wsa; + // m_wsa_data = right.m_wsa_data; + m_type = right.m_type; - m_wsa_data = right.m_wsa_data; m_af = right.m_af; m_proto = right.m_proto; m_sai = right.m_sai; @@ -170,12 +177,12 @@ const vu::Socket& Socket::operator=(const Socket& right) return *this; } -bool vuapi Socket::valid(const SOCKET& socket) +bool vuapi Socket::valid(const SOCKET& socket) const { return !(socket == 0 || socket == INVALID_SOCKET); } -bool vuapi Socket::available() +bool vuapi Socket::available() const { return this->valid(m_socket); } @@ -229,7 +236,7 @@ const SOCKET& vuapi Socket::handle() const return m_socket; } -const sockaddr_in vuapi Socket::get_local_sai() +const sockaddr_in vuapi Socket::get_local_sai() const { sockaddr_in result = { 0 }; @@ -242,7 +249,7 @@ const sockaddr_in vuapi Socket::get_local_sai() return result; } -const sockaddr_in vuapi Socket::get_remote_sai() +const sockaddr_in vuapi Socket::get_remote_sai() const { sockaddr_in result = { 0 }; @@ -651,7 +658,7 @@ VUResult vuapi Socket::disconnect(const shutdowns_t flags, const bool cleanup) return VU_OK; } -std::string vuapi Socket::get_host_name() +std::string vuapi Socket::get_host_name() const { std::string result = ""; @@ -669,7 +676,7 @@ std::string vuapi Socket::get_host_name() ZeroMemory(h.get(), MAXBYTE); if (::gethostname(h.get(), MAXBYTE) == SOCKET_ERROR) { - m_last_error_code = GetLastError(); + //m_last_error_code = GetLastError(); return result; } @@ -678,7 +685,7 @@ std::string vuapi Socket::get_host_name() return result; } -std::string vuapi Socket::get_host_address(const std::string& name) +std::string vuapi Socket::get_host_address(const std::string& name) const { std::string result = ""; @@ -718,7 +725,7 @@ std::string vuapi Socket::get_host_address(const std::string& name) return result; } -bool vuapi Socket::is_host_name(const std::string& s) +bool vuapi Socket::is_host_name(const std::string& s) const { bool result = false; const std::string MASK = "01234567890."; @@ -745,7 +752,7 @@ bool vuapi Socket::is_host_name(const std::string& s) return result; } -bool vuapi Socket::parse(const Handle& socket) +bool vuapi Socket::parse(const Handle& socket) const { if (sprintf( (char*)socket.ip,