Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QNativeSocketEngine Class Reference

The QNativeSocketEngine class provides low level access to a socket. More...

#include <qnativesocketengine_p.h>

+ Inheritance diagram for QNativeSocketEngine:
+ Collaboration diagram for QNativeSocketEngine:

Public Slots

void connectionNotification ()
 If there's a connection activity on the socket, process it.
 
- Public Slots inherited from QAbstractSocketEngine
void readNotification ()
 
void writeNotification ()
 
void closeNotification ()
 
void exceptionNotification ()
 
void connectionNotification ()
 
void proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *authenticator)
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Public Member Functions

 QNativeSocketEngine (QObject *parent=nullptr)
 Constructs a QNativeSocketEngine.
 
 ~QNativeSocketEngine ()
 Destructs a QNativeSocketEngine.
 
bool initialize (QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol=QAbstractSocket::IPv4Protocol) override
 Initializes a QNativeSocketEngine by creating a new socket of type socketType and network layer protocol protocol.
 
bool initialize (qintptr socketDescriptor, QAbstractSocket::SocketState socketState=QAbstractSocket::ConnectedState) override
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Initializes the socket using socketDescriptor instead of creating a new one.
 
qintptr socketDescriptor () const override
 Returns the native socket descriptor.
 
bool isValid () const override
 Returns true if the socket is valid; otherwise returns false.
 
bool connectToHost (const QHostAddress &address, quint16 port) override
 Connects to the IP address and port specified by address and port.
 
bool connectToHostByName (const QString &name, quint16 port) override
 Connects to the remote host name given by name on port port.
 
bool bind (const QHostAddress &address, quint16 port) override
 Binds the socket to the address address and port port.
 
bool listen (int backlog) override
 Prepares a TCP server for accepting incoming connections.
 
qintptr accept () override
 Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket descriptor.
 
void close () override
 Closes the socket.
 
qint64 bytesAvailable () const override
 Returns the number of bytes that are currently available for reading.
 
qint64 read (char *data, qint64 maxlen) override
 Reads up to maxSize bytes into data from the socket.
 
qint64 write (const char *data, qint64 len) override
 Writes a block of size bytes from data to the socket.
 
bool joinMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface) override
 
bool leaveMulticastGroup (const QHostAddress &groupAddress, const QNetworkInterface &iface) override
 
QNetworkInterface multicastInterface () const override
 
bool setMulticastInterface (const QNetworkInterface &iface) override
 
bool hasPendingDatagrams () const override
 Returns true if there is at least one datagram pending.
 
qint64 pendingDatagramSize () const override
 Returns the size of the pending datagram, or -1 if no datagram is pending.
 
qint64 readDatagram (char *data, qint64 maxlen, QIpPacketHeader *=nullptr, PacketHeaderOptions=WantNone) override
 Reads up to maxSize bytes of a datagram from the socket, stores it in data and returns the number of bytes read.
 
qint64 writeDatagram (const char *data, qint64 len, const QIpPacketHeader &) override
 Writes a datagram of size size bytes to the socket from data to the destination contained in header, and returns the number of bytes written, or -1 if an error occurred.
 
qint64 bytesToWrite () const override
 
int option (SocketOption option) const override
 Returns the value of the option socketOption.
 
bool setOption (SocketOption option, int value) override
 Sets the option option to the value value.
 
bool waitForRead (QDeadlineTimer deadline=QDeadlineTimer{DefaultTimeout}, bool *timedOut=nullptr) override
 Waits until deadline has expired or until the socket is ready for reading.
 
bool waitForWrite (QDeadlineTimer deadline=QDeadlineTimer{DefaultTimeout}, bool *timedOut=nullptr) override
 Waits until deadline has expired or until the socket is ready for writing.
 
bool waitForReadOrWrite (bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, QDeadlineTimer deadline=QDeadlineTimer{DefaultTimeout}, bool *timedOut=nullptr) override
 
bool isReadNotificationEnabled () const override
 
void setReadNotificationEnabled (bool enable) override
 
bool isWriteNotificationEnabled () const override
 
void setWriteNotificationEnabled (bool enable) override
 
bool isExceptionNotificationEnabled () const override
 
void setExceptionNotificationEnabled (bool enable) override
 
