interim update while working TcpServer

This commit is contained in:
hyung-hwan 2018-06-26 09:28:12 +00:00
parent d6b9e78114
commit eb2755fa6b
2 changed files with 16 additions and 17 deletions

View File

@ -56,7 +56,6 @@ public:
}; };
virtual int start (int* err_code = QSE_NULL) QSE_CPP_NOEXCEPT; 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; virtual int stop () QSE_CPP_NOEXCEPT;
bool isServing () const QSE_CPP_NOEXCEPT bool isServing () const QSE_CPP_NOEXCEPT
@ -126,6 +125,11 @@ public:
} }
protected: protected:
class Listener: public QSE::Socket
{
Listener* next_listener;
};
class Client: public QSE::Thread class Client: public QSE::Thread
{ {
public: public:
@ -142,6 +146,9 @@ protected:
SocketAddress address; SocketAddress address;
}; };
Listener* listener_head;
Listener* listener_tail;
SocketAddress binding_address; SocketAddress binding_address;
bool stop_requested; bool stop_requested;
bool server_serving; bool server_serving;
@ -158,7 +165,7 @@ protected:
private: private:
void delete_dead_clients () QSE_CPP_NOEXCEPT; void delete_dead_clients () QSE_CPP_NOEXCEPT;
void delete_all_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? // TODO: are there any ways to achieve this without memory allocation?
//this->__lfunc = new TCallable<T> (QSE_CPP_RVREF(f)); //this->__lfunc = new TCallable<T> (QSE_CPP_RVREF(f));
// TODO: this->__lfunc = new TCallable<T> (QSE_CPP_RVREF(f));
} }
catch (...) catch (...)
{ {

View File

@ -96,27 +96,18 @@ TcpServer::TcpServer (const SocketAddress& address):
TcpServer::~TcpServer () QSE_CPP_NOEXCEPT TcpServer::~TcpServer () QSE_CPP_NOEXCEPT
{ {
// QSE_ASSERT (server_serving == false); // QSE_ASSERT (this->server_serving == false);
this->delete_all_clients (); 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); if (socket.open(this->binding_address.getFamily(), QSE_SOCK_STREAM, Socket::T_CLOEXEC | Socket::T_NONBLOCK) <= -1)
}
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 (err_code) *err_code = ERR_OPEN; if (err_code) *err_code = ERR_OPEN;
return -1; return -1;
} }
#if defined(_WIN32)
SetHandleInformation ((HANDLE)socket.handle(), HANDLE_FLAG_INHERIT, (winsock_inheritable? HANDLE_FLAG_INHERIT: 0));
#endif
//socket.setReuseAddr (true); //socket.setReuseAddr (true);
//socket.setReusePort (true); //socket.setReusePort (true);
@ -136,7 +127,7 @@ int TcpServer::open_tcp_socket (Socket& socket, bool winsock_inheritable, int* e
return 0; 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; this->server_serving = true;
if (err_code != QSE_NULL) *err_code = ERR_NONE; 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; 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->server_serving = false;
this->setStopRequested (false); this->setStopRequested (false);
@ -204,7 +195,7 @@ int TcpServer::start (bool winsock_inheritable, int* err_code) QSE_CPP_NOEXCEPT
socket.close (); socket.close ();
reopen: 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) if (reopen_count >= 100)
{ {