Clean thread exit
better nonblocking socket mode support
This commit is contained in:
23
Socket.cpp
23
Socket.cpp
@ -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);
|
||||
|
Reference in New Issue
Block a user