- Public Member Functions inherited from QAbstractSocketEngine
 QAbstractSocketEngine (QObject *parent=nullptr)
 
QAbstractSocket::SocketError error () const
 
QString errorString () const
 
QAbstractSocket::SocketState state () const
 
QAbstractSocket::SocketType socketType () const
 
QAbstractSocket::NetworkLayerProtocol protocol () const
 
QHostAddress localAddress () const
 
quint16 localPort () const
 
QHostAddress peerAddress () const
 
quint16 peerPort () const
 
int inboundStreamCount () const
 
int outboundStreamCount () const
 
void setReceiver (QAbstractSocketEngineReceiver *receiver)
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Additional Inherited Members

- Public Types inherited from QAbstractSocketEngine
enum  SocketOption {
  NonBlockingSocketOption , BroadcastSocketOption , ReceiveBufferSocketOption , SendBufferSocketOption ,
  AddressReusable , BindExclusively , ReceiveOutOfBandData , LowDelayOption ,
  KeepAliveOption , MulticastTtlOption , MulticastLoopbackOption , TypeOfServiceOption ,
  ReceivePacketInformation , ReceiveHopLimit , MaxStreamsSocketOption , PathMtuInformation
}
 
enum  PacketHeaderOption {
  WantNone = 0 , WantDatagramSender = 0x01 , WantDatagramDestination = 0x02 , WantDatagramHopLimit = 0x04 ,
  WantStreamNumber = 0x08 , WantEndOfRecord = 0x10 , WantAll = 0xff
}
 Specifies which fields in the IP packet header are desired in the call to readDatagram(). More...
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 
- Static Public Member Functions inherited from QAbstractSocketEngine
static QAbstractSocketEnginecreateSocketEngine (QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent)
 
static QAbstractSocketEnginecreateSocketEngine (qintptr socketDescriptor, QObject *parent)
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Member Functions inherited from QAbstractSocketEngine
 QAbstractSocketEngine (QAbstractSocketEnginePrivate &dd, QObject *parent=nullptr)
 
void setError (QAbstractSocket::SocketError error, const QString &errorString) const
 
void setState (QAbstractSocket::SocketState state)
 
void setSocketType (QAbstractSocket::SocketType socketType)
 
void setProtocol (QAbstractSocket::NetworkLayerProtocol protocol)
 
void setLocalAddress (const QHostAddress &address)
 
void setLocalPort (quint16 port)
 
void setPeerAddress (const QHostAddress &address)
 
void setPeerPort (quint16 port)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Detailed Description

The QNativeSocketEngine class provides low level access to a socket.

\reentrant

\inmodule QtNetwork

QtSocketLayer provides basic socket functionality provided by the operating system. It also keeps track of what state the socket is in, and which errors that occur.

The classes QTcpSocket, QUdpSocket and QTcpServer provide a higher level API, and are in general more useful for the common application.

There are two main ways of initializing the a QNativeSocketEngine; either create a new socket by passing the socket type (TcpSocket or UdpSocket) and network layer protocol (IPv4Protocol or IPv6Protocol) to initialize(), or pass an existing socket descriptor and have QNativeSocketEngine determine the type and protocol itself. The native socket descriptor can later be fetched by calling socketDescriptor(). The socket is made non-blocking, but blocking behavior can still be achieved by calling waitForRead() and waitForWrite(). isValid() can be called to check if the socket has been successfully initialized and is ready to use.

To connect to a host, determine its address and pass this and the port number to connectToHost(). The socket can then be used as a TCP or UDP client. Otherwise; bind(), listen() and accept() are used to have the socket function as a TCP or UDP server. Call close() to close the socket.

bytesAvailable() is called to determine how much data is available for reading. read() and write() are used by both TCP and UDP clients to exchange data with the connected peer. UDP clients can also call hasMoreDatagrams(), nextDatagramSize(), readDatagram(), and writeDatagram().

Call state() to determine the state of the socket, for example, ListeningState or ConnectedState. socketType() tells whether the socket is a TCP socket or a UDP socket, or if the socket type is unknown. protocol() is used to determine the socket's network layer protocol.

localAddress(), localPort() are called to find the address and port that are currently bound to the socket. If the socket is connected, peerAddress() and peerPort() determine the address and port of the connected peer.

Finally, if any function should fail, error() and errorString() can be called to determine the cause of the error.

