libzypp  17.22.0
zyppng::SocketNotifier Class Reference

#include <socketnotifier.h>

Inheritance diagram for zyppng::SocketNotifier:

Public Types

using Ptr = std::shared_ptr< SocketNotifier >
 
using WeakPtr = std::weak_ptr< SocketNotifier >
 
using EventTypes = AbstractEventSource::EventTypes
 
- Public Types inherited from zyppng::AbstractEventSource
enum  EventTypes { Read = 0x01, Write = 0x02, Exception = 0x04, Error = 0x08 }
 
- Public Types inherited from zyppng::Base
using Ptr = std::shared_ptr< Base >
 
using WeakPtr = std::weak_ptr< Base >
 

Public Member Functions

void setMode (int mode)
 
int mode () const
 
void setEnabled (bool enabled=true)
 
int socket () const
 
SignalProxy< void(const SocketNotifier &sock, int evTypes)> sigActivated ()
 
void onFdReady (int, int events) override
 
void onSignal (int signal) override
 
- Public Member Functions inherited from zyppng::AbstractEventSource
 AbstractEventSource ()
 
 AbstractEventSource (AbstractEventSourcePrivate &dd)
 
virtual ~AbstractEventSource ()
 
std::weak_ptr< EventDispatchereventDispatcher () const
 
- Public Member Functions inherited from zyppng::Base
 Base ()
 
virtual ~Base ()
 
WeakPtr parent () const
 
void addChild (Base::Ptr child)
 
void removeChild (Ptr child)
 
const std::unordered_set< Ptr > & children () 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 ()
 

Static Public Member Functions

static Ptr create (int socket, int evTypes, bool enable=true)
 

Protected Member Functions

 SocketNotifier (int socket, int evTypes, bool enable)
 
- Protected Member Functions inherited from zyppng::AbstractEventSource
void updateFdWatch (int fd, int mode)
 
void removeFdWatch (int fd)
 
- Protected Member Functions inherited from zyppng::Base
 Base (BasePrivate &dd)
 

Additional Inherited Members

- Protected Attributes inherited from zyppng::Base
std::unique_ptr< BasePrivated_ptr
 

Detailed Description

The SocketNotifier class provides a generic way to monitor activity on a file descriptors.

Once a file descriptor was created using either low level OS API or was created by a 3rd party library, the SocketNotifier can be created to integrate the file descriptor into the event loop.

Each file descriptor can be monitored for all types of activiy supported by the base class

See also
AbstractEventSource.
// Example code that uses a pipe() to communicate between a thread and the main loop
int wakeupPipe[2] = { -1, -1 };
pipe2 ( wakeupPipe, O_NONBLOCK );
//listen for activity on the event source
notifier->sigActivated().connect([ &loop ]( const zyppng::SocketNotifier &notifier, int ) {
char dummy;
//read all available data
while ( read( notifier.socket(), &dummy, 1 ) > 0 ) {
std::cout<<dummy;
}
std::cout << std::endl;
loop->quit();
});
notifier->setEnabled( true );
std::thread t( [ &wakeupPipe ](){
const char * test = "Some text";
std::chrono::milliseconds dura( 1000 );
std::this_thread::sleep_for( dura );
write( wakeupPipe[1], test, 9 );
std::this_thread::sleep_for( dura );
return;
});
loop->run();
std::cout << "Bye Bye" << std::endl;
t.join();

Definition at line 65 of file socketnotifier.h.

Member Typedef Documentation

◆ Ptr

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

Definition at line 70 of file socketnotifier.h.

◆ WeakPtr

Definition at line 71 of file socketnotifier.h.

◆ EventTypes

Constructor & Destructor Documentation

◆ SocketNotifier()

zyppng::SocketNotifier::SocketNotifier ( int  socket,
int  evTypes,
bool  enable 
)
protected

Definition at line 19 of file socketnotifier.cc.

Member Function Documentation

◆ create()

SocketNotifier::Ptr zyppng::SocketNotifier::create ( int  socket,
int  evTypes,
bool  enable = true 
)
static

Returns a new SocketNotifier

Parameters
socketthis is the filedescriptor to be modified
evTypesthe event types that should be monitored
See also
AbstractEventSource::EventTypes
Parameters
enableIf set to true the notifier is enabled right away, otherwise
See also
setEnabled needs to be called explicitely

Definition at line 29 of file socketnotifier.cc.

◆ setMode()

void zyppng::SocketNotifier::setMode ( int  mode)

Update the fd watch to the mode specified.

See also
AbstractEventSource::EventTypes

Definition at line 34 of file socketnotifier.cc.

◆ mode()

int zyppng::SocketNotifier::mode ( ) const

Returns the current mode used to monitor the file descriptor

See also
AbstractEventSource::EventTypes

Definition at line 46 of file socketnotifier.cc.

◆ setEnabled()

void zyppng::SocketNotifier::setEnabled ( bool  enabled = true)

Enables or disables the SocketNotifier

Definition at line 51 of file socketnotifier.cc.

◆ socket()

int zyppng::SocketNotifier::socket ( ) const

Returns the monitored file descriptor

Definition at line 65 of file socketnotifier.cc.

◆ sigActivated()

SignalProxy< void(const SocketNotifier &, int)> zyppng::SocketNotifier::sigActivated ( )

Emitted when there is activity on the socket according to the requested mode

Definition at line 70 of file socketnotifier.cc.

◆ onFdReady()

void zyppng::SocketNotifier::onFdReady ( int  fd,
int  events 
)
overridevirtual

Called by the

See also
EventDispatcher when activity was detected on a registered fd. The events are set according to the activity on the file descriptor.

Implements zyppng::AbstractEventSource.

Definition at line 75 of file socketnotifier.cc.

◆ onSignal()

void zyppng::SocketNotifier::onSignal ( int  signal)
overridevirtual

Called when a event source has registered to receive unix signals

Note
this is not supported yet

Implements zyppng::AbstractEventSource.

Definition at line 80 of file socketnotifier.cc.


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