summaryrefslogtreecommitdiffstats
path: root/src/network/socket
diff options
context:
space:
mode:
Diffstat (limited to 'src/network/socket')
-rw-r--r--src/network/socket/qabstractsocket.cpp4
-rw-r--r--src/network/socket/qnativesocketengine.cpp4
-rw-r--r--src/network/socket/qnativesocketengine_winrt.cpp40
-rw-r--r--src/network/socket/qnativesocketengine_winrt_p.h2
4 files changed, 37 insertions, 13 deletions
diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp
index 34a8025cdd..02bba2d293 100644
--- a/src/network/socket/qabstractsocket.cpp
+++ b/src/network/socket/qabstractsocket.cpp
@@ -850,7 +850,7 @@ bool QAbstractSocketPrivate::writeToSocket()
const char *ptr = writeBuffer.readPointer();
// Attempt to write it all in one chunk.
- qint64 written = socketEngine->write(ptr, nextSize);
+ qint64 written = nextSize ? socketEngine->write(ptr, nextSize) : Q_INT64_C(0);
if (written < 0) {
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug() << "QAbstractSocketPrivate::writeToSocket() write error, aborting."
@@ -2504,7 +2504,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
if (!d->isBuffered && d->socketType == TcpSocket
&& d->socketEngine && d->writeBuffer.isEmpty()) {
// This code is for the new Unbuffered QTcpSocket use case
- qint64 written = d->socketEngine->write(data, size);
+ qint64 written = size ? d->socketEngine->write(data, size) : Q_INT64_C(0);
if (written < 0) {
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
} else if (written < size) {
diff --git a/src/network/socket/qnativesocketengine.cpp b/src/network/socket/qnativesocketengine.cpp
index f2bc3cec94..11d19682d8 100644
--- a/src/network/socket/qnativesocketengine.cpp
+++ b/src/network/socket/qnativesocketengine.cpp
@@ -873,6 +873,10 @@ qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 size, const Q
/*!
Writes a block of \a size bytes from \a data to the socket.
Returns the number of bytes written, or -1 if an error occurred.
+
+ Passing zero as the \a size parameter on a connected UDP socket
+ will send an empty datagram. For other socket types results are
+ unspecified.
*/
qint64 QNativeSocketEngine::write(const char *data, qint64 size)
{
diff --git a/src/network/socket/qnativesocketengine_winrt.cpp b/src/network/socket/qnativesocketengine_winrt.cpp
index bd9b443602..f8e92d3f50 100644
--- a/src/network/socket/qnativesocketengine_winrt.cpp
+++ b/src/network/socket/qnativesocketengine_winrt.cpp
@@ -317,26 +317,31 @@ bool QNativeSocketEngine::initialize(qintptr socketDescriptor, QAbstractSocket::
// Start processing incoming data
if (d->socketType == QAbstractSocket::TcpSocket) {
HRESULT hr;
- QEventDispatcherWinRT::runOnXamlThread([d, &hr, socket, this]() {
+ QEventDispatcherWinRT::runOnXamlThread([&hr, socket, socketState, this]() {
+ Q_D(QNativeSocketEngine);
ComPtr<IBuffer> buffer;
HRESULT hr = g->bufferFactory->Create(READ_BUFFER_SIZE, &buffer);
RETURN_OK_IF_FAILED("initialize(): Could not create buffer");
ComPtr<IInputStream> stream;
hr = socket->get_InputStream(&stream);
RETURN_OK_IF_FAILED("initialize(): Could not obtain input stream");
- hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, d->readOp.GetAddressOf());
+ ComPtr<IAsyncBufferOperation> readOp;
+ hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, readOp.GetAddressOf());
RETURN_OK_IF_FAILED_WITH_ARGS("initialize(): Failed to read from the socket buffer (%s).",
socketDescription(this).constData());
- hr = d->readOp->put_Completed(Callback<SocketReadCompletedHandler>(d, &QNativeSocketEnginePrivate::handleReadyRead).Get());
+ d->pendingReadOps.append(readOp);
+ d->socketState = socketState;
+ hr = readOp->put_Completed(Callback<SocketReadCompletedHandler>(d, &QNativeSocketEnginePrivate::handleReadyRead).Get());
RETURN_OK_IF_FAILED_WITH_ARGS("initialize(): Failed to set socket read callback (%s).",
socketDescription(this).constData());
return S_OK;
});
if (FAILED(hr))
return false;
+ } else {
+ d->socketState = socketState;
}
- d->socketState = socketState;
return true;
}
@@ -556,10 +561,12 @@ void QNativeSocketEngine::close()
ComPtr<IAsyncAction> action;
hr = socket3->CancelIOAsync(&action);
Q_ASSERT_SUCCEEDED(hr);
- hr = QWinRTFunctions::await(action);
+ hr = QWinRTFunctions::await(action, QWinRTFunctions::YieldThread, 5000);
// If there is no pending IO (no read established before) the function will fail with
// "function was called at an unexpected time" which is fine.
- if (hr != E_ILLEGAL_METHOD_CALL)
+ // Timeout is fine as well. The result will be the socket being hard reset instead of
+ // being closed gracefully
+ if (hr != E_ILLEGAL_METHOD_CALL && hr != ERROR_TIMEOUT)
Q_ASSERT_SUCCEEDED(hr);
return S_OK;
});
@@ -567,9 +574,9 @@ void QNativeSocketEngine::close()
}
#endif // _MSC_VER >= 1900
- if (d->readOp) {
+ for (ComPtr<IAsyncBufferOperation> readOp : d->pendingReadOps) {
ComPtr<IAsyncInfo> info;
- hr = d->readOp.As(&info);
+ hr = readOp.As(&info);
Q_ASSERT_SUCCEEDED(hr);
if (info) {
hr = info->Cancel();
@@ -933,9 +940,11 @@ void QNativeSocketEngine::establishRead()
hr = g->bufferFactory->Create(READ_BUFFER_SIZE, &buffer);
RETURN_HR_IF_FAILED("establishRead(): Failed to create buffer");
- hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, &d->readOp);
+ ComPtr<IAsyncBufferOperation> readOp;
+ hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, readOp.GetAddressOf());
RETURN_HR_IF_FAILED("establishRead(): Failed to initiate socket read");
- hr = d->readOp->put_Completed(Callback<SocketReadCompletedHandler>(d, &QNativeSocketEnginePrivate::handleReadyRead).Get());
+ d->pendingReadOps.append(readOp);
+ hr = readOp->put_Completed(Callback<SocketReadCompletedHandler>(d, &QNativeSocketEnginePrivate::handleReadyRead).Get());
RETURN_HR_IF_FAILED("establishRead(): Failed to register read callback");
return S_OK;
});
@@ -1410,7 +1419,15 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
}
Q_Q(QNativeSocketEngine);
+ for (int i = 0; i < pendingReadOps.count(); ++i) {
+ if (pendingReadOps.at(i).Get() == asyncInfo) {
+ pendingReadOps.takeAt(i);
+ break;
+ }
+ }
+ static QMutex mutex;
+ mutex.lock();
// A read in UnconnectedState will close the socket and return -1 and thus tell the caller,
// that the connection was closed. The socket cannot be closed here, as the subsequent read
// might fail then.
@@ -1463,6 +1480,7 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
if (notifyOnRead)
emit q->readReady();
+ mutex.unlock();
hr = QEventDispatcherWinRT::runOnXamlThread([buffer, q, this]() {
UINT32 readBufferLength;
@@ -1476,12 +1494,14 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
hr = buffer->put_Length(0);
RETURN_HR_IF_FAILED("handleReadyRead(): Could not set buffer length");
+ ComPtr<IAsyncBufferOperation> readOp;
hr = stream->ReadAsync(buffer.Get(), readBufferLength, InputStreamOptions_Partial, &readOp);
if (FAILED(hr)) {
qErrnoWarning(hr, "handleReadyRead(): Could not read into socket stream buffer (%s).",
socketDescription(q).constData());
return S_OK;
}
+ pendingReadOps.append(readOp);
hr = readOp->put_Completed(Callback<SocketReadCompletedHandler>(this, &QNativeSocketEnginePrivate::handleReadyRead).Get());
if (FAILED(hr)) {
qErrnoWarning(hr, "handleReadyRead(): Failed to set socket read callback (%s).",
diff --git a/src/network/socket/qnativesocketengine_winrt_p.h b/src/network/socket/qnativesocketengine_winrt_p.h
index 605f3631b9..79530d57f1 100644
--- a/src/network/socket/qnativesocketengine_winrt_p.h
+++ b/src/network/socket/qnativesocketengine_winrt_p.h
@@ -214,7 +214,7 @@ private:
{ return reinterpret_cast<ABI::Windows::Networking::Sockets::IDatagramSocket *>(socketDescriptor); }
Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocketListener> tcpListener;
Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncAction> connectOp;
- Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperationWithProgress<ABI::Windows::Storage::Streams::IBuffer *, UINT32>> readOp;
+ QVector<Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperationWithProgress<ABI::Windows::Storage::Streams::IBuffer *, UINT32>>> pendingReadOps;
QBuffer readBytes;
QMutex readMutex;
bool emitOnNewDatagram;