Ejemplo n.º 1
0
void CClientDlg::OnBnClickedStart()
{
	CString strAddress;
	CString strPort;
	CString strTestTimes;
	CString strTestInterv;
	CString strThreadCount;
	CString strThreadInterv;
	CString strDataLen;

	m_Address.GetWindowText(strAddress);
	m_Port.GetWindowText(strPort);
	m_TestTimes.GetWindowText(strTestTimes);
	m_TestInterv.GetWindowText(strTestInterv);
	m_ThreadCount.GetWindowText(strThreadCount);
	m_ThreadInterv.GetWindowText(strThreadInterv);
	m_DataLen.GetWindowText(strDataLen);

	m_strAddress	= strAddress.Trim();
	m_usPort		= (USHORT)_ttoi(strPort);
	m_iTestTimes	= _ttoi(strTestTimes);
	m_iTestInterv	= _ttoi(strTestInterv);
	m_iThreadCount	= _ttoi(strThreadCount);
	m_iThreadInterv	= _ttoi(strThreadInterv);
	m_iDataLen		= _ttoi(strDataLen);

	if(!CheckParams())
		return;

	SetAppState(ST_STARTING);

	m_dwBeginTickCount	= 0;
	m_dwEndTickCount	= 0;
	m_dwTimeconsuming	= 0;
	m_llTotalReceived	= 0;
	m_llTotalSent		= 0;
	m_llExpectReceived	= (LONGLONG)m_iTestTimes * (LONGLONG)m_iThreadCount * (LONGLONG)m_iDataLen;

	m_vtClients.Clear();
	m_sendBuffer.Malloc(m_iDataLen, true);

	for(int i = 0; i < m_iThreadCount; i++)
	{
		smart_simple_ptr<CClientSocket> pSocket = new CClientSocket(this);

		if(pSocket->Start(m_strAddress, m_usPort))
			m_vtClients->push_back(pSocket.release());
		else
		{
			::LogClientStartFail(pSocket->GetLastError(), pSocket->GetLastErrorDesc());
			m_vtClients.Clear();
			m_sendBuffer.Free();
			SetAppState(ST_STOPED);
			return;
		}
	}

	::LogClientStart(m_strAddress, m_usPort);

	DWORD dwSendDelay = 3;
	CString strMsg;

	strMsg.Format(_T(" *** willing to send data after %d seconds ..."), dwSendDelay);
	::LogMsg(strMsg);
	::WaitWithMessageLoop(dwSendDelay * 1000);

	SetAppState(ST_STARTED);

	m_dwBeginTickCount = ::GetTickCount();

	BOOL bTerminated = FALSE;
	for(int i = 0; !bTerminated && i < m_iTestTimes; i++)
	{
		for(int j = 0; !bTerminated && j < m_iThreadCount; j++)
		{
			CClientSocket* pSocket = m_vtClients[j];
			if(!pSocket->Send(pSocket->GetConnectionID(), m_sendBuffer, (int)m_sendBuffer.Size()))
			{
				::LogClientSendFail(i + 1, j + 1, pSocket->GetLastError(), pSocket->GetLastErrorDesc());
				bTerminated = TRUE;
				break;
			}

			if(m_iThreadInterv > 0 && j + 1 < m_iThreadCount)
				::WaitWithMessageLoop(m_iThreadInterv);
		}

		if(m_iTestInterv > 0 && i + 1 < m_iTestTimes)
			::WaitWithMessageLoop(m_iTestInterv);
	}
}