dafd064424a885c8170a5fae2eb49d3006a6a230
[platform/core/uifw/dali-adaptor-legacy.git] / dali / internal / system / common / file-descriptor-monitor.h
1 #ifndef DALI_INTERNAL_FILE_DESCRIPTOR_MONITOR_H
2 #define DALI_INTERNAL_FILE_DESCRIPTOR_MONITOR_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/signals/callback.h>
23
24 namespace Dali
25 {
26
27 namespace Integration
28 {
29 class Core;
30 }
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 /**
39  * @brief Monitors the given file descriptor and whenever anything is written to it, the provided
40  * callback is called
41  */
42 class FileDescriptorMonitor
43 {
44 public:
45
46   /**
47    * @brief Bitmask of file descriptor event types
48    */
49   enum EventType
50   {
51     FD_NO_EVENT = 0x0,
52     FD_READABLE = 0x1, // For example when monitoring a socket, data is available to read from the socket receive buffer
53     FD_WRITABLE = 0x2, // For example when monitoring a socket space is available in socket send buffer
54     FD_ERROR    = 0x4,
55   };
56
57   /**
58    * @brief Constructor.
59    *
60    * The callback will be passed a EventType bitmask to signal what type of events occured on the file
61    * descriptor.
62    * Example:
63    *
64    * MyClass::MyClass()
65    * {
66    *    mFileDescriptorMonitor = new FileDescriptorMonitor( myFd, MakeCallback( this, &MyClass::FdCallback ), FileDescriptorMonitor::FD_READABLE );
67    * }
68    *
69    * void MyClass::FdCallback( EventType event )
70    * {
71    *    if( event & FileDescriptorMonitor::FD_ERROR)
72    *    {
73    *      LOG_ERROR("...)
74    *    }
75    *    if( event & FileDescriptorMonitor::FD_READABLE )
76    *    {
77    *      // read from FD
78    *    }
79    *
80    * }
81    *
82    * @param[in] fileDescriptor  The file descriptor to monitor
83    * @param[in] callback Called when anything is written to the file descriptor
84    * @param[in] eventBitmask Bitmask of what to monitor on the file descriptor ( readable / writable ).
85    * @note The ownership of callback is taken by this class.
86    * @note Under Linux it is possible the file descriptor monitor will signal a fd is
87    * readable or writable even when it isn’t. The developer should check for handle EAGAIN or equivalent
88    * when reading from or write to the fd.
89    */
90   FileDescriptorMonitor( int fileDescriptor, CallbackBase* callback, int eventBitmask );
91
92   /**
93    * Destructor
94    */
95   ~FileDescriptorMonitor();
96
97 private:
98
99   // Undefined
100   FileDescriptorMonitor( const FileDescriptorMonitor& fileDescriptorMonitor );
101
102   // Undefined
103   FileDescriptorMonitor& operator=( const FileDescriptorMonitor& fileDescriptorMonitor );
104
105 private:
106   struct Impl;
107   Impl* mImpl;
108 };
109
110 } // namespace Adaptor
111
112 } // namespace Internal
113
114 } // namespace Dali
115
116 #endif // DALI_INTERNAL_FILE_DESCRIPTOR_MONITOR_H