Ejemplo n.º 1
0
void SystemBuilder::BuildParts(){
	//_pLogger = CreateLogger();

	_pTcpServer = CreateTcpServer();
	_pTcpServer->setNetworkAddress(NetAddress(
			Singleton<ConfigFile,CreateThreadSafe>::instance()->getParameter<std::string>("SERVER_ADDRESS", none).c_str()));
	_pBoardActiveContext = CreateBoardControllerContext();
	_pTcpClient = CreateTcpClient();
	_pTcpClient->SetNetAddress(NetAddress(
			Singleton<ConfigFile,CreateThreadSafe>::instance()->getParameter<std::string>("CLIENT_ADDRESS", none).c_str()));
}
Ejemplo n.º 2
0
bool CP2PApp::Connect( const char* szServerIP, unsigned short usPort )
{
	if ( (m_pClient = CreateTcpClient( this )) == NULL )
		return false;

	if ( !m_pClient->connectServer( szServerIP, usPort ) )
	{
		m_pClient->releaseConnection();
		m_pClient = NULL;
		return false;
	}

	if ( !m_evConnect.Wait( 5 * 1000 ) )
	{
		m_pClient->releaseConnection();
		m_pClient = NULL;
		return false;
	}

	if ( !m_bConnected )
	{
		m_pClient->releaseConnection();
		m_pClient = NULL;
		return false;
	}

	if ( !StartThread() ) return false;

	// 发送登录指令
	PushRequestCmd( P2S_COMMAND::CCommandFactory::CreateConnectCmd( 
		"anonymouse", "123456", m_nLocalIP, m_sLocalUdpPort, m_sLocalTcpPort ) );

	// 等待认证成功
#ifdef _DEBUG
	if ( !m_evAuth.Wait( 2 * 1000 ) )
#else
	if ( !m_evAuth.Wait( 10 * 1000 ) )
#endif
	{
		m_bConnected = false;
		m_bAuthSucceed = false;
		m_pClient->releaseConnection();
		m_pClient = NULL;
		return false;
	}

	return true;
}
Ejemplo n.º 3
0
void CFileUpLoad::ThreadProcMain()
{
	USES_CONVERSION;
RESTART:
	if (m_uThreadType == 1)
	{
		while(m_bConnect == FALSE)
		{
			if (m_bStop)
				goto END;

			DWORD dwTimeLatest=0;
			DWORD dwTimeWait=0;
			dwTimeLatest = GetTickCount();
			dwTimeWait = dwTimeLatest - staticTimer;
			if (dwTimeWait > 3000)
			{
				staticTimer = dwTimeLatest;

				if (m_pITcpClient)
				{
					m_pITcpClient->ReleaseConnections();
					m_pITcpClient = NULL;
				}

				TRACE(_T("connect server(%s): %s:%d\n"), A2T(m_strSendExtType.c_str()), m_strAddr, m_usPort);
				m_pITcpClient = CreateTcpClient(*this, T2A(m_strAddr), m_usPort);
				if (m_pITcpClient == NULL)
				{
					m_nConnectFails++;

					//TRACE0("\nconect to File Server failed!\n");
					if (m_UpLoadAddrList.size() > 1 && m_nConnectFails >= 3)
					{
						bool bFindAddr = false;
						std::list<pstUpLoadAddr>::iterator itAddr = m_UpLoadAddrList.begin();
						for (; itAddr != m_UpLoadAddrList.end(); itAddr++)
						{
							if ((*itAddr)->strIP == T2A(m_strAddr) && m_usPort == (*itAddr)->nPort)
							{
								bFindAddr = true;
								pstUpLoadAddr pUpLoadAddr = *itAddr;
								m_UpLoadAddrList.erase(itAddr);
								m_UpLoadAddrList.push_back(pUpLoadAddr);	//将此地址移动到最后
								break;
							}
						}
						if (!bFindAddr)
						{
							pstUpLoadAddr pUpLoadAddr = new stUpLoadAddr;
							pUpLoadAddr->nPort = m_usPort;
							pUpLoadAddr->strIP = T2A(m_strAddr);
							m_UpLoadAddrList.push_back(pUpLoadAddr);	//将此地址移动到最后
						}
						//切换ip地址
						std::list<pstUpLoadAddr>::iterator itFirstAddr = m_UpLoadAddrList.begin();
						std::string strLog = Poco::format("切换文件连接地址%s:%d==>%s:%d(%s)", std::string(T2A(m_strAddr)), (int)m_usPort, (*itFirstAddr)->strIP, (*itFirstAddr)->nPort, m_strSendExtType);
						g_pLogger->information(strLog);
						TRACE("%s\n", strLog.c_str());

						m_strAddr = A2T((*itFirstAddr)->strIP.c_str());
						m_usPort = (*itFirstAddr)->nPort;
						m_nConnectFails = 0;
					}

					g_bFileConnect = false;
					Sleep(100);
					continue;
				}
				else
				{
					TRACE("\nconect to File Server Success!(%s)\n", m_strSendExtType.c_str());
					char szLog[300] = { 0 };
					sprintf_s(szLog, "连接文件服务器成功(%s)(%s:%d)[%s]", m_strSendExtType.c_str(), T2A(m_strAddr), m_usPort, g_strFileVersion.c_str());
					g_pLogger->information(szLog);
					m_bConnect = TRUE;
					m_uThreadType = 2;
					g_bFileConnect = true;
					m_nConnectFails = 0;
					goto RESTART;
				}
			}
			else
				Sleep(300);
		}
	}
	else if (m_uThreadType == 2)
	{
		while (m_bUpLoad)
		{
			if (!m_bConnect)
			{
				m_uThreadType = 1;
				g_bFileConnect = false;
				goto RESTART;
			}
			bool bFindTask = false;
			std::list<stUpLoadAns*>::iterator it = m_listFile.begin();
			for (; it != m_listFile.end(); it++)
			{
				if (m_bStop)
					goto END;

				stUpLoadAns* pTask = *it;

				if (pTask->bUpload == FALSE)
				{
					if (_taccess(pTask->strPath, 0) != 0)		//_access(T2A(pTask->strPath), 0) != 0
					{
						continue;
					}
					bFindTask = true;

					(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
					(reinterpret_cast<pSENDTASK>(pTask->pTask))->nSendState = 1;
					char	*szFileBuff = NULL;
					std::string strAnsName = T2A(pTask->strAnsName);
					DWORD Length = 0;
					try
					{
						CFile MyFileSend(pTask->strPath, CFile::modeRead);
						Length = MyFileSend.GetLength();
						
						szFileBuff = new char[Length];

						MyFileSend.Seek(0, CFile::begin);
						MyFileSend.Read(szFileBuff, Length);
						MyFileSend.Close();
					}
					catch (CMemoryException* e)
					{
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->nSendState = 3;
						std::stringstream ssTmpLog;
						ssTmpLog << "上传文件(" << strAnsName << ")[" << pTask->strPath << "]时内存申请失败,需要重新尝试。";
						g_pLogger->information(ssTmpLog.str());
						Sleep(500);
						continue;
					}
					catch (CFileException* e)
					{
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->nSendState = 3;
						CString strErr = _T("");
						e->GetErrorMessage((LPTSTR)(LPCTSTR)strErr, 1024);
						std::stringstream ssTmpLog;
						ssTmpLog << "上传文件(" << strAnsName << ")[" << pTask->strPath << "]时发生文件异常,需要重新尝试。" << strErr;
						g_pLogger->information(ssTmpLog.str());
						Sleep(500);
						continue;
					}
					catch (...)
					{
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->nSendState = 3;
						std::stringstream ssTmpLog;
						ssTmpLog << "上传文件(" << strAnsName << ")[" << pTask->strPath << "]时发生未知异常,需要重新尝试。";
						g_pLogger->information(ssTmpLog.str());
						Sleep(500);
						continue;
					}
					
					TRACE0("start send ans file\n");
					char szLog[300] = { 0 };
					sprintf_s(szLog, "开始上传文件(%s): %s", T2A(pTask->strAnsName), g_strFileVersion.c_str());
					g_pLogger->information(szLog);
					//上传文件
					ST_CMD_HEADER stHead;
					stHead.usCmd = REQUEST_UPLOADANS;
					stHead.uPackSize = sizeof(ST_FILE_INFO)+Length;
					ST_FILE_INFO stAnsInfo;
					strcpy(stAnsInfo.szFileName, T2A(pTask->strAnsName));

					char *pMd5 = MD5File(T2A(pTask->strPath));
					memcpy(stAnsInfo.szMD5, pMd5, LEN_MD5);
					stAnsInfo.dwFileLen = Length;
					memcpy(m_szSendBuf, &stHead, HEAD_SIZE);
					memcpy(m_szSendBuf + HEAD_SIZE, &stAnsInfo, sizeof(ST_FILE_INFO));
				RESENDHEAD:
					ResetEvent(m_hSendReadyEvent);
					ResetEvent(m_hSendDoneEvent);
					if (!m_bConnect)
					{
						delete szFileBuff;
						delete pMd5;
						m_uThreadType = 1;
						g_bFileConnect = false;
						break;
					}
					if (m_bStop)
					{
						sprintf_s(szLog, "发送文件(%s)时检测到系统退出标志,停止发送数据", T2A(pTask->strAnsName));
						g_pLogger->information(szLog);
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						delete szFileBuff;
						delete pMd5;
						break;
					}
					if (!sendData(m_szSendBuf, HEAD_SIZE + sizeof(ST_FILE_INFO), pTask))
					{
						std::string strLog = "sendData失败,需要重连";
						g_pLogger->information(strLog);

						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						delete szFileBuff;
						delete pMd5;

						m_bConnect = FALSE;
						break;
					}
//					DWORD dwResult1 = WaitForSingleObject(m_hSendReadyEvent, INFINITE);
					bool bRecvResult = false;	//接收服务器是否接收完成数据的结果命令
					DWORD dwResult1 = WaitForSingleObject(m_hSendReadyEvent, 5 * 1000);
					switch (dwResult1)
					{
						case WAIT_OBJECT_0:
							bRecvResult = true;
							break;
						case WAIT_TIMEOUT:
							sprintf_s(szLog, "文件头数据发送完成,接收服务器回复命令失败(%s)失败(5s超时), 需要重传", T2A(pTask->strAnsName));
							g_pLogger->information(szLog);
							break;
					}
					if (!bRecvResult)
					{
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						m_bConnect = FALSE;
						goto RESENDHEAD;
					}
					ResetEvent(m_hSendReadyEvent);
					if (m_bReadyOK == FALSE)
					{
						sprintf_s(szLog, "上传文件(%s)的反馈信息失败,重传", T2A(pTask->strAnsName));
						g_pLogger->information(szLog);

						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						m_bConnect = FALSE;
						goto RESENDHEAD;
					}
					if (!sendData(szFileBuff, Length, pTask))
					{
						std::string strLog = "sendData失败2,需要重连";
						g_pLogger->information(strLog);

						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						delete szFileBuff;
						delete pMd5;

						m_bConnect = FALSE;
						break;
					}
//					DWORD dwResult = WaitForSingleObject(m_hSendDoneEvent, INFINITE);
					bRecvResult = false;	//接收服务器是否接收完成数据的结果命令
					DWORD dwResult = WaitForSingleObject(m_hSendDoneEvent, g_nReUploadWaitTime * 1000);
					switch (dwResult)
					{
						case WAIT_OBJECT_0:
							bRecvResult = true;
							break;
						case WAIT_TIMEOUT:
							sprintf_s(szLog, "文件数据发送完成,接收服务器回复命令失败(%s)失败(%ds超时)", T2A(pTask->strAnsName), g_nReUploadWaitTime);
							g_pLogger->information(szLog);
							break;
					}
					if (!bRecvResult)
					{
						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						m_bConnect = FALSE;
						goto RESENDHEAD;
					}
					ResetEvent(m_hSendDoneEvent);
					if (m_bSendOK == FALSE)
					{
						sprintf_s(szLog, "上传文件(%s)失败,重传", T2A(pTask->strAnsName));
						g_pLogger->information(szLog);

						(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 0;
						m_bConnect = FALSE;
						goto RESENDHEAD;
					}
					pTask->bUpload = TRUE;

					(reinterpret_cast<pSENDTASK>(pTask->pTask))->fSendPercent = 100.0;
					(reinterpret_cast<pSENDTASK>(pTask->pTask))->nSendState = 2;

					delete szFileBuff;
					delete pMd5;
					TRACE0("end send ans file\n");
					if(m_pNotifyObj) m_pNotifyObj->SendFileComplete(stAnsInfo.szFileName, T2A(pTask->strPath));
					bFindTask = false;
				}
			}
			if (!bFindTask)	Sleep(1000);
		}
	}
END:
	return;
}