Definition at line 101 of file qnativesocketengine_p.h.

Constructor & Destructor Documentation

◆ QNativeSocketEngine()

QNativeSocketEngine::QNativeSocketEngine ( QObject * parent = nullptr)

Constructs a QNativeSocketEngine.

See also
initialize()

Definition at line 384 of file qnativesocketengine.cpp.

◆ ~QNativeSocketEngine()

QNativeSocketEngine::~QNativeSocketEngine ( )

Destructs a QNativeSocketEngine.

Definition at line 392 of file qnativesocketengine.cpp.

References close().

+ Here is the call graph for this function:

Member Function Documentation

◆ accept()

qintptr QNativeSocketEngine::accept ( )
overridevirtual

Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket descriptor.

If no pending connections are available, -1 is returned.

See also
bind(), listen()

Implements QAbstractSocketEngine.

Definition at line 682 of file qnativesocketengine.cpp.

References accept(), d, QAbstractSocket::ListeningState, Q_CHECK_STATE, Q_CHECK_TYPE, Q_CHECK_TYPES, Q_CHECK_VALID_SOCKETLAYER, QAbstractSocket::SctpSocket, and QAbstractSocket::TcpSocket.

Referenced by accept(), and if().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bind()

bool QNativeSocketEngine::bind ( const QHostAddress & address,
quint16 port )
overridevirtual

Binds the socket to the address address and port port.

Returns true on success; otherwise false is returned. The port may be 0, in which case an arbitrary unused port is assigned automatically by the operating system.

Servers call this function to set up the server's address and port. TCP servers must in addition call listen() after bind().

Implements QAbstractSocketEngine.

Definition at line 628 of file qnativesocketengine.cpp.

References bind(), d, Q_CHECK_STATE, Q_CHECK_VALID_SOCKETLAYER, and QAbstractSocket::UnconnectedState.

Referenced by bind().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bytesAvailable()

qint64 QNativeSocketEngine::bytesAvailable ( ) const
overridevirtual

Returns the number of bytes that are currently available for reading.

On error, -1 is returned.

For UDP sockets, this function returns the accumulated size of all pending datagrams, and it is therefore more useful for UDP sockets to call hasPendingDatagrams() and pendingDatagramSize().

Implements QAbstractSocketEngine.

Definition at line 705 of file qnativesocketengine.cpp.

References bytesAvailable(), d, Q_CHECK_NOT_STATE, Q_CHECK_VALID_SOCKETLAYER, and QAbstractSocket::UnconnectedState.

Referenced by bytesAvailable().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bytesToWrite()

qint64 QNativeSocketEngine::bytesToWrite ( ) const
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 886 of file qnativesocketengine.cpp.

◆ close()

void QNativeSocketEngine::close ( )
overridevirtual

Closes the socket.

In order to use the socket again, initialize() must be called.

Implements QAbstractSocketEngine.

Definition at line 929 of file qnativesocketengine.cpp.

References d, and QAbstractSocket::UnconnectedState.

Referenced by ~QNativeSocketEngine(), initialize(), initialize(), and read().

+ Here is the caller graph for this function:

◆ connectionNotification

void QNativeSocketEngine::connectionNotification ( )
slot

If there's a connection activity on the socket, process it.

Then notify our parent if there really was activity.

Definition at line 589 of file qnativesocketengine.cpp.

References QAbstractSocket::ConnectingState, QAbstractSocketEngine::connectionNotification(), connectToHost(), d, Q_ASSERT, and QAbstractSocketEngine::state().

Referenced by QWriteNotifier::event(), and QExceptionNotifier::event().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ connectToHost()

bool QNativeSocketEngine::connectToHost ( const QHostAddress & address,
quint16 port )
overridevirtual

Connects to the IP address and port specified by address and port.

If the connection is established, this function returns true and the socket enters ConnectedState. Otherwise, false is returned.

If false is returned, state() should be called to see if the socket is in ConnectingState. If so, a delayed TCP connection is taking place, and connectToHost() must be called again later to determine if the connection was established successfully or not. The second connection attempt must be made when the socket is ready for writing. This state can be determined either by connecting a QSocketNotifier to the socket descriptor returned by socketDescriptor(), or by calling the blocking function waitForWrite().

Example:

