diff --git a/include/Vutils.h b/include/Vutils.h index 8f144b6..c91a89a 100644 --- a/include/Vutils.h +++ b/include/Vutils.h @@ -1271,6 +1271,13 @@ class Socket : public LastError typedef int flags_t; typedef int shutdowns_t; + enum class side_type : uint + { + SERVER, + CLIENT, + UNDEFINED, + }; + struct Handle { SOCKET s; @@ -1305,6 +1312,7 @@ class Socket : public LastError bool operator!=(const Socket& right); const Socket& operator=(const Socket& right); + side_type vuapi side() const; const SOCKET& vuapi handle() const; const address_family_t vuapi af() const; const type_t vuapi type() const; @@ -1357,6 +1365,7 @@ class Socket : public LastError std::string vuapi get_host_address(const std::string& name) const; private: + side_type m_side; type_t m_type; WSADATA m_wsa_data; address_family_t m_af; @@ -1382,12 +1391,6 @@ class AsyncSocket : public LastError UNDEFINED, }; - enum class side_type : uint - { - SERVER, - CLIENT, - }; - AsyncSocket( const vu::Socket::address_family_t af = AF_INET, const vu::Socket::type_t type = SOCK_STREAM, @@ -1396,8 +1399,7 @@ class AsyncSocket : public LastError ); virtual ~AsyncSocket(); - side_type vuapi side() const; - + Socket::side_type vuapi side() const; bool vuapi available() const; bool vuapi running() const; @@ -1453,9 +1455,7 @@ class AsyncSocket : public LastError HANDLE m_thread; std::atomic m_running; - side_type m_side; vu::Socket m_socket; - DWORD m_n_events; SOCKET m_connections[WSA_MAXIMUM_WAIT_EVENTS]; WSAEVENT m_events[WSA_MAXIMUM_WAIT_EVENTS]; diff --git a/src/details/asyncsocket.cpp b/src/details/asyncsocket.cpp index d6b2afd..c8704f8 100644 --- a/src/details/asyncsocket.cpp +++ b/src/details/asyncsocket.cpp @@ -21,8 +21,6 @@ AsyncSocket::AsyncSocket( const vu::Socket::Options* options ) : m_socket(af, type, proto, options), m_thread(INVALID_HANDLE_VALUE), LastError() { - UNREFERENCED_PARAMETER(m_side); - this->initialze(); for (uint i = 0; i < function::UNDEFINED; i++) @@ -45,9 +43,10 @@ void vuapi AsyncSocket::initialze() m_running = false; } -AsyncSocket::side_type vuapi AsyncSocket::side() const + +Socket::side_type vuapi AsyncSocket::side() const { - return m_side; + return m_socket.side(); } bool vuapi AsyncSocket::available() const @@ -68,13 +67,7 @@ VUResult vuapi AsyncSocket::bind(const Endpoint& endpoint) VUResult vuapi AsyncSocket::bind(const std::string& address, const ushort port) { auto result = m_socket.bind(address, port); - if (result == VU_OK) - { - m_side = side_type::SERVER; - } - this->set_last_error_code(m_socket.get_last_error_code()); - return result; } @@ -144,7 +137,6 @@ VUResult vuapi AsyncSocket::connect(const Endpoint& endpoint) if (result == VU_OK) { m_last_error_code = ERROR_SUCCESS; - m_side = side_type::CLIENT; m_connections[m_n_events] = m_socket.handle(); m_events[m_n_events] = event; m_n_events++; @@ -177,7 +169,7 @@ void vuapi AsyncSocket::get_connections(std::set& connections) continue; } - if (m_side == side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle + if (m_socket.side() == Socket::side_type::SERVER && socket == m_socket.handle()) // ignore server socket handle { continue; } diff --git a/src/details/socket.cpp b/src/details/socket.cpp index 8b196e7..5e602c2 100644 --- a/src/details/socket.cpp +++ b/src/details/socket.cpp @@ -104,7 +104,8 @@ Socket::Socket( const type_t type, const protocol_t proto, const Options* options -) : LastError(), m_af(af), m_type(type), m_proto(proto), m_attached(false) +) : LastError(), m_af(af), m_type(type), m_proto(proto) + , m_attached(false), m_side(Socket::side_type::UNDEFINED) { ZeroMemory(&m_wsa_data, sizeof(m_wsa_data)); if (WSAStartup(MAKEWORD(2, 2), &m_wsa_data) == INVALID_SOCKET) @@ -141,11 +142,14 @@ Socket::~Socket() return; // ignore if the connection is attached from outside } - if (::closesocket(m_socket) == INVALID_SOCKET) + if (this->available()) { - assert("close socket failed."); - m_last_error_code = GetLastError(); - m_socket = INVALID_SOCKET; + if (::closesocket(m_socket) == INVALID_SOCKET) + { + assert("close socket failed."); + m_last_error_code = GetLastError(); + m_socket = INVALID_SOCKET; + } } if (WSACleanup() == INVALID_SOCKET) @@ -183,6 +187,11 @@ const vu::Socket& Socket::operator=(const Socket& right) return *this; } +Socket::side_type vuapi Socket::side() const +{ + return m_side; +} + bool vuapi Socket::valid(const SOCKET& socket) const { return !(socket == 0 || socket == INVALID_SOCKET); @@ -330,6 +339,8 @@ VUResult vuapi Socket::bind(const std::string& address, const ushort port) return 3; } + m_side = side_type::SERVER; + return VU_OK; } @@ -393,13 +404,16 @@ VUResult vuapi Socket::connect(const Endpoint& endpoint) m_sai.sin_addr.S_un.S_addr = inet_addr(ip.c_str()); m_sai.sin_port = htons(endpoint.m_port); + VUResult result = VU_OK; + if (::connect(m_socket, (const struct sockaddr*)&m_sai, sizeof(m_sai)) == SOCKET_ERROR) { - m_last_error_code = GetLastError(); - return m_last_error_code == WSAEWOULDBLOCK ? VU_OK : 2; + result = GetLastError() == WSAEWOULDBLOCK ? VU_OK : 2; // ignore 'A non-blocking socket operation could not be completed immediately.' } - return VU_OK; + m_side = side_type::CLIENT; + + return result; } IResult vuapi Socket::send(const char* ptr_data, int size, const flags_t flags)