5151// ///////////////////////////////////////////////////////////////////////////
5252// ///////////////////////////////////////////////////////////////////////////
5353
54- QString HttpServer::g_sPlatform;
54+ QMutex HttpServer::s_platformLock;
55+ QString HttpServer::s_platform;
5556
5657// ///////////////////////////////////////////////////////////////////////////
5758//
5859// ///////////////////////////////////////////////////////////////////////////
5960
60- HttpServer::HttpServer () : QTcpServer(), ThreadPool(" HTTP" )
61+ HttpServer::HttpServer () :
62+ QTcpServer(), m_sSharePath(GetShareDir()),
63+ m_pHtmlServer(new HtmlServerExtension(m_sSharePath)),
64+ m_threadPool(" HttpServerPool" ), m_running(true )
6165{
6266 setMaxPendingConnections (20 );
63- InitializeThreads ();
6467
6568 // ----------------------------------------------------------------------
6669 // Build Platform String
6770 // ----------------------------------------------------------------------
68-
71+ {
72+ QMutexLocker locker (&s_platformLock);
6973#ifdef USING_MINGW
70- g_sPlatform = QString ( " Windows %1.%2" )
71- .arg (LOBYTE (LOWORD (GetVersion ())))
72- .arg (HIBYTE (LOWORD (GetVersion ())));
74+ s_platform = QString (" Windows %1.%2" )
75+ .arg (LOBYTE (LOWORD (GetVersion ())))
76+ .arg (HIBYTE (LOWORD (GetVersion ())));
7377#else
74- struct utsname uname_info;
75-
76- uname ( &uname_info );
77-
78- g_sPlatform = QString ( " %1 %2" ).arg ( uname_info.sysname )
79- .arg ( uname_info.release );
78+ struct utsname uname_info;
79+ uname ( &uname_info );
80+ s_platform = QString (" %1 %2" )
81+ .arg (uname_info.sysname ).arg (uname_info.release );
8082#endif
83+ }
8184
82- // ----------------------------------------------------------------------
83- // Initialize Share Path
84- // ----------------------------------------------------------------------
85-
86- m_sSharePath = GetShareDir ();
8785 LOG (VB_UPNP, LOG_INFO, QString (" HttpServer() - SharePath = %1" )
8886 .arg (m_sSharePath));
8987
90- // ----------------------------------------------------------------------
91- // The HtmlServer Extension is our fall back if a request isn't processed
92- // by any other extension. (This is needed here since it listens for
93- // '/' as it's base url ).
94- // ----------------------------------------------------------------------
95-
96- m_pHtmlServer = new HtmlServerExtension ( m_sSharePath );
97-
98-
9988 // -=>TODO: Load Config XML
10089 // -=>TODO: Load & initialize - HttpServerExtensions
10190}
@@ -106,6 +95,12 @@ HttpServer::HttpServer() : QTcpServer(), ThreadPool("HTTP")
10695
10796HttpServer::~HttpServer ()
10897{
98+ m_rwlock.lockForWrite ();
99+ m_running = false ;
100+ m_rwlock.unlock ();
101+
102+ m_threadPool.Stop ();
103+
109104 while (!m_extensions.empty ())
110105 {
111106 delete m_extensions.takeFirst ();
@@ -119,19 +114,19 @@ HttpServer::~HttpServer()
119114//
120115// ///////////////////////////////////////////////////////////////////////////
121116
122- QScriptEngine* HttpServer::ScriptEngine ( )
117+ QString HttpServer::GetPlatform ( void )
123118{
124- return ((HtmlServerExtension *)m_pHtmlServer)->ScriptEngine ();
119+ QMutexLocker locker (&s_platformLock);
120+ return s_platform;
125121}
126122
127123// ///////////////////////////////////////////////////////////////////////////
128124//
129125// ///////////////////////////////////////////////////////////////////////////
130126
131- WorkerThread *HttpServer::CreateWorkerThread ( ThreadPool * /* pThreadPool */ ,
132- const QString &sName )
127+ QScriptEngine* HttpServer::ScriptEngine ()
133128{
134- return ( new HttpWorkerThread ( this , sName ) );
129+ return ((HtmlServerExtension *)m_pHtmlServer)-> ScriptEngine ( );
135130}
136131
137132// ///////////////////////////////////////////////////////////////////////////
@@ -140,10 +135,9 @@ WorkerThread *HttpServer::CreateWorkerThread( ThreadPool * /*pThreadPool */,
140135
141136void HttpServer::incomingConnection (int nSocket)
142137{
143- HttpWorkerThread *pThread = (HttpWorkerThread *)GetWorkerThread ();
144-
145- if (pThread != NULL )
146- pThread->StartWork ( nSocket );
138+ m_threadPool.startReserved (
139+ new HttpWorker (*this , nSocket),
140+ QString (" HttpServer%1" ).arg (nSocket));
147141}
148142
149143// ///////////////////////////////////////////////////////////////////////////
@@ -195,7 +189,7 @@ void HttpServer::UnregisterExtension( HttpServerExtension *pExtension )
195189//
196190// ///////////////////////////////////////////////////////////////////////////
197191
198- void HttpServer::DelegateRequest ( HttpWorkerThread *pThread, HTTPRequest *pRequest )
192+ void HttpServer::DelegateRequest (HTTPRequest *pRequest)
199193{
200194 bool bProcessed = false ;
201195
@@ -207,7 +201,7 @@ void HttpServer::DelegateRequest( HttpWorkerThread *pThread, HTTPRequest *pReque
207201 {
208202 try
209203 {
210- bProcessed = list[ nIdx ]->ProcessRequest (pThread, pRequest);
204+ bProcessed = list[ nIdx ]->ProcessRequest (pRequest);
211205 }
212206 catch (...)
213207 {
@@ -224,7 +218,7 @@ void HttpServer::DelegateRequest( HttpWorkerThread *pThread, HTTPRequest *pReque
224218 {
225219 try
226220 {
227- bProcessed = (*it)->ProcessRequest(pThread, pRequest);
221+ bProcessed = (*it)->ProcessRequest(pRequest);
228222 }
229223 catch(...)
230224 {
@@ -237,7 +231,7 @@ void HttpServer::DelegateRequest( HttpWorkerThread *pThread, HTTPRequest *pReque
237231 m_rwlock.unlock ();
238232
239233 if (!bProcessed)
240- bProcessed = m_pHtmlServer->ProcessRequest ( pThread, pRequest );
234+ bProcessed = m_pHtmlServer->ProcessRequest (pRequest);
241235
242236 if (!bProcessed)
243237 {
@@ -254,67 +248,22 @@ void HttpServer::DelegateRequest( HttpWorkerThread *pThread, HTTPRequest *pReque
254248// ///////////////////////////////////////////////////////////////////////////
255249// ///////////////////////////////////////////////////////////////////////////
256250
257- // ///////////////////////////////////////////////////////////////////////////
258- //
259- // ///////////////////////////////////////////////////////////////////////////
260-
261- HttpWorkerThread::HttpWorkerThread ( HttpServer *pParent, const QString &sName ) :
262- WorkerThread( (ThreadPool *)pParent, sName )
251+ HttpWorker::HttpWorker (HttpServer &httpServer, int sock) :
252+ m_httpServer(httpServer), m_socket(sock), m_socketTimeout(10000 )
263253{
264- m_pHttpServer = pParent;
265- m_nSocket = 0 ;
266- m_nSocketTimeout = 1000 *
254+ m_socketTimeout = 1000 *
267255 UPnp::GetConfiguration ()->GetValue (" HTTP/KeepAliveTimeoutSecs" , 10 );
268-
269- m_pData = NULL ;
270256}
271257
272258// ///////////////////////////////////////////////////////////////////////////
273259//
274260// ///////////////////////////////////////////////////////////////////////////
275261
276- HttpWorkerThread::~HttpWorkerThread ()
277- {
278- if (m_pData != NULL )
279- delete m_pData;
280- }
281-
282- // ///////////////////////////////////////////////////////////////////////////
283- //
284- // ///////////////////////////////////////////////////////////////////////////
285-
286- void HttpWorkerThread::SetWorkerData ( HttpWorkerData *pData )
287- {
288- // WorkerThread takes ownership of pData pointer.
289- // (Must be allocated on heap)
290-
291- if (m_pData != NULL )
292- delete m_pData;
293-
294- m_pData = pData;
295- }
296-
297- // ///////////////////////////////////////////////////////////////////////////
298- //
299- // ///////////////////////////////////////////////////////////////////////////
300-
301- void HttpWorkerThread::StartWork ( int nSocket )
302- {
303- m_nSocket = nSocket;
304-
305- SignalWork ();
306- }
307-
308- // ///////////////////////////////////////////////////////////////////////////
309- //
310- // ///////////////////////////////////////////////////////////////////////////
311-
312- void HttpWorkerThread::ProcessWork ()
262+ void HttpWorker::run (void )
313263{
314264#if 0
315265 LOG(VB_UPNP, LOG_DEBUG,
316- QString("HttpWorkerThread::ProcessWork:Begin( %1 ) socket=%2")
317- .arg((long)QThread::currentThread()) .arg(m_nSocket));
266+ QString("HttpWorker::run() socket=%1 -- begin").arg(m_socket));
318267#endif
319268
320269 bool bTimeout = false ;
@@ -324,19 +273,21 @@ void HttpWorkerThread::ProcessWork()
324273
325274 try
326275 {
327- if ((pSocket = new BufferedSocketDevice ( m_nSocket )) == NULL )
276+ if ((pSocket = new BufferedSocketDevice ( m_socket )) == NULL )
328277 {
329278 LOG (VB_GENERAL, LOG_ERR, " Error Creating BufferedSocketDevice" );
330279 return ;
331280 }
332281
333282 pSocket->SocketDevice ()->setBlocking ( true );
334283
335- while ( !m_bTermRequested && bKeepAlive && pSocket->IsValid ())
284+ while (m_httpServer. IsRunning () && bKeepAlive && pSocket->IsValid ())
336285 {
337- bTimeout = 0 ;
286+ bTimeout = false ;
338287
339- int64_t nBytes = pSocket->WaitForMore (m_nSocketTimeout, &bTimeout);
288+ int64_t nBytes = pSocket->WaitForMore (m_socketTimeout, &bTimeout);
289+ if (!m_httpServer.IsRunning ())
290+ break ;
340291
341292 if ( nBytes > 0 )
342293 {
@@ -357,7 +308,7 @@ void HttpWorkerThread::ProcessWork()
357308 // ------------------------------------------------------
358309
359310 if (pRequest->m_nResponseStatus != 401 )
360- m_pHttpServer-> DelegateRequest ( this , pRequest );
311+ m_httpServer. DelegateRequest (pRequest);
361312 }
362313 else
363314 {
@@ -391,7 +342,7 @@ void HttpWorkerThread::ProcessWork()
391342 LOG (VB_UPNP, LOG_ERR,
392343 QString (" socket(%1) - Error returned from "
393344 " SendResponse... Closing connection" )
394- .arg (m_nSocket ));
345+ .arg (m_socket ));
395346 }
396347
397348 // -------------------------------------------------------
@@ -429,12 +380,10 @@ void HttpWorkerThread::ProcessWork()
429380 pSocket->Close ();
430381
431382 delete pSocket;
432- m_nSocket = 0 ;
383+ m_socket = 0 ;
433384
434385#if 0
435- LOG(VB_UPNP, LOG_DEBUG,
436- QString( "HttpWorkerThread::ProcessWork:End( %1 )")
437- .arg((long)QThread::currentThread()));
386+ LOG(VB_UPNP, LOG_DEBUG, "HttpWorkerThread::run() -- end");
438387#endif
439388}
440389
0 commit comments