当前位置: 技术问答>linux和unix
请帮忙看一下,为什么Linux下 select 总返回 0,windows下就正常
来源: 互联网 发布时间:2015-07-10
本文导语: 请帮忙看一下,为什么Linux下 select 总返回 0,windows下就正常。 void sock_manager_t::poll() { vector::iterator it; for (it = m_socks.begin(); it != m_socks.end(); ) { if ((*it)->m_sock == -1 || (*it)->m_destr...
请帮忙看一下,为什么Linux下 select 总返回 0,windows下就正常。
void sock_manager_t::poll()
{
vector::iterator it;
for (it = m_socks.begin(); it != m_socks.end(); )
{
if ((*it)->m_sock == -1 || (*it)->m_destroyed)
{
m_socks.erase(it);
sys_msg("sock_manager_t sock num : %d", m_socks.size());
continue;
}
++it;
}
if (m_socks.empty())
{
THREAD_SLEEP(1);
return;
}
int cnt = m_socks.size();
int tick = sys_tick();
struct timeval tv;
fd_set fdRead, fdWrite, fdExcept;
int i, j, index, temp, max_fd;
index = temp = 0;
int reader_num = 0;
for (i = 0; i m_listening || p->m_connected)
{
FD_SET(p->m_sock, &fdRead);
}
if (p->m_connected || p->m_connectting)
{
FD_SET(p->m_sock, &fdWrite);
}
FD_SET(p->m_sock, &fdExcept);
if (++index >= cnt) break;
}
int result = 0;
result = select(max_fd + 1, &fdRead, &fdWrite, &fdExcept, &tv);
if (result == 0) continue;
if (result == SOCKET_ERROR)
{
//sys_msg("result == 0 ||result == SOCKET_ERROR");
continue;
}
index = temp;
for (j = 0; j m_sock, &fdRead))
{
if (p->m_listening)
{
if (p->m_on_accept) p->m_on_accept(p, 0, p->m_param);
}
else if (p->m_connected)
{
if (p->m_on_recv) p->m_on_recv(p, 0, p->m_param);
}
reader_num++;
}
// on_connect(ok)
if (FD_ISSET(p->m_sock, &fdWrite))
{
if (p->m_connectting)
{
bool is_ok = true;
/*
#ifdef LINUX
// in linux should check again
int err, len, ret;
len = sizeof(err);
ret = getsockopt(p->m_sock, SOL_SOCKET, SO_ERROR, (char*)&err, (socklen_t*)&len);
if (ret != SOCKET_ERROR)
{
if (err != 0) is_ok = false;
}
else
{
is_ok = false;
}
#endif
*/
if (is_ok)
{
p->m_connectting = false;
p->m_connected = true;
if (p->m_on_connect) p->m_on_connect(p, 0, p->m_param);
}
else
{
p->m_connectting = false;
p->m_connected = false;
if (p->m_on_connect) p->m_on_connect(p, -1, p->m_param);
}
}
else if (p->m_connected)
{
if (p->m_on_send) p->m_on_send(p, 0, p->m_param);
}
}
// on_connect(error)
if (FD_ISSET(p->m_sock, &fdExcept))
{
if (p->m_connectting)
{
p->m_connectting = false;
p->m_connected = false;
if (p->m_on_connect) p->m_on_connect(p, -1, p->m_param);
}
else if (p->m_connected || p->m_listening)
{
p->close();
if (p->m_on_close) p->m_on_close(p, -1, p->m_param);
}
}
if (++index >= cnt) break;
}
if (index >= cnt) break;
}
if (reader_num == 0 && sys_tick() - tick m_listening || p->m_connected)
{
FD_SET(p->m_sock, &fdRead); //它执行了吗?
}
if (p->m_connected || p->m_connectting)
{
FD_SET(p->m_sock, &fdWrite); //它执行了吗?
}
void sock_manager_t::poll()
{
vector::iterator it;
for (it = m_socks.begin(); it != m_socks.end(); )
{
if ((*it)->m_sock == -1 || (*it)->m_destroyed)
{
m_socks.erase(it);
sys_msg("sock_manager_t sock num : %d", m_socks.size());
continue;
}
++it;
}
if (m_socks.empty())
{
THREAD_SLEEP(1);
return;
}
int cnt = m_socks.size();
int tick = sys_tick();
struct timeval tv;
fd_set fdRead, fdWrite, fdExcept;
int i, j, index, temp, max_fd;
index = temp = 0;
int reader_num = 0;
for (i = 0; i m_listening || p->m_connected)
{
FD_SET(p->m_sock, &fdRead);
}
if (p->m_connected || p->m_connectting)
{
FD_SET(p->m_sock, &fdWrite);
}
FD_SET(p->m_sock, &fdExcept);
if (++index >= cnt) break;
}
int result = 0;
result = select(max_fd + 1, &fdRead, &fdWrite, &fdExcept, &tv);
if (result == 0) continue;
if (result == SOCKET_ERROR)
{
//sys_msg("result == 0 ||result == SOCKET_ERROR");
continue;
}
index = temp;
for (j = 0; j m_sock, &fdRead))
{
if (p->m_listening)
{
if (p->m_on_accept) p->m_on_accept(p, 0, p->m_param);
}
else if (p->m_connected)
{
if (p->m_on_recv) p->m_on_recv(p, 0, p->m_param);
}
reader_num++;
}
// on_connect(ok)
if (FD_ISSET(p->m_sock, &fdWrite))
{
if (p->m_connectting)
{
bool is_ok = true;
/*
#ifdef LINUX
// in linux should check again
int err, len, ret;
len = sizeof(err);
ret = getsockopt(p->m_sock, SOL_SOCKET, SO_ERROR, (char*)&err, (socklen_t*)&len);
if (ret != SOCKET_ERROR)
{
if (err != 0) is_ok = false;
}
else
{
is_ok = false;
}
#endif
*/
if (is_ok)
{
p->m_connectting = false;
p->m_connected = true;
if (p->m_on_connect) p->m_on_connect(p, 0, p->m_param);
}
else
{
p->m_connectting = false;
p->m_connected = false;
if (p->m_on_connect) p->m_on_connect(p, -1, p->m_param);
}
}
else if (p->m_connected)
{
if (p->m_on_send) p->m_on_send(p, 0, p->m_param);
}
}
// on_connect(error)
if (FD_ISSET(p->m_sock, &fdExcept))
{
if (p->m_connectting)
{
p->m_connectting = false;
p->m_connected = false;
if (p->m_on_connect) p->m_on_connect(p, -1, p->m_param);
}
else if (p->m_connected || p->m_listening)
{
p->close();
if (p->m_on_close) p->m_on_close(p, -1, p->m_param);
}
}
if (++index >= cnt) break;
}
if (index >= cnt) break;
}
if (reader_num == 0 && sys_tick() - tick m_listening || p->m_connected)
{
FD_SET(p->m_sock, &fdRead); //它执行了吗?
}
if (p->m_connected || p->m_connectting)
{
FD_SET(p->m_sock, &fdWrite); //它执行了吗?
}