seleect io模型的select操作封装

#pragma once
 

/************************************************************************/
/* 

  seleect io模型的select操作封装 

*/
/************************************************************************/

class Selector
{
public:
	Selector(void){reset();};
	~Selector(void){}

	inline void reset()
	{
		FD_ZERO(&fdSocket);
	}

	inline void add_sock(SOCKET s)
	{
		FD_SET(s, &fdSocket);
	};

	inline int test_sock(SOCKET s)
	{
		return FD_ISSET( s , &fdSocket);
	}

	int select(int timeout=10)
	{
		struct timeval tv = {0, timeout}; //超时定义,10毫秒
 
		int nRet = ::select(0, &fdSocket, NULL, NULL, &tv);
		//if (nRet==0) //超时
		//if( nRet==SOCKET_ERROR )  // 出错 
		return nRet;
	}

private:

	fd_set fdSocket;	 

};

具体使用可以参考

终极网络服务端编程配套源码

lua server 实现了lua脚本处理服务端逻辑+数据库

终极网络服务端编程_pdf.zip

猜你喜欢

转载自blog.csdn.net/smwhotjay/article/details/81703024
今日推荐