From 18f0793f9cc23042fe78efe2a3dcc8e6e9a90ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 27 Jan 2022 16:15:57 +0100 Subject: wasm: add support for blocking sockets Add support for blocking sockets on secondary threads and on the main thread with asyncify. This extends the support for websockify tunneled TCP sockets, which was previously limited to async sockets on the main thread. Blocking sockets support is implemented by emulating select() on top of emscripten's socket notification support. This is requires synchronization between the blockee threads and the main thread, since we get socket notification callbacks on the main thread. The synchronized state is held in g_socketState where the main thread registers socket readiness state and blocking threads register themselves. Blocking using asyncify on the main thread is similar to blocking on a secondary thread, with the exception that the main thread suspends with qt_asyncify_suspend() instead of waiting on a wait condition. Change-Id: Idb5a493644e1e6634057dc2f64f2e99e82e3c01e Reviewed-by: Lorn Potter --- src/network/socket/qnativesocketengine_unix.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/network/socket/qnativesocketengine_unix.cpp') diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index dfa18849a12..2c2ea706e52 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -11,6 +11,9 @@ #include "qvarlengtharray.h" #include "qnetworkinterface.h" #include "qendian.h" +#ifdef Q_OS_WASM +#include +#endif #include #include #include @@ -1353,6 +1356,8 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co return nativeSelect(timeout, selectForRead, !selectForRead, &dummy, &dummy); } +#ifndef Q_OS_WASM + int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite, bool *selectForRead, bool *selectForWrite) const { @@ -1383,4 +1388,24 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool c return ret; } +#else + +int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite, + bool *selectForRead, bool *selectForWrite) const +{ + *selectForRead = checkRead; + *selectForWrite = checkWrite; + bool socketDisconnect = false; + QEventDispatcherWasm::socketSelect(timeout, socketDescriptor, checkRead, checkWrite,selectForRead, selectForWrite, &socketDisconnect); + + // The disconnect/close handling code in QAbstractsScket::canReadNotification() + // does not detect remote disconnect properly; do that here as a workardound. + if (socketDisconnect) + receiver->closeNotification(); + + return 1; +} + +#endif // Q_OS_WASM + QT_END_NAMESPACE -- cgit v1.2.3