Fix QSocketEngineBackend::nativeBytesAvaible for UDP sockets

On OS X ioctl(... FIONREAD) on UDP socket returns more than expected:

"SO_NREAD returns the amount of data in the input buffer that is available to be received
 For datagram oriented sockets, SO_NREAD returns the size of the first packet -- this dif-
 fers from the ioctl() command FIONREAD that returns the total amount of data available."
(man getsockopt). On OS X bytesAvailable for UDP socket seems to include some headers also:
for a datagram of size 1 - bytesAvailable == 17, 2 - bytesAvailable == 18 etc.
Found in a broken tst_qudpsocket test.

Change-Id: I88be827c66208835ed10b010f13d9dc70576fea4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Timur Pocheptsov 2015-04-08 13:30:42 +02:00 committed by Timur Pocheptsov
parent 81514c19c1
commit 6b285de8ec

View File

@ -745,14 +745,23 @@ qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const
{
int nbytes = 0;
// gives shorter than true amounts on Unix domain sockets.
qint64 available = 0;
if (qt_safe_ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
available = (qint64) nbytes;
qint64 available = -1;
#if defined (SO_NREAD)
if (socketType == QAbstractSocket::UdpSocket) {
socklen_t sz = sizeof nbytes;
if (!::getsockopt(socketDescriptor, SOL_SOCKET, SO_NREAD, &nbytes, &sz))
available = nbytes;
}
#endif
if (available == -1 && qt_safe_ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
available = nbytes;
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeBytesAvailable() == %lli", available);
#endif
return available;
return available > 0 ? available : 0;
}
bool QNativeSocketEnginePrivate::nativeHasPendingDatagrams() const