summaryrefslogtreecommitdiffstats
path: root/src/network/socket
diff options
context:
space:
mode:
Diffstat (limited to 'src/network/socket')
-rw-r--r--src/network/socket/qabstractsocket.cpp157
-rw-r--r--src/network/socket/qabstractsocket.h2
-rw-r--r--src/network/socket/qabstractsocket_p.h12
-rw-r--r--src/network/socket/qabstractsocketengine_p.h12
-rw-r--r--src/network/socket/qhttpsocketengine.cpp22
-rw-r--r--src/network/socket/qhttpsocketengine_p.h7
-rw-r--r--src/network/socket/qlocalserver.h1
-rw-r--r--src/network/socket/qlocalserver_p.h2
-rw-r--r--src/network/socket/qlocalsocket.h1
-rw-r--r--src/network/socket/qlocalsocket_p.h2
-rw-r--r--src/network/socket/qnativesocketengine.cpp126
-rw-r--r--src/network/socket/qnativesocketengine_p.h28
-rw-r--r--src/network/socket/qnativesocketengine_unix.cpp182
-rw-r--r--src/network/socket/qnativesocketengine_win.cpp59
-rw-r--r--src/network/socket/qnativesocketengine_winrt.cpp18
-rw-r--r--src/network/socket/qnativesocketengine_winrt_p.h2
-rw-r--r--src/network/socket/qnet_unix_p.h1
-rw-r--r--src/network/socket/qsctpserver.cpp250
-rw-r--r--src/network/socket/qsctpserver.h77
-rw-r--r--src/network/socket/qsctpserver_p.h76
-rw-r--r--src/network/socket/qsctpsocket.cpp547
-rw-r--r--src/network/socket/qsctpsocket.h82
-rw-r--r--src/network/socket/qsctpsocket_p.h90
-rw-r--r--src/network/socket/qsocks5socketengine.cpp66
-rw-r--r--src/network/socket/qsocks5socketengine_p.h7
-rw-r--r--src/network/socket/qtcpserver.cpp39
-rw-r--r--src/network/socket/qtcpserver.h4
-rw-r--r--src/network/socket/qtcpserver_p.h9
-rw-r--r--src/network/socket/qtcpsocket.h1
-rw-r--r--src/network/socket/qtcpsocket_p.h7
-rw-r--r--src/network/socket/qudpsocket.cpp126
-rw-r--r--src/network/socket/qudpsocket.h5
-rw-r--r--src/network/socket/socket.pri29
33 files changed, 1751 insertions, 298 deletions
diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp
index 96021f45f4..34a8025cdd 100644
--- a/src/network/socket/qabstractsocket.cpp
+++ b/src/network/socket/qabstractsocket.cpp
@@ -267,7 +267,8 @@
\value TcpSocket TCP
\value UdpSocket UDP
- \value UnknownSocketType Other than TCP and UDP
+ \value SctpSocket SCTP
+ \value UnknownSocketType Other than TCP, UDP and SCTP
\sa QAbstractSocket::socketType()
*/
@@ -563,6 +564,7 @@ QAbstractSocketPrivate::QAbstractSocketPrivate()
cachedSocketDescriptor(-1),
readBufferMaxSize(0),
isBuffered(false),
+ hasPendingData(false),
connectTimer(0),
disconnectTimer(0),
hostLookupId(-1),
@@ -593,6 +595,7 @@ void QAbstractSocketPrivate::resetSocketLayer()
qDebug("QAbstractSocketPrivate::resetSocketLayer()");
#endif
+ hasPendingData = false;
if (socketEngine) {
socketEngine->close();
socketEngine->disconnect();
@@ -624,6 +627,7 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc
QString typeStr;
if (q->socketType() == QAbstractSocket::TcpSocket) typeStr = QLatin1String("TcpSocket");
else if (q->socketType() == QAbstractSocket::UdpSocket) typeStr = QLatin1String("UdpSocket");
+ else if (q->socketType() == QAbstractSocket::SctpSocket) typeStr = QLatin1String("SctpSocket");
else typeStr = QLatin1String("UnknownSocketType");
QString protocolStr;
if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = QLatin1String("IPv4Protocol");
@@ -668,6 +672,12 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc
*/
void QAbstractSocketPrivate::configureCreatedSocket()
{
+#ifndef QT_NO_SCTP
+ Q_Q(QAbstractSocket);
+ // Set single stream mode for unbuffered SCTP socket
+ if (socketEngine && q->socketType() == QAbstractSocket::SctpSocket)
+ socketEngine->setOption(QAbstractSocketEngine::MaxStreamsSocketOption, 1);
+#endif
}
/*! \internal
@@ -683,15 +693,13 @@ bool QAbstractSocketPrivate::canReadNotification()
qDebug("QAbstractSocketPrivate::canReadNotification()");
#endif
- if (!isBuffered)
- socketEngine->setReadNotificationEnabled(false);
-
// If buffered, read data from the socket into the read buffer
if (isBuffered) {
const qint64 oldBufferSize = buffer.size();
// Return if there is no space in the buffer
if (readBufferMaxSize && oldBufferSize >= readBufferMaxSize) {
+ socketEngine->setReadNotificationEnabled(false);
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::canReadNotification() buffer is full");
#endif
@@ -714,27 +722,21 @@ bool QAbstractSocketPrivate::canReadNotification()
// to indicate that the data was discarded.
return !q->isReadable();
}
-
- // If read buffer is full, disable the read socket notifier.
- if (readBufferMaxSize && buffer.size() == readBufferMaxSize) {
+ } else {
+ if (hasPendingData) {
socketEngine->setReadNotificationEnabled(false);
+ return true;
}
+ hasPendingData = true;
}
emitReadyRead();
- // If we were closed as a result of the readyRead() signal,
- // return.
- if (state == QAbstractSocket::UnconnectedState || state == QAbstractSocket::ClosingState) {
#if defined (QABSTRACTSOCKET_DEBUG)
+ // If we were closed as a result of the readyRead() signal.
+ if (state == QAbstractSocket::UnconnectedState || state == QAbstractSocket::ClosingState)
qDebug("QAbstractSocketPrivate::canReadNotification() socket is closing - returning");
#endif
- return true;
- }
-
- // turn the socket engine off if we've reached the buffer size limit
- if (socketEngine && isBuffered)
- socketEngine->setReadNotificationEnabled(readBufferMaxSize == 0 || readBufferMaxSize > q->bytesAvailable());
return true;
}
@@ -776,7 +778,8 @@ void QAbstractSocketPrivate::canCloseNotification()
QMetaObject::invokeMethod(socketEngine, "closeNotification", Qt::QueuedConnection);
}
- } else if (socketType == QAbstractSocket::TcpSocket && socketEngine) {
+ } else if ((socketType == QAbstractSocket::TcpSocket ||
+ socketType == QAbstractSocket::SctpSocket) && socketEngine) {
emitReadyRead();
}
}
@@ -792,12 +795,8 @@ bool QAbstractSocketPrivate::canWriteNotification()
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::canWriteNotification() flushing");
#endif
- bool dataWasWritten = writeToSocket();
- if (socketEngine && writeBuffer.isEmpty() && socketEngine->bytesToWrite() == 0)
- socketEngine->setWriteNotificationEnabled(false);
-
- return dataWasWritten;
+ return writeToSocket();
}
/*! \internal
@@ -837,8 +836,12 @@ bool QAbstractSocketPrivate::writeToSocket()
#endif
// this covers the case when the buffer was empty, but we had to wait for the socket engine to finish
- if (state == QAbstractSocket::ClosingState)
+ if (state == QAbstractSocket::ClosingState) {
q->disconnectFromHost();
+ } else {
+ if (socketEngine)
+ socketEngine->setWriteNotificationEnabled(false);
+ }
return false;
}
@@ -867,17 +870,12 @@ bool QAbstractSocketPrivate::writeToSocket()
if (written > 0) {
// Remove what we wrote so far.
writeBuffer.free(written);
- // Don't emit bytesWritten() recursively.
- if (!emittedBytesWritten) {
- QScopedValueRollback<bool> r(emittedBytesWritten);
- emittedBytesWritten = true;
- emit q->bytesWritten(written);
- }
- emit q->channelBytesWritten(0, written);
+
+ // Emit notifications.
+ emitBytesWritten(written);
}
- if (writeBuffer.isEmpty() && socketEngine && socketEngine->isWriteNotificationEnabled()
- && !socketEngine->bytesToWrite())
+ if (writeBuffer.isEmpty() && socketEngine && !socketEngine->bytesToWrite())
socketEngine->setWriteNotificationEnabled(false);
if (state == QAbstractSocket::ClosingState)
q->disconnectFromHost();
@@ -895,7 +893,7 @@ bool QAbstractSocketPrivate::flush()
{
bool dataWasWritten = false;
- while (!writeBuffer.isEmpty() && writeToSocket())
+ while (!allWriteBuffersEmpty() && writeToSocket())
dataWasWritten = true;
return dataWasWritten;
@@ -918,6 +916,8 @@ void QAbstractSocketPrivate::resolveProxy(const QString &hostname, quint16 port)
QNetworkProxyQuery query(hostname, port, QString(),
socketType == QAbstractSocket::TcpSocket ?
QNetworkProxyQuery::TcpSocket :
+ socketType == QAbstractSocket::SctpSocket ?
+ QNetworkProxyQuery::SctpSocket :
QNetworkProxyQuery::UdpSocket);
proxies = QNetworkProxyFactory::proxyForQuery(query);
}
@@ -932,6 +932,10 @@ void QAbstractSocketPrivate::resolveProxy(const QString &hostname, quint16 port)
(p.capabilities() & QNetworkProxy::TunnelingCapability) == 0)
continue;
+ if (socketType == QAbstractSocket::SctpSocket &&
+ (p.capabilities() & QNetworkProxy::SctpTunnelingCapability) == 0)
+ continue;
+
proxyInUse = p;
return;
}
@@ -1150,12 +1154,10 @@ void QAbstractSocketPrivate::_q_connectToNextAddress()
*/
void QAbstractSocketPrivate::_q_testConnection()
{
- if (socketEngine) {
- if (threadData->hasEventDispatcher()) {
- if (connectTimer)
- connectTimer->stop();
- }
+ if (connectTimer)
+ connectTimer->stop();
+ if (socketEngine) {
if (socketEngine->state() == QAbstractSocket::ConnectedState) {
// Fetch the parameters if our connection is completed;
// otherwise, fall out and try the next address.
@@ -1172,11 +1174,6 @@ void QAbstractSocketPrivate::_q_testConnection()
addresses.clear();
}
- if (threadData->hasEventDispatcher()) {
- if (connectTimer)
- connectTimer->stop();
- }
-
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::_q_testConnection() connection failed,"
" checking for alternative addresses");
@@ -1291,18 +1288,36 @@ bool QAbstractSocketPrivate::readFromSocket()
/*! \internal
- Prevents from the recursive readyRead() emission.
+ Emits readyRead(), protecting against recursion.
*/
-void QAbstractSocketPrivate::emitReadyRead()
+void QAbstractSocketPrivate::emitReadyRead(int channel)
{
Q_Q(QAbstractSocket);
// Only emit readyRead() when not recursing.
- if (!emittedReadyRead) {
+ if (!emittedReadyRead && channel == currentReadChannel) {
QScopedValueRollback<bool> r(emittedReadyRead);
emittedReadyRead = true;
emit q->readyRead();
}
- emit q->channelReadyRead(0);
+ // channelReadyRead() can be emitted recursively - even for the same channel.
+ emit q->channelReadyRead(channel);
+}
+
+/*! \internal
+
+ Emits bytesWritten(), protecting against recursion.
+*/
+void QAbstractSocketPrivate::emitBytesWritten(qint64 bytes, int channel)
+{
+ Q_Q(QAbstractSocket);
+ // Only emit bytesWritten() when not recursing.
+ if (!emittedBytesWritten && channel == currentWriteChannel) {
+ QScopedValueRollback<bool> r(emittedBytesWritten);
+ emittedBytesWritten = true;
+ emit q->bytesWritten(bytes);
+ }
+ // channelBytesWritten() can be emitted recursively - even for the same channel.
+ emit q->channelBytesWritten(channel, bytes);
}
/*! \internal
@@ -1413,8 +1428,8 @@ QAbstractSocket::QAbstractSocket(SocketType socketType,
Q_D(QAbstractSocket);
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocket::QAbstractSocket(%sSocket, QAbstractSocketPrivate == %p, parent == %p)",
- socketType == TcpSocket ? "Tcp" : socketType == UdpSocket
- ? "Udp" : "Unknown", &dd, parent);
+ socketType == TcpSocket ? "Tcp" : socketType == UdpSocket ? "Udp"
+ : socketType == SctpSocket ? "Sctp" : "Unknown", &dd, parent);
#endif
d->socketType = socketType;
}
@@ -1678,9 +1693,9 @@ void QAbstractSocket::connectToHost(const QString &hostName, quint16 port,
#endif
if (openMode & QIODevice::Unbuffered)
- d->isBuffered = false; // Unbuffered QTcpSocket
+ d->isBuffered = false;
else if (!d_func()->isBuffered)
- openMode |= QAbstractSocket::Unbuffered; // QUdpSocket
+ openMode |= QAbstractSocket::Unbuffered;
QIODevice::open(openMode);
d->readChannelCount = d->writeChannelCount = 0;
@@ -2385,11 +2400,6 @@ void QAbstractSocket::abort()
return;
}
#endif
- if (d->connectTimer) {
- d->connectTimer->stop();
- delete d->connectTimer;
- d->connectTimer = 0;
- }
d->abortCalled = true;
close();
@@ -2436,15 +2446,7 @@ bool QAbstractSocket::atEnd() const
// Note! docs copied to QSslSocket::flush()
bool QAbstractSocket::flush()
{
- Q_D(QAbstractSocket);
-#ifndef QT_NO_SSL
- // Manual polymorphism; flush() isn't virtual, but QSslSocket overloads
- // it.
- if (QSslSocket *socket = qobject_cast<QSslSocket *>(this))
- return socket->flush();
-#endif
- Q_CHECK_SOCKETENGINE(false);
- return d->flush();
+ return d_func()->flush();
}
/*! \reimp
@@ -2467,8 +2469,9 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
d->resetSocketLayer();
d->state = QAbstractSocket::UnconnectedState;
- } else if (!d->socketEngine->isReadNotificationEnabled()) {
+ } else {
// Only do this when there was no error
+ d->hasPendingData = false;
d->socketEngine->setReadNotificationEnabled(true);
}
@@ -2528,10 +2531,8 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
qt_prettyDebug(data, qMin((int)size, 32), size).data(),
size, written);
#endif
- if (written >= 0) {
- emit bytesWritten(written);
- emit channelBytesWritten(0, written);
- }
+ if (written >= 0)
+ d->emitBytesWritten(written);
return written;
}
@@ -2731,14 +2732,14 @@ void QAbstractSocket::disconnectFromHost()
}
// Wait for pending data to be written.
- if (d->socketEngine && d->socketEngine->isValid() && (d->writeBuffer.size() > 0
+ if (d->socketEngine && d->socketEngine->isValid() && (!d->allWriteBuffersEmpty()
|| d->socketEngine->bytesToWrite() > 0)) {
// hack: when we are waiting for the socket engine to write bytes (only
// possible when using Socks5 or HTTP socket engine), then close
// anyway after 2 seconds. This is to prevent a timeout on Mac, where we
// sometimes just did not get the write notifier from the underlying
// CFSocket and no progress was made.
- if (d->writeBuffer.size() == 0 && d->socketEngine->bytesToWrite() > 0) {
+ if (d->allWriteBuffersEmpty() && d->socketEngine->bytesToWrite() > 0) {
if (!d->disconnectTimer) {
d->disconnectTimer = new QTimer(this);
connect(d->disconnectTimer, SIGNAL(timeout()), this,
@@ -2827,12 +2828,12 @@ void QAbstractSocket::setReadBufferSize(qint64 size)
if (d->readBufferMaxSize == size)
return;
d->readBufferMaxSize = size;
- if (!d->emittedReadyRead && d->socketEngine) {
- // ensure that the read notification is enabled if we've now got
- // room in the read buffer
- // but only if we're not inside canReadNotification -- that will take care on its own
- if ((size == 0 || d->buffer.size() < size) && d->state == QAbstractSocket::ConnectedState) // Do not change the notifier unless we are connected.
- d->socketEngine->setReadNotificationEnabled(true);
+
+ // Do not change the notifier unless we are connected.
+ if (d->socketEngine && d->state == QAbstractSocket::ConnectedState) {
+ // Ensure that the read notification is enabled if we've now got
+ // room in the read buffer.
+ d->socketEngine->setReadNotificationEnabled(size == 0 || d->buffer.size() < size);
}
}
diff --git a/src/network/socket/qabstractsocket.h b/src/network/socket/qabstractsocket.h
index aabdcd4776..73a8f11537 100644
--- a/src/network/socket/qabstractsocket.h
+++ b/src/network/socket/qabstractsocket.h
@@ -40,6 +40,7 @@
#ifndef QABSTRACTSOCKET_H
#define QABSTRACTSOCKET_H
+#include <QtNetwork/qtnetworkglobal.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qobject.h>
#ifndef QT_NO_DEBUG_STREAM
@@ -63,6 +64,7 @@ public:
enum SocketType {
TcpSocket,
UdpSocket,
+ SctpSocket,
UnknownSocketType = -1
};
Q_ENUM(SocketType)
diff --git a/src/network/socket/qabstractsocket_p.h b/src/network/socket/qabstractsocket_p.h
index b718c21ff5..1578d7bb35 100644
--- a/src/network/socket/qabstractsocket_p.h
+++ b/src/network/socket/qabstractsocket_p.h
@@ -51,6 +51,7 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "QtNetwork/qabstractsocket.h"
#include "QtCore/qbytearray.h"
#include "QtCore/qlist.h"
@@ -85,7 +86,7 @@ public:
virtual bool bind(const QHostAddress &address, quint16 port, QAbstractSocket::BindMode mode);
- bool canReadNotification();
+ virtual bool canReadNotification();
bool canWriteNotification();
void canCloseNotification();
@@ -128,22 +129,23 @@ public:
inline void resolveProxy(quint16 port) { resolveProxy(QString(), port); }
void resetSocketLayer();
- bool flush();
+ virtual bool flush();
bool initSocketLayer(QAbstractSocket::NetworkLayerProtocol protocol);
virtual void configureCreatedSocket();
void startConnectingByName(const QString &host);
void fetchConnectionParameters();
- void setupSocketNotifiers();
bool readFromSocket();
- bool writeToSocket();
- void emitReadyRead();
+ virtual bool writeToSocket();
+ void emitReadyRead(int channel = 0);
+ void emitBytesWritten(qint64 bytes, int channel = 0);
void setError(QAbstractSocket::SocketError errorCode, const QString &errorString);
void setErrorAndEmit(QAbstractSocket::SocketError errorCode, const QString &errorString);
qint64 readBufferMaxSize;
bool isBuffered;
+ bool hasPendingData;
QTimer *connectTimer;
QTimer *disconnectTimer;
diff --git a/src/network/socket/qabstractsocketengine_p.h b/src/network/socket/qabstractsocketengine_p.h
index 0073a8b1f2..0cb519ce90 100644
--- a/src/network/socket/qabstractsocketengine_p.h
+++ b/src/network/socket/qabstractsocketengine_p.h
@@ -52,6 +52,7 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "QtNetwork/qhostaddress.h"
#include "QtNetwork/qabstractsocket.h"
#include "private/qobject_p.h"
@@ -103,7 +104,8 @@ public:
MulticastLoopbackOption,
TypeOfServiceOption,
ReceivePacketInformation,
- ReceiveHopLimit
+ ReceiveHopLimit,
+ MaxStreamsSocketOption
};
enum PacketHeaderOption {
@@ -111,6 +113,8 @@ public:
WantDatagramSender = 0x01,
WantDatagramDestination = 0x02,
WantDatagramHopLimit = 0x04,
+ WantStreamNumber = 0x08,
+ WantEndOfRecord = 0x10,
WantAll = 0xff
};
@@ -146,13 +150,13 @@ public:
virtual bool setMulticastInterface(const QNetworkInterface &iface) = 0;
#endif // QT_NO_NETWORKINTERFACE
- virtual qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header = 0,
- PacketHeaderOptions = WantNone) = 0;
- virtual qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header) = 0;
virtual bool hasPendingDatagrams() const = 0;
virtual qint64 pendingDatagramSize() const = 0;
#endif // QT_NO_UDPSOCKET
+ virtual qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header = 0,
+ PacketHeaderOptions = WantNone) = 0;
+ virtual qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header) = 0;
virtual qint64 bytesToWrite() const = 0;
virtual int option(SocketOption option) const = 0;
diff --git a/src/network/socket/qhttpsocketengine.cpp b/src/network/socket/qhttpsocketengine.cpp
index f9ff958525..899c02fba6 100644
--- a/src/network/socket/qhttpsocketengine.cpp
+++ b/src/network/socket/qhttpsocketengine.cpp
@@ -289,34 +289,34 @@ bool QHttpSocketEngine::setMulticastInterface(const QNetworkInterface &)
}
#endif // QT_NO_NETWORKINTERFACE
-qint64 QHttpSocketEngine::readDatagram(char *, qint64, QIpPacketHeader *, PacketHeaderOptions)
+bool QHttpSocketEngine::hasPendingDatagrams() const
{
qWarning("Operation is not supported");
- setError(QAbstractSocket::UnsupportedSocketOperationError,
- QLatin1String("Unsupported socket operation"));
- return -1;
+ return false;
}
-qint64 QHttpSocketEngine::writeDatagram(const char *, qint64, const QIpPacketHeader &)
+qint64 QHttpSocketEngine::pendingDatagramSize() const
{
qWarning("Operation is not supported");
- setError(QAbstractSocket::UnsupportedSocketOperationError,
- QLatin1String("Unsupported socket operation"));
return -1;
}
+#endif // QT_NO_UDPSOCKET
-bool QHttpSocketEngine::hasPendingDatagrams() const
+qint64 QHttpSocketEngine::readDatagram(char *, qint64, QIpPacketHeader *, PacketHeaderOptions)
{
qWarning("Operation is not supported");
- return false;
+ setError(QAbstractSocket::UnsupportedSocketOperationError,
+ QLatin1String("Unsupported socket operation"));
+ return -1;
}
-qint64 QHttpSocketEngine::pendingDatagramSize() const
+qint64 QHttpSocketEngine::writeDatagram(const char *, qint64, const QIpPacketHeader &)
{
qWarning("Operation is not supported");
+ setError(QAbstractSocket::UnsupportedSocketOperationError,
+ QLatin1String("Unsupported socket operation"));
return -1;
}
-#endif // QT_NO_UDPSOCKET
qint64 QHttpSocketEngine::bytesToWrite() const
{
diff --git a/src/network/socket/qhttpsocketengine_p.h b/src/network/socket/qhttpsocketengine_p.h
index 1cc168afec..07815a7e51 100644
--- a/src/network/socket/qhttpsocketengine_p.h
+++ b/src/network/socket/qhttpsocketengine_p.h
@@ -51,6 +51,7 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "private/qabstractsocketengine_p.h"
#include "qabstractsocket.h"
#include "qnetworkproxy.h"
@@ -111,13 +112,13 @@ public:
bool setMulticastInterface(const QNetworkInterface &iface) Q_DECL_OVERRIDE;
#endif // QT_NO_NETWORKINTERFACE
- qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *,
- PacketHeaderOptions) Q_DECL_OVERRIDE;
- qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) Q_DECL_OVERRIDE;
bool hasPendingDatagrams() const Q_DECL_OVERRIDE;
qint64 pendingDatagramSize() const Q_DECL_OVERRIDE;
#endif // QT_NO_UDPSOCKET
+ qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader *,
+ PacketHeaderOptions) Q_DECL_OVERRIDE;
+ qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) Q_DECL_OVERRIDE;
qint64 bytesToWrite() const Q_DECL_OVERRIDE;
int option(SocketOption option) const Q_DECL_OVERRIDE;
diff --git a/src/network/socket/qlocalserver.h b/src/network/socket/qlocalserver.h
index 52f0813746..786885b6cd 100644
--- a/src/network/socket/qlocalserver.h
+++ b/src/network/socket/qlocalserver.h
@@ -40,6 +40,7 @@
#ifndef QLOCALSERVER_H
#define QLOCALSERVER_H
+#include <QtNetwork/qtnetworkglobal.h>
#include <QtNetwork/qabstractsocket.h>
QT_BEGIN_NAMESPACE
diff --git a/src/network/socket/qlocalserver_p.h b/src/network/socket/qlocalserver_p.h
index 28d2b08902..988140c1a4 100644
--- a/src/network/socket/qlocalserver_p.h
+++ b/src/network/socket/qlocalserver_p.h
@@ -51,6 +51,8 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
+
#ifndef QT_NO_LOCALSERVER
#include "qlocalserver.h"
diff --git a/src/network/socket/qlocalsocket.h b/src/network/socket/qlocalsocket.h
index 0eecab206b..ea074db90e 100644
--- a/src/network/socket/qlocalsocket.h
+++ b/src/network/socket/qlocalsocket.h
@@ -40,6 +40,7 @@
#ifndef QLOCALSOCKET_H
#define QLOCALSOCKET_H
+#include <QtNetwork/qtnetworkglobal.h>
#include <QtCore/qiodevice.h>
#include <QtNetwork/qabstractsocket.h>
diff --git a/src/network/socket/qlocalsocket_p.h b/src/network/socket/qlocalsocket_p.h
index 56f8b590f1..560d74328e 100644
--- a/src/network/socket/qlocalsocket_p.h
+++ b/src/network/socket/qlocalsocket_p.h
@@ -51,6 +51,8 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
+
#ifndef QT_NO_LOCALSOCKET
#include "qlocalsocket.h"
diff --git a/src/network/socket/qnativesocketengine.cpp b/src/network/socket/qnativesocketengine.cpp
index 9ba9d5317e..f2bc3cec94 100644
--- a/src/network/socket/qnativesocketengine.cpp
+++ b/src/network/socket/qnativesocketengine.cpp
@@ -111,7 +111,7 @@
\value WantAll this is a catch-all value to indicate the caller is
interested in all the available information
- \sa readDatagram()
+ \sa readDatagram(), QNetworkDatagram
*/
#include "qnativesocketengine_p.h"
@@ -174,6 +174,12 @@ QT_BEGIN_NAMESPACE
" socket other than "#type""); \
return (returnValue); \
} } while (0)
+#define Q_CHECK_TYPES(function, type1, type2, returnValue) do { \
+ if (d->socketType != (type1) && d->socketType != (type2)) { \
+ qWarning(#function" was called by a" \
+ " socket other than "#type1" or "#type2); \
+ return (returnValue); \
+ } } while (0)
#define Q_TR(a) QT_TRANSLATE_NOOP(QNativeSocketEngine, a)
/*! \internal
@@ -417,6 +423,7 @@ bool QNativeSocketEngine::initialize(QAbstractSocket::SocketType socketType, QAb
QString typeStr = QLatin1String("UnknownSocketType");
if (socketType == QAbstractSocket::TcpSocket) typeStr = QLatin1String("TcpSocket");
else if (socketType == QAbstractSocket::UdpSocket) typeStr = QLatin1String("UdpSocket");
+ else if (socketType == QAbstractSocket::SctpSocket) typeStr = QLatin1String("SctpSocket");
QString protocolStr = QLatin1String("UnknownProtocol");
if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = QLatin1String("IPv4Protocol");
else if (protocol == QAbstractSocket::IPv6Protocol) protocolStr = QLatin1String("IPv6Protocol");
@@ -659,7 +666,12 @@ bool QNativeSocketEngine::listen()
Q_D(QNativeSocketEngine);
Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::listen(), false);
Q_CHECK_STATE(QNativeSocketEngine::listen(), QAbstractSocket::BoundState, false);
+#ifndef QT_NO_SCTP
+ Q_CHECK_TYPES(QNativeSocketEngine::listen(), QAbstractSocket::TcpSocket,
+ QAbstractSocket::SctpSocket, false);
+#else
Q_CHECK_TYPE(QNativeSocketEngine::listen(), QAbstractSocket::TcpSocket, false);
+#endif
// We're using a backlog of 50. Most modern kernels support TCP
// syncookies by default, and if they do, the backlog is ignored.
@@ -680,7 +692,12 @@ int QNativeSocketEngine::accept()
Q_D(QNativeSocketEngine);
Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::accept(), -1);
Q_CHECK_STATE(QNativeSocketEngine::accept(), QAbstractSocket::ListeningState, -1);
+#ifndef QT_NO_SCTP
+ Q_CHECK_TYPES(QNativeSocketEngine::accept(), QAbstractSocket::TcpSocket,
+ QAbstractSocket::SctpSocket, -1);
+#else
Q_CHECK_TYPE(QNativeSocketEngine::accept(), QAbstractSocket::TcpSocket, -1);
+#endif
return d->nativeAccept();
}
@@ -793,6 +810,7 @@ qint64 QNativeSocketEngine::pendingDatagramSize() const
return d->nativePendingDatagramSize();
}
+#endif // QT_NO_UDPSOCKET
/*!
Reads up to \a maxSize bytes of a datagram from the socket,
@@ -800,9 +818,10 @@ qint64 QNativeSocketEngine::pendingDatagramSize() const
address, port, and other IP header fields are stored in \a header
according to the request in \a options.
- To avoid unnecessarily loss of data, call pendingDatagramSize() to
- determine the size of the pending message before reading it. If \a
- maxSize is too small, the rest of the datagram will be lost.
+ For UDP sockets, to avoid unnecessarily loss of data, call
+ pendingDatagramSize() to determine the size of the pending message
+ before reading it. If \a maxSize is too small, the rest of the
+ datagram will be lost.
Returns -1 if an error occurred.
@@ -813,13 +832,14 @@ qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxSize, QIpPacketHe
{
Q_D(QNativeSocketEngine);
Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::readDatagram(), -1);
- Q_CHECK_TYPE(QNativeSocketEngine::readDatagram(), QAbstractSocket::UdpSocket, -1);
+ Q_CHECK_STATES(QNativeSocketEngine::readDatagram(), QAbstractSocket::BoundState,
+ QAbstractSocket::ConnectedState, -1);
return d->nativeReceiveDatagram(data, maxSize, header, options);
}
/*!
- Writes a UDP datagram of size \a size bytes to the socket from
+ Writes a datagram of size \a size bytes to the socket from
\a data to the destination contained in \a header, and returns the
number of bytes written, or -1 if an error occurred. If \a header
contains other settings like hop limit or source address, this function
@@ -844,11 +864,11 @@ qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 size, const Q
{
Q_D(QNativeSocketEngine);
Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::writeDatagram(), -1);
- Q_CHECK_TYPE(QNativeSocketEngine::writeDatagram(), QAbstractSocket::UdpSocket, -1);
+ Q_CHECK_STATES(QNativeSocketEngine::writeDatagram(), QAbstractSocket::BoundState,
+ QAbstractSocket::ConnectedState, -1);
return d->nativeSendDatagram(data, size, header);
}
-#endif // QT_NO_UDPSOCKET
/*!
Writes a block of \a size bytes from \a data to the socket.
@@ -881,7 +901,11 @@ qint64 QNativeSocketEngine::read(char *data, qint64 maxSize)
qint64 readBytes = d->nativeRead(data, maxSize);
// Handle remote close
- if (readBytes == 0 && d->socketType == QAbstractSocket::TcpSocket) {
+ if (readBytes == 0 && (d->socketType == QAbstractSocket::TcpSocket
+#ifndef QT_NO_SCTP
+ || d->socketType == QAbstractSocket::SctpSocket
+#endif
+ )) {
d->setError(QAbstractSocket::RemoteHostClosedError,
QNativeSocketEnginePrivate::RemoteHostClosedErrorString);
close();
@@ -1007,26 +1031,28 @@ bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut)
// select(writable) is successful. In this case we should not
// issue a second call to WSAConnect()
#if defined (Q_OS_WIN)
- if (ret > 0) {
- setState(QAbstractSocket::ConnectedState);
- d_func()->fetchConnectionParameters();
- return true;
- } else {
- int value = 0;
- int valueSize = sizeof(value);
- if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
- if (value == WSAECONNREFUSED) {
- d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString);
- d->socketState = QAbstractSocket::UnconnectedState;
- return false;
- } else if (value == WSAETIMEDOUT) {
- d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString);
- d->socketState = QAbstractSocket::UnconnectedState;
- return false;
- } else if (value == WSAEHOSTUNREACH) {
- d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString);
- d->socketState = QAbstractSocket::UnconnectedState;
- return false;
+ if (state() == QAbstractSocket::ConnectingState) {
+ if (ret > 0) {
+ setState(QAbstractSocket::ConnectedState);
+ d_func()->fetchConnectionParameters();
+ return true;
+ } else {
+ int value = 0;
+ int valueSize = sizeof(value);
+ if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
+ if (value == WSAECONNREFUSED) {
+ d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString);
+ d->socketState = QAbstractSocket::UnconnectedState;
+ return false;
+ } else if (value == WSAETIMEDOUT) {
+ d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString);
+ d->socketState = QAbstractSocket::UnconnectedState;
+ return false;
+ } else if (value == WSAEHOSTUNREACH) {
+ d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString);
+ d->socketState = QAbstractSocket::UnconnectedState;
+ return false;
+ }
}
}
}
@@ -1060,26 +1086,28 @@ bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWri
// select(writable) is successful. In this case we should not
// issue a second call to WSAConnect()
#if defined (Q_OS_WIN)
- if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) {
- setState(QAbstractSocket::ConnectedState);
- d_func()->fetchConnectionParameters();
- return true;
- } else {
- int value = 0;
- int valueSize = sizeof(value);
- if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
- if (value == WSAECONNREFUSED) {
- d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString);
- d->socketState = QAbstractSocket::UnconnectedState;
- return false;
- } else if (value == WSAETIMEDOUT) {
- d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString);
- d->socketState = QAbstractSocket::UnconnectedState;
- return false;
- } else if (value == WSAEHOSTUNREACH) {
- d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString);
- d->socketState = QAbstractSocket::UnconnectedState;
- return false;
+ if (state() == QAbstractSocket::ConnectingState) {
+ if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) {
+ setState(QAbstractSocket::ConnectedState);
+ d_func()->fetchConnectionParameters();
+ return true;
+ } else {
+ int value = 0;
+ int valueSize = sizeof(value);
+ if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
+ if (value == WSAECONNREFUSED) {
+ d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString);
+ d->socketState = QAbstractSocket::UnconnectedState;
+ return false;
+ } else if (value == WSAETIMEDOUT) {
+ d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString);
+ d->socketState = QAbstractSocket::UnconnectedState;
+ return false;
+ } else if (value == WSAEHOSTUNREACH) {
+ d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString);
+ d->socketState = QAbstractSocket::UnconnectedState;
+ return false;
+ }
}
}
}
diff --git a/src/network/socket/qnativesocketengine_p.h b/src/network/socket/qnativesocketengine_p.h
index 19e9e1d9b7..46c7ae5c55 100644
--- a/src/network/socket/qnativesocketengine_p.h
+++ b/src/network/socket/qnativesocketengine_p.h
@@ -45,12 +45,14 @@
// W A R N I N G
// -------------
//
-// This file is not part of the Qt API. It exists for the convenience
-// of the QLibrary class. This header file may change from
-// version to version without notice, or even be removed.
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
//
// We mean it.
//
+
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "QtNetwork/qhostaddress.h"
#include "QtNetwork/qnetworkinterface.h"
#include "private/qabstractsocketengine_p.h"
@@ -66,21 +68,20 @@
QT_BEGIN_NAMESPACE
#ifdef Q_OS_WIN
-#define QT_SOCKLEN_T int
-#define QT_SOCKOPTLEN_T int
+# define QT_SOCKLEN_T int
+# define QT_SOCKOPTLEN_T int
// The following definitions are copied from the MinGW header mswsock.h which
// was placed in the public domain. The WSASendMsg and WSARecvMsg functions
// were introduced with Windows Vista, so some Win32 headers are lacking them.
// There are no known versions of Windows CE or Embedded that contain them.
-#ifndef Q_OS_WINCE
# ifndef WSAID_WSARECVMSG
typedef INT (WINAPI *LPFN_WSARECVMSG)(SOCKET s, LPWSAMSG lpMsg,
LPDWORD lpdwNumberOfBytesRecvd,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
# define WSAID_WSARECVMSG {0xf689d7c8,0x6f1f,0x436b,{0x8a,0x53,0xe5,0x4f,0xe3,0x51,0xc3,0x22}}
-# endif
+# endif // !WSAID_WSARECVMSG
# ifndef WSAID_WSASENDMSG
typedef struct {
LPWSAMSG lpMsg;
@@ -96,9 +97,8 @@ typedef INT (WSAAPI *LPFN_WSASENDMSG)(SOCKET s, LPWSAMSG lpMsg, DWORD dwFlags,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
# define WSAID_WSASENDMSG {0xa441e712,0x754f,0x43ca,{0x84,0xa7,0x0d,0xee,0x44,0xcf,0x60,0x6d}}
-# endif
-#endif
-#endif
+# endif // !WSAID_WSASENDMSG
+#endif // Q_OS_WIN
union qt_sockaddr {
sockaddr a;
@@ -157,13 +157,13 @@ public:
bool setMulticastInterface(const QNetworkInterface &iface) Q_DECL_OVERRIDE;
#endif
- qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader * = 0,
- PacketHeaderOptions = WantNone) Q_DECL_OVERRIDE;
- qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) Q_DECL_OVERRIDE;
bool hasPendingDatagrams() const Q_DECL_OVERRIDE;
qint64 pendingDatagramSize() const Q_DECL_OVERRIDE;
#endif // QT_NO_UDPSOCKET
+ qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader * = 0,
+ PacketHeaderOptions = WantNone) Q_DECL_OVERRIDE;
+ qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) Q_DECL_OVERRIDE;
qint64 bytesToWrite() const Q_DECL_OVERRIDE;
qint64 receiveBufferSize() const;
@@ -210,7 +210,7 @@ public:
QSocketNotifier *readNotifier, *writeNotifier, *exceptNotifier;
-#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
+#if defined(Q_OS_WIN)
LPFN_WSASENDMSG sendmsg;
LPFN_WSARECVMSG recvmsg;
# endif
diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp
index 2d50cc1f8d..d1efc21e09 100644
--- a/src/network/socket/qnativesocketengine_unix.cpp
+++ b/src/network/socket/qnativesocketengine_unix.cpp
@@ -68,6 +68,11 @@
#endif
#include <netinet/tcp.h>
+#ifndef QT_NO_SCTP
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/sctp.h>
+#endif
QT_BEGIN_NAMESPACE
@@ -142,6 +147,7 @@ static void convertToLevelAndOption(QNativeSocketEngine::SocketOption opt,
switch (opt) {
case QNativeSocketEngine::NonBlockingSocketOption: // fcntl, not setsockopt
case QNativeSocketEngine::BindExclusively: // not handled on Unix
+ case QNativeSocketEngine::MaxStreamsSocketOption:
Q_UNREACHABLE();
case QNativeSocketEngine::BroadcastSocketOption:
@@ -229,13 +235,28 @@ static void convertToLevelAndOption(QNativeSocketEngine::SocketOption opt,
bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType socketType,
QAbstractSocket::NetworkLayerProtocol &socketProtocol)
{
- int protocol = (socketProtocol == QAbstractSocket::IPv6Protocol || socketProtocol == QAbstractSocket::AnyIPProtocol) ? AF_INET6 : AF_INET;
+#ifndef QT_NO_SCTP
+ int protocol = (socketType == QAbstractSocket::SctpSocket) ? IPPROTO_SCTP : 0;
+#else
+ if (socketType == QAbstractSocket::SctpSocket) {
+ setError(QAbstractSocket::UnsupportedSocketOperationError,
+ ProtocolUnsupportedErrorString);
+#if defined (QNATIVESOCKETENGINE_DEBUG)
+ qDebug("QNativeSocketEnginePrivate::createNewSocket(%d, %d): unsupported protocol",
+ socketType, socketProtocol);
+#endif
+ return false;
+ }
+ int protocol = 0;
+#endif // QT_NO_SCTP
+ int domain = (socketProtocol == QAbstractSocket::IPv6Protocol
+ || socketProtocol == QAbstractSocket::AnyIPProtocol) ? AF_INET6 : AF_INET;
int type = (socketType == QAbstractSocket::UdpSocket) ? SOCK_DGRAM : SOCK_STREAM;
- int socket = qt_safe_socket(protocol, type, 0, O_NONBLOCK);
+ int socket = qt_safe_socket(domain, type, protocol, O_NONBLOCK);
if (socket < 0 && socketProtocol == QAbstractSocket::AnyIPProtocol && errno == EAFNOSUPPORT) {
- protocol = AF_INET;
- socket = qt_safe_socket(protocol, type, 0, O_NONBLOCK);
+ domain = AF_INET;
+ socket = qt_safe_socket(domain, type, protocol, O_NONBLOCK);
socketProtocol = QAbstractSocket::IPv4Protocol;
}
@@ -291,10 +312,26 @@ int QNativeSocketEnginePrivate::option(QNativeSocketEngine::SocketOption opt) co
if (!q->isValid())
return -1;
- // handle non-getsockopt cases first
- if (opt == QNativeSocketEngine::BindExclusively || opt == QNativeSocketEngine::NonBlockingSocketOption
- || opt == QNativeSocketEngine::BroadcastSocketOption)
+ // handle non-getsockopt and specific cases first
+ switch (opt) {
+ case QNativeSocketEngine::BindExclusively:
+ case QNativeSocketEngine::NonBlockingSocketOption:
+ case QNativeSocketEngine::BroadcastSocketOption:
+ return -1;
+ case QNativeSocketEngine::MaxStreamsSocketOption: {
+#ifndef QT_NO_SCTP
+ sctp_initmsg sctpInitMsg;
+ QT_SOCKOPTLEN_T sctpInitMsgSize = sizeof(sctpInitMsg);
+ if (::getsockopt(socketDescriptor, SOL_SCTP, SCTP_INITMSG, &sctpInitMsg,
+ &sctpInitMsgSize) == 0)
+ return int(qMin(sctpInitMsg.sinit_num_ostreams, sctpInitMsg.sinit_max_instreams));
+#endif
return -1;
+ }
+
+ default:
+ break;
+ }
int n, level;
int v = -1;
@@ -317,7 +354,7 @@ bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt
if (!q->isValid())
return false;
- // handle non-setsockopt cases first
+ // handle non-setsockopt and specific cases first
switch (opt) {
case QNativeSocketEngine::NonBlockingSocketOption: {
// Make the socket nonblocking.
@@ -351,6 +388,20 @@ bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt
case QNativeSocketEngine::BindExclusively:
return true;
+ case QNativeSocketEngine::MaxStreamsSocketOption: {
+#ifndef QT_NO_SCTP
+ sctp_initmsg sctpInitMsg;
+ QT_SOCKOPTLEN_T sctpInitMsgSize = sizeof(sctpInitMsg);
+ if (::getsockopt(socketDescriptor, SOL_SCTP, SCTP_INITMSG, &sctpInitMsg,
+ &sctpInitMsgSize) == 0) {
+ sctpInitMsg.sinit_num_ostreams = sctpInitMsg.sinit_max_instreams = uint16_t(v);
+ return ::setsockopt(socketDescriptor, SOL_SCTP, SCTP_INITMSG, &sctpInitMsg,
+ sctpInitMsgSize) == 0;
+ }
+#endif
+ return false;
+ }
+
default:
break;
}
@@ -830,6 +881,9 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxS
#if !defined(IP_PKTINFO) && defined(IP_RECVIF) && defined(Q_OS_BSD4)
+ CMSG_SPACE(sizeof(sockaddr_dl))
#endif
+#ifndef QT_NO_SCTP
+ + CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))
+#endif
+ sizeof(quintptr) - 1) / sizeof(quintptr)];
struct msghdr msg;
@@ -848,7 +902,8 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxS
msg.msg_name = &aa;
msg.msg_namelen = sizeof(aa);
}
- if (options & (QAbstractSocketEngine::WantDatagramHopLimit | QAbstractSocketEngine::WantDatagramDestination)) {
+ if (options & (QAbstractSocketEngine::WantDatagramHopLimit | QAbstractSocketEngine::WantDatagramDestination
+ | QAbstractSocketEngine::WantStreamNumber)) {
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
}
@@ -859,13 +914,27 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxS
} while (recvResult == -1 && errno == EINTR);
if (recvResult == -1) {
- setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString);
+ switch (errno) {
+#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
+#endif
+ case EAGAIN:
+ // No datagram was available for reading
+ recvResult = -2;
+ break;
+ case ECONNREFUSED:
+ setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString);
+ break;
+ default:
+ setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString);
+ }
if (header)
header->clear();
} else if (options != QAbstractSocketEngine::WantNone) {
Q_ASSERT(header);
qt_socket_getPortAndAddress(&aa, &header->senderPort, &header->senderAddress);
header->destinationPort = localPort;
+ header->endOfRecord = (msg.msg_flags & MSG_EOR) != 0;
// parse the ancillary data
struct cmsghdr *cmsgptr;
@@ -912,6 +981,15 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxS
|| (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_TTL))) {
header->hopLimit = *reinterpret_cast<int *>(CMSG_DATA(cmsgptr));
}
+
+#ifndef QT_NO_SCTP
+ if (cmsgptr->cmsg_level == IPPROTO_SCTP && cmsgptr->cmsg_type == SCTP_SNDRCV
+ && cmsgptr->cmsg_len >= CMSG_LEN(sizeof(sctp_sndrcvinfo))) {
+ sctp_sndrcvinfo *rcvInfo = reinterpret_cast<sctp_sndrcvinfo *>(CMSG_DATA(cmsgptr));
+
+ header->streamNumber = int(rcvInfo->sinfo_stream);
+ }
+#endif
}
}
@@ -924,13 +1002,17 @@ qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxS
? header->senderPort : 0, (qint64) recvResult);
#endif
- return qint64(maxSize ? recvResult : recvResult == -1 ? -1 : 0);
+ return qint64((maxSize || recvResult < 0) ? recvResult : Q_INT64_C(0));
}
qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 len, const QIpPacketHeader &header)
{
// we use quintptr to force the alignment
- quintptr cbuf[(CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + sizeof(quintptr) - 1) / sizeof(quintptr)];
+ quintptr cbuf[(CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int))
+#ifndef QT_NO_SCTP
+ + CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))
+#endif
+ + sizeof(quintptr) - 1) / sizeof(quintptr)];
struct cmsghdr *cmsgptr = reinterpret_cast<struct cmsghdr *>(cbuf);
struct msghdr msg;
@@ -943,10 +1025,13 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l
vec.iov_len = len;
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
- msg.msg_name = &aa.a;
msg.msg_control = &cbuf;
- setPortAndAddress(header.destinationPort, header.destinationAddress, &aa, &msg.msg_namelen);
+ if (header.destinationPort != 0) {
+ msg.msg_name = &aa.a;
+ setPortAndAddress(header.destinationPort, header.destinationAddress,
+ &aa, &msg.msg_namelen);
+ }
if (msg.msg_namelen == sizeof(aa.a6)) {
if (header.hopLimit != -1) {
@@ -1001,15 +1086,37 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l
#endif
}
+#ifndef QT_NO_SCTP
+ if (header.streamNumber != -1) {
+ struct sctp_sndrcvinfo *data = reinterpret_cast<sctp_sndrcvinfo *>(CMSG_DATA(cmsgptr));
+ memset(data, 0, sizeof(*data));
+ msg.msg_controllen += CMSG_SPACE(sizeof(sctp_sndrcvinfo));
+ cmsgptr->cmsg_len = CMSG_LEN(sizeof(sctp_sndrcvinfo));
+ cmsgptr->cmsg_level = IPPROTO_SCTP;
+ cmsgptr->cmsg_type = SCTP_SNDRCV;
+ data->sinfo_stream = uint16_t(header.streamNumber);
+ cmsgptr = reinterpret_cast<cmsghdr *>(reinterpret_cast<char *>(cmsgptr) + CMSG_SPACE(sizeof(*data)));
+ }
+#endif
+
if (msg.msg_controllen == 0)
msg.msg_control = 0;
ssize_t sentBytes = qt_safe_sendmsg(socketDescriptor, &msg, 0);
if (sentBytes < 0) {
switch (errno) {
+#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
+#endif
+ case EAGAIN:
+ sentBytes = -2;
+ break;
case EMSGSIZE:
setError(QAbstractSocket::DatagramTooLargeError, DatagramTooLargeErrorString);
break;
+ case ECONNRESET:
+ setError(QAbstractSocket::RemoteHostClosedError, RemoteHostClosedErrorString);
+ break;
default:
setError(QAbstractSocket::NetworkError, SendDatagramErrorString);
}
@@ -1082,21 +1189,51 @@ bool QNativeSocketEnginePrivate::fetchConnectionParameters()
#endif
// Determine the remote address
- if (!::getpeername(socketDescriptor, &sa.a, &sockAddrSize)) {
+ bool connected = ::getpeername(socketDescriptor, &sa.a, &sockAddrSize) == 0;
+ if (connected) {
qt_socket_getPortAndAddress(&sa, &peerPort, &peerAddress);
inboundStreamCount = outboundStreamCount = 1;
}
- // Determine the socket type (UDP/TCP)
+ // Determine the socket type (UDP/TCP/SCTP)
int value = 0;
QT_SOCKOPTLEN_T valueSize = sizeof(int);
if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, &value, &valueSize) == 0) {
- if (value == SOCK_STREAM)
- socketType = QAbstractSocket::TcpSocket;
- else if (value == SOCK_DGRAM)
- socketType = QAbstractSocket::UdpSocket;
- else
- socketType = QAbstractSocket::UnknownSocketType;
+ if (value == SOCK_STREAM) {
+#ifndef QT_NO_SCTP
+ if (option(QNativeSocketEngine::MaxStreamsSocketOption) != -1) {
+ socketType = QAbstractSocket::SctpSocket;
+ if (connected) {
+ sctp_status sctpStatus;
+ QT_SOCKOPTLEN_T sctpStatusSize = sizeof(sctpStatus);
+ sctp_event_subscribe sctpEvents;
+
+ memset(&sctpEvents, 0, sizeof(sctpEvents));
+ sctpEvents.sctp_data_io_event = 1;
+ if (::getsockopt(socketDescriptor, SOL_SCTP, SCTP_STATUS, &sctpStatus,
+ &sctpStatusSize) == 0 &&
+ ::setsockopt(socketDescriptor, SOL_SCTP, SCTP_EVENTS, &sctpEvents,
+ sizeof(sctpEvents)) == 0) {
+ inboundStreamCount = int(sctpStatus.sstat_instrms);
+ outboundStreamCount = int(sctpStatus.sstat_outstrms);
+ } else {
+ setError(QAbstractSocket::UnsupportedSocketOperationError,
+ InvalidSocketErrorString);
+ return false;
+ }
+ }
+ } else {
+ socketType = QAbstractSocket::TcpSocket;
+ }
+#else
+ socketType = QAbstractSocket::TcpSocket;
+#endif
+ } else {
+ if (value == SOCK_DGRAM)
+ socketType = QAbstractSocket::UdpSocket;
+ else
+ socketType = QAbstractSocket::UnknownSocketType;
+ }
}
#if defined (QNATIVESOCKETENGINE_DEBUG)
QString socketProtocolStr = QStringLiteral("UnknownProtocol");
@@ -1106,6 +1243,7 @@ bool QNativeSocketEnginePrivate::fetchConnectionParameters()
QString socketTypeStr = QStringLiteral("UnknownSocketType");
if (socketType == QAbstractSocket::TcpSocket) socketTypeStr = QStringLiteral("TcpSocket");
else if (socketType == QAbstractSocket::UdpSocket) socketTypeStr = QStringLiteral("UdpSocket");
+ else if (socketType == QAbstractSocket::SctpSocket) socketTypeStr = QStringLiteral("SctpSocket");
qDebug("QNativeSocketEnginePrivate::fetchConnectionParameters() local == %s:%i,"
" peer == %s:%i, socket == %s - %s, inboundStreamCount == %i, outboundStreamCount == %i",
diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp
index 5ffe7b11b8..5a9641a9fe 100644
--- a/src/network/socket/qnativesocketengine_win.cpp
+++ b/src/network/socket/qnativesocketengine_win.cpp
@@ -214,6 +214,7 @@ static void convertToLevelAndOption(QNativeSocketEngine::SocketOption opt,
switch (opt) {
case QNativeSocketEngine::NonBlockingSocketOption: // WSAIoctl
case QNativeSocketEngine::TypeOfServiceOption: // not supported
+ case QNativeSocketEngine::MaxStreamsSocketOption:
Q_UNREACHABLE();
case QNativeSocketEngine::ReceiveBufferSocketOption:
@@ -325,6 +326,14 @@ bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType soc
return -1;
}
*/
+
+ //### SCTP not implemented
+ if (socketType == QAbstractSocket::SctpSocket) {
+ setError(QAbstractSocket::UnsupportedSocketOperationError,
+ ProtocolUnsupportedErrorString);
+ return false;
+ }
+
QSysInfo::WinVersion osver = QSysInfo::windowsVersion();
//Windows XP and 2003 support IPv6 but not dual stack sockets
@@ -387,7 +396,6 @@ bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType soc
return false;
}
-#if !defined(Q_OS_WINCE)
if (socketType == QAbstractSocket::UdpSocket) {
// enable new behavior using
// SIO_UDP_CONNRESET
@@ -414,7 +422,6 @@ bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType soc
&sendmsgguid, sizeof(sendmsgguid),
&sendmsg, sizeof(sendmsg), &bytesReturned, NULL, NULL) == SOCKET_ERROR)
sendmsg = 0;
-#endif
socketDescriptor = socket;
if (socket != INVALID_SOCKET) {
@@ -453,6 +460,7 @@ int QNativeSocketEnginePrivate::option(QNativeSocketEngine::SocketOption opt) co
break;
}
case QNativeSocketEngine::TypeOfServiceOption:
+ case QNativeSocketEngine::MaxStreamsSocketOption:
return -1;
default:
@@ -503,6 +511,7 @@ bool QNativeSocketEnginePrivate::setOption(QNativeSocketEngine::SocketOption opt
break;
}
case QNativeSocketEngine::TypeOfServiceOption:
+ case QNativeSocketEngine::MaxStreamsSocketOption:
return false;
default:
@@ -703,7 +712,7 @@ bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &address, quin
if (errorDetected)
break;
- // fall through
+ Q_FALLTHROUGH();
}
case WSAEINPROGRESS:
setError(QAbstractSocket::UnfinishedSocketOperationError, InvalidSocketErrorString);
@@ -1091,7 +1100,6 @@ qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const
bool QNativeSocketEnginePrivate::nativeHasPendingDatagrams() const
{
-#if !defined(Q_OS_WINCE)
// Create a sockaddr struct and reset its port number.
qt_sockaddr storage;
QT_SOCKLEN_T storageSize = sizeof(storage);
@@ -1118,18 +1126,6 @@ bool QNativeSocketEnginePrivate::nativeHasPendingDatagrams() const
result = true;
}
-#else // Q_OS_WINCE
- bool result = false;
- fd_set readS;
- FD_ZERO(&readS);
- FD_SET((SOCKET)socketDescriptor, &readS);
- timeval timeout;
- timeout.tv_sec = 0;
- timeout.tv_usec = 5000;
- int available = ::select(1, &readS, 0, 0, &timeout);
- result = available > 0;
-#endif
-
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativeHasPendingDatagrams() == %s",
result ? "true" : "false");
@@ -1141,7 +1137,6 @@ bool QNativeSocketEnginePrivate::nativeHasPendingDatagrams() const
qint64 QNativeSocketEnginePrivate::nativePendingDatagramSize() const
{
qint64 ret = -1;
-#if !defined(Q_OS_WINCE)
int recvResult = 0;
DWORD flags;
DWORD bufferCount = 5;
@@ -1186,18 +1181,6 @@ qint64 QNativeSocketEnginePrivate::nativePendingDatagramSize() const
if (buf)
delete[] buf;
-#else // Q_OS_WINCE
- DWORD size = -1;
- DWORD bytesReturned;
- int ioResult = WSAIoctl(socketDescriptor, FIONREAD, 0,0, &size, sizeof(size), &bytesReturned, 0, 0);
- if (ioResult == SOCKET_ERROR) {
- int err = WSAGetLastError();
- WS_ERROR_DEBUG(err);
- } else {
- ret = qint64(size);
- }
-#endif
-
#if defined (QNATIVESOCKETENGINE_DEBUG)
qDebug("QNativeSocketEnginePrivate::nativePendingDatagramSize() == %lli", ret);
#endif
@@ -1205,12 +1188,6 @@ qint64 QNativeSocketEnginePrivate::nativePendingDatagramSize() const
return ret;
}
-#ifdef Q_OS_WINCE
-// Windows CE has no support for sendmsg or recvmsg. We set it to null here to simplify the code below.
-static int (*const recvmsg)(...) = 0;
-static int (*const sendmsg)(...) = 0;
-#endif
-
qint64 QNativeSocketEnginePrivate::nativeReceiveDatagram(char *data, qint64 maxLength, QIpPacketHeader *header,
QAbstractSocketEngine::PacketHeaderOptions options)
{
@@ -1330,12 +1307,7 @@ qint64 QNativeSocketEnginePrivate::nativeSendDatagram(const char *data, qint64 l
memset(&msg, 0, sizeof(msg));
memset(&aa, 0, sizeof(aa));
-#if !defined(Q_OS_WINCE)
buf.buf = len ? (char*)data : 0;
-#else
- char tmp;
- buf.buf = len ? (char*)data : &tmp;
-#endif
msg.lpBuffers = &buf;
msg.dwBufferCount = 1;
msg.name = &aa.a;
@@ -1497,9 +1469,6 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxLength)
buf.len = maxLength;
DWORD flags = 0;
DWORD bytesRead = 0;
-#if defined(Q_OS_WINCE)
- WSASetLastError(0);
-#endif
if (::WSARecv(socketDescriptor, &buf, 1, &bytesRead, &flags, 0,0) == SOCKET_ERROR) {
int err = WSAGetLastError();
WS_ERROR_DEBUG(err);
@@ -1613,11 +1582,7 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout,
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
-#if !defined(Q_OS_WINCE)
ret = select(socketDescriptor + 1, &fdread, &fdwrite, &fdexception, timeout < 0 ? 0 : &tv);
-#else
- ret = select(1, &fdread, &fdwrite, &fdexception, timeout < 0 ? 0 : &tv);
-#endif
//... but if it is actually set, pretend it did not happen
if (ret > 0 && FD_ISSET((SOCKET)socketDescriptor, &fdexception))
diff --git a/src/network/socket/qnativesocketengine_winrt.cpp b/src/network/socket/qnativesocketengine_winrt.cpp
index 9817d4c57d..6b71912838 100644
--- a/src/network/socket/qnativesocketengine_winrt.cpp
+++ b/src/network/socket/qnativesocketengine_winrt.cpp
@@ -632,6 +632,7 @@ qint64 QNativeSocketEngine::write(const char *data, qint64 len)
qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header,
PacketHeaderOptions)
{
+#ifndef QT_NO_UDPSOCKET
Q_D(QNativeSocketEngine);
if (d->socketType != QAbstractSocket::UdpSocket || d->pendingDatagrams.isEmpty()) {
if (header)
@@ -654,10 +655,17 @@ qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHea
}
memcpy(data, readOrigin, qMin(maxlen, qint64(datagram.data.length())));
return readOrigin.length();
+#else
+ Q_UNUSED(data)
+ Q_UNUSED(maxlen)
+ Q_UNUSED(header)
+ return -1;
+#endif // QT_NO_UDPSOCKET
}
qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header)
{
+#ifndef QT_NO_UDPSOCKET
Q_D(QNativeSocketEngine);
if (d->socketType != QAbstractSocket::UdpSocket)
return -1;
@@ -684,6 +692,12 @@ qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 len, const QI
Q_ASSERT_SUCCEEDED(hr);
return writeIOStream(stream, data, len);
+#else
+ Q_UNUSED(data)
+ Q_UNUSED(len)
+ Q_UNUSED(header)
+ return -1;
+#endif // QT_NO_UDPSOCKET
}
bool QNativeSocketEngine::hasPendingDatagrams() const
@@ -1088,6 +1102,7 @@ int QNativeSocketEnginePrivate::option(QAbstractSocketEngine::SocketOption opt)
case QAbstractSocketEngine::MulticastTtlOption:
case QAbstractSocketEngine::MulticastLoopbackOption:
case QAbstractSocketEngine::TypeOfServiceOption:
+ case QAbstractSocketEngine::MaxStreamsSocketOption:
default:
return -1;
}
@@ -1146,6 +1161,7 @@ bool QNativeSocketEnginePrivate::setOption(QAbstractSocketEngine::SocketOption o
case QAbstractSocketEngine::MulticastTtlOption:
case QAbstractSocketEngine::MulticastLoopbackOption:
case QAbstractSocketEngine::TypeOfServiceOption:
+ case QAbstractSocketEngine::MaxStreamsSocketOption:
default:
return false;
}
@@ -1290,10 +1306,12 @@ HRESULT QNativeSocketEnginePrivate::handleConnectOpFinished(IAsyncAction *action
if (socketType != QAbstractSocket::TcpSocket)
return S_OK;
+#ifndef QT_NO_SSL
// Delay the reader so that the SSL socket can upgrade
if (sslSocket)
QObject::connect(qobject_cast<QSslSocket *>(sslSocket), &QSslSocket::encrypted, q, &QNativeSocketEngine::establishRead);
else
+#endif
q->establishRead();
return S_OK;
}
diff --git a/src/network/socket/qnativesocketengine_winrt_p.h b/src/network/socket/qnativesocketengine_winrt_p.h
index 5b76c2d223..ef219e61df 100644
--- a/src/network/socket/qnativesocketengine_winrt_p.h
+++ b/src/network/socket/qnativesocketengine_winrt_p.h
@@ -50,6 +50,8 @@
//
// We mean it.
//
+
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include <QtCore/QEventLoop>
#include <QtCore/QBuffer>
#include <QtCore/QMutex>
diff --git a/src/network/socket/qnet_unix_p.h b/src/network/socket/qnet_unix_p.h
index 8d2a4ae852..46fbc9757a 100644
--- a/src/network/socket/qnet_unix_p.h
+++ b/src/network/socket/qnet_unix_p.h
@@ -51,6 +51,7 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "private/qcore_unix_p.h"
#include <sys/types.h>
diff --git a/src/network/socket/qsctpserver.cpp b/src/network/socket/qsctpserver.cpp
new file mode 100644
index 0000000000..24f18e1ee8
--- /dev/null
+++ b/src/network/socket/qsctpserver.cpp
@@ -0,0 +1,250 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+//#define QSCTPSERVER_DEBUG
+
+/*!
+ \class QSctpServer
+ \since 5.8
+
+ \brief The QSctpServer class provides an SCTP-based server.
+
+ \ingroup network
+ \inmodule QtNetwork
+
+ SCTP (Stream Control Transmission Protocol) is a transport layer
+ protocol serving in a similar role as the popular protocols TCP
+ and UDP. Like UDP, SCTP is message-oriented, but it ensures reliable,
+ in-sequence transport of messages with congestion control like
+ TCP. See the QSctpSocket documentation for more protocol details.
+
+ QSctpServer is a convenience subclass of QTcpServer that allows
+ you to accept incoming SCTP socket connections either in TCP
+ emulation or in datagram mode.
+
+ The most common way to use QSctpServer is to construct an object
+ and set the maximum number of channels that the server is
+ prepared to support, by calling setMaxChannelCount(). You can set
+ the TCP emulation mode by passing a negative argument in this
+ call. Also, a special value of 0 (the default) indicates to use
+ the peer's value as the actual number of channels. The new incoming
+ connection inherits this number from the server socket descriptor
+ and adjusts it according to the remote endpoint settings.
+
+ In TCP emulation mode, accepted clients use a single continuous
+ byte stream for data transmission, and QSctpServer acts like a
+ plain QTcpServer. Call nextPendingConnection() to accept the
+ pending connection as a connected QTcpSocket. The function returns
+ a pointer to a QTcpSocket in QAbstractSocket::ConnectedState that
+ you can use for communicating with the client. This mode gives
+ access only to basic SCTP protocol features. The socket transmits SCTP
+ packets over IP at system level and interacts through the
+ QTcpSocket interface with the application.
+
+ In contrast, datagram mode is message-oriented and provides a
+ complete simultaneous transmission of multiple data streams
+ between endpoints. Call nextPendingDatagramConnection() to accept
+ the pending datagram-mode connection as a connected QSctpSocket.
+
+ \note This feature is not supported on the Windows platform.
+
+ \sa QTcpServer, QSctpSocket, QAbstractSocket
+*/
+
+#include "qsctpserver.h"
+#include "qsctpserver_p.h"
+
+#include "qsctpsocket.h"
+#include "qabstractsocketengine_p.h"
+
+#ifdef QSCTPSERVER_DEBUG
+#include <qdebug.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+/*! \internal
+*/
+QSctpServerPrivate::QSctpServerPrivate()
+ : maxChannelCount(0)
+{
+}
+
+/*! \internal
+*/
+QSctpServerPrivate::~QSctpServerPrivate()
+{
+}
+
+/*! \internal
+*/
+void QSctpServerPrivate::configureCreatedSocket()
+{
+ QTcpServerPrivate::configureCreatedSocket();
+ if (socketEngine)
+ socketEngine->setOption(QAbstractSocketEngine::MaxStreamsSocketOption,
+ maxChannelCount == -1 ? 1 : maxChannelCount);
+}
+
+/*!
+ Constructs a QSctpServer object.
+
+ Sets the datagram operation mode. The \a parent argument is passed
+ to QObject's constructor.
+
+ \sa setMaxChannelCount(), listen(), setSocketDescriptor()
+*/
+QSctpServer::QSctpServer(QObject *parent)
+ : QTcpServer(QAbstractSocket::SctpSocket, *new QSctpServerPrivate, parent)
+{
+#if defined(QSCTPSERVER_DEBUG)
+ qDebug("QSctpServer::QSctpServer()");
+#endif
+}
+
+/*!
+ Destroys the QSctpServer object. If the server is listening for
+ connections, the socket is automatically closed.
+
+ \sa close()
+*/
+QSctpServer::~QSctpServer()
+{
+#if defined(QSCTPSERVER_DEBUG)
+ qDebug("QSctpServer::~QSctpServer()");
+#endif
+}
+
+/*!
+ Sets the maximum number of channels that the server is prepared to
+ support in datagram mode, to \a count. If \a count is 0, endpoint
+ maximum number of channels value would be used. Negative \a count
+ sets a TCP emulation mode.
+
+ Call this method only when QSctpServer is in UnconnectedState.
+
+ \sa maxChannelCount(), QSctpSocket
+*/
+void QSctpServer::setMaxChannelCount(int count)
+{
+ Q_D(QSctpServer);
+ if (d->state != QAbstractSocket::UnconnectedState) {
+ qWarning("QSctpServer::setMaxChannelCount() is only allowed in UnconnectedState");
+ return;
+ }
+#if defined(QSCTPSERVER_DEBUG)
+ qDebug("QSctpServer::setMaxChannelCount(%i)", count);
+#endif
+ d->maxChannelCount = count;
+}
+
+/*!
+ Returns the maximum number of channels that the accepted sockets are
+ able to support.
+
+ A value of 0 (the default) means that the number of connection
+ channels would be set by the remote endpoint.
+
+ Returns -1, if QSctpServer running in TCP emulation mode.
+
+ \sa setMaxChannelCount()
+*/
+int QSctpServer::maxChannelCount() const
+{
+ return d_func()->maxChannelCount;
+}
+
+/*! \reimp
+*/
+void QSctpServer::incomingConnection(qintptr socketDescriptor)
+{
+#if defined (QSCTPSERVER_DEBUG)
+ qDebug("QSctpServer::incomingConnection(%i)", socketDescriptor);
+#endif
+
+ QSctpSocket *socket = new QSctpSocket(this);
+ socket->setMaxChannelCount(d_func()->maxChannelCount);
+ socket->setSocketDescriptor(socketDescriptor);
+ addPendingConnection(socket);
+}
+
+/*!
+ Returns the next pending datagram-mode connection as a connected
+ QSctpSocket object.
+
+ Datagram-mode connection provides a message-oriented, multi-stream
+ communication.
+
+ The socket is created as a child of the server, which means that
+ it is automatically deleted when the QSctpServer object is
+ destroyed. It is still a good idea to delete the object
+ explicitly when you are done with it, to avoid wasting memory.
+
+ This function returns null if there are no pending datagram-mode
+ connections.
+
+ \note The returned QSctpSocket object cannot be used from another
+ thread. If you want to use an incoming connection from another
+ thread, you need to override incomingConnection().
+
+ \sa hasPendingConnections(), nextPendingConnection(), QSctpSocket
+*/
+QSctpSocket *QSctpServer::nextPendingDatagramConnection()
+{
+ Q_D(QSctpServer);
+
+ QMutableListIterator<QTcpSocket *> i(d->pendingConnections);
+ while (i.hasNext()) {
+ QSctpSocket *socket = qobject_cast<QSctpSocket *>(i.next());
+ Q_ASSERT(socket);
+
+ if (socket->inDatagramMode()) {
+ i.remove();
+ Q_ASSERT(d->socketEngine);
+ d->socketEngine->setReadNotificationEnabled(true);
+ return socket;
+ }
+ }
+
+ return nullptr;
+}
+
+QT_END_NAMESPACE
+
+#include "moc_qsctpserver.cpp"
diff --git a/src/network/socket/qsctpserver.h b/src/network/socket/qsctpserver.h
new file mode 100644
index 0000000000..fb017a924d
--- /dev/null
+++ b/src/network/socket/qsctpserver.h
@@ -0,0 +1,77 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSCTPSERVER_H
+#define QSCTPSERVER_H
+
+#include <QtNetwork/qtcpserver.h>
+
+QT_BEGIN_NAMESPACE
+
+
+#ifndef QT_NO_SCTP
+
+class QSctpServerPrivate;
+class QSctpSocket;
+
+class Q_NETWORK_EXPORT QSctpServer : public QTcpServer
+{
+ Q_OBJECT
+public:
+ explicit QSctpServer(QObject *parent = nullptr);
+ virtual ~QSctpServer();
+
+ void setMaxChannelCount(int count);
+ int maxChannelCount() const;
+
+ QSctpSocket *nextPendingDatagramConnection();
+
+protected:
+ void incomingConnection(qintptr handle) Q_DECL_OVERRIDE;
+
+private:
+ Q_DISABLE_COPY(QSctpServer)
+ Q_DECLARE_PRIVATE(QSctpServer)
+};
+
+#endif // QT_NO_SCTP
+
+QT_END_NAMESPACE
+
+#endif // QSCTPSERVER_H
diff --git a/src/network/socket/qsctpserver_p.h b/src/network/socket/qsctpserver_p.h
new file mode 100644
index 0000000000..32760caffe
--- /dev/null
+++ b/src/network/socket/qsctpserver_p.h
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSCTPSERVER_P_H
+#define QSCTPSERVER_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of the QLibrary class. This header file may change from
+// version to version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "private/qtcpserver_p.h"
+
+QT_BEGIN_NAMESPACE
+
+#ifndef QT_NO_SCTP
+
+class QSctpServerPrivate : public QTcpServerPrivate
+{
+ Q_DECLARE_PUBLIC(QSctpServer)
+public:
+ QSctpServerPrivate();
+ virtual ~QSctpServerPrivate();
+
+ int maxChannelCount;
+
+ void configureCreatedSocket() Q_DECL_OVERRIDE;
+};
+
+#endif // QT_NO_SCTP
+
+QT_END_NAMESPACE
+
+#endif // QSCTPSERVER_P_H
diff --git a/src/network/socket/qsctpsocket.cpp b/src/network/socket/qsctpsocket.cpp
new file mode 100644
index 0000000000..cb07e80299
--- /dev/null
+++ b/src/network/socket/qsctpsocket.cpp
@@ -0,0 +1,547 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+//#define QSCTPSOCKET_DEBUG
+
+/*!
+ \class QSctpSocket
+ \since 5.8
+
+ \brief The QSctpSocket class provides an SCTP socket.
+
+ \ingroup network
+ \inmodule QtNetwork
+
+ SCTP (Stream Control Transmission Protocol) is a transport layer
+ protocol serving in a similar role as the popular protocols TCP
+ and UDP. Like UDP, SCTP is message-oriented, but it ensures reliable,
+ in-sequence transport of messages with congestion control like
+ TCP.
+
+ SCTP is connection-oriented protocol, which provides the complete
+ simultaneous transmission of multiple data streams between
+ endpoints. This multi-streaming allows data to be delivered by
+ independent channels, so that if there is data loss in one stream,
+ delivery will not be affected for the other streams.
+
+ Being message-oriented, SCTP transports a sequence of messages,
+ rather than transporting an unbroken stream of bytes as does TCP.
+ Like in UDP, in SCTP a sender sends a message in one operation,
+ and that exact message is passed to the receiving application
+ process in one operation. But unlike UDP, the delivery is
+ guaranteed.
+
+ It also supports multi-homing, meaning that a connected endpoint
+ can have alternate IP addresses associated with it in order to
+ route around network failure or changing conditions.
+
+ QSctpSocket is a convenience subclass of QTcpSocket that allows
+ you to emulate TCP data stream over SCTP or establish an SCTP
+ connection for reliable datagram service.
+
+ QSctpSocket can operate in one of two possible modes:
+
+ \list
+ \li Continuous byte stream (TCP emulation).
+ \li Multi-streamed datagram mode.
+ \endlist
+
+ To set a continuous byte stream mode, instantiate QSctpSocket and
+ call setMaxChannelCount() with a negative value. This gives the
+ ability to use QSctpSocket as a regular buffered QTcpSocket. You
+ can call connectToHost() to initiate connection with endpoint,
+ write() to transmit and read() to receive data from the peer, but
+ you cannot distinguish message boundaries.
+
+ By default, QSctpSocket operates in datagram mode. Before
+ connecting, call setMaxChannelCount() to set the maximum number of
+ channels that the application is prepared to support. This number
+ is a parameter negotiated with the remote endpoint and its value
+ can be bounded by the operating system. The default value of 0
+ indicates to use the peer's value. If both endpoints have default
+ values, then number of connection channels is system-dependent.
+ After establishing a connection, you can fetch the actual number
+ of channels by calling readChannelCount() and writeChannelCount().
+
+ \snippet code/src_network_socket_qsctpsocket.cpp 0
+
+ In datagram mode, QSctpSocket performs the buffering of datagrams
+ independently for each channel. You can queue a datagram to the
+ buffer of the current channel by calling writeDatagram() and read
+ a pending datagram by calling readDatagram() respectively.
+
+ Using the standard QIODevice functions read(), readLine(), write(),
+ etc. is allowed in datagram mode with the same limitations as in
+ continuous byte stream mode.
+
+ \note This feature is not supported on the Windows platform.
+
+ \sa QSctpServer, QTcpSocket, QAbstractSocket
+*/
+
+#include "qsctpsocket.h"
+#include "qsctpsocket_p.h"
+
+#include "qabstractsocketengine_p.h"
+#include "private/qbytearray_p.h"
+
+#ifdef QSCTPSOCKET_DEBUG
+#include <qdebug.h>
+#endif
+
+QT_BEGIN_NAMESPACE
+
+/*! \internal
+*/
+QSctpSocketPrivate::QSctpSocketPrivate()
+ : maxChannelCount(0)
+{
+}
+
+/*! \internal
+*/
+QSctpSocketPrivate::~QSctpSocketPrivate()
+{
+}
+
+/*! \internal
+*/
+bool QSctpSocketPrivate::canReadNotification()
+{
+ Q_Q(QSctpSocket);
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::canReadNotification()");
+#endif
+
+ // Handle TCP emulation mode in the base implementation.
+ if (!q->inDatagramMode())
+ return QTcpSocketPrivate::canReadNotification();
+
+ const int savedCurrentChannel = currentReadChannel;
+ bool currentChannelRead = false;
+ do {
+ int datagramSize = incomingDatagram.size();
+ QIpPacketHeader header;
+
+ do {
+ // Determine the size of the pending datagram.
+ qint64 bytesToRead = socketEngine->bytesAvailable();
+ if (bytesToRead == 0) {
+ // As a corner case, if we can't determine the size of the pending datagram,
+ // try to read 4K of data from the socket. Subsequent ::recvmsg call either
+ // fails or returns the actual length of the datagram.
+ bytesToRead = 4096;
+ }
+
+ Q_ASSERT((datagramSize + int(bytesToRead)) < MaxByteArraySize);
+ incomingDatagram.resize(datagramSize + int(bytesToRead));
+
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::canReadNotification() about to read %lli bytes",
+ bytesToRead);
+#endif
+ qint64 readBytes = socketEngine->readDatagram(
+ incomingDatagram.data() + datagramSize, bytesToRead, &header,
+ QAbstractSocketEngine::WantAll);
+ if (readBytes <= 0) {
+ if (readBytes == -2) { // no data available for reading
+ incomingDatagram.resize(datagramSize);
+ return currentChannelRead;
+ }
+
+ socketEngine->close();
+ if (readBytes == 0) {
+ setErrorAndEmit(QAbstractSocket::RemoteHostClosedError,
+ QSctpSocket::tr("The remote host closed the connection"));
+ } else {
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::canReadNotification() read failed: %s",
+ socketEngine->errorString().toLatin1().constData());
+#endif
+ setErrorAndEmit(socketEngine->error(), socketEngine->errorString());
+ }
+
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::canReadNotification() disconnecting socket");
+#endif
+ q->disconnectFromHost();
+ return currentChannelRead;
+ }
+ datagramSize += int(readBytes); // update datagram size
+ } while (!header.endOfRecord);
+
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::canReadNotification() got datagram from channel %i, size = %i",
+ header.streamNumber, datagramSize);
+#endif
+
+ // Drop the datagram, if opened only for writing
+ if (!q->isReadable()) {
+ incomingDatagram.clear();
+ continue;
+ }
+
+ // Store datagram in the channel buffer
+ Q_ASSERT(header.streamNumber < readBuffers.size());
+ incomingDatagram.resize(datagramSize);
+ readBuffers[header.streamNumber].setChunkSize(0); // set packet mode on channel buffer
+ readBuffers[header.streamNumber].append(incomingDatagram);
+ incomingDatagram = QByteArray();
+
+ if (readHeaders.size() != readBuffers.size())
+ readHeaders.resize(readBuffers.size());
+ readHeaders[header.streamNumber].push_back(header);
+
+ // Emit notifications.
+ if (header.streamNumber == savedCurrentChannel)
+ currentChannelRead = true;
+ emitReadyRead(header.streamNumber);
+
+ } while (state == QAbstractSocket::ConnectedState);
+
+ return currentChannelRead;
+}
+
+/*! \internal
+*/
+bool QSctpSocketPrivate::writeToSocket()
+{
+ Q_Q(QSctpSocket);
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::writeToSocket()");
+#endif
+
+ // Handle TCP emulation mode in the base implementation.
+ if (!q->inDatagramMode())
+ return QTcpSocketPrivate::writeToSocket();
+
+ if (!socketEngine)
+ return false;
+
+ QIpPacketHeader defaultHeader;
+ const int savedCurrentChannel = currentWriteChannel;
+ bool currentChannelWritten = false;
+ bool transmitting;
+ do {
+ transmitting = false;
+
+ for (int channel = 0; channel < writeBuffers.size(); ++channel) {
+ QRingBuffer &channelBuffer = writeBuffers[channel];
+
+ if (channelBuffer.isEmpty())
+ continue;
+
+ const bool hasHeader = (channel < writeHeaders.size())
+ && !writeHeaders[channel].empty();
+ QIpPacketHeader &header = hasHeader ? writeHeaders[channel].front() : defaultHeader;
+ header.streamNumber = channel;
+ qint64 sent = socketEngine->writeDatagram(channelBuffer.readPointer(),
+ channelBuffer.nextDataBlockSize(),
+ header);
+ if (sent < 0) {
+ if (sent == -2) // temporary error in writeDatagram
+ return currentChannelWritten;
+
+ socketEngine->close();
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::writeToSocket() write error, aborting. %s",
+ socketEngine->errorString().toLatin1().constData());
+#endif
+ setErrorAndEmit(socketEngine->error(), socketEngine->errorString());
+ // An unexpected error so close the socket.
+ q->disconnectFromHost();
+ return currentChannelWritten;
+ }
+ Q_ASSERT(sent == channelBuffer.nextDataBlockSize());
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::writeToSocket() sent datagram of size %lli to channel %i",
+ sent, channel);
+#endif
+ transmitting = true;
+
+ // Remove datagram from the buffer
+ channelBuffer.read();
+ if (hasHeader)
+ writeHeaders[channel].pop_front();
+
+ // Emit notifications.
+ if (channel == savedCurrentChannel)
+ currentChannelWritten = true;
+ emitBytesWritten(sent, channel);
+
+ // If we were closed as a result of the bytesWritten() signal, return.
+ if (state == QAbstractSocket::UnconnectedState) {
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocketPrivate::writeToSocket() socket is closing - returning");
+#endif
+ return currentChannelWritten;
+ }
+ }
+ } while (transmitting);
+
+ // At this point socket is either in Connected or Closing state,
+ // write buffers are empty.
+ if (state == QAbstractSocket::ClosingState)
+ q->disconnectFromHost();
+ else
+ socketEngine->setWriteNotificationEnabled(false);
+
+ return currentChannelWritten;
+}
+
+/*! \internal
+*/
+void QSctpSocketPrivate::configureCreatedSocket()
+{
+ if (socketEngine)
+ socketEngine->setOption(QAbstractSocketEngine::MaxStreamsSocketOption,
+ maxChannelCount < 0 ? 1 : maxChannelCount);
+}
+
+/*!
+ Creates a QSctpSocket object in state \c UnconnectedState.
+
+ Sets the datagram operation mode. The \a parent argument is passed
+ to QObject's constructor.
+
+ \sa socketType(), setMaxChannelCount()
+*/
+QSctpSocket::QSctpSocket(QObject *parent)
+ : QTcpSocket(SctpSocket, *new QSctpSocketPrivate, parent)
+{
+#if defined(QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocket::QSctpSocket()");
+#endif
+ d_func()->isBuffered = true;
+}
+
+/*!
+ Destroys the socket, closing the connection if necessary.
+
+ \sa close()
+*/
+QSctpSocket::~QSctpSocket()
+{
+#if defined(QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocket::~QSctpSocket()");
+#endif
+}
+
+/*! \reimp
+*/
+qint64 QSctpSocket::readData(char *data, qint64 maxSize)
+{
+ Q_D(QSctpSocket);
+
+ // Cleanup headers, if the user calls the standard QIODevice functions
+ if (d->currentReadChannel < d->readHeaders.size())
+ d->readHeaders[d->currentReadChannel].clear();
+
+ return QTcpSocket::readData(data, maxSize);
+}
+
+/*! \reimp
+*/
+qint64 QSctpSocket::readLineData(char *data, qint64 maxlen)
+{
+ Q_D(QSctpSocket);
+
+ // Cleanup headers, if the user calls the standard QIODevice functions
+ if (d->currentReadChannel < d->readHeaders.size())
+ d->readHeaders[d->currentReadChannel].clear();
+
+ return QTcpSocket::readLineData(data, maxlen);
+}
+
+/*! \reimp
+*/
+void QSctpSocket::close()
+{
+ QTcpSocket::close();
+ d_func()->readHeaders.clear();
+}
+
+/*! \reimp
+*/
+void QSctpSocket::disconnectFromHost()
+{
+ Q_D(QSctpSocket);
+
+ QTcpSocket::disconnectFromHost();
+ if (d->state == QAbstractSocket::UnconnectedState) {
+ d->incomingDatagram.clear();
+ d->writeHeaders.clear();
+ }
+}
+
+/*!
+ Sets the maximum number of channels that the application is
+ prepared to support in datagram mode, to \a count. If \a count
+ is 0, endpoint's value for maximum number of channels is used.
+ Negative \a count sets a continuous byte stream mode.
+
+ Call this method only when QSctpSocket is in UnconnectedState.
+
+ \sa maxChannelCount(), readChannelCount(), writeChannelCount()
+*/
+void QSctpSocket::setMaxChannelCount(int count)
+{
+ Q_D(QSctpSocket);
+ if (d->state != QAbstractSocket::UnconnectedState) {
+ qWarning("QSctpSocket::setMaxChannelCount() is only allowed in UnconnectedState");
+ return;
+ }
+#if defined(QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocket::setMaxChannelCount(%i)", count);
+#endif
+ d->maxChannelCount = qMax(count, -1);
+}
+
+/*!
+ Returns the maximum number of channels that QSctpSocket is able to
+ support.
+
+ A value of 0 (the default) means that the number of connection
+ channels would be set by the remote endpoint.
+
+ Returns -1 if QSctpSocket is running in continuous byte stream
+ mode.
+
+ \sa setMaxChannelCount(), readChannelCount(), writeChannelCount()
+*/
+int QSctpSocket::maxChannelCount() const
+{
+ return d_func()->maxChannelCount;
+}
+
+/*!
+ Returns \c true if the socket is running in datagram mode.
+
+ \sa setMaxChannelCount()
+*/
+bool QSctpSocket::inDatagramMode() const
+{
+ Q_D(const QSctpSocket);
+ return d->maxChannelCount != -1 && d->isBuffered;
+}
+
+/*!
+ Reads a datagram from the buffer of the current read channel, and
+ returns it as a QNetworkDatagram object, along with the sender's
+ host address and port. If possible, this function will also try to
+ determine the datagram's destination address, port, and the number
+ of hop counts at reception time.
+
+ On failure, returns a QNetworkDatagram that reports \l
+ {QNetworkDatagram::isValid()}{not valid}.
+
+ \sa writeDatagram(), inDatagramMode(), currentReadChannel()
+*/
+QNetworkDatagram QSctpSocket::readDatagram()
+{
+ Q_D(QSctpSocket);
+
+ if (!isReadable() || !inDatagramMode()) {
+ qWarning("QSctpSocket::readDatagram(): operation is not permitted");
+ return QNetworkDatagram();
+ }
+
+ if (d->currentReadChannel >= d->readHeaders.size()
+ || d->readHeaders[d->currentReadChannel].size() == 0) {
+ Q_ASSERT(d->buffer.isEmpty());
+ return QNetworkDatagram();
+ }
+
+ QNetworkDatagram result(*new QNetworkDatagramPrivate(d->buffer.read(),
+ d->readHeaders[d->currentReadChannel].front()));
+ d->readHeaders[d->currentReadChannel].pop_front();
+
+#if defined (QSCTPSOCKET_DEBUG)
+ qDebug("QSctpSocket::readDatagram() returning datagram (%p, %i, \"%s\", %i)",
+ result.d->data.constData(),
+ result.d->data.size(),
+ result.senderAddress().toString().toLatin1().constData(),
+ result.senderPort());
+#endif
+
+ return result;
+}
+
+/*!
+ Writes a \a datagram to the buffer of the current write channel.
+ Returns true on success; otherwise returns false.
+
+ \sa readDatagram(), inDatagramMode(), currentWriteChannel()
+*/
+bool QSctpSocket::writeDatagram(const QNetworkDatagram &datagram)
+{
+ Q_D(QSctpSocket);
+
+ if (!isWritable() || d->state != QAbstractSocket::ConnectedState || !d->socketEngine
+ || !d->socketEngine->isValid() || !inDatagramMode()) {
+ qWarning("QSctpSocket::writeDatagram(): operation is not permitted");
+ return false;
+ }
+
+ if (datagram.d->data.isEmpty()) {
+ qWarning("QSctpSocket::writeDatagram() is called with empty datagram");
+ return false;
+ }
+
+
+#if defined QSCTPSOCKET_DEBUG
+ qDebug("QSctpSocket::writeDatagram(%p, %i, \"%s\", %i)",
+ datagram.d->data.constData(),
+ datagram.d->data.size(),
+ datagram.destinationAddress().toString().toLatin1().constData(),
+ datagram.destinationPort());
+#endif
+
+ if (d->writeHeaders.size() != d->writeBuffers.size())
+ d->writeHeaders.resize(d->writeBuffers.size());
+ Q_ASSERT(d->currentWriteChannel < d->writeHeaders.size());
+ d->writeHeaders[d->currentWriteChannel].push_back(datagram.d->header);
+ d->writeBuffer.setChunkSize(0); // set packet mode on channel buffer
+ d->writeBuffer.append(datagram.d->data);
+
+ d->socketEngine->setWriteNotificationEnabled(true);
+ return true;
+}
+
+QT_END_NAMESPACE
diff --git a/src/network/socket/qsctpsocket.h b/src/network/socket/qsctpsocket.h
new file mode 100644
index 0000000000..aaa4e1ecca
--- /dev/null
+++ b/src/network/socket/qsctpsocket.h
@@ -0,0 +1,82 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSCTPSOCKET_H
+#define QSCTPSOCKET_H
+
+#include <QtNetwork/qtcpsocket.h>
+#include <QtNetwork/qnetworkdatagram.h>
+
+QT_BEGIN_NAMESPACE
+
+#ifndef QT_NO_SCTP
+
+class QSctpSocketPrivate;
+
+class Q_NETWORK_EXPORT QSctpSocket : public QTcpSocket
+{
+ Q_OBJECT
+public:
+ explicit QSctpSocket(QObject *parent = nullptr);
+ virtual ~QSctpSocket();
+
+ void close() Q_DECL_OVERRIDE;
+ void disconnectFromHost() Q_DECL_OVERRIDE;
+
+ void setMaxChannelCount(int count);
+ int maxChannelCount() const;
+ bool inDatagramMode() const;
+
+ QNetworkDatagram readDatagram();
+ bool writeDatagram(const QNetworkDatagram &datagram);
+
+protected:
+ qint64 readData(char *data, qint64 maxlen) Q_DECL_OVERRIDE;
+ qint64 readLineData(char *data, qint64 maxlen) Q_DECL_OVERRIDE;
+
+private:
+ Q_DISABLE_COPY(QSctpSocket)
+ Q_DECLARE_PRIVATE(QSctpSocket)
+};
+
+#endif // QT_NO_SCTP
+
+QT_END_NAMESPACE
+
+#endif // QSCTPSOCKET_H
diff --git a/src/network/socket/qsctpsocket_p.h b/src/network/socket/qsctpsocket_p.h
new file mode 100644
index 0000000000..f38095330f
--- /dev/null
+++ b/src/network/socket/qsctpsocket_p.h
@@ -0,0 +1,90 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSCTPSOCKET_P_H
+#define QSCTPSOCKET_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of the QLibrary class. This header file may change from
+// version to version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QtNetwork/qsctpsocket.h>
+#include <private/qtcpsocket_p.h>
+#include <QtCore/qbytearray.h>
+#include <QtCore/qvector.h>
+#include <private/qnetworkdatagram_p.h>
+
+#include <deque>
+
+QT_BEGIN_NAMESPACE
+
+#ifndef QT_NO_SCTP
+
+class QSctpSocketPrivate : public QTcpSocketPrivate
+{
+ Q_DECLARE_PUBLIC(QSctpSocket)
+public:
+ QSctpSocketPrivate();
+ virtual ~QSctpSocketPrivate();
+
+ bool canReadNotification() Q_DECL_OVERRIDE;
+ bool writeToSocket() Q_DECL_OVERRIDE;
+
+ QByteArray incomingDatagram;
+ int maxChannelCount;
+
+ typedef std::deque<QIpPacketHeader> IpHeaderList;
+ QVector<IpHeaderList> readHeaders;
+ QVector<IpHeaderList> writeHeaders;
+
+ void configureCreatedSocket() Q_DECL_OVERRIDE;
+};
+
+#endif // QT_NO_SCTP
+
+QT_END_NAMESPACE
+
+#endif // QSCTPSOCKET_P_H
diff --git a/src/network/socket/qsocks5socketengine.cpp b/src/network/socket/qsocks5socketengine.cpp
index a57a1dca2c..a1a8e4649d 100644
--- a/src/network/socket/qsocks5socketengine.cpp
+++ b/src/network/socket/qsocks5socketengine.cpp
@@ -64,11 +64,7 @@ static const int MaxWriteBufferSize = 128*1024;
//#define QSOCKS5SOCKETLAYER_DEBUG
#define MAX_DATA_DUMP 256
-#if !defined(Q_OS_WINCE)
#define SOCKS5_BLOCKING_BIND_TIMEOUT 5000
-#else
-#define SOCKS5_BLOCKING_BIND_TIMEOUT 10000
-#endif
#define Q_INIT_CHECK(returnValue) do { \
if (!d->data) { \
@@ -622,7 +618,7 @@ void QSocks5SocketEnginePrivate::setErrorState(Socks5State state, const QString
QSocks5SocketEngine::tr("Connection to proxy timed out"));
break;
}
- /* fall through */
+ Q_FALLTHROUGH();
default:
q->setError(controlSocketError, data->controlSocket->errorString());
break;
@@ -1209,7 +1205,7 @@ void QSocks5SocketEnginePrivate::_q_controlSocketReadNotification()
break;
}
- // fall through
+ Q_FALLTHROUGH();
default:
qWarning("QSocks5SocketEnginePrivate::_q_controlSocketReadNotification: "
"Unexpectedly received data while in state=%d and mode=%d",
@@ -1609,8 +1605,31 @@ bool QSocks5SocketEngine::setMulticastInterface(const QNetworkInterface &)
}
#endif // QT_NO_NETWORKINTERFACE
+bool QSocks5SocketEngine::hasPendingDatagrams() const
+{
+ Q_D(const QSocks5SocketEngine);
+ Q_INIT_CHECK(false);
+
+ d->checkForDatagrams();
+
+ return !d->udpData->pendingDatagrams.isEmpty();
+}
+
+qint64 QSocks5SocketEngine::pendingDatagramSize() const
+{
+ Q_D(const QSocks5SocketEngine);
+
+ d->checkForDatagrams();
+
+ if (!d->udpData->pendingDatagrams.isEmpty())
+ return d->udpData->pendingDatagrams.head().data.size();
+ return 0;
+}
+#endif // QT_NO_UDPSOCKET
+
qint64 QSocks5SocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHeader *header, PacketHeaderOptions)
{
+#ifndef QT_NO_UDPSOCKET
Q_D(QSocks5SocketEngine);
d->checkForDatagrams();
@@ -1624,10 +1643,17 @@ qint64 QSocks5SocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHea
header->senderAddress = datagram.address;
header->senderPort = datagram.port;
return copyLen;
+#else
+ Q_UNUSED(data)
+ Q_UNUSED(maxlen)
+ Q_UNUSED(header)
+ return -1;
+#endif // QT_NO_UDPSOCKET
}
qint64 QSocks5SocketEngine::writeDatagram(const char *data, qint64 len, const QIpPacketHeader &header)
{
+#ifndef QT_NO_UDPSOCKET
Q_D(QSocks5SocketEngine);
// it is possible to send with out first binding with udp, but socks5 requires a bind.
@@ -1664,29 +1690,13 @@ qint64 QSocks5SocketEngine::writeDatagram(const char *data, qint64 len, const QI
}
return len;
-}
-
-bool QSocks5SocketEngine::hasPendingDatagrams() const
-{
- Q_D(const QSocks5SocketEngine);
- Q_INIT_CHECK(false);
-
- d->checkForDatagrams();
-
- return !d->udpData->pendingDatagrams.isEmpty();
-}
-
-qint64 QSocks5SocketEngine::pendingDatagramSize() const
-{
- Q_D(const QSocks5SocketEngine);
-
- d->checkForDatagrams();
-
- if (!d->udpData->pendingDatagrams.isEmpty())
- return d->udpData->pendingDatagrams.head().data.size();
- return 0;
-}
+#else
+ Q_UNUSED(data)
+ Q_UNUSED(len)
+ Q_UNUSED(header)
+ return -1;
#endif // QT_NO_UDPSOCKET
+}
qint64 QSocks5SocketEngine::bytesToWrite() const
{
diff --git a/src/network/socket/qsocks5socketengine_p.h b/src/network/socket/qsocks5socketengine_p.h
index 30caa4b50c..864b163489 100644
--- a/src/network/socket/qsocks5socketengine_p.h
+++ b/src/network/socket/qsocks5socketengine_p.h
@@ -51,6 +51,7 @@
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "qabstractsocketengine_p.h"
#include "qnetworkproxy.h"
@@ -99,13 +100,13 @@ public:
bool setMulticastInterface(const QNetworkInterface &iface) Q_DECL_OVERRIDE;
#endif // QT_NO_NETWORKINTERFACE
- qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader * = 0,
- PacketHeaderOptions = WantNone) Q_DECL_OVERRIDE;
- qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) Q_DECL_OVERRIDE;
bool hasPendingDatagrams() const Q_DECL_OVERRIDE;
qint64 pendingDatagramSize() const Q_DECL_OVERRIDE;
#endif // QT_NO_UDPSOCKET
+ qint64 readDatagram(char *data, qint64 maxlen, QIpPacketHeader * = 0,
+ PacketHeaderOptions = WantNone) Q_DECL_OVERRIDE;
+ qint64 writeDatagram(const char *data, qint64 len, const QIpPacketHeader &) Q_DECL_OVERRIDE;
qint64 bytesToWrite() const Q_DECL_OVERRIDE;
int option(SocketOption option) const Q_DECL_OVERRIDE;
diff --git a/src/network/socket/qtcpserver.cpp b/src/network/socket/qtcpserver.cpp
index de1dc29cfb..eddf789921 100644
--- a/src/network/socket/qtcpserver.cpp
+++ b/src/network/socket/qtcpserver.cpp
@@ -119,6 +119,7 @@ QT_BEGIN_NAMESPACE
*/
QTcpServerPrivate::QTcpServerPrivate()
: port(0)
+ , socketType(QAbstractSocket::UnknownSocketType)
, state(QAbstractSocket::UnconnectedState)
, socketEngine(0)
, serverSocketError(QAbstractSocket::UnknownSocketError)
@@ -148,13 +149,21 @@ QNetworkProxy QTcpServerPrivate::resolveProxy(const QHostAddress &address, quint
proxies << proxy;
} else {
// try the application settings instead
- QNetworkProxyQuery query(port, QString(), QNetworkProxyQuery::TcpServer);
+ QNetworkProxyQuery query(port, QString(),
+ socketType == QAbstractSocket::SctpSocket ?
+ QNetworkProxyQuery::SctpServer :
+ QNetworkProxyQuery::TcpServer);
proxies = QNetworkProxyFactory::proxyForQuery(query);
}
// return the first that we can use
for (const QNetworkProxy &p : qAsConst(proxies)) {
- if (p.capabilities() & QNetworkProxy::ListeningCapability)
+ if (socketType == QAbstractSocket::TcpSocket &&
+ (p.capabilities() & QNetworkProxy::ListeningCapability) != 0)
+ return p;
+
+ if (socketType == QAbstractSocket::SctpSocket &&
+ (p.capabilities() & QNetworkProxy::SctpListeningCapability) != 0)
return p;
}
@@ -228,9 +237,11 @@ void QTcpServerPrivate::readNotification()
QTcpServer::QTcpServer(QObject *parent)
: QObject(*new QTcpServerPrivate, parent)
{
+ Q_D(QTcpServer);
#if defined(QTCPSERVER_DEBUG)
qDebug("QTcpServer::QTcpServer(%p)", parent);
#endif
+ d->socketType = QAbstractSocket::TcpSocket;
}
/*!
@@ -251,13 +262,22 @@ QTcpServer::~QTcpServer()
}
/*! \internal
+
+ Constructs a new server object with socket of type \a socketType. The \a
+ parent argument is passed to QObject's constructor.
*/
-QTcpServer::QTcpServer(QTcpServerPrivate &dd, QObject *parent)
- : QObject(dd, parent)
+QTcpServer::QTcpServer(QAbstractSocket::SocketType socketType, QTcpServerPrivate &dd,
+ QObject *parent) : QObject(dd, parent)
{
+ Q_D(QTcpServer);
#if defined(QTCPSERVER_DEBUG)
- qDebug("QTcpServer::QTcpServer(QTcpServerPrivate == %p, parent == %p)", &dd, parent);
+ qDebug("QTcpServer::QTcpServer(%sSocket, QTcpServerPrivate == %p, parent == %p)",
+ socketType == QAbstractSocket::TcpSocket ? "Tcp"
+ : socketType == QAbstractSocket::UdpSocket ? "Udp"
+ : socketType == QAbstractSocket::SctpSocket ? "Sctp"
+ : "Unknown", &dd, parent);
#endif
+ d->socketType = socketType;
}
/*!
@@ -288,7 +308,7 @@ bool QTcpServer::listen(const QHostAddress &address, quint16 port)
#endif
delete d->socketEngine;
- d->socketEngine = QAbstractSocketEngine::createSocketEngine(QAbstractSocket::TcpSocket, proxy, this);
+ d->socketEngine = QAbstractSocketEngine::createSocketEngine(d->socketType, proxy, this);
if (!d->socketEngine) {
d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError;
d->serverSocketErrorString = tr("Operation on socket is not supported");
@@ -298,7 +318,7 @@ bool QTcpServer::listen(const QHostAddress &address, quint16 port)
//copy network session down to the socket engine (if it has been set)
d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
#endif
- if (!d->socketEngine->initialize(QAbstractSocket::TcpSocket, proto)) {
+ if (!d->socketEngine->initialize(d->socketType, proto)) {
d->serverSocketError = d->socketEngine->error();
d->serverSocketErrorString = d->socketEngine->errorString();
return false;
@@ -543,8 +563,11 @@ QTcpSocket *QTcpServer::nextPendingConnection()
if (d->pendingConnections.isEmpty())
return 0;
- if (!d->socketEngine->isReadNotificationEnabled())
+ if (!d->socketEngine) {
+ qWarning("QTcpServer::nextPendingConnection() called while not listening");
+ } else if (!d->socketEngine->isReadNotificationEnabled()) {
d->socketEngine->setReadNotificationEnabled(true);
+ }
return d->pendingConnections.takeFirst();
}
diff --git a/src/network/socket/qtcpserver.h b/src/network/socket/qtcpserver.h
index 90781564b4..192cbce54c 100644
--- a/src/network/socket/qtcpserver.h
+++ b/src/network/socket/qtcpserver.h
@@ -40,6 +40,7 @@
#ifndef QTCPSERVER_H
#define QTCPSERVER_H
+#include <QtNetwork/qtnetworkglobal.h>
#include <QtCore/qobject.h>
#include <QtNetwork/qabstractsocket.h>
#include <QtNetwork/qhostaddress.h>
@@ -93,7 +94,8 @@ protected:
virtual void incomingConnection(qintptr handle);
void addPendingConnection(QTcpSocket* socket);
- QTcpServer(QTcpServerPrivate &dd, QObject *parent = Q_NULLPTR);
+ QTcpServer(QAbstractSocket::SocketType socketType, QTcpServerPrivate &dd,
+ QObject *parent = Q_NULLPTR);
Q_SIGNALS:
void newConnection();
diff --git a/src/network/socket/qtcpserver_p.h b/src/network/socket/qtcpserver_p.h
index b53f53b921..71dc4d985f 100644
--- a/src/network/socket/qtcpserver_p.h
+++ b/src/network/socket/qtcpserver_p.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtNetwork module of the Qt Toolkit.
@@ -44,13 +45,14 @@
// W A R N I N G
// -------------
//
-// This file is not part of the Qt API. It exists for the convenience
-// of the QLibrary class. This header file may change from
-// version to version without notice, or even be removed.
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
//
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include "QtNetwork/qtcpserver.h"
#include "private/qobject_p.h"
#include "private/qabstractsocketengine_p.h"
@@ -73,6 +75,7 @@ public:
quint16 port;
QHostAddress address;
+ QAbstractSocket::SocketType socketType;
QAbstractSocket::SocketState state;
QAbstractSocketEngine *socketEngine;
diff --git a/src/network/socket/qtcpsocket.h b/src/network/socket/qtcpsocket.h
index b2c89dcad2..3c3e3b69fd 100644
--- a/src/network/socket/qtcpsocket.h
+++ b/src/network/socket/qtcpsocket.h
@@ -40,6 +40,7 @@
#ifndef QTCPSOCKET_H
#define QTCPSOCKET_H
+#include <QtNetwork/qtnetworkglobal.h>
#include <QtNetwork/qabstractsocket.h>
#include <QtCore/qvariant.h>
diff --git a/src/network/socket/qtcpsocket_p.h b/src/network/socket/qtcpsocket_p.h
index dfa55a6109..ba1a0aa920 100644
--- a/src/network/socket/qtcpsocket_p.h
+++ b/src/network/socket/qtcpsocket_p.h
@@ -44,13 +44,14 @@
// W A R N I N G
// -------------
//
-// This file is not part of the Qt API. It exists for the convenience
-// of the QLibrary class. This header file may change from
-// version to version without notice, or even be removed.
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
//
// We mean it.
//
+#include <QtNetwork/private/qtnetworkglobal_p.h>
#include <QtNetwork/qtcpsocket.h>
#include <private/qabstractsocket_p.h>
diff --git a/src/network/socket/qudpsocket.cpp b/src/network/socket/qudpsocket.cpp
index c406009069..37b385dfb5 100644
--- a/src/network/socket/qudpsocket.cpp
+++ b/src/network/socket/qudpsocket.cpp
@@ -55,10 +55,10 @@
datagrams.
The most common way to use this class is to bind to an address and port
- using bind(), then call writeDatagram() and readDatagram() to transfer
- data. If you want to use the standard QIODevice functions read(),
- readLine(), write(), etc., you must first connect the socket directly to a
- peer by calling connectToHost().
+ using bind(), then call writeDatagram() and readDatagram() /
+ receiveDatagram() to transfer data. If you want to use the standard
+ QIODevice functions read(), readLine(), write(), etc., you must first
+ connect the socket directly to a peer by calling connectToHost().
The socket emits the bytesWritten() signal every time a datagram
is written to the network. If you just want to send datagrams,
@@ -67,7 +67,7 @@
The readyRead() signal is emitted whenever datagrams arrive. In
that case, hasPendingDatagrams() returns \c true. Call
pendingDatagramSize() to obtain the size of the first pending
- datagram, and readDatagram() to read it.
+ datagram, and readDatagram() or receiveDatagram() to read it.
\note An incoming datagram should be read when you receive the readyRead()
signal, otherwise this signal will not be emitted for the next datagram.
@@ -94,11 +94,12 @@
\l{multicastreceiver}{Multicast Receiver} examples illustrate how
to use QUdpSocket in applications.
- \sa QTcpSocket
+ \sa QTcpSocket, QNetworkDatagram
*/
#include "qudpsocket.h"
#include "qhostaddress.h"
+#include "qnetworkdatagram.h"
#include "qnetworkinterface.h"
#include "qabstractsocket_p.h"
@@ -346,6 +347,12 @@ qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddre
if (sent >= 0) {
emit bytesWritten(sent);
} else {
+ if (sent == -2) {
+ // Socket engine reports EAGAIN. Treat as a temporary error.
+ d->setErrorAndEmit(QAbstractSocket::TemporaryError,
+ tr("Unable to send a datagram"));
+ return -1;
+ }
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
}
return sent;
@@ -358,7 +365,101 @@ qint64 QUdpSocket::writeDatagram(const char *data, qint64 size, const QHostAddre
Sends the datagram \a datagram to the host address \a host and at
port \a port.
+
+ The function returns the number of bytes sent if it succeeded or -1 if it
+ encountered an error.
+*/
+
+/*!
+ \overload
+
+ Sends the datagram \a datagram to the host address and port numbers
+ contained in \a datagram, using the network interface and hop count limits
+ also set there. If the destination address and port numbers are unset, this
+ function will send to the address that was passed to connectToHost().
+
+ If the destination address is IPv6 with a non-empty
+ \l{QHostAddress::scopeId()}{scope id} but differs from the interface index
+ in \a datagram, it is undefined which interface the operating system will
+ choose to send on.
+
+ The function returns the number of bytes sent if it succeeded or -1 if it
+ encountered an error.
+
+ \warning Calling this function on a connected UDP socket may
+ result in an error and no packet being sent. If you are using a
+ connected socket, use write() to send datagrams.
+
+ \sa QNetworkDatagram::setDestination(), QNetworkDatagram::setHopLimit(), QNetworkDatagram::setInterfaceIndex()
+*/
+qint64 QUdpSocket::writeDatagram(const QNetworkDatagram &datagram)
+{
+ Q_D(QUdpSocket);
+#if defined QUDPSOCKET_DEBUG
+ qDebug("QUdpSocket::writeDatagram(%p, %i, \"%s\", %i)",
+ datagram.d->data.constData(),
+ datagram.d->data.size(),
+ datagram.destinationAddress().toString().toLatin1().constData(),
+ datagram.destinationPort());
+#endif
+ if (!d->doEnsureInitialized(QHostAddress::Any, 0, datagram.destinationAddress()))
+ return -1;
+ if (state() == UnconnectedState)
+ bind();
+
+ qint64 sent = d->socketEngine->writeDatagram(datagram.d->data,
+ datagram.d->data.size(),
+ datagram.d->header);
+ d->cachedSocketDescriptor = d->socketEngine->socketDescriptor();
+
+ if (sent >= 0) {
+ emit bytesWritten(sent);
+ } else {
+ d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
+ }
+ return sent;
+}
+
+/*!
+ Receives a datagram no larger than \a maxSize bytes and returns it in the
+ QNetworkDatagram object, along with the sender's host address and port. If
+ possible, this function will also try to determine the datagram's
+ destination address, port, and the number of hop counts at reception time.
+
+ On failure, returns a QNetworkDatagram that reports \l
+ {QNetworkDatagram::isValid()}{not valid}.
+
+ If \a maxSize is too small, the rest of the datagram will be lost. If \a
+ maxSize is 0, the datagram will be discarded. If \a maxSize is -1 (the
+ default), this function will attempt to read the entire datagram.
+
+ \sa writeDatagram(), hasPendingDatagrams(), pendingDatagramSize()
*/
+QNetworkDatagram QUdpSocket::receiveDatagram(qint64 maxSize)
+{
+ Q_D(QUdpSocket);
+
+#if defined QUDPSOCKET_DEBUG
+ qDebug("QUdpSocket::receiveDatagram(%lld)", maxSize);
+#endif
+ QT_CHECK_BOUND("QUdpSocket::receiveDatagram()", QNetworkDatagram());
+
+ if (maxSize < 0)
+ maxSize = d->socketEngine->pendingDatagramSize();
+ if (maxSize < 0)
+ return QNetworkDatagram();
+
+ QNetworkDatagram result(QByteArray(maxSize, Qt::Uninitialized));
+ qint64 readBytes = d->socketEngine->readDatagram(result.d->data.data(), maxSize, &result.d->header,
+ QAbstractSocketEngine::WantAll);
+ d->hasPendingData = false;
+ d->socketEngine->setReadNotificationEnabled(true);
+ if (readBytes < 0)
+ d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
+ else if (readBytes != result.d->data.size())
+ result.d->data.truncate(readBytes);
+ return result;
+}
/*!
Receives a datagram no larger than \a maxSize bytes and stores
@@ -398,11 +499,20 @@ qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *addres
readBytes = d->socketEngine->readDatagram(data, maxSize);
}
- d_func()->socketEngine->setReadNotificationEnabled(true);
- if (readBytes < 0)
+ d->hasPendingData = false;
+ d->socketEngine->setReadNotificationEnabled(true);
+ if (readBytes < 0) {
+ if (readBytes == -2) {
+ // No pending datagram. Treat as a temporary error.
+ d->setErrorAndEmit(QAbstractSocket::TemporaryError,
+ tr("No datagram available for reading"));
+ return -1;
+ }
d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
+ }
return readBytes;
}
+
#endif // QT_NO_UDPSOCKET
QT_END_NAMESPACE
diff --git a/src/network/socket/qudpsocket.h b/src/network/socket/qudpsocket.h
index cf2f05c86d..6ef10e2edb 100644
--- a/src/network/socket/qudpsocket.h
+++ b/src/network/socket/qudpsocket.h
@@ -40,6 +40,7 @@
#ifndef QUDPSOCKET_H
#define QUDPSOCKET_H
+#include <QtNetwork/qtnetworkglobal.h>
#include <QtNetwork/qabstractsocket.h>
#include <QtNetwork/qhostaddress.h>
@@ -48,6 +49,7 @@ QT_BEGIN_NAMESPACE
#ifndef QT_NO_UDPSOCKET
+class QNetworkDatagram;
class QNetworkInterface;
class QUdpSocketPrivate;
@@ -72,7 +74,10 @@ public:
bool hasPendingDatagrams() const;
qint64 pendingDatagramSize() const;
+ QNetworkDatagram receiveDatagram(qint64 maxSize = -1);
qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *host = Q_NULLPTR, quint16 *port = Q_NULLPTR);
+
+ qint64 writeDatagram(const QNetworkDatagram &datagram);
qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &host, quint16 port);
inline qint64 writeDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port)
{ return writeDatagram(datagram.constData(), datagram.size(), host, port); }
diff --git a/src/network/socket/socket.pri b/src/network/socket/socket.pri
index f50a7b1229..793ea60b07 100644
--- a/src/network/socket/socket.pri
+++ b/src/network/socket/socket.pri
@@ -25,6 +25,18 @@ SOURCES += socket/qabstractsocketengine.cpp \
socket/qlocalsocket.cpp \
socket/qlocalserver.cpp
+# SCTP support.
+
+qtConfig(sctp) {
+ HEADERS += socket/qsctpserver.h \
+ socket/qsctpserver_p.h \
+ socket/qsctpsocket.h \
+ socket/qsctpsocket_p.h
+
+ SOURCES += socket/qsctpserver.cpp \
+ socket/qsctpsocket.cpp
+}
+
!winrt {
SOURCES += socket/qnativesocketengine.cpp
HEADERS += socket/qnativesocketengine_p.h
@@ -39,11 +51,15 @@ unix: {
unix:HEADERS += \
socket/qnet_unix_p.h
+# Suppress deprecation warnings with moc because MS headers have
+# invalid C/C++ code otherwise.
+msvc: QMAKE_MOC_OPTIONS += -D_WINSOCK_DEPRECATED_NO_WARNINGS
+
win32:!winrt:SOURCES += socket/qnativesocketengine_win.cpp \
socket/qlocalsocket_win.cpp \
socket/qlocalserver_win.cpp
-win32:!wince:!winrt:LIBS_PRIVATE += -ladvapi32
+win32:!winrt:LIBS_PRIVATE += -ladvapi32
winrt {
SOURCES += socket/qnativesocketengine_winrt.cpp \
@@ -54,15 +70,6 @@ winrt {
DEFINES += QT_LOCALSOCKET_TCP
}
-wince {
- SOURCES -= socket/qlocalsocket_win.cpp \
- socket/qlocalserver_win.cpp
- SOURCES += socket/qlocalsocket_tcp.cpp \
- socket/qlocalserver_tcp.cpp
-
- DEFINES += QT_LOCALSOCKET_TCP
-}
-
integrity: {
SOURCES -= socket/qlocalsocket_unix.cpp \
socket/qlocalserver_unix.cpp
@@ -73,6 +80,6 @@ integrity: {
DEFINES += QT_LOCALSOCKET_TCP
}
-contains(QT_CONFIG, system-proxies) {
+qtConfig(system-proxies) {
DEFINES += QT_USE_SYSTEM_PROXIES
}