KNetwork::KSocketBase

Search for usage in LXR

#include <k3socketbase.h>

Inheritance diagram for KNetwork::KSocketBase:

Public Types

enum  SocketError {
  NoError = 0, LookupFailure, AddressInUse, AlreadyCreated,
  AlreadyBound, AlreadyConnected, NotConnected, NotBound,
  NotCreated, WouldBlock, ConnectionRefused, ConnectionTimedOut,
  InProgress, NetFailure, NotSupported, Timeout,
  UnknownError, RemotelyDisconnected
}
 
enum  SocketOptions {
  Blocking = 0x01, AddressReuseable = 0x02, IPv6Only = 0x04, Keepalive = 0x08,
  Broadcast = 0x10, NoDelay = 0x20
}
 

Public Member Functions

 KSocketBase ()
 
virtual ~KSocketBase ()
 
bool addressReuseable () const
 
bool blocking () const
 
bool broadcast () const
 
SocketError error () const
 
QString errorString () const
 
bool isIPv6Only () const
 
QMutexmutex () const
 
bool noDelay () const
 
virtual bool setAddressReuseable (bool enable)
 
virtual bool setBlocking (bool enable)
 
virtual bool setBroadcast (bool enable)
 
virtual bool setIPv6Only (bool enable)
 
virtual bool setNoDelay (bool enable)
 
int setRequestedCapabilities (int add, int remove=0)
 
virtual void setSocketDevice (KSocketDevice *device)
 
KSocketDevicesocketDevice () const
 

Static Public Member Functions

static QString errorString (SocketError code)
 
static bool isFatalError (int code)
 

Protected Member Functions

bool hasDevice () const
 
void resetError ()
 
void setError (SocketError error)
 
virtual bool setSocketOptions (int opts)
 
virtual int socketOptions () const
 

Detailed Description

Basic socket functionality.

This class provides the basic socket functionlity for descended classes. Socket classes are thread-safe and provide a recursive mutex should it be needed.

Note
This class is abstract.
Author
Thiago Macieira thiag[email protected][email protected][email protected]e.org
Deprecated:
Use KSocketFactory or KLocalSocket instead

Definition at line 86 of file k3socketbase.h.

Member Enumeration Documentation

◆ SocketError

Possible socket error codes.

This is a list of possible error conditions that socket classes may be expected to find.

  • NoError: no error has been detected
  • LookupFailure: if a name lookup has failed
  • AddressInUse: address is already in use
  • AlreadyBound: cannot bind again
  • AlreadyCreated: cannot recreate the socket
  • NotBound: operation required socket to be bound and it isn't
  • NotCreated: operation required socket to exist and it doesn't
  • WouldBlock: requested I/O operation would block
  • ConnectionRefused: connection actively refused
  • ConnectionTimedOut: connection timed out
  • InProgress: operation (connection) is already in progress
  • NetFailure: a network failure occurred (no route, host down, host unreachable or similar)
  • NotSupported: requested operation is not supported
  • Timeout: a timed operation timed out
  • UnknownError: an unknown/unexpected error has happened
  • RemotelyDisconnected: when a connection is disconnected by the other end (since 3.4)
See also
error, errorString

Definition at line 143 of file k3socketbase.h.

◆ SocketOptions

Possible socket options.

These are the options that may be set on a socket:

  • Blocking: whether the socket shall operate in blocking or non-blocking mode. This flag defaults to on. See setBlocking().
  • AddressReusable: whether the address used by this socket will be available for reuse by other sockets. This flag defaults to off. See setAddressReuseable().
  • IPv6Only: whether an IPv6 socket will accept IPv4 connections through a mapped address. This flag defaults to off. See setIPv6Only().
  • KeepAlive: whether TCP should send keepalive probes when a connection has gone idle for far too long.
  • Broadcast: whether this socket is allowed to send broadcast packets and will receive packets sent to broadcast.
  • NoDelay: disable the Nagle algorithm for socket types that support it.

Definition at line 109 of file k3socketbase.h.

Constructor & Destructor Documentation

◆ KSocketBase()

KSocketBase::KSocketBase ( )

Default constructor.

Definition at line 66 of file k3socketbase.cpp.

◆ ~KSocketBase()

KSocketBase::~KSocketBase ( )
virtual

Destructor.

Definition at line 78 of file k3socketbase.cpp.

