fixed TcpServer to close pipes properly upon termination

This commit is contained in:
hyung-hwan 2018-06-26 15:45:44 +00:00
parent b2f9abef77
commit 19df0af32a
3 changed files with 19 additions and 14 deletions

View File

@ -45,15 +45,6 @@ public:
TcpServer () QSE_CPP_NOEXCEPT;
virtual ~TcpServer () QSE_CPP_NOEXCEPT;
enum
{
ERR_NONE = 0,
ERR_OPEN = 1,
ERR_BIND = 2,
ERR_LISTEN = 3,
ERR_EXCEPTION = 4
};
virtual int start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT;
virtual int stop () QSE_CPP_NOEXCEPT;

View File

@ -106,8 +106,6 @@ void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
Listener* lp;
struct epoll_event dummy_ev;
::epoll_ctl (this->listener.ep_fd, EPOLL_CTL_DEL, this->listener.mux_pipe[0], &dummy_ev);
while (this->listener.head)
{
lp = this->listener.head;
@ -119,7 +117,19 @@ void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
delete lp;
}
if (this->listener.mux_pipe[0] >= 0)
{
::epoll_ctl (this->listener.ep_fd, EPOLL_CTL_DEL, this->listener.mux_pipe[0], &dummy_ev);
close (this->listener.mux_pipe[0]);
this->listener.mux_pipe[0] = -1;
}
if (this->listener.mux_pipe[1] >= 0)
{
close (this->listener.mux_pipe[1]);
this->listener.mux_pipe[1] = -1;
}
QSE_ASSERT (this->listener.ep_fd >= 0);
::close (this->listener.ep_fd);
this->listener.ep_fd = -1;
}

View File

@ -12,7 +12,7 @@
#include <signal.h>
#include <string.h>
static int g_stopreq = 0;
class ClientHandler
@ -20,16 +20,20 @@ class ClientHandler
public:
int operator() (QSE::Socket* sck, QSE::SocketAddress* addr)
{
qse_printf (QSE_T("XXXXXXXXXXXXXXXXXXXXXXXXXX\n"));p
qse_printf (QSE_T("XXXXXXXXXXXXXXXXXXXXXXXXXX\n"));
return 0;
}
};
static QSE::TcpServerF<ClientHandler>* g_server;
static int test1 (void)
{
QSE::TcpServerF<ClientHandler> server;
server.setThreadStackSize (256000);
g_server = &server;
server.start (QSE_T("0.0.0.0:9998"));
g_server = QSE_NULL;
return 0;
}
@ -56,7 +60,7 @@ static int test2 (void)
static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx)
{
g_stopreq = 1;
if (g_server) g_server->stop ();
}
static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*))