- 论坛徽章:
- 0
|
本帖最后由 urapple 于 2011-03-03 15:00 编辑
非阻塞发送,我的select超时设置为5秒,居然有时还出现很多超时信息。该如何避免?- boost::mutex::scoped_lock scoped_lock(this->send_mutex) ;
- {
- while (bytes_to_write_total > 0)
- {
- //超时由1秒增加为5秒. 2010/9/25.
- if ((selret=SendableTimeOut(5, 0))>0)//only one, so we dont FD_ISSET.
- {
- bytes_write = Send(ptemp, bytes_to_write_total);
- if (bytes_write < 0)
- {
- #if defined(WIN32) || defined(WINNT)
- if((iErrorRet=WSAGetLastError()) == WSAEWOULDBLOCK)
- #else
- if (EAGAIN == errno || EINTR == errno)
- #endif
- {
- //在select 检查能写的情况下,又进入这里,本身就是矛盾的,异常情况.2010/9/17.
- cout<<"[send]: bytes_write<0, WSAEWOULDBLOCK but select ok!"<<endl;
- #if !defined(WIN32) && !defined(WINNT)
- iErrorRet = errno;
- #endif
- cx ++;
- if (cx>SEND_RETRY_COUNT)
- {
- error = 2;
- //printf("[send]: retry <0 error!\n");
- break;
- }
- }
- else
- {
- #if !defined(WIN32) && !defined(WINNT)
- iErrorRet = errno;
- #endif
- error = 1;
- //printf("[send]: <0 error!\n");
- break;
- }
- }
- else
- {
- bytes_to_write_total -= bytes_write;
- bytes_have_written_total += bytes_write;
- ptemp += bytes_have_written_total;
- }
- }
- else if(selret==0)
- {
- //超时不作为出错处理. 2010/9/25.
- cout<<"[send]: select timeout!"<<this->dev_type<<":"<<this->list_id<<endl;
- }
- else
- {
- error = 1;
- cout<<"[send]: select error!"<<endl;
- break;
- }
- }
- }
- if (error==1)//net break,exit.
- {
- cout<<"send pack fail! "<<iErrorRet<<":"<<this->dev_type<<endl;
- string swip;
- unsigned int port;
- GetPeerName(swip,port);
- cout<<"sock_type="<<m_type<<" ip="<<swip<<" port="<<port<<endl;
-
- m_needExit=true;
- }
- else if (error==2)
- {
- cout<<"retry too many! give it up! exit!"<<endl;
- m_needExit=true;//2010/9/17.
- }
复制代码 |
|