Otherwise, error() should be called to determine the cause of the error.

Implements QAbstractSocketEngine.

Definition at line 565 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, connected, QAbstractSocket::ConnectingState, connectToHost(), d, Q_CHECK_STATES3, Q_CHECK_VALID_SOCKETLAYER, and QAbstractSocket::UnconnectedState.

Referenced by connectionNotification(), connectToHost(), waitForRead(), waitForReadOrWrite(), and waitForWrite().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ connectToHostByName()

bool QNativeSocketEngine::connectToHostByName ( const QString & name,
quint16 port )
overridevirtual

Connects to the remote host name given by name on port port.

When this function is called, the upper-level will not perform a hostname lookup.

The native socket engine does not support this operation, but some other socket engines (notably proxy-based ones) do.

Implements QAbstractSocketEngine.

Definition at line 609 of file qnativesocketengine.cpp.

References d, QNativeSocketEnginePrivate::OperationUnsupportedErrorString, Q_UNUSED, and QAbstractSocket::UnsupportedSocketOperationError.

◆ hasPendingDatagrams()

bool QNativeSocketEngine::hasPendingDatagrams ( ) const
overridevirtual

Returns true if there is at least one datagram pending.

This function is only called by UDP sockets, where a datagram can have a size of 0. TCP sockets call bytesAvailable().

Implements QAbstractSocketEngine.

Definition at line 785 of file qnativesocketengine.cpp.

References d, hasPendingDatagrams(), Q_CHECK_NOT_STATE, Q_CHECK_TYPE, Q_CHECK_VALID_SOCKETLAYER, QAbstractSocket::UdpSocket, and QAbstractSocket::UnconnectedState.

Referenced by hasPendingDatagrams().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initialize() [1/2]

bool QNativeSocketEngine::initialize ( QAbstractSocket::SocketType socketType,
QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol )
overridevirtual

Initializes a QNativeSocketEngine by creating a new socket of type socketType and network layer protocol protocol.

Returns true on success; otherwise returns false.

If the socket was already initialized, this function closes the socket before reeinitializing it.

The new socket is non-blocking, and for UDP sockets it's also broadcast enabled.

Implements QAbstractSocketEngine.

Definition at line 408 of file qnativesocketengine.cpp.

References QNativeSocketEnginePrivate::BroadcastingInitFailedErrorString, QAbstractSocketEngine::BroadcastSocketOption, close(), d, QAbstractSocket::IPv4Protocol, QAbstractSocket::IPv6Protocol, isValid(), QAbstractSocketEngine::protocol(), qDebug, qWarning, QAbstractSocketEngine::ReceiveHopLimit, QAbstractSocketEngine::ReceiveOutOfBandData, QAbstractSocketEngine::ReceivePacketInformation, QAbstractSocket::SctpSocket, setOption(), QAbstractSocketEngine::socketType(), QAbstractSocket::TcpSocket, typeStr(), QAbstractSocket::UdpSocket, and QAbstractSocket::UnsupportedSocketOperationError.

+ Here is the call graph for this function:

◆ initialize() [2/2]

bool QNativeSocketEngine::initialize ( qintptr socketDescriptor,
QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState )
overridevirtual

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Initializes the socket using socketDescriptor instead of creating a new one.

The socket type and network layer protocol are determined automatically. The socket's state is set to socketState.

If the socket type is either TCP or UDP, it is made non-blocking. UDP sockets are also broadcast enabled.

Implements QAbstractSocketEngine.

Definition at line 480 of file qnativesocketengine.cpp.

References QNativeSocketEnginePrivate::BroadcastingInitFailedErrorString, QAbstractSocketEngine::BroadcastSocketOption, close(), d, isValid(), QNativeSocketEnginePrivate::NonBlockingInitFailedErrorString, QAbstractSocketEngine::NonBlockingSocketOption, qDebug, setOption(), socketDescriptor(), QAbstractSocket::UdpSocket, QAbstractSocket::UnknownSocketType, and QAbstractSocket::UnsupportedSocketOperationError.

+ Here is the call graph for this function:

◆ isExceptionNotificationEnabled()

bool QNativeSocketEngine::isExceptionNotificationEnabled ( ) const
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 1331 of file qnativesocketengine.cpp.

References d.

◆ isReadNotificationEnabled()

