// CientMgr.cpp : 实现文件 // #include "stdafx.h" #include "ClientMgr.h" #include "Program_SZ_XL.h" #include "CameraHawkvis.h" #include "FileMgr.h" #include "LogMgr.h" #include "ObjComponentMgr.h" // CCientMgr CClientMgr * gClientMgr = new CClientMgr; CClientMgr::CClientMgr() { } CClientMgr::~CClientMgr() { DisConnectServer(); } bool CClientMgr::Connect2Server() { if (m_bDisableServer)//屏蔽副机,则不连接不发指令 { return false; } m_ClientIP = gProgram_SZ_XL->m_ServerIP; m_ClientPort = gProgram_SZ_XL->m_ServerPort; if (m_hSocket == INVALID_SOCKET) { if (!Socket()) { AfxMessageBox("Socket初始化失败。"); return false; } } if (!m_bOnline) { if (!Connect(m_ClientIP, m_ClientPort)) { return false; } m_bOnline = true; } return true; } void CClientMgr::DisConnectServer() { if (m_hSocket!=INVALID_SOCKET) { Close(); m_bOnline = false; } } void CClientMgr::SendCmd_TransFile(CString filePath) { int sPos = filePath.ReverseFind('\\'); int ePos1 = filePath.ReverseFind('.'); int ePos2 = filePath.GetLength(); CString fileName = filePath.Mid(sPos, ePos1 - sPos); CString fileType = filePath.Mid(ePos1, ePos2 - ePos1); //.DXF OR .MAK CFileMgr fg; CString parFileDir = fg.GetWorkPath() + "\\Cutpar"; CString parFile = parFileDir + fileName + ".par"; CString areFile = parFileDir + fileName + ".are"; CString PIFileDir = fg.GetWorkPath() + "\\WorkPar"; CString PIFile = PIFileDir + fileName + ".txt"; CString DestFolder = fg.GetWorkPath() + "\\TempMarkData"; if (!PathIsDirectory(DestFolder))//不存在则创建文件夹 { CreateDirectory(DestFolder, 0); } CString NewFilePath = DestFolder + fileName + fileType; CString NewParFilePath = DestFolder + fileName + ".par"; CString NewAreFilePath = DestFolder + fileName + ".are"; CString NewPIFilePath = DestFolder + fileName + ".txt"; CopyFile(filePath, NewFilePath, false); CopyFile(parFile, NewParFilePath, false); CopyFile(areFile, NewAreFilePath, false); CopyFile(PIFile, NewPIFilePath, false); } void CClientMgr::SendCmd_OpenFile(CString filePath) { SendCmd_TransFile(filePath); Sleep(100); if (Connect2Server()) { CString str = "OPEN_FILE;" + filePath; Send(str, str.GetLength()); } } void CClientMgr::SendCmd_CollectData() { if (Connect2Server()) { bool b = gProgram_SZ_XL->IsbSelMarkMode(); gClientMgr->SendCmd_bMarkSelMode(b); if (b) { gClientMgr->SendCmd_SelObj(); Sleep(200); } CString str = ";COLLECT_DATA;"; Send(str, str.GetLength()); } } void CClientMgr::SendCmd_ProductInfo(CProduct product) { if (Connect2Server()) { m_bServerReady = false; CString str = ACTION_TANSPRODUCTINFO; if (gCameraHawkvis->GetCamIndex() == 0) { str += TRACK_1; } if (gCameraHawkvis->GetCamIndex() == 1) { str += TRACK_2; } CString temp; temp.Format("[%.6f][%.6f][%.6f][%.6f][%.6f][%.6f]", product.m_p00, product.m_p01, product.m_p02, \ product.m_p10, product.m_p11, product.m_p12); str += temp; Send(str, str.GetLength()); //m_bServerMarkReady = false; gLogMgr->WriteDebugLog("Send: "+str); /*Send(str, str.GetLength()); Sleep(50); Send(&product, sizeof(product));*/ } } void CClientMgr::SendCmd_SetSpecialObj() { if (Connect2Server()) { int SelObjCnt = m_SelObjIndexVec.size(); if (SelObjCnt > 0) { CString str; str.Format("%s%d", ACTION_SETSPECAILOBJ, SelObjCnt); gLogMgr->WriteDebugLog(str); Send(str, str.GetLength()); Sleep(50); int* IdxArry = new int[SelObjCnt](); for (int i = 0; i < SelObjCnt; i++) { IdxArry[i] = m_SelObjIndexVec[i]; } Send(IdxArry, SelObjCnt*(sizeof(int))); delete[]IdxArry; IdxArry = NULL; } } } void CClientMgr::SendCmd_SelObj() { if (Connect2Server()) { auto & ComponentVec = gObjComponentMgr->GetComponentVec(); auto & SelIndexVec = gClientMgr->m_SelObjIndexVec; SelIndexVec.clear(); int cnt = ComponentVec.size(); if (cnt == 0)//没有打开文件,直接返回 return; for (int idx = 0; idx < cnt; idx++) { if ((ComponentVec[idx]).IsSelected()) { SelIndexVec.push_back(idx); } } int SelObjCnt = m_SelObjIndexVec.size(); CString str; str.Format("%s%d", ACTION_SELECTOBJ, SelObjCnt); gLogMgr->WriteDebugLog(str); Send(str, str.GetLength()); if (SelObjCnt == 0)//无选中数据,不需再发数组 return; int* IdxArry = new int[SelObjCnt](); for (int i = 0; i < SelObjCnt; i++) { IdxArry[i] = m_SelObjIndexVec[i]; } Send(IdxArry, SelObjCnt*(sizeof(int))); delete[]IdxArry; IdxArry = NULL; } } void CClientMgr::SendCmd_bMarkSelMode(int bMarkSel) { if (Connect2Server()) { CString str; str.Format("%s%d", ACTION_BMARKSELOBJMODE, bMarkSel); Send(str, str.GetLength()); } } void CClientMgr::SendCmd_StartLaserMark(int AreaIndex) { if (Connect2Server()) { int cnt = 0; while (!m_bServerReady&&cnt<100) { Sleep(200); cnt++; } if (cnt==15) { AfxMessageBox("副机准备数据超时!"); return; } CString str; str.Format("%s%d", ACTION_STARTWORK, AreaIndex); Send(str, str.GetLength()); m_bAreaMarkDone = false; } } // CCientMgr 成员函数 int CClientMgr::Send(const void * lpBuf, int nBufLen, int nFlags) { if (!CheckServerDoneCmd()) { return -1; } int sendcnt = CSocket::Send(lpBuf, nBufLen, nFlags); m_bLastCmdFinished = false; return sendcnt; } void CClientMgr::OnReceive(int nErrorCode) { // TODO: 在此添加专用代码和/或调用基类 if (0 == nErrorCode) { char buf[512] = { 0 }; int nRcved = Receive(buf, sizeof(buf)); if (SOCKET_ERROR != nRcved) { CString RcvStr = (CString)buf; if (RcvStr.Find("OK") != -1) { m_bLastCmdFinished = true; } if (RcvStr.Find("AREA_FINISHED") != -1) { m_bAreaMarkDone = true; } if (RcvStr.Find("READY") != -1) { m_bServerReady = true; } } } CSocket::OnReceive(nErrorCode); } void CClientMgr::OnClose(int nErrorCode) { // TODO: 在此添加专用代码和/或调用基类 Close(); m_bOnline = false; m_bLastCmdFinished = true; m_bAreaMarkDone = true; AfxMessageBox("与副机的连接已断开!"); CSocket::OnClose(nErrorCode); } void CClientMgr::OnConnect(int nErrorCode) { // TODO: 在此添加专用代码和/或调用基类 if (nErrorCode == 0) { m_bOnline = true; } CSocket::OnConnect(nErrorCode); } BOOL CClientMgr::Connect(LPCTSTR lpszHostAddress, UINT nHostPort) { SetTimeOut(); auto ret = CSocket::Connect(lpszHostAddress, nHostPort); KillTimeOut(); return ret; } BOOL CClientMgr::SetTimeOut(UINT uTimeOut) { LARGE_INTEGER llCnt; ::QueryPerformanceCounter(&llCnt); m_llDtStart = llCnt.QuadPart; m_uTimeOut = uTimeOut; return TRUE; } BOOL CClientMgr::KillTimeOut() { m_llDtStart = 0;//表明取消计时 return TRUE; } BOOL CClientMgr::OnMessagePending() { if (m_llDtStart) { LARGE_INTEGER lldtEnd; ::QueryPerformanceCounter(&lldtEnd); LARGE_INTEGER llFrq; ::QueryPerformanceFrequency(&llFrq); double dbDealy = (double)(lldtEnd.QuadPart - m_llDtStart) * 1000 / llFrq.QuadPart; if (dbDealy > m_uTimeOut) { CancelBlockingCall();//退出阻塞的函数 AfxMessageBox("网络超时!"); return FALSE; // No need for idle time processing. } } return CSocket::OnMessagePending(); } bool CClientMgr::CheckServerDoneCmd() { int waitCnt = 0; while ((!m_bLastCmdFinished)||(!m_bAreaMarkDone) && waitCnt < 12000) { DoEvents(); Sleep(10); waitCnt++; } if (waitCnt == 12000) { return false; } return true; } void CClientMgr::DoEvents() { MSG msg; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //非阻塞查看消息队列是否有消息过来,有消息返回非零进入循环 //无消息返回0则跳出 { TranslateMessage(&msg); //有消息时,虚拟键消息转换为字符消息 DispatchMessage(&msg);//发送消息给窗口程序 } }