Member Function Documentation

◆ addressReuseable()

bool KSocketBase::addressReuseable ( ) const

Retrieves this socket's address reuseability flag.

Returns
true if this socket's address can be reused, false if it can't.

Definition at line 110 of file k3socketbase.cpp.

◆ blocking()

bool KSocketBase::blocking ( ) const

Retrieves this socket's blocking mode.

Returns
true if this socket is/will be operated in blocking mode, false if non-blocking.

Definition at line 100 of file k3socketbase.cpp.

◆ broadcast()

bool KSocketBase::broadcast ( ) const

Retrieves this socket's Broadcast flag.

Returns
true if this socket can send and receive broadcast packets, false if it can't.

Definition at line 130 of file k3socketbase.cpp.

◆ error()

KSocketBase::SocketError KSocketBase::error ( ) const

Retrieves the socket error code.

See also
errorString

Definition at line 199 of file k3socketbase.cpp.

◆ errorString() [1/2]

QString KSocketBase::errorString ( ) const

Returns the error string corresponding to this error condition.

Definition at line 204 of file k3socketbase.cpp.

◆ errorString() [2/2]

QString KSocketBase::errorString ( KSocketBase::SocketError  code)
static

Returns the string describing the given error code, i18n'ed.

Parameters
codethe error code

Definition at line 210 of file k3socketbase.cpp.

◆ hasDevice()

bool KSocketBase::hasDevice ( ) const
protected

Returns true if the socket device has been initialised in this object, either by calling socketDevice() or setSocketDevice()

Definition at line 184 of file k3socketbase.cpp.

◆ isFatalError()

bool KSocketBase::isFatalError ( int  code)
static

Returns true if the given error code is a fatal one, false otherwise.

The parameter here is of type int so that casting isn't necessary when using the parameter to signal QClientSocketBase::gotError.

Parameters
codethe code to test

Definition at line 302 of file k3socketbase.cpp.

◆ isIPv6Only()

bool KSocketBase::isIPv6Only ( ) const

Retrieves this socket's IPv6 Only flag.

Returns
true if this socket will ignore IPv4-compatible and IPv4-mapped addresses, false if it will accept them.

Definition at line 120 of file k3socketbase.cpp.

◆ mutex()

QMutex * KSocketBase::mutex ( ) const

Returns the internal mutex for this class.

Note on multithreaded use of sockets: the socket classes are thread-safe by design, but you should be aware of problems regarding socket creation, connection and destruction in multi-threaded programs. The classes are guaranteed to work while the socket exists, but it's not wise to call connect in multiple threads.

Also, this mutex must be unlocked before the object is destroyed, which means you cannot use it to guard against other threads accessing the object while destroying it. You must ensure there are no further references to this object when deleting it.

Definition at line 320 of file k3socketbase.cpp.

◆ noDelay()

bool KSocketBase::noDelay ( ) const

Retrieves this socket's NoDelay flag.

Returns
true if this socket's Nagle algorithm is disabled.

Definition at line 140 of file k3socketbase.cpp.

◆ resetError()

void KSocketBase::resetError ( )
protected

Resets the socket error code and the I/O Device's status.

Definition at line 194 of file k3socketbase.cpp.

◆ setAddressReuseable()

bool KSocketBase::setAddressReuseable ( bool  enable)
virtual

Sets this socket's address reuseable flag.

When the address reuseable flag is active, the address used by this socket is left reuseable for other sockets to bind. If the flag is not active, no other sockets may reuse the same address.

The default implementation toggles the AddressReuseable flag with the current socket options and calls setSocketOptions().

Parameters
enablewhether to set the flag on or off
Returns
true if setting this flag was successful

Definition at line 105 of file k3socketbase.cpp.

◆ setBlocking()

bool KSocketBase::setBlocking ( bool  enable)
virtual

Sets this socket's blocking mode.

In blocking operation, all I/O functions are susceptible to blocking – i.e., will not return unless the I/O can be satisfied. In non-blocking operation, if the I/O would block, the function will return an error and set the corresponding error code.

The default implementation toggles the Blocking flag with the current socket options and calls setSocketOptions().

Parameters
enablewhether to set this socket to blocking mode
Returns
whether setting this value was successful; it is NOT the final blocking mode.

Definition at line 95 of file k3socketbase.cpp.

◆ setBroadcast()

