11#include <QtCore/QLoggingCategory>
12#include <QtCore/private/qfunctions_winrt_p.h>
14#include <windows.networking.h>
15#include <windows.networking.connectivity.h>
16#include <windows.networking.sockets.h>
19using namespace Microsoft::WRL::Wrappers;
21using namespace ABI::Windows::Devices::Enumeration;
24using namespace ABI::Windows::Networking::Sockets;
25using namespace ABI::Windows::Networking::Connectivity;
27typedef ITypedEventHandler<StreamSocketListener *, StreamSocketListenerConnectionReceivedEventArgs *>
ClientConnectedHandler;
37 : serverType(sType), q_ptr(
parent)
44 deactivateActiveListening();
48 socketListener =
nullptr;
52bool QBluetoothServerPrivate::isListening()
const
57bool QBluetoothServerPrivate::initiateActiveListening(
const QString &serviceName)
59 HStringReference serviceNameRef(
reinterpret_cast<LPCWSTR
>(serviceName.
utf16()));
62 HRESULT hr = socketListener->BindServiceNameAsync(serviceNameRef.Get(), &bindAction);
63 Q_ASSERT_SUCCEEDED(hr);
64 hr = QWinRTFunctions::await(bindAction);
65 Q_ASSERT_SUCCEEDED(hr);
69bool QBluetoothServerPrivate::deactivateActiveListening()
75 hr = socketListener->remove_ConnectionReceived(connectionToken);
76 Q_ASSERT_SUCCEEDED(hr);
80HRESULT QBluetoothServerPrivate::handleClientConnection(IStreamSocketListener *listener,
81 IStreamSocketListenerConnectionReceivedEventArgs *
args)
84 if (!socketListener || socketListener.Get() != listener) {
85 qCDebug(QT_BT_WINDOWS) <<
"Accepting connection from wrong listener. We should not be here.";
93 Q_ASSERT_SUCCEEDED(hr);
96 qCDebug(QT_BT_WINDOWS) <<
"Accepting connection";
97 pendingConnections.append(
socket);
101 qCDebug(QT_BT_WINDOWS) <<
"Refusing connection";
111 d->deactivateActiveListening();
128 HRESULT hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Networking_Sockets_StreamSocketListener).Get(),
130 Q_ASSERT_SUCCEEDED(hr);
131 hr =
d->socketListener->add_ConnectionReceived(Callback<ClientConnectedHandler>(
d, &QBluetoothServerPrivate::handleClientConnection).Get(),
132 &
d->connectionToken);
133 Q_ASSERT_SUCCEEDED(hr);
138 for (
int i = 1; ;
i++){
149 qCDebug(QT_BT_WINDOWS) <<
"Port" <<
port <<
"registered";
151 qCWarning(QT_BT_WINDOWS) <<
"server with port" <<
port <<
"already registered or port invalid";
164 if (
d->pendingConnections.size() > numConnections) {
165 qCWarning(QT_BT_WINDOWS) <<
"There are currently more than" << numConnections <<
"connections"
166 <<
"pending. Number of maximum pending connections was not changed.";
170 d->maxPendingConnections = numConnections;
177 return !
d->pendingConnections.isEmpty();
183 if (
d->pendingConnections.isEmpty())
205 return hosts.
at(0).address();
static QList< QBluetoothHostInfo > allDevices()
Returns a list of all available local Bluetooth devices.
~QBluetoothServerPrivate()
int maxPendingConnections
QBluetoothSocket * socket
bool hasPendingConnections() const
Returns true if a connection is pending, otherwise false.
void setSecurityFlags(QBluetooth::SecurityFlags security)
Sets the Bluetooth security flags to security.
@ UnsupportedProtocolError
@ ServiceAlreadyRegisteredError
bool isListening() const
Returns true if the server is listening for incoming connections, otherwise false.
QBluetoothSocket * nextPendingConnection()
Returns a pointer to the QBluetoothSocket for the next pending connection.
bool listen(const QBluetoothAddress &address=QBluetoothAddress(), quint16 port=0)
Start listening for incoming connections to address on port.
void close()
Closes and resets the listening socket.
void errorOccurred(QBluetoothServer::Error error)
This signal is emitted when an error occurs.
quint16 serverPort() const
Returns the server port number.
QBluetoothAddress serverAddress() const
Returns the server address.
QBluetooth::SecurityFlags securityFlags() const
Returns the Bluetooth security flags.
void setMaxPendingConnections(int numConnections)
Sets the maximum number of pending connections to numConnections.
QBluetoothServiceInfo::Protocol serverType() const
Returns the type of the QBluetoothServer.
virtual bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState=QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::OpenMode openMode=QBluetoothSocket::ReadWrite)=0
QBluetoothSocketBasePrivate * d_ptr
bool isEmpty() const noexcept
const_reference at(qsizetype i) const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
const ushort * utf16() const
Returns the QString as a '\0\'-terminated array of unsigned shorts.
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE QHash< QBluetoothServerPrivate *, int > __fakeServerPorts
ITypedEventHandler< StreamSocketListener *, StreamSocketListenerConnectionReceivedEventArgs * > ClientConnectedHandler
QT_BEGIN_NAMESPACE QHash< QBluetoothServerPrivate *, int > __fakeServerPorts
void mainThreadCoInit(void *caller)
void mainThreadCoUninit(void *caller)
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent