From eb2755fa6b5710b4b2920cd87b280cf87d2324ae Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 26 Jun 2018 09:28:12 +0000 Subject: [PATCH] interim update while working TcpServer --- qse/include/qse/si/TcpServer.hpp | 12 ++++++++++-- qse/lib/si/TcpServer.cpp | 21 ++++++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/qse/include/qse/si/TcpServer.hpp b/qse/include/qse/si/TcpServer.hpp index 9fdaea74..77dfd54a 100644 --- a/qse/include/qse/si/TcpServer.hpp +++ b/qse/include/qse/si/TcpServer.hpp @@ -56,7 +56,6 @@ public: }; virtual int start (int* err_code = QSE_NULL) QSE_CPP_NOEXCEPT; - virtual int start (bool winsock_inheritable, int* err_code = QSE_NULL) QSE_CPP_NOEXCEPT; virtual int stop () QSE_CPP_NOEXCEPT; bool isServing () const QSE_CPP_NOEXCEPT @@ -126,6 +125,11 @@ public: } protected: + class Listener: public QSE::Socket + { + Listener* next_listener; + }; + class Client: public QSE::Thread { public: @@ -142,6 +146,9 @@ protected: SocketAddress address; }; + Listener* listener_head; + Listener* listener_tail; + SocketAddress binding_address; bool stop_requested; bool server_serving; @@ -158,7 +165,7 @@ protected: private: void delete_dead_clients () QSE_CPP_NOEXCEPT; void delete_all_clients () QSE_CPP_NOEXCEPT; - int open_tcp_socket (Socket& socket, bool winsock_inheritable, int* err_code) QSE_CPP_NOEXCEPT; + int open_tcp_socket (Socket& socket, int* err_code) QSE_CPP_NOEXCEPT; }; @@ -213,6 +220,7 @@ public: { // TODO: are there any ways to achieve this without memory allocation? //this->__lfunc = new TCallable (QSE_CPP_RVREF(f)); + // TODO: this->__lfunc = new TCallable (QSE_CPP_RVREF(f)); } catch (...) { diff --git a/qse/lib/si/TcpServer.cpp b/qse/lib/si/TcpServer.cpp index d5ef1145..0b1cb484 100644 --- a/qse/lib/si/TcpServer.cpp +++ b/qse/lib/si/TcpServer.cpp @@ -96,27 +96,18 @@ TcpServer::TcpServer (const SocketAddress& address): TcpServer::~TcpServer () QSE_CPP_NOEXCEPT { - // QSE_ASSERT (server_serving == false); + // QSE_ASSERT (this->server_serving == false); this->delete_all_clients (); } -int TcpServer::start (int* err_code) QSE_CPP_NOEXCEPT +int TcpServer::open_tcp_socket (Socket& socket, int* err_code) QSE_CPP_NOEXCEPT { - return this->start(true, err_code); -} - -int TcpServer::open_tcp_socket (Socket& socket, bool winsock_inheritable, int* err_code) QSE_CPP_NOEXCEPT -{ - if (socket.open(this->binding_address.getFamily(), QSE_SOCK_STREAM, 0) <= -1) + if (socket.open(this->binding_address.getFamily(), QSE_SOCK_STREAM, Socket::T_CLOEXEC | Socket::T_NONBLOCK) <= -1) { if (err_code) *err_code = ERR_OPEN; return -1; } -#if defined(_WIN32) - SetHandleInformation ((HANDLE)socket.handle(), HANDLE_FLAG_INHERIT, (winsock_inheritable? HANDLE_FLAG_INHERIT: 0)); -#endif - //socket.setReuseAddr (true); //socket.setReusePort (true); @@ -136,7 +127,7 @@ int TcpServer::open_tcp_socket (Socket& socket, bool winsock_inheritable, int* e return 0; } -int TcpServer::start (bool winsock_inheritable, int* err_code) QSE_CPP_NOEXCEPT +int TcpServer::start (int* err_code) QSE_CPP_NOEXCEPT { this->server_serving = true; if (err_code != QSE_NULL) *err_code = ERR_NONE; @@ -149,7 +140,7 @@ int TcpServer::start (bool winsock_inheritable, int* err_code) QSE_CPP_NOEXCEPT { Socket socket; - if (this->open_tcp_socket(socket, winsock_inheritable, err_code) <= -1) + if (this->open_tcp_socket(socket, err_code) <= -1) { this->server_serving = false; this->setStopRequested (false); @@ -204,7 +195,7 @@ int TcpServer::start (bool winsock_inheritable, int* err_code) QSE_CPP_NOEXCEPT socket.close (); reopen: - if (this->open_tcp_socket (socket, winsock_inheritable, err_code) <= -1) + if (this->open_tcp_socket (socket, err_code) <= -1) { if (reopen_count >= 100) {