⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchserver.cpp

📁 c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++6.0
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		sizeof(TCHAR));

	// Copy it into the buffer.
	memset((void *)resHeader.tczMyName, 0, sizeof(resHeader.tczMyName));
	memcpy((void *)resHeader.tczMyName, (void *)cszMyEMail.GetBuffer(0),
		cszMyEMail.GetLength() * sizeof(TCHAR));

	// Save the number of results.
	resHeader.nResults = csaResults.GetSize();

	// Open a connection to the requesting machine.
	CTimeoutSocket *pSock = NULL;
	for(int i = 0; i < MAX_CONNECT_RETRIES && !pSock; i++)
		pSock = OpenConnection(cszIPAddress);

	if(!pSock) return FALSE;

	// Push the header over the wire.
	pSock->SetTimeout(SOCKET_TIMEOUT);
	if(pSock->Send((void *)&resHeader, sizeof(resHeader)) !=
		sizeof(resHeader))
	{
		// Failed.
		CloseConnection(pSock);
		return FALSE;
	}

	if(resHeader.nResults <= 0)    // No results; just quit.
	{
		CloseConnection(pSock);
		return FALSE;
	}

	// Now construct the buffer for the results.
	TCHAR *pczResults = new TCHAR[resHeader.nResults * (MAX_PATH + 1)];
	memset((void *)pczResults, 0, 
		sizeof(TCHAR) * resHeader.nResults * (MAX_PATH + 1));

	// Fill the buffer.
	for(i = 0; i < resHeader.nResults; i++)
	{
		// Get the right place in the buffer.
		TCHAR *pInsert = pczResults + (i * (MAX_PATH + 1));

		// Get a name of appropriate length.
		CString cszName;
		ASSERT(i < csaResults.GetSize());      // (in case it changes somehow).
		cszName = csaResults.GetAt(i);
		cszName = cszName.Left(MAX_PATH);

		// Copy it in.
		memcpy((void *)pInsert, (void *)cszName.GetBuffer(0),
			cszName.GetLength() * sizeof(TCHAR));
	}

	// Push the buffer over.
	pSock->SetTimeout(SOCKET_TIMEOUT * resHeader.nResults);
	pSock->Send((void *)pczResults, 
		sizeof(TCHAR) * resHeader.nResults * (MAX_PATH + 1));

	// Now construct the buffer for the titles.
	delete pczResults;
	pczResults = new TCHAR[resHeader.nResults * (MAX_PATH + 1)];
	memset((void *)pczResults, 0, 
		sizeof(TCHAR) * resHeader.nResults * (MAX_PATH + 1));

	// Fill the buffer.
	for(i = 0; i < resHeader.nResults; i++)
	{
		// Get the right place in the buffer.
		TCHAR *pInsert = pczResults + (i * (MAX_PATH + 1));

		// Get a name of appropriate length.
		CString cszName;
		ASSERT(i < csaTitles.GetSize());      // (in case it changes somehow).
		cszName = csaTitles.GetAt(i);
		cszName = cszName.Left(MAX_PATH);

		// Copy it in.
		memcpy((void *)pInsert, (void *)cszName.GetBuffer(0),
			cszName.GetLength() * sizeof(TCHAR));
	}

	// Push the buffer over.
	pSock->SetTimeout(SOCKET_TIMEOUT * resHeader.nResults);
	pSock->Send((void *)pczResults, 
		sizeof(TCHAR) * resHeader.nResults * (MAX_PATH + 1));

	// Close the connection.
	CloseConnection(pSock);

	// Free memory.
	delete pczResults;

	// Done.
	return TRUE;
}

bool CSearchServer::IP_ExplicitToByte(CString &exprep, unsigned char *brep)
{
	int nums[4];

	ASSERT(brep);
	if(!brep) return false;

	if(sscanf(exprep.GetBuffer(0), _T("%i.%i.%i.%i"),
		&nums[0], &nums[1], &nums[2], &nums[3]) != 4)
	{
		memset((void *)brep, 0, sizeof(char) * 4);
		return false;
	}

	brep[0] = (unsigned char)nums[0];
	brep[1] = (unsigned char)nums[1];
	brep[2] = (unsigned char)nums[2];
	brep[3] = (unsigned char)nums[3];

	return true;
}

bool CSearchServer::IP_ByteToExplicit(unsigned char *brep, CString &exprep)
{
	ASSERT(brep);
	if(!brep) return false;

	exprep.Format(_T("%i.%i.%i.%i"), 
		(int)brep[0], (int)brep[1], (int)brep[2], (int)brep[3]);

	return true;
}

CTimeoutSocket *CSearchServer::OpenConnection(LPCTSTR czIP, UINT nPort)
{
	CTimeoutSocket *sockRet = new CTimeoutSocket;

	if(!sockRet->Create())
	{
		delete sockRet;
		return NULL;
	}

	if(!sockRet->Connect(czIP, nPort))
	{
		int nLastErr = sockRet->GetLastError();
		CString czErr; czErr.Format(_T("Last error: %i\n"), nLastErr);
		TRACE(czErr);

		delete sockRet;
		return NULL;
	}

	return sockRet;
}

