Clean thread exit

better nonblocking socket mode support
This commit is contained in:
IMback
2017-11-05 22:07:26 +01:00
parent e07866b906
commit 7603d1c59d
3 changed files with 78 additions and 20 deletions

View File

@ -129,6 +129,14 @@ void Socket::setKeepalive()
setsockopt(sockDesc, SOL_SOCKET, TCP_KEEPINTVL, &optval, sizeof(optval));
}
void Socket::setBlocking(bool flag)
{
int flags = fcntl(sockDesc, F_GETFL, 0);
if( !flag ) flags = flags | O_NONBLOCK;
else flags = flags & ~O_NONBLOCK;
fcntl(sockDesc, F_SETFL, flags);
}
void Socket::cleanUp() {
}
@ -240,15 +248,22 @@ TCPServerSocket::TCPServerSocket(const string &localAddress, unsigned short loca
TCPSocket* TCPServerSocket::accept()
{
int newConnSD;
if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0 )
int newConnSD = -1;
if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0 && errno != EAGAIN && errno != EWOULDBLOCK )
{
throw SocketException("Accept failed (accept())", true);
}
if(keepalive) setKeepalive();
return new TCPSocket(newConnSD);
TCPSocket* newSocket = nullptr;
if(newConnSD > 0)
{
newSocket = new TCPSocket(newConnSD);
if(keepalive) newSocket->setKeepalive();
}
return newSocket;
}
void TCPServerSocket::setListen(int queueLen) {
if (listen(sockDesc, queueLen) < 0) {
throw SocketException("Set listening socket failed (listen())", true);