threngin.cpp
来自「著名的 helix realplayer 基于手机 symbian 系统的 播放」· C++ 代码 · 共 692 行 · 第 1/2 页
CPP
692 行
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include <stdio.h>
#include "hxresult.h"
#include "hxassert.h"
#include "hxmap.h"
#include "hxcom.h"
#include "hxengin.h"
#include "hxthread.h"
#include "conn.h"
#include "thrdconn.h"
#include "threngin.h"
#include "hxtick.h"
#if defined(_UNIX) && (defined( _UNIX_THREADED_NETWORK_IO ) || defined(THREADS_SUPPORTED))
#include "platform/unix/UnixThreads.h"
#include "platform/unix/unix_net.h"
#endif
#if defined(_MACINTOSH) && defined(_CARBON) && defined(THREADS_SUPPORTED)
#include "carbthrd.h"
#endif
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
#define ALLFS 0xFFFFFFFF
#define LOCAL_LOOPBACK_ADDR 0x7F000001 // 127.0.0.1
#define LOCAL_LOOPBACK_NAME "127.0.0.1"
#define LOCAL_LOOPBACK_PORT 0x1234
void* NetworkThreadMainLoop(void* pArg);
ThreadEngine* ThreadEngine::m_pzThreadEngine = NULL;
/* Global destructor will be called at DLL shutdown and thread engine
* will be destroyed
*/
DestructEngine selfDestructor;
ThreadEngine*
ThreadEngine::GetThreadEngine(void)
{
if (!m_pzThreadEngine)
{
m_pzThreadEngine = new ThreadEngine;
}
return m_pzThreadEngine;
}
ThreadEngine::ThreadEngine()
: m_pNetworkThread(0)
, m_pMainAppThread(0)
, m_pMutex(0)
, m_pSockMap(0)
, m_pQuitEvent(NULL)
, m_bInDestructor(FALSE)
#if defined(_MACINTOSH) && defined(_CARBON) && defined(THREADS_SUPPORTED)
, m_ThreadedNetworkingCarbonTimerUPP(NULL)
, m_ThreadedNetworkingCarbonTimerRef(NULL)
#endif
#if defined(HELIX_FEATURE_NETWORK_USE_SELECT)
, m_pLocalListener(NULL)
, m_pLocalReader(NULL)
, m_pLocalWriter(NULL)
#endif // HELIX_FEATURE_NETWORK_USE_SELECT
{
m_pzThreadEngine = this;
#if defined( THREADS_SUPPORTED ) || defined( _UNIX_THREADED_NETWORK_IO )
HXMutex::MakeMutex(m_pMutex);
HXThread::MakeThread(m_pNetworkThread);
HXEvent::MakeEvent(m_pQuitEvent, NULL);
#else
HXMutex::MakeStubMutex(m_pMutex);
HXThread::MakeStubThread(m_pNetworkThread);
HXEvent::MakeStubEvent(m_pQuitEvent, NULL);
#endif
m_pNetworkThread->CreateThread(NetworkThreadMainLoop, (void*) this);
m_pSockMap = new CHXMapPtrToPtr;
/* This is just a wrapper thread for the main application
* It is really a hack. Will do it the right way sometime soon*/
#if defined( THREADS_SUPPORTED ) || defined( _UNIX_THREADED_NETWORK_IO )
HXThread::MakeThread(m_pMainAppThread);
#else
HXThread::MakeStubThread(m_pMainAppThread);
#endif
m_pMainAppThread->YieldTimeSlice();
#if defined(_MACINTOSH) && defined(_CARBON) && defined(THREADS_SUPPORTED)
HX_ASSERT(!m_ThreadedNetworkingCarbonTimerUPP);
HX_ASSERT(!m_ThreadedNetworkingCarbonTimerRef);
m_ThreadedNetworkingCarbonTimerUPP = ::NewEventLoopTimerUPP(
(EventLoopTimerProcPtr)FauxMainAppCarbonTimer);
InstallEventLoopTimer(GetCurrentEventLoop(), 0, 50 * kEventDurationMillisecond,
m_ThreadedNetworkingCarbonTimerUPP, this, &m_ThreadedNetworkingCarbonTimerRef);
#endif
}
ThreadEngine::~ThreadEngine()
{
m_pMutex->Lock();
m_bInDestructor = TRUE;
if (m_pSockMap && m_pSockMap->GetCount() > 0)
{
CHXMapPtrToPtr::Iterator ndxConn = m_pSockMap->Begin();
for (; ndxConn != m_pSockMap->End(); ++ndxConn)
{
ThreadedConn* pConn = (ThreadedConn*) (*ndxConn);
pConn->AddRef();
}
}
m_pMutex->Unlock();
if (m_pSockMap)
{
//HX_ASSERT(m_pSockMap->GetCount() == 0);
if (m_pSockMap->GetCount() > 0)
{
CHXMapPtrToPtr::Iterator ndxConn = m_pSockMap->Begin();
for (; ndxConn != m_pSockMap->End(); ++ndxConn)
{
ThreadedConn* pConn = (ThreadedConn*) (*ndxConn);
#if defined(HELIX_FEATURE_NETWORK_USE_SELECT)
if (pConn == m_pLocalReader ||
pConn == m_pLocalWriter ||
pConn == m_pLocalListener)
continue;
#endif // HELIX_FEATURE_NETWORK_USE_SELECT
pConn->finaldone();
pConn->Release();
}
}
}
if (m_pNetworkThread)
{
HXThreadMessage msg(HXMSG_QUIT, NULL, NULL);
if (m_pNetworkThread->PostMessage(&msg) == HXR_OK)
{
m_pQuitEvent->Wait(ALLFS);
}
m_pNetworkThread->Exit(0);
delete m_pNetworkThread;
m_pNetworkThread = 0;
}
#if defined(HELIX_FEATURE_NETWORK_USE_SELECT)
m_pLocalReader->Release();
m_pLocalWriter->Release();
m_pLocalListener->Release();
#endif // HELIX_FEATURE_NETWORK_USE_SELECT
#if defined(_MACINTOSH) && defined(_CARBON) && defined(THREADS_SUPPORTED)
::RemoveEventLoopTimer(m_ThreadedNetworkingCarbonTimerRef);
m_ThreadedNetworkingCarbonTimerRef;
::DisposeEventLoopTimerUPP(m_ThreadedNetworkingCarbonTimerUPP);
m_ThreadedNetworkingCarbonTimerUPP;
#endif
if (m_pMainAppThread)
{
delete m_pMainAppThread;
m_pMainAppThread = 0;
}
if (m_pMutex)
{
delete m_pMutex;
m_pMutex = 0;
}
if (m_pQuitEvent)
{
delete m_pQuitEvent;
m_pQuitEvent = 0;
}
if (m_pSockMap)
{
m_pSockMap->RemoveAll();
delete m_pSockMap;
m_pSockMap = 0;
}
}
void
ThreadEngine::DestroyThreadEngine(void)
{
if (m_pzThreadEngine)
{
ThreadEngine* pThreadEngine = m_pzThreadEngine;
m_pzThreadEngine = NULL;
delete pThreadEngine;
}
}
HX_RESULT
ThreadEngine::AttachSocket(ThreadedConn* pConn)
{
void* pVoid = 0;
HX_RESULT theErr = HXR_OK;
m_pMutex->Lock();
if (m_pSockMap->Lookup(pConn, pVoid))
{
theErr = HXR_FAIL;
}
else
{
pConn->AddRef();
m_pSockMap->SetAt((void*) pConn, (void*) pConn);
}
if (m_pSockMap->GetCount() == 1)
{
#ifdef WIN32_PLATFORM_PSPC
HXThreadMessage msg(HXMSG_ASYNC_RESUME, (void*) 200, NULL);
#else
HXThreadMessage msg(HXMSG_ASYNC_RESUME, (void*) 20, NULL);
#endif
m_pNetworkThread->PostMessage(&msg);
}
m_pMutex->Unlock();
return theErr;
}
HX_RESULT
ThreadEngine::DetachSocket(ThreadedConn* pConn)
{
void* pVoid = 0;
HX_RESULT theErr = HXR_OK;
m_pMutex->Lock();
void* blah;
if (!m_pSockMap->Lookup((void*) pConn, (void*&) blah))
{
theErr = HXR_FAIL;
}
else
{
/* if we are in the destructor, no need to remove from the map */
if (!m_bInDestructor)
{
m_pSockMap->RemoveKey((void*) pConn);
}
pConn->Detached();
pConn->Release();
}
if (m_pSockMap->GetCount() == 0)
{
HXThreadMessage msg(HXMSG_ASYNC_STOP, NULL, NULL);
m_pNetworkThread->PostMessage(&msg);
}
m_pMutex->Unlock();
return theErr;
}
//-----------------------------------------------------------
void
ThreadEngine::DoAsyncCallback(ThreadedConn* pConn, IHXCallback* pCallback)
{
void* pVoid = 0;
m_pMutex->Lock();
/* Fire callback ONLY if the connected socket is still active*/
if (m_pSockMap->Lookup(pConn, pVoid))
{
pCallback->Func();
}
pCallback->Release();
m_pMutex->Unlock();
}
//-----------------------------------------------------------
void
ThreadEngine::DoNetworkIO(ThreadedConn* pConn)
{
CHXMapPtrToPtr::Iterator ndxConn;
m_pMutex->Lock();
if (m_bInDestructor)
{
goto exit;
}
if (pConn)
{
void* pVoid = 0;
if (m_pSockMap->Lookup(pConn, pVoid))
{
pConn->DoNetworkIO();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?