// ServerMgr.cpp : 实现文件 #include "stdafx.h" #include "ServerMgr.h" #include "LogMgr.h" #include "GlobalFunction.h" #define RCVFILEBUFLEN 1024 * 1024 //接收文件的缓冲区长度 CServerMgr * gServer = new CServerMgr; // CServerMgr vector CServerMgr::ServerVec; CServerMgr::CServerMgr() { } CServerMgr::~CServerMgr() { } bool CServerMgr::StartServer(int ListenPort) { if (INVALID_SOCKET != m_hSocket) Close(); if (!Create(ListenPort, SOCK_STREAM)) return false; if (!Listen()) return false; return true; } bool CServerMgr::StopServer() { int cnt = ServerVec.size(); CServerMgr * p = NULL; for (int i = 0;i != cnt;i++) { auto iter = ServerVec.begin(); p = *iter; ServerVec.erase(iter); p->Close(); delete p; p = NULL; } Close(); return false; } void CServerMgr::BroadCast(CString msg) { auto iter = ServerVec.begin(); auto iter_e = ServerVec.end(); for (;iter!=iter_e;iter++) { (*iter)->Send(msg, msg.GetLength()); } } void CServerMgr::OnAccept(int nErrorCode) { TRACE("CServerSock::OnAccept(int nErrorCode) \n"); if (0 == nErrorCode) { CServerMgr* pNewConn = new CServerMgr; Accept(*pNewConn); ServerVec.push_back(pNewConn); CString strip(""); UINT nPort = 0; pNewConn->GetPeerName(strip, nPort); CString strPortIP; strPortIP.Format("客户端%s:%d 已链接!", strip, nPort); if (gLogMgr->IsDebuging()) { //AfxMessageBox(strPortIP); } } CSocket::OnAccept(nErrorCode); } void CServerMgr::OnClose(int nErrorCode) { TRACE("CConnectSock::OnClose(int nErrorCode) \n"); CString strAddr(""); CString strip(""); UINT nPort = 0; GetPeerName(strip, nPort); strAddr.Format("客户端%s:%d已断开链接!", strip, nPort); gLogMgr->WriteDebugLog(strAddr); if (gLogMgr->IsDebuging()) { AfxMessageBox(strAddr); } DeleteClientObject(this); CSocket::OnClose(nErrorCode); } #include "FileMgr.h" #include "DxfReadMgr.h" #include "CommonFlowMgr.h" #include "ObjComponentMgr.h" void CServerMgr::OnReceive(int nErrorCode) { TRACE("CConnectSock::OnReceive(int nErrorCode) \n"); if (0 == nErrorCode) { char buf[512] = { 0 }; int nRcved = Receive(buf, sizeof(buf)); if (SOCKET_ERROR != nRcved) { CString RcvStr = (CString)buf; gLogMgr->WriteDebugLog("Receive: " + RcvStr); if (RcvStr.Find(ACTION_OPENFILE) != -1)//打开文件 { int sPos = RcvStr.ReverseFind('\\'); int ePos1 = RcvStr.ReverseFind('.'); int ePos = RcvStr.GetLength(); CString fileName = RcvStr.Mid(sPos + 1, ePos - sPos - 1); CString Name = RcvStr.Mid(sPos, ePos1 - sPos); ASSERT(!fileName.IsEmpty()); CFileMgr fg; CString dir = fg.GetWorkPath(); CString DestFolder = fg.GetWorkPath() + "\\Cutpar"; CString NewParFilePath = DestFolder + Name + ".par"; CString NewAreFilePath = DestFolder + Name + ".are"; CString PIDeFolder = fg.GetWorkPath() + "\\WorkPar"; CString NewPIFilePath = PIDeFolder + Name + ".txt"; #ifndef DEBUG_lOCAL CString NetDir = gProgram_SZ_XL->m_NetPath + ":"; //共享盘 #else CString NetDir = "D:\\紫外切割机\\同兴达双头 - 推治具\\LaiPuLaser\\Debug\\TempMarkData";//共享盘 #endif CString NetparfilePath = NetDir + Name + ".par"; CString NetarefilePath = NetDir + Name + ".are"; CString NetPIfilePath = NetDir + Name + ".txt"; CopyFile(NetparfilePath, NewParFilePath,false); CopyFile(NetarefilePath, NewAreFilePath, false); CopyFile(NetPIfilePath, NewPIFilePath, false); //fileName.MakeLower(); if (fileName.Find("dxf") != -1) { gServer->m_RcvFileName = NetDir +"\\"+ fileName; gDlgSW_XL_Flow->SendMessage(OPEN_FILE, 0, 1); } if (fileName.Find("mak") != -1) { gServer->m_RcvFileName = NetDir +"\\" +fileName; gDlgSW_XL_Flow->SendMessage(OPEN_FILE, 0, 0); } } if (RcvStr.Find(ACTION_TANSPRODUCTINFO) != -1) //传数据. { /* CString str = "EXECMD_OK;"; Send(str, str.GetLength()); Receive(&gServer->m_RcvProduct, sizeof(CProduct));*/ gLogMgr->WriteDebugLog(ACTION_TANSPRODUCTINFO + "OK"); if (RcvStr.Find(TRACK_1) != -1) { gServer->m_RcvCurTrackIndex = 0; } if (RcvStr.Find(TRACK_2) != -1) { gServer->m_RcvCurTrackIndex = 1; } int spos = 0; int epos = 0; CString temp; vector dvec; for (int i = 0;i != 6;i++) { if ( (spos = RcvStr.Find("[",spos+1))<1|| (epos = RcvStr.Find("]",epos+1))<1) { AfxMessageBox("接收数据错误!"); return; } temp = RcvStr.Mid(spos+1, epos - spos-1); dvec.push_back(atof(temp)); } gServer->m_RcvProduct.m_p00 = dvec[0]; gServer->m_RcvProduct.m_p01 = dvec[1]; gServer->m_RcvProduct.m_p02 = dvec[2]; gServer->m_RcvProduct.m_p10 = dvec[3]; gServer->m_RcvProduct.m_p11 = dvec[4]; gServer->m_RcvProduct.m_p12 = dvec[5]; //gTrackWorkFlow1.SetCurTrackWorkStep(_ETrack_Step_Catch_mark_End); gTrackWorkFlow1.MarkProcessExt(); if (!m_bLogicErr ) { CString str = "EXECMD_OK;"; Send(str, str.GetLength()); gLogMgr->WriteDebugLog("Send: " + str); } return CSocket::OnReceive(nErrorCode); } if (RcvStr.Find(ACTION_COLLECT) != -1) //收集数据. { gServer->m_bLogicErr = true; gMarkAreaMgr->CollectOrgWorkData(gProgram_SZ_XL->IsbSelMarkMode(), gServer->m_RcvProduct); gLogMgr->WriteDebugLog(ACTION_COLLECT + "OK"); } if (RcvStr.Find(ACTION_STARTWORK) != -1) //启动加工 { if (m_bLogicErr) return CSocket::OnReceive(nErrorCode); int sPos = RcvStr.ReverseFind(';'); int ePos = RcvStr.GetLength(); CString Index = RcvStr.Mid(sPos + 1, ePos - sPos - 1); m_RcvAreaIndex = atoi(Index); /* StartMarkArea(); }*/ gCommonFlowMgr->MarkAreaByIdx(m_RcvAreaIndex);//根据加工区域索引加工 gLogMgr->WriteDebugLog("MarkArea " + Index + " Finished!"); CString str = "AREA_FINISHED;"; Send(str, str.GetLength()); gLogMgr->WriteDebugLog("Send: " + str); Sleep(50); } if (RcvStr.Find(ACTION_PAUSEWORK) != -1) //暂停加工 { PauseMark(); } if (RcvStr.Find(ACTION_RESUMEWORK) != -1) //恢复加工 { ResumeMark(); } if (RcvStr.Find(ACTION_EXITWORK) != -1) //退出加工 { ExitMark(); } if (RcvStr.Find(ACTION_SETSPECAILOBJ) != -1) //设置特殊对象 { CString str = "EXECMD_OK;"; Send(str, str.GetLength()); int sPos = RcvStr.ReverseFind(';'); int ePos = RcvStr.GetLength(); CString Index = RcvStr.Mid(sPos + 1, ePos - sPos - 1); int arryCnt = atoi(Index); gLogMgr->WriteDebugLog(to_string(arryCnt).c_str()); int *RcvObjIndexArry = new int[arryCnt](); Sleep(100); int rcvcnt=Receive(RcvObjIndexArry, arryCnt*(sizeof(int))); if (rcvcnt!= arryCnt*(sizeof(int))) { gLogMgr->WriteDebugLog("接收已选中Obj索引数组错误."); } int idx; auto & ComponentVec = gObjComponentMgr->GetComponentVec(); int ComponentVecSize = ComponentVec.size(); for (int i = 0; i < arryCnt;i++) { idx = RcvObjIndexArry[i]; if (idx>= (ComponentVecSize)||idx<0) { CString str; str.Format("第%d个数据错误,值为%d。", i, idx); gLogMgr->WriteDebugLog(str); continue; } ComponentVec[idx].SetSelected(true); } gObjComponentMgr->SetSelObjPenNum(); gLogMgr->WriteDebugLog(ACTION_SETSPECAILOBJ + "OK"); delete[]RcvObjIndexArry; RcvObjIndexArry = NULL; } if (RcvStr.Find(ACTION_BMARKSELOBJMODE) != -1) //设置选择加工模式 { if (RcvStr.Find("0") != -1) { gProgram_SZ_XL->SetbSelMarkMode(false); } if (RcvStr.Find("1") != -1) { gProgram_SZ_XL->SetbSelMarkMode(true); } /*int sPos = RcvStr.ReverseFind(';'); int ePos = RcvStr.GetLength(); CString Index = RcvStr.Mid(sPos + 1, ePos - sPos - 1); int arryCnt = atoi(Index); gLogMgr->WriteDebugLog(to_string(arryCnt).c_str()); int *RcvObjIndexArry = new int[arryCnt](); Sleep(100); int rcvcnt = Receive(RcvObjIndexArry, arryCnt*(sizeof(int))); if (rcvcnt != arryCnt*(sizeof(int))) { gLogMgr->WriteDebugLog("接收已选中Obj索引数组错误。"); } int idx; auto & ComponentVec = gObjComponentMgr->GetComponentVec(); int ComponentVecSize = ComponentVec.size(); for (int i = 0; i < arryCnt; i++) { idx = RcvObjIndexArry[i]; if (idx >= (ComponentVecSize) || idx < 0) { CString str; str.Format("第%d个数据错误,值为%d。", i, idx); gLogMgr->WriteDebugLog(str); continue; } ComponentVec[idx].SetSelected(true); } gObjComponentMgr->SetSelObjPenNum(); gLogMgr->WriteDebugLog(ACTION_SETSPECAILOBJ + "OK"); delete[]RcvObjIndexArry; RcvObjIndexArry = NULL;*/ } if (RcvStr.Find(ACTION_SELECTOBJ) != -1) //选择主机已选中的OBJ { CString str = "EXECMD_OK;"; Send(str, str.GetLength()); gLogMgr->WriteDebugLog("ACTION_SELECTOBJ START!"); int sPos = RcvStr.ReverseFind(';'); int ePos = RcvStr.GetLength(); CString Index = RcvStr.Mid(sPos + 1, ePos - sPos - 1); int arryCnt = atoi(Index); //客户端告知的选中数量 gLogMgr->WriteDebugLog(to_string(arryCnt).c_str()); gObjComponentMgr->NotSelAllObj();//先全部取消选择 if (arryCnt==0) //没有数据,不用接收 { GetCurViewPtr()->RefreshView(); return; } //接收数据 int *RcvObjIndexArry = new int[arryCnt](); //需要选择的Obj的索引数组 Sleep(100); int rcvcnt = Receive(RcvObjIndexArry, arryCnt*(sizeof(int))); if (rcvcnt != arryCnt*(sizeof(int))) { gLogMgr->WriteDebugLog("接收已选中Obj索引数组 字节数错误。"); } //数据应用到本机 int idx; auto & ComponentVec = gObjComponentMgr->GetComponentVec(); int ComponentVecSize = ComponentVec.size(); for (int i = 0; i < arryCnt; i++) { idx = RcvObjIndexArry[i]; if (idx >= (ComponentVecSize) || idx < 0) { CString str; str.Format("第%d个数据错误,值为%d。", i, idx); gLogMgr->WriteDebugLog(str); continue; } ComponentVec[idx].SetSelected(true); } gLogMgr->WriteDebugLog(ACTION_SELECTOBJ + "OK"); GetCurViewPtr()->RefreshView(); delete[]RcvObjIndexArry; RcvObjIndexArry = NULL; } } } CString str = "EXECMD_OK;"; Send(str, str.GetLength()); gLogMgr->WriteDebugLog("Send: " + str); CSocket::OnReceive(nErrorCode); } void CServerMgr::DeleteClientObject(CServerMgr * pConn) { int cnt = ServerVec.size(); for (int i = 0;i != cnt;i++) { if (ServerVec[i] == pConn) { ServerVec.erase(ServerVec.begin() + i); //pConn->Close(); delete pConn; pConn = NULL; break; } } } void CServerMgr::StartMarkArea() { if (m_workThread.joinable()) { return; } m_workThread=thread(&CCommonFlowMgr::MarkAreaByIdx, gCommonFlowMgr,m_RcvAreaIndex);//根据加工区域索引加工) //gCommonFlowMgr->gCommonFlowMgr->MarkAreaByIdx(m_RcvAreaIndex);//根据加工区域索引加工(m_RcvAreaIndex);//根据加工区域索引加工 m_workThread.join(); gLogMgr->WriteDebugLog("MarkArea " +(CString) (to_string(m_RcvAreaIndex).c_str()) + " Finished!"); CString str = "AREA_FINISHED;"; Send(str, str.GetLength()); gLogMgr->WriteDebugLog("Send: " + str); Sleep(50); } void CServerMgr::PauseMark() { SuspendThread(m_workThread.native_handle()); } void CServerMgr::ResumeMark() { ResumeThread(m_workThread.native_handle()); } void CServerMgr::ExitMark() { TerminateThread(m_workThread.native_handle(),0); }