| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include "StdAfx.h"
- #include "ClientAssistant.h"
- #include "NetClient.h"
- #include "NetLinkMain.h"
- CClientAssistant::CClientAssistant(bool IsAutoReconnect) : m_pParent(NULL), m_IsAutoReconnect(IsAutoReconnect),
- m_LinkType(PDU_LINK_TYPE_SERVER), m_FarPort(0), m_FarDevType(PDU_DEV_TYPE_UNKNOWN), m_FarDevId(-1)
- {
- }
- CClientAssistant::~CClientAssistant(void)
- {
- }
- /*****************************************************************
- **【函数名称】 __onTimerListen
- **【函数功能】 心跳定时器处理函数
- **【参数】
- **【返回值
- ****************************************************************/
- void CClientAssistant::__onTimerListen( void )
- {
- m_pParent->monitor();
- }
- /*****************************************************************
- **【函数名称】 __onTimerRecon
- **【函数功能】 重连定时器处理函数
- **【参数】
- **【返回值
- ****************************************************************/
- void CClientAssistant::__onTimerRecon( void )
- {
- m_pParent->__connect(m_FarIp, m_FarPort);
- }
- /*****************************************************************
- **【函数名称】 __setTimer
- **【函数功能】 设立定时器
- **【参数】
- **【返回值
- ****************************************************************/
- void CClientAssistant::__setTimer( UINT TimmerId )
- {
- if(TimmerId == PDU_RECONN_TIMER)
- {
- m_Timer.Stop();
- m_Timer.SetTimedEvent(this, &CClientAssistant::__onTimerRecon);
- m_Timer.Start(VAL_RECONNECT_INTERVAL, false, true);
- }
- else if(TimmerId == PDU_LISTEN_TIMER)
- {
- m_Timer.Stop();
- m_Timer.SetTimedEvent(this, &CClientAssistant::__onTimerListen);
- m_Timer.Start(VAL_LISTEN_INTERVAL);
- }
- else
- {
- ASSERT(FALSE);
- }
- }
- /*****************************************************************
- **【函数名称】 __killTimer
- **【函数功能】 删除定时器
- **【参数】
- **【返回值
- ****************************************************************/
- void CClientAssistant::__killTimer( UINT TimmerId )
- {
- m_Timer.Stop();
- }
- /*****************************************************************
- **【函数名称】 getFarLinkInfo
- **【函数功能】 读取远端连接信息
- **【参数】
- **【返回值】 a_strIp SOCKET对端IP
- a_nPort SOCKET对端PORT
- ****************************************************************/
- void CClientAssistant::getFarLinkInfo( CString& a_FarIp, int& a_FarPort ) const
- {
- a_FarIp = m_FarIp;
- a_FarPort = m_FarPort;
- }
- /*****************************************************************
- **【函数名称】 setFarLinkInfo
- **【函数功能】 设置远端连接信息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CClientAssistant::setFarLinkInfo( LPCTSTR a_FarIp, int a_FarPort )
- {
- m_FarIp = a_FarIp;
- m_FarPort = a_FarPort;
- }
- /*****************************************************************
- **【函数名称】 getFarDevInfo
- **【函数功能】 读取远端设备信息
- **【参数】
- **【返回值】 a_nDevType 对端设备类型
- a_nDevId 对端设备ID
- ****************************************************************/
- void CClientAssistant::getFarDevInfo( PDU_DEV_TYPE& a_DevType, int& a_DevId ) const
- {
- a_DevType = m_FarDevType;
- a_DevId = m_FarDevId;
- }
- /*****************************************************************
- **【函数名称】 setFarDevInfo
- **【函数功能】 设置对端设备信息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CClientAssistant::setFarDevInfo( PDU_DEV_TYPE a_DevType, int a_DevId )
- {
- m_FarDevType = a_DevType;
- m_FarDevId = a_DevId;
- }
- /*****************************************************************
- **【函数名称】 OnConnEstablisthed
- **【函数功能】 SOCKET连接建立事件处理
- **【参数】
- **【返回值】
- ****************************************************************/
- void CClientAssistant::onConnEstablisthed( void )
- {
- ASSERT(m_pParent != NULL);
- if(m_LinkType == PDU_LINK_TYPE_CLIENT) // 客户端
- m_pParent->regist(); // 注册连接
- }
- /*****************************************************************
- **【函数名称】 onConnFailed
- **【函数功能】 连接断开/失败事件处理
- **【参数】
- **【返回值】
- ****************************************************************/
- void CClientAssistant::onConnFailed( void )
- {
- __killTimer(PDU_LISTEN_TIMER);
- if(m_LinkType == PDU_LINK_TYPE_CLIENT && m_IsAutoReconnect) // 如果是客户端连接,则启动自动重连
- {
- __setTimer(PDU_RECONN_TIMER); // 启动自动重连定时器
- }
- }
- /*****************************************************************
- **【函数名称】 onConnRegistOK
- **【函数功能】 连接注册成功事件处理
- **【参数】
- **【返回值】
- ****************************************************************/
- void CClientAssistant::onConnRegistOK( void )
- {
- ASSERT(m_pParent != NULL);
- // 对OCX、SC客户端以外的所有设备端启用心跳监听
- PDU_DEV_TYPE a_nDevType = PDU_DEV_TYPE_UNKNOWN;
- int a_nDevId = 0;
- m_pParent->getLocalInfo(a_nDevType, a_nDevId);
- if(a_nDevType != PDU_DEV_TYPE_UNKNOWN && a_nDevType != PDU_DEV_TYPE_AGENT && a_nDevType != PDU_DEV_TYPE_SC_CLIENT)
- {
- __setTimer(PDU_LISTEN_TIMER);
- }
- }
- /*****************************************************************
- **【函数名称】 onConnRegistFailed
- **【函数功能】 连接注册失败事件处理
- **【参数】
- **【返回值】
- ****************************************************************/
- void CClientAssistant::onConnRegistFailed( void )
- {
- ASSERT(m_pParent != NULL);
- TRACE(_T("Do Nothing @CClientAssistant::onConnRegistFailed"));
- }
|