示例#1
0
void CTestTcpIpMultiServer::CTestConnection::runTest() {

	CFileLog::cfilelog() << "---" << std::endl;
	CFileLog::cfilelog() << "CTestTcpIpMultiServer::CTestConnection started" << std::endl;

	CTcpServerManager serverMgr;

	CTcpConnectionListener listener;
	listener.setListenerFunctions(
		CTestTcpIpMultiServer::DoReceive,
		CTestTcpIpMultiServer::DoConnect,
		CTestTcpIpMultiServer::DoDisconnect);

	serverMgr.createServer("127.0.0.1", 20500, listener);

	unique_lock<mutex> lock(s_mutex);

	CTcpClient* client = CTcpClient::clientFactory("127.0.0.1", 20500);

	boost::thread clientThr(&CTcpClient::startClient, client);

	if (s_connect.do_wait_for(lock, {10,0}) == true)
	{
		CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestConnection WRONG", true, client->is_connected());

		this_thread::sleep(posix_time::microseconds(500));
	}
	else
	{
		CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestConnection timed out", 0, 1);
	}

	client->stop();
	clientThr.join();
	delete client;

	client.Disconnect();
}
示例#2
0
void CTestTcpIpMultiServer::CTestSendReceive::runTest() {

	CFileLog::cfilelog() << "---" << std::endl;
	CFileLog::cfilelog() << "CTestTcpIpMultiServer::CTestSendReceive started " << std::endl;

	CTcpServerManager serverMgr;

	CTcpConnectionListener listener;
	listener.setListenerFunctions(
		CTestTcpIpMultiServer::DoReceive,
		CTestTcpIpMultiServer::DoConnect,
		CTestTcpIpMultiServer::DoDisconnect);

	CTcpServer* server = serverMgr.createServer("127.0.0.1", 20500, listener);

	unique_lock<mutex> lock(s_mutex);

	CTcpClient* client = CTcpClient::clientFactory("127.0.0.1", 20500);

	boost::thread clientThr(&CTcpClient::startClient, client);

	if (s_connect.do_wait_for(lock, {5,0}) == true)
	{
		CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestConnection WRONG", true, client->is_connected());

		this_thread::sleep(posix_time::microseconds(500));
	}
	else
	{
		CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestConnection timed out", 0, 1);
	}

	{
		std::vector<uint8_t> expectedData;

		for (auto v: s_testData)
			expectedData.push_back(v);

		unique_lock<mutex> lockSendRcv(s_mutex2);

		boost::thread sendthr(send_message, client, expectedData);

		if (s_read.do_wait_for(lockSendRcv, {10,0} ) == false)
		{
			CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestSendReceive wait data timed out", 0, 1);
		}

		sendthr.join();

		CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestSendReceive expected length != result length",
				expectedData.size(), CTestTcpIpMultiServer::s_resultData.size());

		uint32_t size = expectedData.size();
		for (uint32_t i = 0; i < size; ++i) {
			CPPUNIT_ASSERT_EQUAL_MESSAGE("CTestTcpIpMultiServer::CTestSendReceive receive data WRONG",
				expectedData[i], CTestTcpIpMultiServer::s_resultData[i]);
		}

		std::list<std::vector<uint8_t> > sendData;
		sendData.push_back(s_resultData);
		server->send(0, sendData);

	}

	client->stop();
	clientThr.join();
	delete client;
}