📄 hxnetapi.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: hxnetapi.cpp,v 1.30.8.1 2004/07/09 02:09:08 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 "hxtypes.h"#include "hxresult.h"#include "hxcom.h"#include "hxcomm.h"#include "hxprefs.h"#include "hxerror.h"#if defined(_MACINTOSH)#include "hx_moreprocesses.h"#endif /* _MACINTOSH. */#include "ihxpckts.h"#include "hxfiles.h"#include "chxpckts.h"#include "hxengin.h"#include "hxpnets.h"#include "hxtick.h"#include "hxstrutl.h"#include "chxuuid.h"#include "rtsputil.h"#include "hxslist.h"#include "cbqueue.h"#include "growingq.h"#include "dbcs.h"#include "conn.h"#include "hxmarsh.h"#include "hxthread.h"#include "hxcore.h"#if defined (_UNIX)#include "unix_net.h"#include "platform/unix/hxsignal.h"#elif defined (_WIN32) || defined (_WINDOWS)#include "win_net.h"#elif defined(__TCS__)#include "platform/tm1/tm1_net.h"#elif defined (_MACINTOSH)#ifndef _MAC_MACHO#include "OpenTptInternet.h" //for AF_INET#endif#include "mac_net.h"#include "macsockets.h"#endif#include "threngin.h"#include "netbyte.h"#include "hxnetapi.h"#include "hxnetutil.h"#if defined(HELIX_FEATURE_HTTPCLOAK)#include "hxcloakedtcp.h"#endif /* HELIX_FEATURE_HTTPCLOAK */#include "hxheap.h"#include "hxprefs.h"#include "hxprefutil.h"#ifdef _DEBUG#undef HX_THIS_FILEstatic const char HX_THIS_FILE[] = __FILE__;#endif#ifdef _WIN16extern HINSTANCE g_hInstance; // initialized inside DLLMAIN.CPP(core.dll)#endifconst int UDP_CHUNK_SIZE = 1024;#define QUEUE_START_SIZE 512#if defined _WINDOWS && !defined _WIN32 // for win16#define DESIRED_RCV_BUFSIZE 0xFFFF // ~ 525 KBits#else#define DESIRED_RCV_BUFSIZE 0x0002FFFF // ~ 1.5 MBits#endif //defined _WINDOWS && !defined _WIN32#ifdef _WINCE#define SCHED_GRANULARITY 10#else#define SCHED_GRANULARITY 50#endif// same for all the platforms...may need to tweak it, if necessary#define MAX_ITERATION_COUNT 200#if defined( _UNIX )BOOL ReadAsyncDNSPref( IUnknown* pContext ){ static BOOL bNoAsyncDNS = FALSE; static BOOL bNeedToLoad = TRUE; if( bNeedToLoad && NULL!=pContext) { IHXPreferences* pPreferences = NULL; bNeedToLoad = FALSE; pContext->QueryInterface(IID_IHXPreferences, (void **)&pPreferences); IHXBuffer *pBuffer = NULL; if( NULL!=pPreferences ) { pPreferences->ReadPref("NoAsyncDNS", pBuffer); if (pBuffer) { bNoAsyncDNS = (atoi((const char*)pBuffer->GetBuffer()) == 1); HX_RELEASE(pBuffer); } HX_RELEASE( pPreferences ); } // NULL!=pPreferences }//bNeedToLoad && NULL!=pContext return bNoAsyncDNS;}BOOL ReadThreadedDNSPref( IUnknown* pContext ){ static BOOL bThreadedDNS = TRUE; static BOOL bNeedToLoad = TRUE; if( bNeedToLoad && NULL!=pContext) { IHXPreferences* pPreferences = NULL; bNeedToLoad = FALSE; pContext->QueryInterface(IID_IHXPreferences, (void **)&pPreferences); IHXBuffer *pBuffer = NULL; if( NULL!=pPreferences ) { pPreferences->ReadPref("ThreadedDNS", pBuffer); if (pBuffer) { bThreadedDNS = (atoi((const char*)pBuffer->GetBuffer()) == 1); HX_RELEASE(pBuffer); } else { //XXXGFW HACK!! REMOVE AFTER RNC //Pref was not found. It defaults to ON now so lets see if we //are running under a player. If not, turn it off. IHXClientEngine* pEngine = NULL; if(HXR_OK == pContext->QueryInterface(IID_IHXClientEngine, (void**)&pEngine)) { //We have client Engine. Do nothing. HX_RELEASE( pEngine ); } else { //We don't have a client engine. Turn off threading... bThreadedDNS = FALSE; } } HX_RELEASE( pPreferences ); } // NULL!=pPreferences }//bNeedToLoad && NULL!=pContext return bThreadedDNS;}#endif#if !defined(HELIX_CONFIG_NOSTATICS)UINT16 HXNetworkServices::z_muNumDriverInstance = 0;#else#include "globals/hxglobals.h"const UINT16 HXNetworkServices::z_muNumDriverInstance = 0;#endif/* HXNetworkServices */HXNetworkServices::HXNetworkServices(IUnknown* pContext){ m_bNeedToCleanupDrivers = FALSE; m_lRefCount = 0; m_pContext = pContext; if (m_pContext) { m_pContext->AddRef(); }}HXNetworkServices::~HXNetworkServices(){ Close();}STDMETHODIMP HXNetworkServices::QueryInterface(REFIID riid, void** ppvObj){ QInterfaceList qiList[] = { { GET_IIDHANDLE(IID_IHXNetworkServices), (IHXNetworkServices*)this }, { GET_IIDHANDLE(IID_IHXNetworkInterfaceEnumerator), (IHXNetworkInterfaceEnumerator*)this },#if defined(HELIX_FEATURE_HTTPCLOAK) { GET_IIDHANDLE(IID_IHXCloakedNetworkServices), (IHXCloakedNetworkServices*)this },#endif /* HELIX_FEATURE_HTTPCLOAK */ { GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXNetworkServices*)this }, }; return ::QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);}STDMETHODIMP_(ULONG32) HXNetworkServices::AddRef(){ return InterlockedIncrement(&m_lRefCount);}STDMETHODIMP_(ULONG32) HXNetworkServices::Release(){ if (InterlockedDecrement(&m_lRefCount) > 0) { return m_lRefCount; } delete this; return 0;}voidHXNetworkServices::UseDrivers(){ if (!m_bNeedToCleanupDrivers) { m_bNeedToCleanupDrivers = TRUE;#if defined(HELIX_CONFIG_NOSTATICS) UINT16& z_muNumDriverInstance = (UINT16&) HXGlobalInt16::Get(&HXNetworkServices::z_muNumDriverInstance);#endif z_muNumDriverInstance++; }}voidHXNetworkServices::Close(){ HX_RELEASE(m_pContext); if (m_bNeedToCleanupDrivers) { m_bNeedToCleanupDrivers = FALSE;#if defined(HELIX_CONFIG_NOSTATICS) UINT16& z_muNumDriverInstance = (UINT16&) HXGlobalInt16::Get(&HXNetworkServices::z_muNumDriverInstance);#endif z_muNumDriverInstance--; if (z_muNumDriverInstance == 0) {#if defined(THREADS_SUPPORTED) ThreadEngine::DestroyThreadEngine();#elif defined(_UNIX_THREADED_NETWORK_IO) if( ReadNetworkThreadingPref((IUnknown*)m_pContext) ) ThreadEngine::DestroyThreadEngine();#endif conn::close_drivers(NULL); } }}STDMETHODIMPHXNetworkServices::CreateTCPSocket(IHXTCPSocket** ppTCPSocket){ *ppTCPSocket = new HXTCPSocket(m_pContext, this); if (*ppTCPSocket == NULL) { return HXR_OUTOFMEMORY; } (*ppTCPSocket)->AddRef(); return HXR_OK;}STDMETHODIMPHXNetworkServices::CreateUDPSocket(IHXUDPSocket** ppUDPSocket){ *ppUDPSocket = new HXUDPSocket(m_pContext, this); if (*ppUDPSocket == NULL) { return HXR_OUTOFMEMORY; } (*ppUDPSocket)->AddRef(); return HXR_OK;}STDMETHODIMPHXNetworkServices::CreateListenSocket(IHXListenSocket** ppListenSocket){ *ppListenSocket = new HXListenSocket(m_pContext, this); if (*ppListenSocket == NULL) { return HXR_OUTOFMEMORY; } (*ppListenSocket)->AddRef(); return HXR_OK;}STDMETHODIMPHXNetworkServices::CreateResolver(IHXResolver** ppResolver){ *ppResolver = new HXResolver(this, m_pContext); if(*ppResolver == NULL) { return HXR_OUTOFMEMORY; } (*ppResolver)->AddRef(); return HXR_OK;}STDMETHODIMPHXNetworkServices::CreateClientCloakedSocket(IHXTCPSocket** ppTCPSocket){#if defined(HELIX_FEATURE_HTTPCLOAK) *ppTCPSocket = new HXClientCloakedTCPSocket(m_pContext); if (*ppTCPSocket == NULL) { return HXR_OUTOFMEMORY; } (*ppTCPSocket)->AddRef(); return HXR_OK;#else return HXR_NOTIMPL;#endif /* HELIX_FEATURE_HTTPCLOAK */}STDMETHODIMPHXNetworkServices::CreateServerCloakedSocket(IHXListenSocket** ppListenSocket){ return HXR_NOTIMPL;}/* IHXNetworkInterfaceEnumerator */STDMETHODIMPHXNetworkServices::EnumerateInterfaces (REF(UINT32*) pulInterfaces, REF(UINT32) ulNumInterfaces){ return conn::EnumerateInterfaces(pulInterfaces, ulNumInterfaces);}/* HXResolver */HXResolver::HXResolver(HXNetworkServices* pNetworkServices): m_lRefCount(0) ,m_pCallback(0) ,m_pResp(0) ,m_bResolverPending(FALSE) ,m_pData(0) ,m_pNetworkServices(NULL){ m_pNetworkServices = pNetworkServices; m_pNetworkServices->AddRef();}HXResolver::HXResolver( HXNetworkServices* pNetworkServices, IUnknown* pContext): m_lRefCount(0) ,m_pCallback(0) ,m_pResp(0) ,m_bResolverPending(FALSE) ,m_pData(0) ,m_pNetworkServices(NULL) ,m_pContext( pContext ){ m_pNetworkServices = pNetworkServices; m_pNetworkServices->AddRef();}HXResolver::~HXResolver(){ if (m_pData) { m_pData->done(); m_pData->Release(); m_pData = 0; } if (m_pCallback) { delete m_pCallback; m_pCallback = 0; } if (m_pResp) { m_pResp->Release(); m_pResp = 0; }#if defined( _WIN32 ) || defined( _WINDOWS ) win_net::ReleaseWinsockUsage(this);#endif HX_RELEASE(m_pNetworkServices);}STDMETHODIMPHXResolver::QueryInterface(REFIID riid, void** ppvObj){ QInterfaceList qiList[] = { { GET_IIDHANDLE(IID_IHXResolver), (IHXResolver*)this }, { GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXResolver*)this }, }; return ::QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);}STDMETHODIMP_(ULONG32) HXResolver::AddRef(){ return InterlockedIncrement(&m_lRefCount);}STDMETHODIMP_(ULONG32) HXResolver::Release(){ if (InterlockedDecrement(&m_lRefCount) > 0) { return m_lRefCount; } delete this; return 0;}STDMETHODIMP HXResolver::Init(IHXResolverResponse* pResp){ if (!pResp) { return HXR_UNEXPECTED; } if (m_pResp) { m_pResp->Release(); m_pResp = 0; } m_pResp = pResp; m_pResp->AddRef(); return HXR_OK;}STDMETHODIMP HXResolver::GetHostByName(const char* pHostName){ HX_RESULT theErr = HXR_OK; if (!pHostName || m_bResolverPending) { return HXR_UNEXPECTED; } if (!m_pCallback) { m_pCallback = new HXResolverCallback; if (!m_pCallback) { return HXR_OUTOFMEMORY; } m_pCallback->m_pContext = this; } /* Is this the first time we are calling gethostbyname */ if (!m_pData) {#if defined( _WIN32 ) || defined( _WINDOWS ) // Have we been able to load and initialize the winsock stuff yet? if (!win_net::IsWinsockAvailable(this)) { return HXR_FAIL; // HXR_GENERAL_NONET; }#endif m_pNetworkServices->UseDrivers(); conn::init_drivers(NULL); } if (m_pData) { m_pData->done(); m_pData->Release(); m_pData = 0; }#ifdef _UNIX //This one has to be set before we create a new socket. conn::SetNetworkThreadingPref( ReadNetworkThreadingPref((IUnknown*)m_pContext) ); conn::SetThreadedDNSPref( ReadThreadedDNSPref((IUnknown*)m_pContext) );#endif m_pData = conn::new_socket(HX_UDP_SOCKET); if (!m_pData) { return HXR_OUTOFMEMORY; }#ifdef _UNIX m_pData->SetAsyncDNSPref( ReadAsyncDNSPref((IUnknown*)m_pContext) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -