Dali 3D User Interface Engine
uv-file-descriptor-monitor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 // CLASS HEADER
20 
21 // EXTERNAL INCLUDES
23 #include <uv.h>
24 
25 namespace Dali
26 {
27 
28 namespace Internal
29 {
30 
31 namespace Adaptor
32 {
33 
34 namespace
35 {
36 void FreeHandleCallback(uv_handle_t* handle )
37 {
38  delete handle;
39 }
40 
41 }
42 
46 struct FileDescriptorMonitor::Impl
47 {
48 public:
49 
50  // Constructor
51  Impl( int fileDescriptor, CallbackBase* callback, uv_poll_event eventsToMonitor )
52  : mFileDescriptor( fileDescriptor ),
53  mEventsToMonitor( eventsToMonitor ),
54  mCallback( callback ),
55  pollHandle( NULL )
56  {
57 
58  // heap allocate a handle as it will be alive after the FileDescriptorMonitor::Impl object is deleted.
59  pollHandle = new uv_poll_t;
60 
61  // Node.JS uses uv_default_loop
62  uv_poll_init( uv_default_loop(), pollHandle, fileDescriptor);
63 
64  pollHandle->data = this;
65 
66  uv_poll_start( pollHandle, mEventsToMonitor, PollCabllack);
67  }
68 
70  {
71  uv_poll_stop( pollHandle );
72 
73  // the handle will still be alive for a short period after calling uv_close
74  // set the data to NULL to avoid a dangling pointer
75  pollHandle->data = NULL;
76 
77  uv_close(reinterpret_cast<uv_handle_t*> ( pollHandle ) , FreeHandleCallback );
78 
79  delete mCallback;
80  }
81 
82  static void PollCabllack(uv_poll_t* handle, int status, int events)
83  {
84  if( handle->data )
85  {
86  FileDescriptorMonitor::Impl* impl= static_cast<FileDescriptorMonitor::Impl* >(handle->data);
87 
88  if( status < 0)
89  {
90  DALI_LOG_ERROR("LibUV FD_ERROR occurred on %d", impl->mFileDescriptor);
92  return;
93  }
94  // filter the events that have occured based on what we are monitoring
95 
96  int eventType = FileDescriptorMonitor::FD_NO_EVENT;
97 
98  if (( impl->mEventsToMonitor & UV_READABLE ) && ( events & UV_READABLE ))
99  {
101  }
102  if (( impl->mEventsToMonitor & UV_WRITABLE ) && ( events & UV_WRITABLE ))
103  {
105  }
106 
107  // if there is an event, execute the callback
108  if( eventType != FileDescriptorMonitor::FD_NO_EVENT )
109  {
110  CallbackBase::Execute( *impl->mCallback, static_cast< FileDescriptorMonitor::EventType >(eventType) );
111  }
112  }
113  }
114  // Data
115  int mFileDescriptor;
116  uv_poll_event mEventsToMonitor;
118  uv_poll_t* pollHandle;
119 
120 };
121 
122 
123 FileDescriptorMonitor::FileDescriptorMonitor( int fileDescriptor, CallbackBase* callback, int eventBitmask )
124 {
125  if (fileDescriptor < 1)
126  {
127  DALI_ASSERT_ALWAYS( 0 && "Invalid File descriptor");
128  return;
129  }
130  int events = 0;
131  if( eventBitmask & FD_READABLE)
132  {
133  events = UV_READABLE;
134  }
135  if( eventBitmask & FD_WRITABLE)
136  {
137  events |= UV_WRITABLE;
138  }
139 
140  DALI_ASSERT_ALWAYS( events && "Invalid FileDescriptorMonitor event type ");
141 
142  // waiting for a write event on a file descriptor
143  mImpl = new Impl( fileDescriptor, callback, static_cast< uv_poll_event >( events ) );
144 }
145 
147 {
148  delete mImpl;
149 }
150 
151 } // namespace Adaptor
152 
153 } // namespace Internal
154 
155 } // namespace Dali
Dali Docs Home
Read more about Dali