Exemple #1
0
void testConnect()
{
	CSmartPtr<CPackageBase> pkg = new TestPackage();

	TestPackage* p = (TestPackage*)pkg.Get();
	p->id = 1;

	for (int i = 0; i < 10; i++)
	{
		CTCPIOCP* lpClient = new CTCPIOCP;

		if(lpClient->Connect("127.0.0.1", 3000))
		{
			printf("连接服务器成功!\r\n");
		}

		for (int i = 0; i < 1; i++)
		{
			//TestPackage* pkg = new TestPackage;
			//p->id = i + 1;
			//printf("发送数据包!\r\n");
			lpClient->SendTCP(pkg);
			lpClient->SendTCP(pkg);
		}

		//client.SendTCP2();

		//client.StopTCP();
	}
}
Exemple #2
0
void CTCPIOCP::_SendAsync( LPACCEPT_DATA lpAcceptData )
{
	//printf("启动异步发送数据。\r\n");

	EnterCriticalSection(&(lpAcceptData->SendLok));

	if(lpAcceptData->sendBytesTransferred == lpAcceptData->sendBytesTotal)
	{//单次整个数据缓冲区发送完成。

		//TODO: 检查数据包队列里是否还有数据包未发送。

		lpAcceptData->sendBytesTotal = 0;
		lpAcceptData->sendBytesTransferred = 0;

		EnterCriticalSection(&(lpAcceptData->SendPKGListLok));

		if (lpAcceptData->SendPKGList.size() != 0)
		{//TODO: 数据包队列里有数据包未发送。
			size_t sTemp = 0;

			while(lpAcceptData->sendBytesTotal != BUFF_SIZE && lpAcceptData->SendPKGList.size() != 0)
			{
				CSmartPtr<CPackageBase> lpPKG = lpAcceptData->SendPKGList.front();

				const char* pBuffer = (const char*)lpPKG.Get();
				short bufferSize = *(short*)pBuffer;

				sTemp = lpAcceptData->SenderCryptor->Encrypt(pBuffer, lpAcceptData->SendDataLeft, bufferSize, lpAcceptData->Sender->Buffer, lpAcceptData->sendBytesTotal, BUFF_SIZE);

				if(lpAcceptData->SendDataLeft == 0)
				{
					//int os = (char*)((char*)&(lpPKG->OwnerID)) - (char*)((char*)&(lpPKG->PSize));
					lpAcceptData->SenderCryptor->Encrypt((const char*)(&(lpAcceptData->OwnerID)), 0, sizeof(lpAcceptData->OwnerID), lpAcceptData->Sender->Buffer, lpAcceptData->sendBytesTotal + sizeof(lpPKG->PSize), BUFF_SIZE);
				}

				lpAcceptData->SendDataLeft += sTemp;
				lpAcceptData->sendBytesTotal += sTemp;

				if (lpAcceptData->SendDataLeft == bufferSize)
				{
					lpAcceptData->SendDataLeft = 0;
					lpAcceptData->SendPKGList.pop();
					lpAcceptData->sendPKGCount++;

					lpAcceptData->SenderCryptor->UpdateCryptKey();
				}
			}

			printf("发送成功 %d 个\r\n", lpAcceptData->sendPKGCount);
		}
		else
		{
			//TODO: 数据包队列为空。

			lpAcceptData->Sending = false;
			/*lpAcceptData->SendDataLeft = 0;
			lpAcceptData->sendBytesTotal = 0;
			lpAcceptData->sendBytesTransferred = 0;
			lpAcceptData->Sender->WSABuf.len = BUFF_SIZE;
			lpAcceptData->Sender->WSABuf.buf = lpAcceptData->Sender->Buffer;*/
			LeaveCriticalSection(&(lpAcceptData->SendPKGListLok));
			LeaveCriticalSection(&(lpAcceptData->SendLok));
			return;
		}

		LeaveCriticalSection(&(lpAcceptData->SendPKGListLok));
	}

	ZeroMemory(&(lpAcceptData->Sender->WSAOverLapped), sizeof(WSAOVERLAPPED));

	lpAcceptData->Sender->WSABuf.len = lpAcceptData->sendBytesTotal - lpAcceptData->sendBytesTransferred;
	lpAcceptData->Sender->WSABuf.buf = (char*)(lpAcceptData->Sender->Buffer) + lpAcceptData->sendBytesTransferred;

	DWORD dwFlags = 0;
	DWORD bytesTransferred;
	int iErrorID = WSASend(lpAcceptData->Socket, &(lpAcceptData->Sender->WSABuf), 1, &bytesTransferred, dwFlags, &(lpAcceptData->Sender->WSAOverLapped), NULL);

	if (iErrorID == SOCKET_ERROR)
	{
		//TODO: 启动异步发送失败。WSA_IO_PENDING
		iErrorID = WSAGetLastError();
	}
	else
	{
		//TODO: 启动异步发送成功。
		iErrorID = 0;
	}

	LeaveCriticalSection(&(lpAcceptData->SendLok));
}