bool QNativeSocketEngine::isReadNotificationEnabled ( ) const
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 1208 of file qnativesocketengine.cpp.

References d.

◆ isValid()

bool QNativeSocketEngine::isValid ( ) const
overridevirtual

Returns true if the socket is valid; otherwise returns false.

A socket is valid if it has not been successfully initialized, or if it has been closed.

Implements QAbstractSocketEngine.

Definition at line 527 of file qnativesocketengine.cpp.

References d.

Referenced by initialize(), and initialize().

+ Here is the caller graph for this function:

◆ isWriteNotificationEnabled()

bool QNativeSocketEngine::isWriteNotificationEnabled ( ) const
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 1314 of file qnativesocketengine.cpp.

References d.

◆ joinMulticastGroup()

bool QNativeSocketEngine::joinMulticastGroup ( const QHostAddress & groupAddress,
const QNetworkInterface & iface )
overridevirtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 720 of file qnativesocketengine.cpp.

References QAbstractSocket::AnyIPProtocol, QAbstractSocket::BoundState, d, QAbstractSocket::IPv4Protocol, QAbstractSocket::IPv6Protocol, joinMulticastGroup(), Q_CHECK_STATE, Q_CHECK_TYPE, Q_CHECK_VALID_SOCKETLAYER, qWarning, and QAbstractSocket::UdpSocket.

Referenced by joinMulticastGroup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ leaveMulticastGroup()

bool QNativeSocketEngine::leaveMulticastGroup ( const QHostAddress & groupAddress,
const QNetworkInterface & iface )
overridevirtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 746 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, d, leaveMulticastGroup(), Q_CHECK_STATE, Q_CHECK_TYPE, Q_CHECK_VALID_SOCKETLAYER, and QAbstractSocket::UdpSocket.

Referenced by leaveMulticastGroup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ listen()

bool QNativeSocketEngine::listen ( int backlog)
overridevirtual

Prepares a TCP server for accepting incoming connections.

This function must be called after bind(), and only by TCP sockets.

After this function has been called, pending client connections are detected by checking if the socket is ready for reading. This can be done by either creating a QSocketNotifier, passing the socket descriptor returned by socketDescriptor(), or by calling the blocking function waitForRead().

Example:

// a client is connected
}
static constexpr auto IPv4Protocol
The QNativeSocketEngine class provides low level access to a socket.
qintptr accept() override
Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket...
bool listen(int backlog) override
Prepares a TCP server for accepting incoming connections.
bool waitForRead(QDeadlineTimer deadline=QDeadlineTimer{DefaultTimeout}, bool *timedOut=nullptr) override
Waits until deadline has expired or until the socket is ready for reading.
bool connectToHost(const QHostAddress &address, quint16 port) override
Connects to the IP address and port specified by address and port.
bool bind(const QHostAddress &address, quint16 port) override
Binds the socket to the address address and port port.
bool waitForWrite(QDeadlineTimer deadline=QDeadlineTimer{DefaultTimeout}, bool *timedOut=nullptr) override
Waits until deadline has expired or until the socket is ready for writing.
bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol=QAbstractSocket::IPv4Protocol) override
Initializes a QNativeSocketEngine by creating a new socket of type socketType and network layer proto...
QNativeSocketEngine socketLayer
[0]
See also
bind(), accept()

Implements QAbstractSocketEngine.

Definition at line 660 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, d, listen(), Q_CHECK_STATE, Q_CHECK_TYPE, Q_CHECK_TYPES, Q_CHECK_VALID_SOCKETLAYER, QAbstractSocket::SctpSocket, and QAbstractSocket::TcpSocket.

Referenced by listen().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ multicastInterface()

QNetworkInterface QNativeSocketEngine::multicastInterface ( ) const
overridevirtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 759 of file qnativesocketengine.cpp.

References d, multicastInterface(), Q_CHECK_TYPE, Q_CHECK_VALID_SOCKETLAYER, and QAbstractSocket::UdpSocket.

Referenced by multicastInterface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ option()

int QNativeSocketEngine::option ( SocketOption option) const
overridevirtual

Returns the value of the option socketOption.

Implements QAbstractSocketEngine.

Definition at line 1202 of file qnativesocketengine.cpp.

References d.

◆ pendingDatagramSize()

