Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / zypp / zyppng / base / socketnotifier.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \----------------------------------------------------------------------/
9 *
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
12 *
13 */
14
15 #include <zypp/zyppng/base/zyppglobal.h>
16 #include <zypp/zyppng/base/AbstractEventSource>
17 #include <zypp/zyppng/base/Signals>
18
19 namespace zyppng {
20 class SocketNotifierPrivate;
21
22 /*!
23  * The SocketNotifier class provides a generic way to monitor activity on a file descriptors.
24  *
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.
27  *
28  * Each file descriptor can be monitored for all types of activiy supported by the base class \sa AbstractEventSource.
29  *
30  * \code
31  * // Example code that uses a pipe() to communicate between a thread and the main loop
32  * zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
33  *
34  * int wakeupPipe[2] = { -1, -1 };
35  * pipe2 ( wakeupPipe, O_NONBLOCK );
36  *
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 &notifier, int ) {
40  *   char dummy;
41  *   //read all available data
42  *   while ( read( notifier.socket(), &dummy, 1 ) > 0 ) {
43  *     std::cout<<dummy;
44  *   }
45  *   std::cout << std::endl;
46  *   loop->quit();
47  * });
48  * notifier->setEnabled( true );
49  *
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 );
56  *   return;
57  * });
58  *
59  * loop->run();
60  *
61  * std::cout << "Bye Bye" << std::endl;
62  * t.join();
63  * \endcode
64  */
65 class SocketNotifier : public AbstractEventSource
66 {
67   ZYPP_DECLARE_PRIVATE( SocketNotifier )
68 public:
69
70   using Ptr = std::shared_ptr<SocketNotifier>;
71   using WeakPtr = std::weak_ptr<SocketNotifier>;
72
73   using EventTypes = AbstractEventSource::EventTypes;
74
75   /*!
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
80    */
81   static Ptr create ( int socket, int evTypes, bool enable = true );
82
83   /*!
84    * Update the fd watch to the \a mode specified.
85    * \sa AbstractEventSource::EventTypes
86    */
87   void setMode ( int mode );
88
89   /*!
90    * Returns the current mode used to monitor the file descriptor
91    * \sa AbstractEventSource::EventTypes
92    */
93   int  mode () const;
94
95   /*!
96    * Enables or disables the SocketNotifier
97    */
98   void setEnabled ( bool enabled = true );
99
100   /*!
101    * Returns the monitored file descriptor
102    */
103   int socket () const;
104
105   /*!
106    * Emitted when there is activity on the socket according to the requested mode
107    */
108   SignalProxy<void (const SocketNotifier &sock, int evTypes)> sigActivated();
109
110 protected:
111   SocketNotifier( int socket, int evTypes, bool enable  );
112
113   // AbstractEventSource interface
114 public:
115   void onFdReady(int, int events) override;
116   void onSignal(int signal) override;
117 };
118
119 }