libzypp 17.37.10

zyppng::Socket Class Reference

`#include <zypp-core/zyppng/io/socket.h>`

Inheritance diagram for zyppng::Socket:

## Public Types
enum | SocketError { NoError , UnknownSocketError , InsufficientPermissions , InvalidSocketOptions , InsufficientRessources , UnsupportedSocketOptions , FailedSocketOperation , SocketAlreadyBound , SocketNotListening , AddressInUse , AddressNotAvailable , AddressIssue , OperationNotSupported , ConnectionTimeout , ConnectionDelayed , ConnectionRefused , ConnectionClosedByRemote , NetworkUnreachable , InternalError }
enum | SocketState { InitialState , ConnectingState , ConnectedState , ListeningState , ClosingState , ClosedState }
using | Ptr = std::shared_ptr<Socket>
Public Types inherited from zyppng::IODevice
enum | OpenModeFlag { Closed = 0x0 , ReadOnly = 0x1 , WriteOnly = 0x2 , ReadWrite = ReadOnly | WriteOnly }
using | Ptr = std::shared_ptr<IODevice>
using | WeakPtr = std::weak_ptr<IODevice>
Public Types inherited from zyppng::Base
using | Ptr = std::shared_ptr<Base>
using | WeakPtr = std::weak_ptr<Base>

