您的位置:首页 > 其它

将pimpl习惯用法运用到server的编写。

2008-07-07 23:29 246 查看
在网络服务器的编写过程中,常常有各种各样的模型。以Linux操作系统上为例,主要有3种常用的编程模型:select, poll, epoll。select和poll是比较旧I/O多路复用模型,epoll是2.6内核采支持的模型,当然,可以通过打补丁来支持旧2.6以下的内核版本。从效率角度来讲,epoll明显高于select和poll,但是select的移植性比较好。

Pimpl习惯用法,通常被用于隐藏实现细节。通常在类中声名struct XxxxImpl* pClsImpl或class XxxxImpl* pClsImpl来将私有数据封装到另一个对象中(包括私有成员和函数)。在提供接口时,这样可以避免私有数据的可见性,而且很好地分离了接口与实现,降低了耦合性。而在servere 的编写过程中,可以将各种服务器模型视为具体的实现。对于一个server可以这样声明:

class CMyServer

{

public: 

CMyServer(string sListenIp, int iListenPort, int iBackLog); 

~CMyServer();

bool Init(); 

bool ProcessConnection(int iSocket, unsigned int iSrcIp, unsigned int iPort);

private:

class CMyServerImpl* m_pServerImpl;

}; 

       而对于CMyServerImpl的实现,则可以有很多的选择模型,具体的下次在写了,主要是提供连接队列的管理,超时的关闭,甚至的会话的管理等。如,以poll为例:    class CMyServerImpl

{

public:

CMyServerImpl(string sListenIp, int iListenPort, int iBackLog);

~CMyServerImpl();

private:

unsigned int m_iListenSocket;

string m_sListenIp;

unsigned int m_iListenPort;

//其他相关成员。。。

int m_iBackLog;

int m_iCurrentSize;

int m_iEventsCapability;

 //poll 队列    

pollfd* m_pPollEvents;

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息