bool KSocketBase::setBroadcast ( bool  enable)
virtual

Sets this socket Broadcast flag.

Datagram-oriented sockets cannot normally send packets to broadcast addresses, nor will they receive packets that were sent to a broadcast address. To do so, you need to enable the Broadcast flag.

This option has no effect on stream-oriented sockets.

Returns
true if setting this flag was successful.

Definition at line 125 of file k3socketbase.cpp.

◆ setError()

void KSocketBase::setError ( SocketError  error)
protected

Sets the socket's error code.

Parameters
errorthe error code

Definition at line 189 of file k3socketbase.cpp.

◆ setIPv6Only()

bool KSocketBase::setIPv6Only ( bool  enable)
virtual

Sets this socket's IPv6 Only flag.

When this flag is on, an IPv6 socket will only accept, connect, send to or receive from IPv6 addresses. When it is off, it will also talk to IPv4 addresses through v4-mapped addresses.

This option has no effect on non-IPv6 sockets.

The default implementation toggles the IPv6Only flag with the current socket options and calls setSocketOptions().

Parameters
enablewhether to set the flag on or off
Returns
true if setting this flag was successful

Definition at line 115 of file k3socketbase.cpp.

◆ setNoDelay()

bool KSocketBase::setNoDelay ( bool  enable)
virtual

Sets this socket's NoDelay flag.

Stream-oriented protocols, like TCP, have an internal algorithm (called Nagle's algorithm) that collects data in a buffer so that the transmission doesn't occur after every single write operation. The side-effect is that the transmission of short messages is delayed.

Setting NoDelay to 'true' will disable this algorithm.

Returns
true if setting this flag was successful.

Definition at line 135 of file k3socketbase.cpp.

◆ setRequestedCapabilities()

int KSocketBase::setRequestedCapabilities ( int  add,
int  remove = 0 
)

Sets the internally requested capabilities for a socket device.

Most socket classes can use any back-end implementation. However, a few may require specific capabilities not provided in the default implementation. By using this function, derived classes can request that a backend with those capabilities be created when necessary.

For the possible flags, see KSocketDevice::Capabilities. However, note that only the Can* flags make sense in this context.

Note
Since socketDevice must always return a valid backend object, it is is possible that the created device does not conform to all requirements requested. Implementations sensitive to this fact should test the object returned by socketDevice() (through KSocketDevice::capabilities(), for instance) the availability.
Parameters
addmask of KSocketDevice::Capabilities to add
removemask of bits to remove from the requirements
Returns
the current mask of requested capabilities

Definition at line 177 of file k3socketbase.cpp.

◆ setSocketDevice()

void KSocketBase::setSocketDevice ( KSocketDevice device)
virtual

Sets the socket implementation to be used on this socket.

Note: it is an error to set this if the socket device has already been set once.

This function is provided virtual so that derived classes can catch the setting of a device and properly set their own states and internal variables. The parent class must be called.

This function is called by socketDevice() above when the socket is first created.

Reimplemented in KNetwork::KActiveSocketBase, and KNetwork::KBufferedSocket.

Definition at line 169 of file k3socketbase.cpp.

◆ setSocketOptions()

bool KSocketBase::setSocketOptions ( int  opts)
protectedvirtual

Set the given socket options.

The default implementation does nothing but store the mask internally. Descended classes must override this function to achieve functionality and must also call this implementation.

Parameters
optsa mask of SocketOptions or-ed bits of options to set or unset
Returns
true on success
Note
this function sets the options corresponding to the bits enabled in opts but will also unset the optiosn corresponding to the bits not set.

Reimplemented in KNetwork::KServerSocket, KNetwork::KSocketDevice, KNetwork::KClientSocketBase, and KNetwork::KBufferedSocket.

Definition at line 84 of file k3socketbase.cpp.

◆ socketDevice()

KSocketDevice * KSocketBase::socketDevice ( ) const

Retrieves the socket implementation used on this socket.

This function creates the device if none has been set using the default factory.

Definition at line 145 of file k3socketbase.cpp.

◆ socketOptions()

int KSocketBase::socketOptions ( ) const
protectedvirtual

Retrieves the socket options that have been set.

The default implementation just retrieves the mask from an internal variable. Descended classes may choose to override this function to read the values from the operating system.

Returns
the mask of the options set

Definition at line 90 of file k3socketbase.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:56:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.