## Public Member Functions
| ~Socket () override
void | close () override
int64_t | bytesPending () const override
SocketState | state () const
bool | bind (const std::shared_ptr< SockAddr > &addr)
bool | listen (int backlog=50)
Ptr | accept ()
bool | setBlocking (const bool set=true)
void | disconnect ()
void | abort ()
bool | connect (std::shared_ptr< SockAddr > addr)
bool | waitForConnected (int timeout=-1)
bool | waitForAllBytesWritten (int timeout=-1)
bool | waitForReadyRead (uint channel, int timeout=-1) override
int | nativeSocket () const
int | releaseSocket ()
SocketError | lastError () const
SignalProxy< void()> | sigIncomingConnection ()
SignalProxy< void()> | sigConnected ()
SignalProxy< void()> | sigDisconnected ()
SignalProxy< void(Socket::SocketError)> | sigError ()
Public Member Functions inherited from zyppng::IODevice
| ZYPP_DECLARE_FLAGS (OpenMode, OpenModeFlag)
| IODevice ()
void | setReadChannel (uint channel)
uint | currentReadChannel () const
int | readChannelCount () const
bool | canRead () const
bool | canWrite () const
bool | isOpen () const
ByteArray | readAll ()
ByteArray | read (int64_t maxSize)
int64_t | read (char *buf, int64_t maxSize)
virtual ByteArray | readLine (const int64_t maxSize=0)
virtual int64_t | bytesAvailable () const
int64_t | readBufferCount () const
bool | canReadLine () const
ByteArray | readAll (uint channel)
ByteArray | read (uint channel, int64_t maxSize)
int64_t | read (uint channel, char *buf, int64_t maxSize)
ByteArray | channelReadUntil (uint channel, const char delim, int64_t maxSize=0)
ByteArray | channelReadLine (uint channel, int64_t maxSize=0)
int64_t | channelReadLine (uint channel, char *buf, const int64_t maxSize)
virtual int64_t | bytesAvailable (uint channel) const
int64_t | readBufferCount (uint channel) const
virtual int64_t | channelReadUntil (uint channel, char *buf, const char delimiter, const int64_t maxSize)
bool | canReadLine (uint channel) const
bool | canReadUntil (uint channel, const char delim) const
int64_t | write (const ByteArray &data)
int64_t | write (const char *data, int64_t len)
bool | waitForReadyRead (int timeout)
SignalProxy< void()> | sigReadyRead ()
SignalProxy< void(uint)> | sigChannelReadyRead ()
SignalProxy< void(int64_t)> | sigBytesWritten ()
SignalProxy< void()> | sigAllBytesWritten ()
SignalProxy< void(uint) > | sigReadChannelFinished ()
Public Member Functions inherited from zyppng::Base
| Base ()
virtual | ~Base ()
WeakPtr | parent () const
void | addChild (const Base::Ptr &child)
void | removeChild (const Ptr &child)
const std::unordered_set< Ptr > & | children () const
std::thread::id | threadId () const
template<typename T>
std::vector< std::weak_ptr< T > > | findChildren () const
template<typename T>
std::shared_ptr< T > | shared_this () const
template<typename T>
std::shared_ptr< T > | shared_this ()
template<typename T>
std::weak_ptr< T > | weak_this () const
template<typename T>
std::weak_ptr< T > | weak_this ()
template<typename SenderFunc, typename ReceiverFunc>
auto | connect (SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
template<typename SenderFunc, typename ReceiverFunc, typename ... Tracker>
std::enable_if_t< std::is_member_function_pointer_v< SenderFunc >, connection > | connectFunc (SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)

## Static Public Member Functions
static Ptr | create (int domain, int type, int protocol)
static Ptr | fromSocket (int fd, SocketState state)
Static Public Member Functions inherited from zyppng::Base
template<typename Obj, typename Functor>
static decltype(auto) | make_base_slot (Obj *o, Functor &&f)
template<typename SenderFunc, typename ReceiverFunc>
static auto | connect (typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
template<typename SenderFunc, typename ReceiverFunc, typename ... Tracker>
static auto | connectFunc (typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)

## Protected Member Functions
| Socket (int domain, int type, int protocol)
int64_t | rawBytesAvailable (uint channel=0) const override
int64_t | writeData (const char *data, int64_t count) override
int64_t | readData (uint channel, char *buffer, int64_t bufsize) override
void | readChannelChanged (uint channel) override
Protected Member Functions inherited from zyppng::IODevice
| IODevice (IODevicePrivate &d)
virtual bool | open (const OpenMode mode)
void | setReadChannelCount (uint channels)
void | finishReadChannel (uint channel)
Protected Member Functions inherited from zyppng::Base
| Base (BasePrivate &dd)

## Additional Inherited Members
Protected Attributes inherited from zyppng::Base
std::unique_ptr< BasePrivate > | d_ptr

## Detailed Description

Combines Sockets with the zypp event loop. Generally every socket type that is supported by the socket(2) API should work, however currently only Unix Domain sockets are tested.

The useage pattern of this class is similar to the socket(2) API, on the server endpoint one listening socket is created and bound to a adress to accept incoming connections. For every appected connections a connected socket instance is returned which can be used for communication with the peer.

Definition at line 35 of file socket.h.

## Member Typedef Documentation

## ◆ Ptr

using zyppng::Socket::Ptr = std::shared_ptr<Socket>

Definition at line 71 of file socket.h.

## Member Enumeration Documentation

## ◆ SocketError

enum zyppng::Socket::SocketError

Enumerator
NoError |
UnknownSocketError |
InsufficientPermissions |
InvalidSocketOptions |
InsufficientRessources |
UnsupportedSocketOptions |
FailedSocketOperation |
SocketAlreadyBound |
SocketNotListening |
AddressInUse |
AddressNotAvailable |
AddressIssue |
OperationNotSupported |
ConnectionTimeout |
ConnectionDelayed |
ConnectionRefused |
ConnectionClosedByRemote |
NetworkUnreachable |
InternalError |

Definition at line 40 of file socket.h.

## ◆ SocketState

enum zyppng::Socket::SocketState

Enumerator
InitialState |
ConnectingState |
ConnectedState |
ListeningState |
ClosingState |
ClosedState |

Definition at line 62 of file socket.h.

## Constructor & Destructor Documentation

## ◆ ~Socket()

zyppng::Socket::~Socket | ( |  | ) |
| override

Definition at line 453 of file socket.cc.

## ◆ Socket()

zyppng::Socket::Socket | ( | int | domain,
|  | int | type,
|  | int | protocol )
| protected

Definition at line 439 of file socket.cc.

## Member Function Documentation

## ◆ create()

Socket::Ptr zyppng::Socket::create | ( | int | domain,
|  | int | type,
|  | int | protocol )
| static

Creates a new socket with the given doman , type and protocol. See socket(2) for available arguments.

Note
  currently only AF_UNIX, SOCK_STREAM sockets are tested

Definition at line 458 of file socket.cc.

## ◆ close()

void zyppng::Socket::close | ( |  | ) |
| override virtual

Closed the socket and disconnects from the peer. The disconnected signal will be emitted. This is similar to calling disconnect.

Reimplemented from zyppng::IODevice.

Definition at line 665 of file socket.cc.

## ◆ bytesPending()

int64_t zyppng::Socket::bytesPending | ( |  | ) | const
| override virtual

Returns the current number of bytes that are not yet written to the socket.

Implements zyppng::IODevice.

Definition at line 855 of file socket.cc.

## ◆ state()

Socket::SocketState zyppng::Socket::state | ( |  | ) | const

Returns the current state the socket is in, check SocketState for possible values.

Definition at line 867 of file socket.cc.

## ◆ bind()

bool zyppng::Socket::bind | ( | const std::shared_ptr< SockAddr > & | addr | ) |

Bind the socket to a local address, this is usually required to listen for incoming connections.

Definition at line 463 of file socket.cc.

## ◆ listen()

bool zyppng::Socket::listen | ( | int | backlog = 50 | ) |

Puts the socket in listen mode, the state is set accordingly.

Definition at line 509 of file socket.cc.

## ◆ accept()

Socket::Ptr zyppng::Socket::accept | ( |  | ) |

Accepts a pending incoming connection and returns it as a intialized connected socket. Returns nullptr if there is no pending connection.

Definition at line 538 of file socket.cc.

## ◆ setBlocking()

bool zyppng::Socket::setBlocking | ( | const bool | set = true | ) |

This will set the socket into blocking mode. Can be used if there is no EventLoop running. (Make sure a EventDispatcher instance exists for the thread)

Definition at line 597 of file socket.cc.

## ◆ disconnect()

void zyppng::Socket::disconnect | ( |  | ) |

Starts to disconnect from the host, wait for the actual sigDisconnected signal to arrive before destroying the socket otherwise the connection is forcefully aborted

Definition at line 670 of file socket.cc.

## ◆ abort()

void zyppng::Socket::abort | ( |  | ) |

Forcefully aborts the connection, no matter if there is still data to be written in the write buffer

Definition at line 659 of file socket.cc.

## ◆ connect()

bool zyppng::Socket::connect | ( | std::shared_ptr< SockAddr > | addr | ) |

Starts the connection process to the given adress in addr. Returns true on success, if false is returned the current state should be checked if the connection was simply delayed. In that case the state is ConnectingState and error is set to ConnectionDelayed and the socket still tries to connect.

Definition at line 619 of file socket.cc.

## ◆ waitForConnected()

bool zyppng::Socket::waitForConnected | ( | int | timeout = -1 | ) |

Blocks the current event loop to wait for the connected event on the socket.

Note
  do not use until there is no other way

Definition at line 741 of file socket.cc.

## ◆ waitForAllBytesWritten()

bool zyppng::Socket::waitForAllBytesWritten | ( | int | timeout = -1 | ) |

Blocks the current event loop to wait until all bytes from the buffer have been written to the device.

Note
  do not use until there is no other way

Definition at line 759 of file socket.cc.

## ◆ waitForReadyRead()

bool zyppng::Socket::waitForReadyRead | ( | uint | channel,
|  | int | timeout = -1 )
| override virtual

Blocks the current event loop to wait until there are bytes available to read from the device

Note
  do not use until there is no other way

Implements zyppng::IODevice.

Definition at line 792 of file socket.cc.

## ◆ nativeSocket()

int zyppng::Socket::nativeSocket | ( |  | ) | const

Returns the native socket handle.

Definition at line 638 of file socket.cc.

## ◆ releaseSocket()

int zyppng::Socket::releaseSocket | ( |  | ) |

This will release the current socket and instantly transition into ClosedState. All unwritten data in the buffers will be discarded, so consider to use waitForAllBytesWritten before you call this function.

Returns
  the socket or -1 if there was no socket connected

Definition at line 644 of file socket.cc.

## ◆ lastError()

Socket::SocketError zyppng::Socket::lastError | ( |  | ) | const

Return the last error that was encountered by the socket.

Definition at line 653 of file socket.cc.

## ◆ sigIncomingConnection()

SignalProxy< void()> zyppng::Socket::sigIncomingConnection | ( |  | ) |

Signals when there are new incoming connection pending.

Note
  Make sure to accept all connection otherwise the event loop keeps triggering that signal

Definition at line 872 of file socket.cc.

## ◆ sigConnected()

SignalProxy< void()> zyppng::Socket::sigConnected | ( |  | ) |

Signal is emitted as soon as the socket enters the connected state. It is not possible to read and write data.

Definition at line 877 of file socket.cc.

## ◆ sigDisconnected()

SignalProxy< void()> zyppng::Socket::sigDisconnected | ( |  | ) |

Signal is emitted always when the socket was closed. In the signal handler it is still possible to read all pending data in the read buffer, which will be discarded right after emitting the signal.

Note
  when the socket should be deleted in a slot connected to this signal make sure to delay the delete using the EventDispatcher.

Definition at line 882 of file socket.cc.

## ◆ sigError()

SignalProxy< void(Socket::SocketError)> zyppng::Socket::sigError | ( |  | ) |

Signal is emitted whenever a error happend in the socket. Make sure to check the actual error code to determine if the error is fatal.

Definition at line 887 of file socket.cc.

## ◆ fromSocket()

Socket::Ptr zyppng::Socket::fromSocket | ( | int | fd,
|  | Socket::SocketState | state )
| static

Makes a socket instance out of a existing socket file descriptor, the instance takes ownership of the fd

Definition at line 564 of file socket.cc.

## ◆ rawBytesAvailable()

int64_t zyppng::Socket::rawBytesAvailable | ( | uint | channel = 0 | ) | const
| override protected virtual

Implements zyppng::IODevice.

Definition at line 443 of file socket.cc.

## ◆ writeData()

int64_t zyppng::Socket::writeData | ( | const char * | data,
|  | int64_t | count )
| override protected virtual

Implements zyppng::IODevice.

Definition at line 687 of file socket.cc.

## ◆ readData()

int64_t zyppng::Socket::readData | ( | uint | channel,
|  | char * | buffer,
|  | int64_t | bufsize )
| override protected virtual

Implements zyppng::IODevice.

Definition at line 811 of file socket.cc.

## ◆ readChannelChanged()

void zyppng::Socket::readChannelChanged | ( | uint | channel | ) |
| override protected virtual

Implements zyppng::IODevice.

Definition at line 846 of file socket.cc.

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

- zypp-core/zyppng/io/socket.h
- zypp-core/zyppng/io/socket.cc