got TcpServer and Thread to inherit Mmged

This commit is contained in:
2018-07-01 02:11:33 +00:00
parent 1d12ec3e3f
commit 27039d9693
13 changed files with 118 additions and 98 deletions

View File

@ -86,7 +86,8 @@ int TcpServer::Client::stop () QSE_CPP_NOEXCEPT
return 0;
}
TcpServer::TcpServer () QSE_CPP_NOEXCEPT:
TcpServer::TcpServer (Mmgr* mmgr) QSE_CPP_NOEXCEPT:
Mmged(mmgr),
errcode(E_ENOERR),
stop_requested(false),
server_serving(false),
@ -116,7 +117,7 @@ void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
qse_mux_delete (this->listener_list.mux, &evt);
lp->close ();
delete lp;
this->getMmgr()->dispose(lp); //delete lp;
}
if (this->listener_list.mux_pipe[0] >= 0)
@ -175,9 +176,11 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
if (server->max_connections > 0 && server->max_connections <= server->client_list.getSize())
{
// too many connections. accept the connection and close it.
Socket s;
SocketAddress sa;
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
// TODO: logging.
return;
}
@ -186,13 +189,15 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
// allocating the client object before accept is
// a bit awkward. but socket.accept() can be passed
// the socket field inside the client object.
try { client = new Client (lsck); }
try { client = new(server->getMmgr()) Client (lsck); }
catch (...)
{
// memory alloc failed. accept the connection and close it.
Socket s;
SocketAddress sa;
if (lsck->accept(&s, &sa, Socket::T_CLOEXEC) >= 0) s.close();
// TODO: logging.
return;
}
@ -215,7 +220,7 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
if (client->start(0) <= -1)
#endif
{
delete client;
server->getMmgr()->dispose (client); //delete client;
return;
}
@ -223,7 +228,7 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
catch (...)
{
// TODO: logging.
delete client;
server->getMmgr()->dispose (client); //delete client;
return;
}
@ -296,7 +301,7 @@ int TcpServer::setup_listeners (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
try
{
lsck = new Listener(this);
lsck = new(this->getMmgr()) Listener(this);
}
catch (...)
{
@ -393,12 +398,12 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
}
this->delete_all_clients ();
if (client != QSE_NULL) delete client;
if (client != QSE_NULL) this->getMmgr()->dispose (client); // delete client;
}
catch (...)
{
this->delete_all_clients ();
if (client != QSE_NULL) delete client;
if (client != QSE_NULL) this->getMmgr()->dispose (client); //delete client;
this->setErrorCode (E_EEXCEPT);
this->server_serving = false;
@ -446,7 +451,7 @@ void TcpServer::delete_dead_clients () QSE_CPP_NOEXCEPT
p->join ();
#endif
delete p;
this->getMmgr()->dispose (p); //delete p;
np2 = np; np = np->getNextNode();
this->client_list.remove (np2);
continue;
@ -478,7 +483,7 @@ void TcpServer::delete_all_clients () QSE_CPP_NOEXCEPT
#else
p->join ();
#endif
delete p;
this->getMmgr()->dispose (p); //delete p;
np2 = np; np = np->getNextNode();
this->client_list.remove (np2);
}

View File

@ -32,21 +32,20 @@ QSE_BEGIN_NAMESPACE(QSE)
Thread::Handle Thread::INVALID_HANDLE = QSE_THR_HND_INVALID;
Thread::Thread() QSE_CPP_NOEXCEPT : __exctx(QSE_NULL)
Thread::Thread(Mmgr* mmgr) QSE_CPP_NOEXCEPT: Mmged(mmgr), __exctx(QSE_NULL)
{
//qse_thr_init (this, this->getMmgr());
qse_thr_init (this, QSE_NULL);
qse_thr_init (&this->thr, this->getMmgr());
}
Thread::~Thread () QSE_CPP_NOEXCEPT
{
QSE_ASSERT (this->__state != QSE_THR_RUNNING);
QSE_ASSERT (this->thr.__state != QSE_THR_RUNNING);
// it is subclasses' responsibility to stop the thread gracefully.
// so stop is not called here.
// this->stop ();
/*if (this->__joinable)*/ this->join ();
qse_thr_fini (this);
/*if (this->thr.__joinable)*/ this->join ();
qse_thr_fini (&this->thr);
}
@ -58,7 +57,7 @@ static int thr_func_call_main (qse_thr_t* thr, void* ctx)
int Thread::start (int flags) QSE_CPP_NOEXCEPT
{
return qse_thr_start(this, thr_func_call_main, this, flags);
return qse_thr_start(&this->thr, thr_func_call_main, this, flags);
}
int Thread::stop () QSE_CPP_NOEXCEPT
@ -67,7 +66,7 @@ int Thread::stop () QSE_CPP_NOEXCEPT
// make sure that subclasses override "stop" and call it
// properly so that the thread can be terminated gracefully.
// "stop" here just aborts the running thread.
return qse_thr_stop(this);
return qse_thr_stop(&this->thr);
}
@ -82,7 +81,7 @@ int ThreadR::thr_func_call_rtn (qse_thr_t* thr, void* ctx)
int ThreadR::start (ThreadRoutine rtn, int flags) QSE_CPP_NOEXCEPT
{
if (this->__state == QSE_THR_RUNNING) return -1;
if (this->thr.__state == QSE_THR_RUNNING) return -1;
// this != (qse_thr_t*)this may not be equal if this class
// has some internal added data fields. e.g. it contains
@ -92,7 +91,7 @@ int ThreadR::start (ThreadRoutine rtn, int flags) QSE_CPP_NOEXCEPT
// qse_thr_start (this, (qse_thr_rtn_t)rtn, QSE_NULL, flags);
// so i pass a void pointer 'this' as the third argument.
this->__tmprtn = rtn;
return qse_thr_start(this, thr_func_call_rtn, this, flags);
return qse_thr_start(&this->thr, thr_func_call_rtn, this, flags);
}
QSE_END_NAMESPACE(QSE)