qint64 QNativeSocketEngine::pendingDatagramSize ( ) const
overridevirtual

Returns the size of the pending datagram, or -1 if no datagram is pending.

A datagram size of 0 is perfectly valid. This function is called by UDP sockets before receiveMessage(). For TCP sockets, call bytesAvailable().

Implements QAbstractSocketEngine.

Definition at line 801 of file qnativesocketengine.cpp.

References d, pendingDatagramSize(), Q_CHECK_TYPE, Q_CHECK_VALID_SOCKETLAYER, and QAbstractSocket::UdpSocket.

Referenced by pendingDatagramSize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read()

qint64 QNativeSocketEngine::read ( char * data,
qint64 maxSize )
overridevirtual

Reads up to maxSize bytes into data from the socket.

Returns the number of bytes read, or -1 if an error occurred.

Implements QAbstractSocketEngine.

Definition at line 895 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, close(), QAbstractSocket::ConnectedState, d, QAbstractSocket::NetworkError, Q_CHECK_STATES, Q_CHECK_VALID_SOCKETLAYER, qt_error_string(), read(), QAbstractSocket::RemoteHostClosedError, QNativeSocketEnginePrivate::RemoteHostClosedErrorString, QAbstractSocket::SctpSocket, and QAbstractSocket::TcpSocket.

Referenced by read().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readDatagram()

qint64 QNativeSocketEngine::readDatagram ( char * data,
qint64 maxSize,
QIpPacketHeader * header = nullptr,
PacketHeaderOptions options = WantNone )
overridevirtual

Reads up to maxSize bytes of a datagram from the socket, stores it in data and returns the number of bytes read.

The address, port, and other IP header fields are stored in header according to the request in options.

For UDP sockets, to avoid unnecessarily loss of data, call pendingDatagramSize() to determine the size of the pending message before reading it. If maxSize is too small, the rest of the datagram will be lost.

Returns -1 if an error occurred.

See also
hasPendingDatagrams()

Implements QAbstractSocketEngine.

Definition at line 826 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, QAbstractSocket::ConnectedState, d, header(), Q_CHECK_STATES, Q_CHECK_VALID_SOCKETLAYER, and readDatagram().

Referenced by readDatagram().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setExceptionNotificationEnabled()

void QNativeSocketEngine::setExceptionNotificationEnabled ( bool enable)
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 1337 of file qnativesocketengine.cpp.

References d.

◆ setMulticastInterface()

bool QNativeSocketEngine::setMulticastInterface ( const QNetworkInterface & iface)
overridevirtual
Since
4.8

Implements QAbstractSocketEngine.

Definition at line 770 of file qnativesocketengine.cpp.

References d, Q_CHECK_TYPE, Q_CHECK_VALID_SOCKETLAYER, setMulticastInterface(), and QAbstractSocket::UdpSocket.

Referenced by setMulticastInterface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setOption()

bool QNativeSocketEngine::setOption ( SocketOption option,
int value )
overridevirtual

Sets the option option to the value value.

Implements QAbstractSocketEngine.

Definition at line 1193 of file qnativesocketengine.cpp.

References d.

Referenced by initialize(), and initialize().

+ Here is the caller graph for this function:

◆ setReadNotificationEnabled()

void QNativeSocketEngine::setReadNotificationEnabled ( bool enable)
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 1303 of file qnativesocketengine.cpp.

References d.

◆ setWriteNotificationEnabled()

void QNativeSocketEngine::setWriteNotificationEnabled ( bool enable)
overridevirtual

Implements QAbstractSocketEngine.

Definition at line 1320 of file qnativesocketengine.cpp.

References d.

◆ socketDescriptor()

qintptr QNativeSocketEngine::socketDescriptor ( ) const
overridevirtual

Returns the native socket descriptor.

Any use of this descriptor stands the risk of being non-portable.

Implements QAbstractSocketEngine.

Definition at line 537 of file qnativesocketengine.cpp.

References d.

Referenced by initialize().

+ Here is the caller graph for this function:

◆ waitForRead()

bool QNativeSocketEngine::waitForRead ( QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout},
bool * timedOut = nullptr )
overridevirtual

Waits until deadline has expired or until the socket is ready for reading.

If timedOut is not \nullptr and deadline has expired, the value of timedOut is set to true.

Returns true if data is available for reading; otherwise returns false.

