📄 wyselect.cpp
字号:
/* Copyright is licensed by GNU LGPL. by I.J.Wang 2004*/#define WYLIB_SOURCE#include "wyselect.h"#include <new>const char Wy_FdSet::class_name[]="Wy_FdSet";Wy_FdSet::Wy_FdSet() WY__TSPC() : _maxfd(DefaultMaxFd),_sel_used(false){ FD_ZERO(&_fdset);};Wy_FdSet::Wy_FdSet(const Wy_FdSet& src) WY__TSPC() : _maxfd(src._maxfd), _sel_used(src._sel_used){ ::memcpy(&_fdset,&src._fdset,sizeof(_fdset));};Wy_FdSet::Wy_FdSet(Wy_FdSet& src,Wy::ByMove_t) WY__TSPC() : _maxfd(src._maxfd), _sel_used(src._sel_used){ ::memcpy(&_fdset,&src._fdset,sizeof(_fdset)); src._sel_used=true; // for safety and error detection};void Wy_FdSet::reset(void) WY__TSPC(){ _maxfd=DefaultMaxFd; _sel_used=false; FD_ZERO(&_fdset);};void Wy_FdSet::reset(const Wy_FdSet& src) WY__TSPC(){ _maxfd=src._maxfd; _sel_used=src._sel_used; ::memcpy(&_fdset,&src._fdset,sizeof(_fdset));};void Wy_FdSet::swap(Wy_FdSet& src) WY__TSPC(){ Wy__Base::vswap(_maxfd,src._maxfd); Wy__Base::vswap(_sel_used,src._sel_used); Wy__Base::memswp(&_fdset,&src._fdset,sizeof(_fdset));};/*const Wy_FdSet& Wy_FdSet::operator=(const Wy_FdSet& src) WY__TSPC(){ this->reset(src); return(*this);};*/bool Wy_FdSet::is_set(WyFileHandle fh) const WY__TSPC(){ const Wy__TypeFD fd(fh.fd()); if((fd<0)||(fd>=FD_SETSIZE)) { return(false); } return( FD_ISSET(fd,&_fdset) );};bool Wy_FdSet::is_set(const WySysFile& bf) const WY__TSPC(){ const Wy__TypeFD fd(bf.fh().fd()); if((fd<0)||(fd>=FD_SETSIZE)) { return(false); } return( FD_ISSET(fd,&_fdset) );};void Wy_FdSet::set(WyFileHandle fh) WY__TSPC(){ const Wy__TypeFD fd(fh.fd()); if((fd<0)||(fd>=FD_SETSIZE)) { return; } if(fd>_maxfd) { _maxfd=fd; } FD_SET(fd,&_fdset);};void Wy_FdSet::set(const WySysFile& bf) WY__TSPC(){ const Wy__TypeFD fd(bf.fh().fd()); if((fd<0)||(fd>=FD_SETSIZE)) { return; } if(fd>_maxfd) { _maxfd=fd; } FD_SET(fd,&_fdset);};void Wy_FdSet::clr(WyFileHandle fh) WY__TSPC(){ Wy__TypeFD fd(fh.fd()); if((fd<0)||(fd>=FD_SETSIZE)) { return; } FD_CLR(fd,&_fdset); if(fd==_maxfd) { for( --fd; fd>=0; --fd) { if( FD_ISSET(fd,&_fdset) ) { break; } } _maxfd=fd; // -1 if not in _fdset }};void Wy_FdSet::clr(const WySysFile& bf) WY__TSPC(){ this->clr(bf.fh());};/*bool Wy_FdSet::operator==(const Wy_FdSet& rhs) const WY__TSPC(){ if((_maxfd!=rhs._maxfd)||(_sel_used!=rhs._sel_used)) { return(false); } return ::memcmp(&_fdset,&rhs._fdset,sizeof(_fdset))==0;};*/Wy__TypeFD Wy_FdSet::wy_maxfd(void) const WY__NOTHROW__{ return _maxfd;};::fd_set* Wy_FdSet::wy_fdset_ptr(void) WY__NOTHROW__{ if(_sel_used) { return(0); } _sel_used=true; return(&_fdset);};//------------------------------------------------------------------WyRet Wy::select(Wy_FdSet* readfds, Wy_FdSet* writefds, Wy_FdSet* exceptfds){ ::fd_set* pfds_read=NULL; ::fd_set* pfds_write=NULL; ::fd_set* pfds_except=NULL; Wy__TypeFD max_fd=-1; // initial value must be -1 (implement reason) if(readfds!=NULL) { if((pfds_read=readfds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(readfds->wy_maxfd()>max_fd) { max_fd=readfds->wy_maxfd(); } } if(writefds!=NULL) { if((pfds_write=writefds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(writefds->wy_maxfd()>max_fd) { max_fd=writefds->wy_maxfd(); } } if(exceptfds!=NULL) { if((pfds_except=exceptfds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(exceptfds->wy_maxfd()>max_fd) { max_fd=exceptfds->wy_maxfd(); } } max_fd=::select(max_fd+1,pfds_read,pfds_write,pfds_except,NULL); if(max_fd<=0) { if(max_fd==0) { WY_RETURN(Wym_ETIMEDOUT); } WY_RETURN(errno); } return(Ok);};// behavior is not defined if a is a duplicate of bWyRet Wy::select(Wy_FdSet* readfds, Wy_FdSet* writefds, Wy_FdSet* exceptfds,const WyTimeSpec& timeout){ ::fd_set* pfds_read=NULL; ::fd_set* pfds_write=NULL; ::fd_set* pfds_except=NULL; Wy__TypeFD max_fd=-1; // initial value must be -1 (implement reason) if(readfds!=NULL) { if((pfds_read=readfds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(readfds->wy_maxfd()>max_fd) { max_fd=readfds->wy_maxfd(); } } if(writefds!=NULL) { if((pfds_write=writefds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(writefds->wy_maxfd()>max_fd) { max_fd=writefds->wy_maxfd(); } } if(exceptfds!=NULL) { if((pfds_except=exceptfds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(exceptfds->wy_maxfd()>max_fd) { max_fd=exceptfds->wy_maxfd(); } } if(timeout<WyTimeSpec()) { WY_RETURN(Wym_EINVAL); } struct timeval tm; tm.tv_sec= timeout.second(); tm.tv_usec=timeout.nano()/1000; max_fd=::select(max_fd+1,pfds_read,pfds_write,pfds_except,&tm); if(max_fd<=0) { if(max_fd==0) { WY_RETURN(Wym_ETIMEDOUT); } WY_RETURN(errno); } return(Ok);};WyRet Wy::select(Wy_FdSet* readfds, Wy_FdSet* writefds, Wy_FdSet* exceptfds,WyTimeSpec& timeout){ ::fd_set* pfds_read=NULL; ::fd_set* pfds_write=NULL; ::fd_set* pfds_except=NULL; Wy__TypeFD max_fd=-1; // initial value must be -1 (implement reason) if(readfds!=NULL) { if((pfds_read=readfds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(readfds->wy_maxfd()>max_fd) { max_fd=readfds->wy_maxfd(); } } if(writefds!=NULL) { if((pfds_write=writefds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(writefds->wy_maxfd()>max_fd) { max_fd=writefds->wy_maxfd(); } } if(exceptfds!=NULL) { if((pfds_except=exceptfds->wy_fdset_ptr())==NULL) { WY_RETURN(Wym_ENODEV); } if(exceptfds->wy_maxfd()>max_fd) { max_fd=exceptfds->wy_maxfd(); } } if(timeout<WyTimeSpec()) { WY_RETURN(Wym_EINVAL); } struct timeval tm; tm.tv_sec= timeout.second(); tm.tv_usec=timeout.nano()/1000; max_fd=::select(max_fd+1,pfds_read,pfds_write,pfds_except,&tm); timeout.wy_timespec().tv_sec=tm.tv_sec; timeout.wy_timespec().tv_nsec=tm.tv_usec*1000; if(max_fd<=0) { if(max_fd==0) { WY_RETURN(Wym_ETIMEDOUT); } WY_RETURN(errno); } return(Ok);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -