📄 thrdconn.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: thrdconn.cpp,v 1.11.2.4 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 "hlxclib/time.h"#include <stdio.h>#include <string.h>#include "hxcom.h"#include "conn.h"#if defined (_WIN32) || defined (WIN32)#include "platform/win/win_net.h"#include "platform/win/casynthr.h"#endif#if defined(_UNIX) && (defined( _UNIX_THREADED_NETWORK_IO ) || defined(THREADS_SUPPORTED))#include "platform/unix/UnixThreads.h"#endif /* _UNIX_THREADED_NETWORK_IO */#ifdef _CARBON#include "carbthrd.h"#endif#include "hxslist.h"#include "growingq.h"#include "hxengin.h"#include "ihxpckts.h"#include "hxbuffer.h"#include "timebuff.h"#include "hxtick.h"#include "hxthread.h"#include "threngin.h"#include "conn.h"#include "thrdconn.h"#include "hxheap.h"#ifdef _DEBUG#undef HX_THIS_FILEstatic const char HX_THIS_FILE[] = __FILE__;#endif#define QUEUE_START_SIZE 512ThreadedConn*ThreadedConn::new_socket(UINT16 type){ return new ThreadedConn(type);}#ifdef THREADS_SUPPORTED#ifdef HELIX_FEATURE_ADD_NETWORK_THREAD_SLEEP#define DEFAULT_NETWORK_THREAD_SLEEP 50#endif //HELIX_FEATURE_ADD_NETWORK_THREAD_SLEEP#endif //THREADS_SUPPORTED/*HXThread*ThreadedConn::GetNetworkThread(void){#if defined (_WIN32) return win_net::GetNetworkThread();#else HX_ASSERT(FALSE); return HXR_UNEXXPECTED;#endif}voidThreadedConn::DestroyNetworkThread(void){#if defined (_WIN32) win_net::DestroyNetworkThread();#else HX_ASSERT(FALSE); return P NR_UNEXXPECTED;#endif}*/ThreadedConn::ThreadedConn(UINT16 type) : m_lRefCount(0) , m_pActualConn(NULL) , m_uSocketType(type) , m_pNetworkThread(NULL) , m_pMainAppThread(NULL) , m_pNetCallback(0) , m_pSendTCP (0) , m_pReceiveTCP (0) , m_pTempBuffer (0) , m_ulUserHandle(0) , m_pInternalWindowHandle(NULL) , m_bConnected(FALSE) , m_bIsDone(FALSE) , m_bDetachPending(TRUE) , m_pInitEvent(0) , m_pQuitEvent(0) , m_pListenEvent(NULL) , m_pDetachEvent(0) , m_bInitialized(FALSE) , m_bOutstandingReadNotification(FALSE) , m_bOutstandingWriteNotification(FALSE) , m_bWriteFlushPending(FALSE) , m_bNetworkIOPending(FALSE) , m_bReadNowPending(FALSE) , m_bReadPostPendingWouldBlock(FALSE) , m_pIncommingConnections(NULL) , m_bIgnoreWSAECONNRESET(FALSE)#if defined (_WIN32) || defined (WIN32) , m_pNotifier(NULL)#endif#ifdef THREADS_SUPPORTED#ifdef HELIX_FEATURE_ADD_NETWORK_THREAD_SLEEP , m_ulNetworkThreadSleep(DEFAULT_NETWORK_THREAD_SLEEP)#endif //HELIX_FEATURE_ADD_NETWORK_THREAD_SLEEP#endif //THREADS_SUPPORTED{ m_pActualConn = conn::actual_new_socket(type); m_pActualConn->AddRef(); conn::add_connection_to_list (m_pActualConn); ThreadEngine* pEngine = ThreadEngine::GetThreadEngine(); // NOTE: pEngine allocation is not checked for success. pEngine->AttachSocket(this); m_pNetworkThread = pEngine->GetNetworkThread(); m_pMainAppThread = pEngine->GetMainAppThread(); m_pNetCallback = new ThrConnSocketCallback(this);#if defined(THREADS_SUPPORTED) || defined(_UNIX_THREADED_NETWORK_IO) HXMutex::MakeMutex(m_pMutex); HXEvent::MakeEvent(m_pInitEvent, NULL, FALSE); HXEvent::MakeEvent(m_pQuitEvent, NULL); HXEvent::MakeEvent(m_pDetachEvent, NULL); HXEvent::MakeEvent(m_pListenEvent, NULL, FALSE);#else HXMutex::MakeStubMutex(m_pMutex); HXEvent::MakeStubEvent(m_pInitEvent, NULL, FALSE); HXEvent::MakeStubEvent(m_pQuitEvent, NULL); HXEvent::MakeStubEvent(m_pDetachEvent, NULL); HXEvent::MakeStubEvent(m_pListenEvent, NULL, FALSE);#endif m_pTempBuffer = new char[TCP_BUF_SIZE]; /* Allocate byte queues ONLY if it is a TCP socket */ if (m_uSocketType == HX_TCP_SOCKET) { // allocate TCP send and receive queue m_pSendTCP = new CByteGrowingQueue(QUEUE_START_SIZE,1); if (!m_pSendTCP || !m_pSendTCP->IsQueueValid()) { mLastError = HXR_OUTOFMEMORY; } m_pSendTCP->SetMaxSize(TCP_BUF_SIZE); m_pReceiveTCP = new CByteGrowingQueue(QUEUE_START_SIZE,1); if (!m_pReceiveTCP || !m_pReceiveTCP->IsQueueValid()) { mLastError = HXR_OUTOFMEMORY; } m_pReceiveTCP->SetMaxSize(TCP_BUF_SIZE); }}ThreadedConn::~ThreadedConn(){ if (m_pNetCallback) { m_pNetCallback->m_pContext = 0; } if (m_pActualConn) { m_pActualConn->done(); m_pActualConn->Release(); m_pActualConn = 0; } HX_DELETE(m_pNetCallback); HX_VECTOR_DELETE(m_pTempBuffer); HX_DELETE(m_pSendTCP); HX_DELETE(m_pReceiveTCP); while (m_WriteUDPBuffers.GetCount() > 0) { UDPPacketInfo* pPacket = (UDPPacketInfo*) m_WriteUDPBuffers.RemoveHead(); pPacket->m_pBuffer->Release(); delete pPacket; } while (m_ReadUDPBuffers.GetCount() > 0) { UDP_PACKET* pPacket = (UDP_PACKET*) m_ReadUDPBuffers.RemoveHead(); HX_RELEASE(pPacket->pBuffer); HX_DELETE(pPacket); } HX_DELETE(m_pMutex); HX_DELETE(m_pInitEvent); HX_DELETE(m_pQuitEvent); HX_DELETE(m_pDetachEvent); HX_DELETE(m_pListenEvent); HX_DELETE(m_pIncommingConnections); mCallBack = NULL;#ifdef _UNIX_THREADED_NETWORK_IO if( m_bNetworkThreading ) { //Remove any messages from the main app thread for us. HX_ASSERT( m_pMainAppThread ); HXThreadMessage msgBack; HXThreadMessage msgMatch(0, (void*)this, NULL, NULL); while(((HXUnixThread*)m_pMainAppThread)->PeekMessageMatching(&msgBack, &msgMatch, TRUE )==HXR_OK) { } } m_pMainAppThread=NULL;#elif defined(_CARBON) && defined(THREADS_SUPPORTED) // remove any messages from the main app thread for us. HX_ASSERT(m_pMainAppThread); HXThreadMessage msgBack; HXThreadMessage msgMatch(0, (void*)this, NULL, NULL); while (((HXCarbonThread*)m_pMainAppThread)->PeekMessageMatching(&msgBack, &msgMatch, TRUE) == HXR_OK) { } m_pMainAppThread = NULL;#endif}ULONG32 ThreadedConn::AddRef(){ return InterlockedIncrement(&m_lRefCount);}ULONG32 ThreadedConn::Release(){ if (InterlockedDecrement(&m_lRefCount) > 0) { return m_lRefCount; } delete this; return 0;}HX_RESULTThreadedConn::dns_find_ip_addr(const char* host, UINT16 blocking){ ThrdConnGenericCallback* pCallback = new ThrdConnGenericCallback(this, DNS_CALLBACK_TYPE); pCallback->m_HostName = host; pCallback->m_bBlocking = (BOOL) blocking; /* Will be released by the thread engine */ pCallback->AddRef(); HXThreadMessage msg(HXMSG_ASYNC_CALLBACK, this, pCallback); return m_pNetworkThread->PostMessage(&msg);}HX_RESULTThreadedConn::ActualDnsFindIpAddr(const char* host, UINT16 blocking){ HX_RESULT theErr = HXR_UNEXPECTED; HX_ASSERT(m_pActualConn); if (m_pActualConn) { m_pMutex->Lock(); theErr = m_pActualConn->dns_find_ip_addr(host, blocking); m_pMutex->Unlock(); } return theErr;}BOOLThreadedConn::dns_ip_addr_found(BOOL* valid, ULONG32* addr){ BOOL bResult = FALSE; HX_ASSERT(m_pActualConn); if (m_pActualConn) { m_pMutex->Lock(); bResult = m_pActualConn->dns_ip_addr_found(valid, addr); m_pMutex->Unlock(); } return bResult;}voidThreadedConn::finaldone (void){ if (!m_bIsDone) { done(); } /* final attempt to cleanup */ PostDoneAndDetach(); if (m_pQuitEvent) { m_pQuitEvent->Wait(ALLFS); } if (m_pDetachEvent) { m_pDetachEvent->Wait(ALLFS); }}voidThreadedConn::Detached (void){ if (m_pDetachEvent) { m_pDetachEvent->SignalEvent(); }}voidThreadedConn::done (void){ /* Do not pass any more callbacks to the client above */ mCallBack = NULL; m_bIsDone = TRUE; /* Actual message to release the socket will be posted in DoWrite() */#if (defined (_WIN32) || defined (WIN32)) && !defined(WIN32_PLATFORM_PSPC) if (!m_pNotifier) { m_pNotifier = CAsyncNetThread::GetCAsyncNetThreadNotifier((HINSTANCE)m_ulUserHandle, FALSE); } if (m_pNotifier) { m_pNotifier->DetachSocket(this); m_pNotifier = NULL; }#endif /*defined (_WINDOWS) || defined (_WIN32)*/ if (!m_bConnected) { PostDoneAndDetach(); }}voidThreadedConn::PostDoneAndDetach(){ // If we are out of memory, let's just get out of here. Ideally, we should // not ever get to this point, but lots of functions here have void return // types, so it is possible. if( mLastError == HXR_OUTOFMEMORY ) { return; } m_pMutex->Lock(); if (m_bDetachPending) { m_bDetachPending = FALSE; ThrdConnGenericCallback* pCallback = new ThrdConnGenericCallback(this, DONE_CALLBACK_TYPE); /* Will be released by the thread engine */ pCallback->AddRef(); HXThreadMessage msg(HXMSG_ASYNC_CALLBACK, this, pCallback); m_pNetworkThread->PostMessage(&msg); HXThreadMessage msg1(HXMSG_ASYNC_DETACH, this, NULL); m_pNetworkThread->PostMessage(&msg1); } m_pMutex->Unlock();}voidThreadedConn::ActualDone (void){ HX_ASSERT(m_pActualConn); if (m_pActualConn) { m_pMutex->Lock(); m_bConnected = FALSE; m_pActualConn->done(); m_pActualConn->Release(); m_pActualConn = 0; m_pMutex->Unlock(); } if (m_pQuitEvent) { m_pQuitEvent->SignalEvent(); }}HX_RESULTThreadedConn::init (UINT32 local_addr,UINT16 port, UINT16 blocking){ ThrdConnGenericCallback* pCallback = new ThrdConnGenericCallback(this, INIT_CALLBACK_TYPE); pCallback->m_ulLocalAddr = local_addr; pCallback->m_uPort = port; pCallback->m_bBlocking = (BOOL) blocking; /* Will be released by the thread engine */ pCallback->AddRef(); HXThreadMessage msg(HXMSG_ASYNC_CALLBACK, this, pCallback); m_pNetworkThread->PostMessage(&msg); /* Wait for the actual Initialization to complete. This is the only function we wait for the networking thread to complete before passing the result to the user since there is no async interface to pass the result back and it very much possible that socket binding may fail during initialization */ m_pInitEvent->Wait(); if (m_bInitialized) { return HXR_OK; } else { return HXR_FAIL; }}HX_RESULTThreadedConn::ActualInit(UINT32 local_addr,UINT16 port, UINT16 blocking){ HX_RESULT theErr = HXR_UNEXPECTED; HX_ASSERT(m_pActualConn); if (m_pActualConn) { m_pMutex->Lock(); theErr = m_pActualConn->init(local_addr, port, blocking); if (!theErr && m_uSocketType == HX_UDP_SOCKET) { m_bConnected = TRUE; } if (!theErr) { m_bInitialized = TRUE; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -