Add infinite wait default to waitForThreadToExit

This commit is contained in:
Vinnie Falco
2013-07-29 01:38:58 -07:00
parent 50c1e75411
commit ae4071d1e0
3 changed files with 56 additions and 46 deletions

View File

@@ -150,7 +150,7 @@ public:
is less than zero, it will wait forever. is less than zero, it will wait forever.
@returns true if the thread exits, or false if the timeout expires first. @returns true if the thread exits, or false if the timeout expires first.
*/ */
bool waitForThreadToExit (int timeOutMilliseconds) const; bool waitForThreadToExit (int timeOutMilliseconds = -1) const;
//============================================================================== //==============================================================================
/** Changes the thread's priority. /** Changes the thread's priority.

View File

@@ -21,9 +21,35 @@ SETUP_LOG (WSDoor)
// //
// VFALCO NOTE NetworkOPs isn't used here... // VFALCO NOTE NetworkOPs isn't used here...
// //
void WSDoor::startListening ()
WSDoor::WSDoor (std::string const& strIp, int iPort, bool bPublic)
: Thread ("websocket")
, mPublic (bPublic)
, mIp (strIp)
, mPort (iPort)
{ {
setCallingThreadName ("websocket"); }
WSDoor::~WSDoor ()
{
{
CriticalSection::ScopedLockType lock (m_endpointLock);
if (m_endpoint != nullptr)
m_endpoint->stop ();
}
m_thread.signalThreadShouldExit ();
m_thread.waitForThreadToExit ();
}
void WSDoor::run ()
{
WriteLog (lsINFO, WSDoor) << boost::str (boost::format ("Websocket: %s: Listening: %s %d ")
% (mPublic ? "Public" : "Private")
% mIp
% mPort);
// Generate a single SSL context for use by all connections. // Generate a single SSL context for use by all connections.
boost::shared_ptr<boost::asio::ssl::context> mCtx; boost::shared_ptr<boost::asio::ssl::context> mCtx;
mCtx = boost::make_shared<boost::asio::ssl::context> (boost::asio::ssl::context::sslv23); mCtx = boost::make_shared<boost::asio::ssl::context> (boost::asio::ssl::context::sslv23);
@@ -35,11 +61,13 @@ void WSDoor::startListening ()
SSL_CTX_set_tmp_dh_callback (mCtx->native_handle (), handleTmpDh); SSL_CTX_set_tmp_dh_callback (mCtx->native_handle (), handleTmpDh);
// Construct a single handler for all requests.
websocketpp::server_autotls::handler::ptr handler (new WSServerHandler<websocketpp::server_autotls> (mCtx, mPublic)); websocketpp::server_autotls::handler::ptr handler (new WSServerHandler<websocketpp::server_autotls> (mCtx, mPublic));
// Construct a websocket server. {
mSEndpoint = new websocketpp::server_autotls (handler); CriticalSection::ScopedLockType lock (m_endpointLock);
m_endpoint = new websocketpp::server_autotls (handler);
}
// mEndpoint->alog().unset_level(websocketpp::log::alevel::ALL); // mEndpoint->alog().unset_level(websocketpp::log::alevel::ALL);
// mEndpoint->elog().unset_level(websocketpp::log::elevel::ALL); // mEndpoint->elog().unset_level(websocketpp::log::elevel::ALL);
@@ -47,7 +75,7 @@ void WSDoor::startListening ()
// Call the main-event-loop of the websocket server. // Call the main-event-loop of the websocket server.
try try
{ {
mSEndpoint->listen ( m_endpoint->listen (
boost::asio::ip::tcp::endpoint ( boost::asio::ip::tcp::endpoint (
boost::asio::ip::address ().from_string (mIp), mPort)); boost::asio::ip::address ().from_string (mIp), mPort));
} }
@@ -60,7 +88,7 @@ void WSDoor::startListening ()
// https://github.com/zaphoyd/websocketpp/issues/98 // https://github.com/zaphoyd/websocketpp/issues/98
try try
{ {
mSEndpoint->get_io_service ().run (); m_endpoint->get_io_service ().run ();
break; break;
} }
catch (websocketpp::exception& e) catch (websocketpp::exception& e)
@@ -70,32 +98,18 @@ void WSDoor::startListening ()
} }
} }
delete mSEndpoint; delete m_endpoint;
}
WSDoor* WSDoor::createWSDoor (const std::string& strIp, const int iPort, bool bPublic)
{
WSDoor* wdpResult = new WSDoor (strIp, iPort, bPublic);
WriteLog (lsINFO, WSDoor) <<
boost::str (boost::format ("Websocket: %s: Listening: %s %d ")
% (bPublic ? "Public" : "Private")
% strIp
% iPort);
wdpResult->mThread = new boost::thread (BIND_TYPE (&WSDoor::startListening, wdpResult));
return wdpResult;
} }
void WSDoor::stop () void WSDoor::stop ()
{ {
if (mThread)
{ {
if (mSEndpoint) CriticalSection::ScopedLockType lock (m_endpointLock);
mSEndpoint->stop ();
if (m_endpoint != nullptr)
mThread->join (); m_endpoint->stop ();
} }
m_thread.signalThreadShouldExit ();
m_thread.waitForThreadToExit ();
} }

View File

@@ -7,28 +7,24 @@
#ifndef RIPPLE_WSDOOR_RIPPLEHEADER #ifndef RIPPLE_WSDOOR_RIPPLEHEADER
#define RIPPLE_WSDOOR_RIPPLEHEADER #define RIPPLE_WSDOOR_RIPPLEHEADER
class WSDoor : LeakChecked <WSDoor> class WSDoor : protected Thread, LeakChecked <WSDoor>
{ {
private: public:
websocketpp::server_autotls* mSEndpoint; WSDoor (std::string const& strIp, int iPort, bool bPublic);
boost::thread* mThread; ~WSDoor ();
void stop ();
private:
void run ();
private:
ScopedPointer <websocketpp::server_autotls*> m_endpoint;
CriticalSection m_endpointLock;
bool mPublic; bool mPublic;
std::string mIp; std::string mIp;
int mPort; int mPort;
void startListening ();
public:
WSDoor (const std::string& strIp, int iPort, bool bPublic) : mSEndpoint (0), mThread (0), mPublic (bPublic), mIp (strIp), mPort (iPort)
{
;
}
void stop ();
static WSDoor* createWSDoor (const std::string& strIp, const int iPort, bool bPublic);
}; };
#endif #endif