This is a blocking function call; its use is disadvised in a single threaded application, as the whole thread will stop responding until the function returns. waitForRead() is most useful when there is no event loop available. The general approach is to create a QSocketNotifier, passing the socket descriptor returned by socketDescriptor() to its constructor.

Implements QAbstractSocketEngine.

Definition at line 979 of file qnativesocketengine.cpp.

References QAbstractSocket::ConnectingState, connectToHost(), d, deadline, Q_CHECK_NOT_STATE, Q_CHECK_VALID_SOCKETLAYER, ret, QAbstractSocket::SocketTimeoutError, QAbstractSocketEngine::state(), QNativeSocketEnginePrivate::TimeOutErrorString, QAbstractSocket::UnconnectedState, and waitForRead().

Referenced by waitForRead().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ waitForReadOrWrite()

bool QNativeSocketEngine::waitForReadOrWrite ( bool * readyToRead,
bool * readyToWrite,
bool checkRead,
bool checkWrite,
QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout},
bool * timedOut = nullptr )
overridevirtual

◆ waitForWrite()

bool QNativeSocketEngine::waitForWrite ( QDeadlineTimer deadline = QDeadlineTimer{DefaultTimeout},
bool * timedOut = nullptr )
overridevirtual

Waits until deadline has expired or until the socket is ready for writing.

If timedOut is not \nullptr and deadline has expired, the value of timedOut is set to true.

Returns true if data is available for writing; otherwise returns false.

This is a blocking function call; its use is disadvised in a single threaded application, as the whole thread will stop responding until the function returns. waitForWrite() is most useful when there is no event loop available. The general approach is to create a QSocketNotifier, passing the socket descriptor returned by socketDescriptor() to its constructor.

Implements QAbstractSocketEngine.

Definition at line 1019 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, QAbstractSocket::ConnectedState, QAbstractSocket::ConnectingState, QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString, connectToHost(), d, deadline, QNativeSocketEnginePrivate::HostUnreachableErrorString, QAbstractSocket::NetworkError, Q_CHECK_NOT_STATE, Q_CHECK_VALID_SOCKETLAYER, ret, QAbstractSocketEngine::setState(), QAbstractSocket::SocketTimeoutError, QAbstractSocketEngine::state(), QNativeSocketEnginePrivate::TimeOutErrorString, QAbstractSocket::UnconnectedState, and waitForWrite().

Referenced by waitForWrite().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write()

qint64 QNativeSocketEngine::write ( const char * data,
qint64 size )
overridevirtual

Writes a block of size bytes from data to the socket.

Returns the number of bytes written, or -1 if an error occurred.

Passing zero as the size parameter on a connected UDP socket will send an empty datagram. For other socket types results are unspecified.

Implements QAbstractSocketEngine.

Definition at line 877 of file qnativesocketengine.cpp.

References QAbstractSocket::ConnectedState, d, Q_CHECK_STATE, Q_CHECK_VALID_SOCKETLAYER, and write().

Referenced by write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeDatagram()

qint64 QNativeSocketEngine::writeDatagram ( const char * data,
qint64 size,
const QIpPacketHeader & header )
overridevirtual

Writes a datagram of size size bytes to the socket from data to the destination contained in header, and returns the number of bytes written, or -1 if an error occurred.

If header contains other settings like hop limit or source address, this function will try to pass them to the operating system too, but will not indicate an error if it could not pass them.

Only one datagram is sent, and if there is too much data to fit into a single datagram, the operation will fail and error() will return QAbstractSocket::DatagramTooLargeError. Operating systems impose an upper limit to the size of a datagram, but this size is different on almost all platforms. Sending large datagrams is in general disadvised, as even if they are sent successfully, they are likely to be fragmented before arriving at their destination.

Experience has shown that it is in general safe to send IPv4 datagrams no larger than 512 bytes or IPv6 datagrams no larger than 1280 (the minimum MTU).

See also
readDatagram()

Implements QAbstractSocketEngine.

Definition at line 859 of file qnativesocketengine.cpp.

References QAbstractSocket::BoundState, QAbstractSocket::ConnectedState, d, header(), Q_CHECK_STATES, Q_CHECK_VALID_SOCKETLAYER, and writeDatagram().

Referenced by writeDatagram().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: