]> git.saurik.com Git - wxWidgets.git/blob - src/common/gsocketiohandler.cpp
added wxUSE_REGKEY checks
[wxWidgets.git] / src / common / gsocketiohandler.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/gsocketiohandler.cpp
3 // Purpose: implementation of wxFDIOHandler for GSocket
4 // Author: Angel Vidal, Lukasz Michalski
5 // Created: 08.24.06
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Angel vidal
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER
23
24 #include "wx/private/gsocketiohandler.h"
25 #include "wx/unix/private.h"
26 #include "wx/gsocket.h"
27 #include "wx/unix/gsockunx.h"
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 // ----------------------------------------------------------------------------
34 // wxGSocketIOHandler
35 // ----------------------------------------------------------------------------
36
37 wxGSocketIOHandler::wxGSocketIOHandler(GSocket* socket)
38 : m_socket(socket),
39 m_flags(0)
40 {
41
42 }
43
44 void wxGSocketIOHandler::OnReadWaiting()
45 {
46 m_socket->Detected_Read();
47 }
48
49 void wxGSocketIOHandler::OnWriteWaiting()
50 {
51 m_socket->Detected_Write();
52 }
53
54 void wxGSocketIOHandler::OnExceptionWaiting()
55 {
56 m_socket->Detected_Read();
57 }
58
59 int wxGSocketIOHandler::GetFlags() const
60 {
61 return m_flags;
62 }
63
64
65 void wxGSocketIOHandler::RemoveFlag(wxFDIODispatcherEntryFlags flag)
66 {
67 m_flags &= ~flag;
68 }
69
70 void wxGSocketIOHandler::AddFlag(wxFDIODispatcherEntryFlags flag)
71 {
72 m_flags |= flag;
73 }
74
75 // ----------------------------------------------------------------------------
76 // GSocket interface
77 // ----------------------------------------------------------------------------
78
79 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
80 {
81 return true;
82 }
83
84 bool GSocketGUIFunctionsTableConcrete::OnInit()
85 {
86 return true;
87 }
88
89 void GSocketGUIFunctionsTableConcrete::OnExit()
90 {
91 }
92
93 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
94 {
95 int *m_id;
96
97 socket->m_gui_dependent = (char *)malloc(sizeof(int)*2);
98 m_id = (int *)(socket->m_gui_dependent);
99
100 m_id[0] = -1;
101 m_id[1] = -1;
102
103 return true;
104 }
105
106 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
107 {
108 free(socket->m_gui_dependent);
109 }
110
111 void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket,
112 GSocketEvent event)
113 {
114 int *m_id = (int *)(socket->m_gui_dependent);
115 const int fd = socket->m_fd;
116
117 if ( fd == -1 )
118 return;
119
120 int c;
121 switch (event)
122 {
123 case GSOCK_LOST: /* fall-through */
124 case GSOCK_INPUT: c = 0; break;
125 case GSOCK_OUTPUT: c = 1; break;
126 case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
127 default: return;
128 }
129
130 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
131 if ( !dispatcher )
132 return;
133
134 wxGSocketIOHandler *& handler = socket->m_handler;
135
136 // we should register the new handlers but modify the existing ones in place
137 bool registerHandler;
138 if ( handler )
139 {
140 registerHandler = false;
141 }
142 else // no existing handler
143 {
144 registerHandler = true;
145 handler = new wxGSocketIOHandler(socket);
146 }
147
148 if (c == 0)
149 {
150 m_id[0] = fd;
151 handler->AddFlag(wxFDIO_INPUT);
152 }
153 else
154 {
155 m_id[1] = fd;
156 handler->AddFlag(wxFDIO_OUTPUT);
157 }
158
159 if ( registerHandler )
160 dispatcher->RegisterFD(fd, handler, handler->GetFlags());
161 else
162 dispatcher->ModifyFD(fd, handler, handler->GetFlags());
163 }
164
165 void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket,
166 GSocketEvent event)
167 {
168 int *m_id = (int *)(socket->m_gui_dependent);
169 int c;
170
171 switch (event)
172 {
173 case GSOCK_LOST: /* fall-through */
174 case GSOCK_INPUT: c = 0; break;
175 case GSOCK_OUTPUT: c = 1; break;
176 case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
177 default: return;
178 }
179
180 if ( m_id[c] == -1 )
181 return;
182
183 int fd = m_id[c];
184 m_id[c] = -1;
185
186 const wxFDIODispatcherEntryFlags flag = c == 0 ? wxFDIO_INPUT : wxFDIO_OUTPUT;
187
188 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
189 if ( !dispatcher )
190 return;
191
192 wxGSocketIOHandler *& handler = socket->m_handler;
193 if ( handler )
194 {
195 handler->RemoveFlag(flag);
196
197 if ( !handler->GetFlags() )
198 {
199 dispatcher->UnregisterFD(fd);
200 delete handler;
201 }
202 else
203 {
204 dispatcher->ModifyFD(fd, handler, handler->GetFlags());
205 }
206 }
207 else
208 {
209 dispatcher->UnregisterFD(fd);
210 }
211 }
212
213 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
214 {
215 Install_Callback(socket, GSOCK_INPUT);
216 Install_Callback(socket, GSOCK_OUTPUT);
217 }
218
219 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
220 {
221 Uninstall_Callback(socket, GSOCK_INPUT);
222 Uninstall_Callback(socket, GSOCK_OUTPUT);
223 }
224
225 #endif // wxUSE_SOCKETS