Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Nov 4, 2023
1 parent 1230ec0 commit cdf4507
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Test/Sample.AsyncSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions Test/Sample.Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Test/Sample.Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
41 changes: 27 additions & 14 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,8 @@ class Buffer
bool append(const void* ptr, const size_t size);
bool append(const Buffer& right);

std::unique_ptr<std::string> to_string_A() const;
std::unique_ptr<std::wstring> to_string_W() const;
std::unique_ptr<std::string> as_string_A() const;
std::unique_ptr<std::wstring> as_string_W() const;

bool save_to_file(const std::string& file_path);
bool save_to_file(const std::wstring& file_path);
Expand Down Expand Up @@ -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<class stream_T>
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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -1298,20 +1311,20 @@ 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();

VUResult vuapi set_option(const int level, const int option, const void* value, const int size);
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;
Expand Down Expand Up @@ -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
Expand All @@ -1381,7 +1394,7 @@ class AsyncSocket : public LastError
VUResult vuapi stop();
IResult vuapi close();

std::set<SOCKET> vuapi get_connections();
std::set<SOCKET> 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);
Expand Down
28 changes: 14 additions & 14 deletions src/details/asyncsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ VUResult vuapi AsyncSocket::connect(const std::string& address, const ushort por
return this->connect(endpoint);
}

std::set<SOCKET> vuapi AsyncSocket::get_connections()
std::set<SOCKET> vuapi AsyncSocket::get_connections() const
{
std::set<SOCKET> result;

Expand Down Expand Up @@ -197,10 +197,10 @@ std::set<SOCKET> 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);
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/details/mbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ bool Buffer::append(const Buffer& right)
return this->append(right.pointer(), right.size());
}

std::unique_ptr<std::string> Buffer::to_string_A() const
std::unique_ptr<std::string> Buffer::as_string_A() const
{
std::unique_ptr<std::string> result(new std::string);
if (m_ptr == nullptr || m_size == 0)
Expand All @@ -306,7 +306,7 @@ std::unique_ptr<std::string> Buffer::to_string_A() const
return result;
}

std::unique_ptr<std::wstring> Buffer::to_string_W() const
std::unique_ptr<std::wstring> Buffer::as_string_W() const
{
std::unique_ptr<std::wstring> result(new std::wstring);
if (m_ptr == nullptr || m_size == 0)
Expand Down
29 changes: 18 additions & 11 deletions src/details/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ Socket::Socket(
m_sai.sin_family = m_af;
}

Socket::Socket(const Socket& right)
{
*this = right;
}

Socket::~Socket()
{
this->close();
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 };

Expand All @@ -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 };

Expand Down Expand Up @@ -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 = "";

Expand All @@ -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;
}

Expand All @@ -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 = "";

Expand Down Expand Up @@ -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.";
Expand All @@ -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,
Expand Down

0 comments on commit cdf4507

Please sign in to comment.