📄 threngin.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: threngin.cpp,v 1.4.2.3 2004/07/09 01:47:02 hubbe Exp $ * * Portions Copyright (c) 1995-2004 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 (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (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. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * 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 0x1234void* 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_RESULTThreadEngine::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_RESULTThreadEngine::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;}//-----------------------------------------------------------voidThreadEngine::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();}//-----------------------------------------------------------voidThreadEngine::DoNetworkIO(ThreadedConn* pConn){ CHXMapPtrToPtr::Iterator ndxConn; m_pMutex->Lock(); if (m_bInDestructor) { goto exit; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -