optimize internal socket creation

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-02-24 03:52:18 +02:00
parent e611701095
commit ca6acd1e24
2 changed files with 13 additions and 8 deletions

View file

@ -481,14 +481,6 @@ bool QAbstractSocketEngine::initialize(QAbstractSocket::SocketType socketType, Q
return false;
}
// Make the socket nonblocking.
if (!setOption(NonBlockingSocketOption, 1)) {
d->setError(QAbstractSocket::UnsupportedSocketOperationError,
QAbstractSocketEnginePrivate::NonBlockingInitFailedErrorString);
close();
return false;
}
// Set the broadcasting flag if it's a UDP socket.
if (socketType == QAbstractSocket::UdpSocket
&& !setOption(BroadcastSocketOption, 1)) {

View file

@ -98,6 +98,10 @@ bool QAbstractSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType s
int protocol = AF_INET;
#endif
int type = (socketType == QAbstractSocket::UdpSocket) ? SOCK_DGRAM : SOCK_STREAM;
#ifdef SOCK_NONBLOCK
// Linux specific
type |= SOCK_NONBLOCK;
#endif
int socket = qt_safe_socket(protocol, type, 0);
@ -124,6 +128,15 @@ bool QAbstractSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType s
return false;
}
#ifndef SOCK_NONBLOCK
// Make the socket nonblocking.
int flags = ::fcntl(socket, F_GETFL, 0);
if (flags == -1 || ::fcntl(socket, F_SETFL, flags | O_NONBLOCK) == -1) {
d->errorOccurred(QAbstractSocket::UnsupportedSocketOperationError, ProtocolUnsupportedErrorString);
return;
}
#endif
socketDescriptor = socket;
return true;
}