📄 socksvr.h
字号:
/*************************************************************************** socksvr.h - description ------------------- begin : Fri Jul 20 2001 copyright : (C) 2001 by Mark email : alben@yeah.net ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifndef SOCKSVR_H#define SOCKSVR_H#include <list>#include "exception.h"#include "socket.h"#include "shm.h"#include "filelock.h"//==============================================================================// Non-block tcp server//==============================================================================class CNBTcpSvr : public CTcpSocket{public: CNBTcpSvr(const CInetAddress& stInetAddr, tcport_t tPort, int iBufSize = 512, int iMaxClients = 512, int iTimeout = 15, int iWaitTime = 5); CNBTcpSvr(const CUnixAddress& stUnixAddr, int iBufSize = 512, int iMaxClients = 512, int iTimeout = 15, int iWaitTime = 5); virtual ~CNBTcpSvr(); void Run(); void Shutdown() { m_bShutdown = true; }protected: struct CConn { int m_iLastAccessTime; CTcpStream* m_pstTcpStream; inline CConn(); inline ~CConn(); }; typedef list<CConn*> CConnList;protected: int m_iBufSize; //tcpstream buffer size int m_iMaxClients; //max connected clients int m_iTimeout; //when timeout connection closed (second), 0 means no timeout int m_iWaitTime; //select timeout (second) CConnList m_stConnList; bool m_bShutdown; //if to shutdown server protected: void CheckEvent(); virtual void CheckConnTimeout(); virtual void PrepareSelect(int& iMaxFd, fd_set& rdset, fd_set& wrset); virtual void CheckPendingConn(const fd_set& rdset); virtual void CheckPendingRead(const fd_set& rdset); virtual void CheckPendingWrite(const fd_set& wrset); //return false will shutdown server virtual bool OnException(CException& e) { return true; } //return fase will close stTcpStream socket virtual bool OnRead(CTcpStream& stTcpStream) { return true; } virtual bool OnWrite(CTcpStream& stTcpStream) { return true; } virtual void OnCheck() { }};inline CNBTcpSvr::CConn::CConn(){ m_iLastAccessTime = 0; m_pstTcpStream = 0;}inline CNBTcpSvr::CConn::~CConn(){ if (m_pstTcpStream) delete m_pstTcpStream;}//==============================================================================////==============================================================================class CScoreBoard : protected CShm{public: enum { S_EMPTY = 0, S_IDLE = 1, S_BUSY = 2, }; private: const int m_iScores;public: CScoreBoard(key_t tKey, int iScores) : CShm(tKey, iScores), m_iScores(iScores) { for (int i = 0; i < iScores; i++) m_pchShm[i] = S_EMPTY; } virtual ~CScoreBoard() { } inline char& operator [] (int iIndex) { if (iIndex < 0 || iIndex >= m_iScores) throw CPreforkSvrException("scoreboard index overflow", __FILE__, __LINE__); return m_pchShm[iIndex]; }};class CPreforkSvr{public: enum { PROCESS_COUNT_HARD_LIMIT = 1024, };private: enum { DEFAULT_CHECK_PROC_TIMEVAL = 15, //seconds }; public: static int s_iMaxProcs; //process count cannot be greater than m_iMaxProcs static pid_t s_atPId[PROCESS_COUNT_HARD_LIMIT]; protected: int m_iProcIndex; //be used to ident process index in child process //equar -1 in parent processprivate: int m_iMaxIdleProcs; //some processes will be killed if idle process count > m_iMaxIdleProcs int m_iMinIdleProcs; //some processes will be forked if idle process count < m_iMinIdleProcs int m_iStartProcs; //process count when server starts unsigned int m_uiCheckProcTimeval; CScoreBoard m_stScoreBoard; CFileLock m_stLock; public: CPreforkSvr(key_t tKey, const char* sFileLockPath, int iMaxProcs, int iMaxIdleProcs, int iMinIdleProcs, int iStartProcs, unsigned int uiCheckProcTimeval = DEFAULT_CHECK_PROC_TIMEVAL); virtual ~CPreforkSvr() { } void Run(); protected: virtual void OnNoticeException(CException& e) { } inline void SetChildEmpty() //change current child process state to EMPTY { WriteLockW(); m_stScoreBoard[m_iProcIndex] = CScoreBoard::S_EMPTY; Unlock(); } inline void SetChildIdle() //change current child process state to IDLE { WriteLockW(); m_stScoreBoard[m_iProcIndex] = CScoreBoard::S_IDLE; Unlock(); } inline void SetChildBusy() //change current child process state to BUSY { WriteLockW(); m_stScoreBoard[m_iProcIndex] = CScoreBoard::S_BUSY; Unlock(); } virtual void ChildRun() = 0; virtual void OnChildException(CException& e) {} virtual void ChildEnd() { SetChildEmpty(); } private: int ForkChildSvrs(int iSvrs); void KillChildSvrs(int iSvrs); void KillAllChildSvrs(); //signals should be masked before call these two functions //be used in parent process inline void AllWriteLockW() { while (!m_stLock.WriteLockW(0, PROCESS_COUNT_HARD_LIMIT)); } inline void AllUnlock() { while (!m_stLock.Unlock(0, PROCESS_COUNT_HARD_LIMIT)); } //signals should be masked before call these two functions //be used in child process inline void WriteLockW() { while (!m_stLock.WriteLockW(m_iProcIndex)); } inline void Unlock() { while (!m_stLock.Unlock(m_iProcIndex)); }};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -