📄 commpool.cpp
字号:
// CommPool.cpp: implementation of the CCommPool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MDF.h"
#include "CommPool.h"
#include "ClientSock.h"
#include "System.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CMDFApp theApp ;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//##ModelId=44B6F8870177
CCommPool::CCommPool()
{
}
//##ModelId=44B6F8870178
CCommPool::~CCommPool()
{
}
//##ModelId=44B6F8870128
void CCommPool::Open()
{
//初始化串口
InitSCom() ;
InitSock() ;
m_TCP.InitTCP(m_unTCPPort) ;
//初始化Web通讯
if( theApp.m_pComm->m_bIsWebOpen )
m_Web.InitWeb(m_unWebPort,m_strWebIp) ;
if( theApp.m_pComm->m_bIsMsgOpen || theApp.m_pComm->m_bIsVoiceOpen )
{
if( m_GSM.InitGSM(m_unGSMPort) != 0 )
m_GSM.CloseGsm() ;
}
}
//##ModelId=44B6F8870129
void CCommPool::Close()
{
m_TCP.CloseTCP() ;
m_Web.CloseWeb() ;
CloseAllSCom() ;
CloseAllSock() ;
m_GSM.CloseGsm() ;
theApp.m_pMedia->CloseVoice() ;
}
//初始化socket
//##ModelId=44B6F887011D
void CCommPool::InitSock()
{
int i,nCount ;
sCommEQ *pEQ ;
CString strIP ;
sSocket sSock ;
nCount = theApp.m_pComm->m_pEQPool->m_EQArray.GetSize() ;
for( i = 0 ; i < nCount ; i++ )
{
pEQ = &(theApp.m_pComm->m_pEQPool->m_EQArray[i]) ;
if( pEQ->nModel == COMM_TYPE_TCP )
{
strIP = theApp.m_pComm->m_pEQPool->m_EQArray[i].strCommAddr ;
if( strIP.IsEmpty() )
continue ;
if( m_ListSock.IsEmpty() )
{
sSock.pSock = NULL ;
sSock.strIP = strIP ;
sSock.bIsConnect = FALSE ;
m_ListSock.AddTail(sSock) ;
}
else
{
if( !FindSock(strIP) )
{
sSock.pSock = NULL ;
sSock.strIP = strIP ;
sSock.bIsConnect = FALSE ;
m_ListSock.AddTail(sSock) ;
}
}
}
}
}
//初始化通讯串口
//##ModelId=44B6F887011C
void CCommPool::InitSCom()
{
int i,nCount ;
sCommEQ *pEQ ;
CString strPort ;
CSerialComm *pSCom ;
nCount = theApp.m_pComm->m_pEQPool->GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pEQ = &(theApp.m_pComm->m_pEQPool->m_EQArray[i]) ;
if( pEQ == NULL )
continue ;
if( pEQ->nModel == COMM_TYPE_SC )
{
strPort = theApp.m_pComm->m_pEQPool->m_EQArray[i].strCommAddr ;
if( strPort.IsEmpty() || ( m_GSM.GetPort() == strPort ) )
continue ;
if(m_ListSCom.IsEmpty())
{
pSCom = new CSerialComm () ;
if( pSCom->InitCom(strPort) )
m_ListSCom.AddTail((LPVOID)pSCom) ;
else
delete pSCom ;
}
else
{
if( !FindSCom(strPort) )
{
pSCom = new CSerialComm () ;
if( pSCom->InitCom(strPort) )
m_ListSCom.AddTail((LPVOID)pSCom) ;
else
delete pSCom ;
}
}
}
}
}
//##ModelId=44B6F887011B
void CCommPool::CloseAllSCom()
{
int i,nCount ;
POSITION pos ;
CSerialComm *pSCom ;
if( m_ListSCom.IsEmpty())
return ;
nCount = m_ListSCom.GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSCom.FindIndex(i) ;
pSCom = (CSerialComm*)m_ListSCom.GetAt(pos) ;
if( pSCom != NULL )
pSCom->CloseCom() ;
}
m_ListSCom.RemoveAll() ;
}
//##ModelId=44B6F887011A
void CCommPool::CloseAllSock()
{
int i,nCount ;
POSITION pos ;
CClientSock* pSock ;
sSocket sSock ;
nCount = m_ListSock.GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSock.FindIndex(i) ;
sSock = m_ListSock.GetAt(pos) ;
pSock = (CClientSock*)(sSock.pSock) ;
if( pSock != NULL && pSock->IsConnect)
pSock->CloseClient() ;
}
Sleep(200) ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSock.FindIndex(i) ;
sSock = m_ListSock.GetAt(pos) ;
pSock = (CClientSock*)(sSock.pSock) ;
if( pSock != NULL )
delete pSock ;
}
m_ListSock.RemoveAll() ;
}
//查找串口池中与strPort名称相同的串口,成功返回TRUE
//##ModelId=44B6F887010E
BOOL CCommPool::FindSCom(CString strPort)
{
int i,nCount ;
POSITION pos ;
CSerialComm *pSCom ;
if( m_ListSCom.IsEmpty())
return FALSE ;
nCount = m_ListSCom.GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSCom.FindIndex(i) ;
pSCom = (CSerialComm*)m_ListSCom.GetAt(pos) ;
if ( strPort == pSCom->GetPort() )
return TRUE ;
}
return FALSE ;
}
//##ModelId=44B6F887010C
BOOL CCommPool::FindSock(CString strIP)
{
int i,nCount ;
POSITION pos ;
sSocket sSock ;
nCount = m_ListSock.GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSock.FindIndex(i) ;
sSock = m_ListSock.GetAt(pos) ;
if ( strIP == sSock.strIP )
return TRUE ;
}
return FALSE ;
}
//##ModelId=44B6F887010A
LPVOID CCommPool::GetSocket(CString strIP)
{
int i,nCount ;
POSITION pos ;
sSocket sSock ;
nCount = m_ListSock.GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSock.FindIndex(i) ;
sSock = m_ListSock.GetAt(pos) ;
if ( strIP == sSock.strIP )
return (LPVOID)&(m_ListSock.GetAt(pos)) ;
}
return NULL ;
}
//##ModelId=44B6F88700FB
LPVOID CCommPool::GetSCom(CString strPort)
{
int i,nCount ;
POSITION pos ;
CSerialComm *pSCom ;
nCount = m_ListSCom.GetCount() ;
for( i = 0 ; i < nCount ; i++ )
{
pos = m_ListSCom.FindIndex(i) ;
pSCom = (CSerialComm*)m_ListSCom.GetAt(pos) ;
if ( strPort == pSCom->GetPort() )
return (LPVOID)pSCom ;
}
return NULL ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -