1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \----------------------------------------------------------------------/
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
15 #include <zypp/zyppng/base/zyppglobal.h>
16 #include <zypp/zyppng/base/AbstractEventSource>
17 #include <zypp/zyppng/base/Signals>
20 class SocketNotifierPrivate;
23 * The SocketNotifier class provides a generic way to monitor activity on a file descriptors.
25 * Once a file descriptor was created using either low level OS API or was created by a 3rd party
26 * library, the SocketNotifier can be created to integrate the file descriptor into the event loop.
28 * Each file descriptor can be monitored for all types of activiy supported by the base class \sa AbstractEventSource.
31 * // Example code that uses a pipe() to communicate between a thread and the main loop
32 * zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
34 * int wakeupPipe[2] = { -1, -1 };
35 * pipe2 ( wakeupPipe, O_NONBLOCK );
37 * //listen for activity on the event source
38 * zyppng::SocketNotifier::Ptr notifier = zyppng::SocketNotifier::create( wakeupPipe[0], zyppng::SocketNotifier::Read, false );
39 * notifier->sigActivated().connect([ &loop ]( const zyppng::SocketNotifier ¬ifier, int ) {
41 * //read all available data
42 * while ( read( notifier.socket(), &dummy, 1 ) > 0 ) {
45 * std::cout << std::endl;
48 * notifier->setEnabled( true );
50 * std::thread t( [ &wakeupPipe ](){
51 * const char * test = "Some text";
52 * std::chrono::milliseconds dura( 1000 );
53 * std::this_thread::sleep_for( dura );
54 * write( wakeupPipe[1], test, 9 );
55 * std::this_thread::sleep_for( dura );
61 * std::cout << "Bye Bye" << std::endl;
65 class SocketNotifier : public AbstractEventSource
67 ZYPP_DECLARE_PRIVATE( SocketNotifier )
70 using Ptr = std::shared_ptr<SocketNotifier>;
71 using WeakPtr = std::weak_ptr<SocketNotifier>;
73 using EventTypes = AbstractEventSource::EventTypes;
76 * Returns a new SocketNotifier
77 * \param socket this is the filedescriptor to be modified
78 * \param evTypes the event types that should be monitored \sa AbstractEventSource::EventTypes
79 * \param enable If set to true the notifier is enabled right away, otherwise \sa setEnabled needs to be called explicitely
81 static Ptr create ( int socket, int evTypes, bool enable = true );
84 * Update the fd watch to the \a mode specified.
85 * \sa AbstractEventSource::EventTypes
87 void setMode ( int mode );
90 * Returns the current mode used to monitor the file descriptor
91 * \sa AbstractEventSource::EventTypes
96 * Enables or disables the SocketNotifier
98 void setEnabled ( bool enabled = true );
101 * Returns the monitored file descriptor
106 * Emitted when there is activity on the socket according to the requested mode
108 SignalProxy<void (const SocketNotifier &sock, int evTypes)> sigActivated();
111 SocketNotifier( int socket, int evTypes, bool enable );
113 // AbstractEventSource interface
115 void onFdReady(int, int events) override;
116 void onSignal(int signal) override;