keepalive keepalive缓存组件


keepalive keepalive缓存组件

文章插图
大家好,小耶来为大家解答以上的问题 。keepalive缓存组件,keepalive这个很多人还不知道,现在让我们一起来看看吧!
1、7.2.6KeepAlive的添加下面是KeepAlive模块的添加,KeepAlive模块指的是在客户端电脑电源突然中断的情况下,为了确认客户端是否结束而定期传送的信号 。
2、如果出现了上面的情况,IOCP或者EventSelect无法测试OnIoDisconnected , 因此必须定期传送数据包通过确认是否传送失败来确认是否访问中断 。
【keepalive keepalive缓存组件】3、(ConnectedSession.h) #pragma once//继承CPacketSession的CConnectedSessionclass CConnectedSession : public CPacketSession { . . . BOOL ReStart (SOCKET hListenSocket ) ;//设置并获得m_bConnected值的函数VOID SetConnected(BOOL bConnected) { CThreadSync Sync ; m_bConnected = bConnected; } BOOL GetConnected(VOID) { CThreadSync Sync; return m_bConnected; } private://判断是否被访问的变量BOOL m_bConnected}; () CConnectedSession: :CConnectedSession(VOID) {//判断是否被访问的变量m_bConnect ed = FALSE; } (ConnectedSessionManager.h) #pragma once//管理ConnectedSession的类//为了在多线程环境中使用更加稳定 , 继承CMultiThreadSyncclass CConnectedSessionManager : public CMultiThreadSync< CConnectedSessionManager> { . . . VOID End(VOID) ;//给访问的所有ConnectedSession传送数据包的函数VOID WriteAll (DWORD dwProtocol , BYTE *pData , DWORD dwLength) ; private: . . . };()//给访问的所有Session传送数据的函数VOID CConnectedSessionManager : :WriteAll (DWORD dwProtocol , BYTE *pData, DWORD dwLength) {//同步对象CThreadSync Sync ;//对访问的所有对象进行WritePacketfor (DWORD i =0;i GetConnected()) m_vConnectedSessions[i ] ->WritePacket (dwProtocol , pData, dwLength) ; } } (ServerIocp.h) #pragma once//继承CIocpclass CServerIocp : public CIocp { . . . VOID End(VOID) ;//KeepAlive Thread Callback函数VOID KeepThreadCallback(VOID) ; . . . CConnectedSessionManager m_oConnectedSessionManager ;//管理KeepAlive Thread的句柄HANDLE m_hKeepThread; //结束KeepAlive Thread时使用的事件HANDLE m_hKeepThreadDestroyEvent ; }; ()//KeepAlive Thread Callback函数DWORD WINAPI KeepThreadCallback(LPVOID pParameter) { CServerIocp *pOwner = (CServerIocp*) pParameter ; pOwner->KeepThreadCallback() ; return 0; } VOID CServerIocp: :KeepThreadCallback(VOID) {//KeepAlive信号DWORD dwKeepAl ive = 0xFFFF; while (TRUE) {//30秒内结束事件没有出现的话,将跳过DWORD dwResult = WaitForSingleObject (m_hKeepThreadDestroyEvent , 30000) ;//结束时间出现的情况下,结束Threadif (dwResult == WAIT_OBJECT_0) return;//给访问中的所有Session传送数据包m_oConnect edSessionManager .WriteAll (0x3000000, (BYTE*)&dwKeepAlive , sizeof (DWORD)) ; } }//CIocp的假想函数VOID CServerIocp: :OnIoConnected(VOID *pObject ) { . . . pConnectedSession->SetConnected(TRUE) ; } VOID CServerIocp: :OnIoDisconnected(VOID *pObject ) { . . . pConnectedSession->SetConnected(FALSE) ; } BOOL CServerIocp: :Begin(VOID) { . . .//创建KeepAlive结束事件m_hKeepThreadDestroyEvent = CreateEvent (NULL, FALSE, FALSE, NULL) ; if ( !m_hKeepThreadDestroyEvent ) {//失败的情况下结束CServerIocp: :End() ; return FALSE; } //创建KeepAlive Threadm_hKeepThread = CreateThread(NULL, 0, : :KeepThreadCallback, this, 0, NULL) ; if ( !m_hKeepThread) {//创建失败的情况下结束CServerIocp: :End() ; return FALSE; } return TRUE; } VOID CServerIocp: :End(VOID) {//结束CServerIocp的时候//m_hKeepThread不是NULL的话,结束KeepAlive Threadif (m_hKeepThread) {//出现结束事件SetEvent (m_hKeepThreadDestroyEvent ) ;//等待至Thread结束为止WaitForSingleObject (m_hKeepThread, INFINITE) ;//关闭句柄CloseHandle(m_hKeepThread) ; m_hKeepThread = NULL; }//如果KeepAlive结束事件不是NULL的话if (m_hKeepThreadDestroyEvent ) {//关闭事件CloseHandle(m_hKeepThreadDestroyEvent ) ; m_hKeepThreadDestroyEvent = NULL; 。
本文到此分享完毕,希望对大家有所帮助 。