void CSearchServer::CloseConnection(CTimeoutSocket * sock)
{
	if(!sock)
		return;

	sock->Close();
	delete sock;
}

BOOL CSearchServer::SendRequest(RequestHeader rHeader, LPCTSTR czReqString)
{
	CAwareNet cANet;
	TCHAR tczRequest[REQUEST_STRING_LEN];
	int nFrnd;

	// Make sure AwareNet is initialized.
	COscarApp *pApp = (COscarApp *)AfxGetApp();
	ASSERT(pApp);
	if(!pApp->m_bANetInit)
		return FALSE;

	int nMyID = cANet.GetMyID();

	AppendComment(_T("SearchServer: Forwarding request"));

	// TODO: Unicode.
	memset((void *)tczRequest, 0, sizeof(tczRequest));
	strncpy(tczRequest, czReqString, REQUEST_STRING_LEN);

	// Send the request to all the friends.
	AppendComment(_T("SearchServer: Sending request to all online friends"));
	for(int i = 0; (nFrnd = cANet.GetFriend(i)) >= 0; i++)
	{
		if(nFrnd == nMyID) continue;

		// Get the IP address.
		CString cszIP;
		if(!cANet.GetIP(cszIP, nFrnd))
			continue;

		// Make sure the friend is online.
		if(cszIP.CompareNoCase(_T("0.0.0.0")) == 0)
			continue;

		AppendComment(_T("SearchServer: Friend: " + cszIP));

		// Now open a connection to the friend's search server.
		CTimeoutSocket *pSock = NULL;
		// Just one shot; who cares?
		pSock = OpenConnection(cszIP, PORT_OSCARSEARCH); 
		
		if(!pSock)
			continue;

		// Send the request header.
		pSock->SetTimeout(SOCKET_TIMEOUT);
		if(pSock->Send((void *)&rHeader, sizeof(rHeader)) != sizeof(rHeader))
		{
			CloseConnection(pSock);
			continue;
		}

		// Send the request string.
		pSock->SetTimeout(SOCKET_TIMEOUT);
		pSock->Send((void *)tczRequest, sizeof(tczRequest));

		// Close the connection.
		CloseConnection(pSock);
	}

	// Send the request to all anonymous users.
	CStringArray csaIPs;
	if(cANet.GetAnonAquaintances(AWARENET_OSCAR_TOS, FALSE, csaIPs))
	{
		AppendComment(_T("SearchServer: Sending request to anonymous users"));

		for(i = 0; i < csaIPs.GetSize(); i++)
		{
			// Get the IP.
			CString cszIP = csaIPs.GetAt(i);

			// Debugging.
			AppendComment(_T("SearchServer: Anonymous acquaintance: " + cszIP));

			// Make sure the acquaintance is online.
			if(cszIP.CompareNoCase(_T("0.0.0.0")) == 0)
				continue;

			// Now open a connection to the friend's search server.
			CTimeoutSocket *pSock = NULL;
			// Just one shot; who cares?
			pSock = OpenConnection(cszIP, PORT_OSCARSEARCH); 
			
			if(!pSock)
				continue;

			// Send the request header.
			pSock->SetTimeout(SOCKET_TIMEOUT);
			if(pSock->Send((void *)&rHeader, sizeof(rHeader)) != sizeof(rHeader))
			{
				CloseConnection(pSock);
				continue;
			}

			// Send the request string.
			pSock->SetTimeout(SOCKET_TIMEOUT);
			pSock->Send((void *)tczRequest, sizeof(tczRequest));

			// Close the connection.
			CloseConnection(pSock);
		}
	}

	return TRUE;
}

BOOL CSearchServer::FromMe(RequestHeader rHeader)
{
	CAwareNet cANet;
	
	// Make sure AwareNet is initialized.
	COscarApp *pApp = (COscarApp *)AfxGetApp();
	ASSERT(pApp);
	if(!pApp->m_bANetInit)
		return FALSE;

	CString cszMyAddress;
	int nMyID = cANet.GetMyID();
	if(!cANet.GetIP(cszMyAddress, nMyID))
		return FALSE;

	CString cszFromAddress;
	if(!IP_ByteToExplicit((unsigned char *)rHeader.tczStart, cszFromAddress))
		return FALSE;

	if(cszFromAddress.CompareNoCase(cszMyAddress) == 0)
		return TRUE;

	return FALSE;
}

BOOL CSearchServer::AppendComment(LPCTSTR czComment)
{
	// Get the app.
	COscarApp *pApp = (COscarApp *)AfxGetApp();
	ASSERT(pApp);

	// Check to see if the user's Network Monitor is open.
	if(pApp->m_pNetworkMonitor)
	{
		// Insert the feedback item in the Network Monitor.
		pApp->m_pNetworkMonitor->AddComment(czComment);
	}

	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -