一个seclect的封装

一个seclect的封装

一个seclect的封装


uint32 SocketWait(TSocket *s, bool rd, bool wr,uint32 timems)    
{
     fd_set rfds,wfds;
#ifdef _WIN32
     TIMEVAL tv;
#else
      struct timeval tv;
#endif   /* _WIN32 */ 
 
     FD_ZERO(&rfds);
     FD_ZERO(&wfds); 

      if (rd)      // TRUE
          FD_SET(*s,&rfds);    // 添加要测试的描述字 

      if (wr)      // FALSE
          FD_SET(*s,&wfds); 

     tv.tv_sec=timems/1000;      // second
     tv.tv_usec=timems%1000;      // ms 

      for (;;)  // 如果errno==EINTR,反复测试缓冲区的可读性
     {
           switch(select((*s)+1,&rfds,&wfds,NULL,
              (timems==TIME_INFINITE?NULL:&tv)))   // 测试在规定的时间内套接口接收缓冲区中是否有数据可读
          {                                               // 0--超时,-1--出错
           case 0:      /*  time out  */
                return 0; 
           case (-1):     /*  socket error  */
                if (SocketError()==EINTR)
                     break;              
                return 0;  // 有错但不是EINTR 
           default:
                if (FD_ISSET(*s,&rfds))  // 如果s是fds中的一员返回非0,否则返回0
                     return 1;
                if (FD_ISSET(*s,&wfds))
                     return 2;
                return 0;
          };
     }

}

你可能感兴趣的:(一个seclect的封装)