📄 casynnet.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: casynnet.cpp,v 1.4.2.3 2004/07/09 01:46:44 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 <windows.h> // For Window-isms#include "hxcom.h"#include "hxslist.h"#include "platform/win/sock.h"#include "platform/win/hxsock.h" // For Socket related constants and macros#include "platform/win/casynnet.h" // Our definition#include "hxmap.h" // Map object#include "debugout.h" // DEBUGOUTSTR()#include "hxassert.h" // HX_ASSERT()#include "hxstrutl.h"#include "hxheap.h"#ifdef _DEBUG#undef HX_THIS_FILE static const char HX_THIS_FILE[] = __FILE__;#endif /* Constant Definitions */#define WND_CLASS_BASE "HX_Internal_Sock_Async"#define MAX_WND_CLASS_LENGTH 50#define OFFSET_THIS 0/* Class Static Definitions */BOOL CAsyncSockN::zm_bClassRegistered = FALSE;CAsyncSockN* CAsyncSockN::zm_pSockNotifiers = NULL;SockGlobals sockGlobals;/* Class Public Methods */// Either creates a socket notifier, or returns a pointer// to the socket notifier that should be used for this instance.CAsyncSockN *CAsyncSockN::GetCAsyncSockNotifier( HINSTANCE hInst , BOOL Create){ CAsyncSockN* pThis = 0; // Does our current session already have a net notifier? if (!(pThis = FindSockNotifier()) && Create) { // No, then create one pThis = new CAsyncSockN(hInst); if (pThis && !pThis->IsValid()) { // If creation failed then delete the pointer delete pThis; pThis = NULL; } if (pThis) { LinkUs(pThis); } } // return the results of our labor return (pThis);}// Our client win_net object calls this method to// start an async DNS request.BOOL CAsyncSockN::DoAsyncDNS( win_net *fpSock, LPCSTR lpHostName, LPSTR pBuff, int cbBuffSize ){ // Preliminary checks HX_ASSERT(this); HX_ASSERT(fpSock); HX_ASSERT(m_hWnd); HX_ASSERT(lpHostName && *lpHostName); // Bail on bad input if (!this || !fpSock || !lpHostName || !*lpHostName) { return(FALSE); } DNS_REQUEST* pDNSRequest = new DNS_REQUEST; pDNSRequest->fpSock = fpSock; if (lpHostName) { pDNSRequest->lpHostName = new CHAR[strlen(lpHostName)+1]; memset(pDNSRequest->lpHostName, 0, strlen(lpHostName)+1); strcpy(pDNSRequest->lpHostName, lpHostName); /* Flawfinder: ignore */ } pDNSRequest->lpBuffer = pBuff; pDNSRequest->cbBufferSize = cbBuffSize; sockGlobals.m_DNSQueue.AddTail(pDNSRequest); return ProcessDNSQueue();}BOOL CAsyncSockN::ProcessDNSQueue(void){ BOOL bResult = TRUE; DNS_REQUEST* pDNSRequest = NULL; HANDLE hAsyncHandle = NULL; int nCount = 0; if (sockGlobals.m_DNSQueue.GetCount()) { if (sockGlobals.m_bWinSock2Suck) { nCount = m_ClientHandlesMap->GetCount(); if (!nCount) { pDNSRequest = (DNS_REQUEST*)sockGlobals.m_DNSQueue.GetHead(); } else { goto cleanup; } } else { pDNSRequest = (DNS_REQUEST*)sockGlobals.m_DNSQueue.RemoveHead(); } // Start the async DNS hAsyncHandle = sockObj->HXWSAAsyncGetHostByName(m_hWnd, PWM_ASYNC_DNS, pDNSRequest->lpHostName, pDNSRequest->lpBuffer, pDNSRequest->cbBufferSize); // Create a mapping from the async handle to the win_net object pointer m_ClientHandlesMap->SetAt((void*)hAsyncHandle, pDNSRequest->fpSock); // You shouldn't already have an async operation active on this socket! HX_ASSERT(pDNSRequest->fpSock->m_hAsyncHandle == NULL); // Remember this in case we need to cancel! pDNSRequest->fpSock->m_hAsyncHandle = hAsyncHandle; // Bump our client count up IncrementHandlesClientCount(); }cleanup: if (!sockGlobals.m_bWinSock2Suck) { HX_DELETE(pDNSRequest); } return bResult;} // Our client win_net object calls this method to // start receiving async select notifications// on a socket.BOOL CAsyncSockN::DoAsyncSelect(win_net* fpSock){ SOCKET theSocket; // Preliminary checks HX_ASSERT(this); HX_ASSERT(fpSock); // Bail on bad input if (!this || !fpSock) { return(FALSE); } // Get and verify we have a good socket handle theSocket = fpSock->get_sock(); HX_ASSERT(theSocket != INVALID_SOCKET); // Request async Notifications on the socket long lEvent = (FD_CONNECT | FD_READ | FD_CLOSE) ;#ifndef HELIX_FEATURE_NETWORK_USE_SELECT lEvent |= (FD_WRITE | FD_ACCEPT);#endif sockObj->HXWSAAsyncSelect(theSocket, m_hWnd, PWM_ASYNC_SELECT, lEvent); // Create a mapping from socket handle to win_net object pointer m_ClientSocketsMap->SetAt((void*)theSocket, fpSock); // Bump our client count up IncrementSocketsClientCount(); return (TRUE);}// Our client win_net object calls this method to // stop receiving async select notifications// on a socket.BOOL CAsyncSockN::CancelSelect(win_net* fpSock){ SOCKET theSocket; DNS_REQUEST* pDNSRequest = NULL; LISTPOSITION lPos; // Preliminary checks HX_ASSERT(this); HX_ASSERT(fpSock); // Bail on bad input if (!this || !fpSock) { return(FALSE); } m_bInCancelMode = TRUE; // If we haven't returned from an Asyn operation like GetHostByName // then we need to tell Winsock to cancel the opertation! HANDLE hAsyncHandle = fpSock->m_hAsyncHandle; if (hAsyncHandle) { sockObj->HXWSACancelAsyncRequest(hAsyncHandle); fpSock->m_hAsyncHandle = NULL; // If we've canceled the async DNS is done, // then we can forget the Async handle if (m_ClientHandlesMap->RemoveKey((void*)hAsyncHandle)) { DecrementHandlesClientCount(); } } if (sockGlobals.m_bWinSock2Suck) { lPos = sockGlobals.m_DNSQueue.GetHeadPosition(); while (lPos) { pDNSRequest = (DNS_REQUEST *)sockGlobals.m_DNSQueue.GetAt(lPos); if (pDNSRequest->fpSock == fpSock) { sockGlobals.m_DNSQueue.RemoveAt(lPos); HX_DELETE(pDNSRequest); break; } sockGlobals.m_DNSQueue.GetNext(lPos); } } // Get and verify we have a good socket handle theSocket = fpSock->get_sock(); // HX_ASSERT(theSocket != INVALID_SOCKET); /* This may actually happen since we do not check for CONN_CLOSED * condition in win_net any more */ if (theSocket == INVALID_SOCKET) { m_bInCancelMode = FALSE; return FALSE; } // Turn off async Notifications on the socket sockObj->HXWSAAsyncSelect(theSocket, m_hWnd, 0, 0); // According to the Winsock help, just cause we cancel // the notifications, doesn't mean we still won't get notified // of something that has already been posted to our window, // so... let's flush our message queue... // // HACK ATTACK: Notice that both of these message pumps // are needed. If you run a debug build or a retail build // on some machines without the two message pumps, you // won't have any trouble. But on some machines having only // this last message pump will cause a GPF. MSG msg; while (m_hWnd && PeekMessage(&msg, m_hWnd,0,0,PM_REMOVE)) { // This will remove any messages! HX_TRACE("There's a message that would've go you!!\r\n"); if(msg.message == WM_QUIT) { // When peeking WM_QUIT message in the main thread of an application // we have to put it back into message queue to allow the application // to exit correctly. SB PostQuitMessage(0); break; } else { // Even though we want them removed, we also want to act on them. DispatchMessage(&msg); } } if (m_ClientSocketsMap) { // Delete the mapping from socket handle to win_net object pointer if (m_ClientSocketsMap->RemoveKey((void*)theSocket)) { // Decrement our client count and conditionally // delete ourselves. DecrementSocketsClientCount(); } } m_bInCancelMode = FALSE; // check to see if we are in use CheckClients(); return (TRUE);}/* * Private Class Methods */// Static method// Computes a session id that should be thread unique// In Win32 we have a thread ID// In Win16 we have the task (single threaded)ULONG32 CAsyncSockN::GetSession(){#if defined( _WIN32 ) return (GetCurrentThreadId());#else return ((ULONG32)(LPSTR)GetCurrentTask());#endif}// Hidden Constructor, called by GetAsyncNotifier() if there// is no Socket Notifier for the current sessionCAsyncSockN::CAsyncSockN(HINSTANCE hInst) : m_hWnd(NULL) , m_bValid(FALSE) , m_hInst(hInst) , m_cbNumHandlesClients(0) , m_cbNumSocketsClients(0) , m_bInCancelMode(FALSE) , m_pNextNotifier(0){ // Validate input HX_ASSERT(this); // assert( hInst ); if (!this) { return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -