]>
Commit | Line | Data |
---|---|---|
30c45bdd VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/gsocketiohandler.cpp | |
3 | // Purpose: implementation of wxFDIOHandler for GSocket | |
4 | // Author: Angel Vidal, Lukasz Michalski | |
30c45bdd VZ |
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 | ||
a1873279 | 22 | #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER |
30c45bdd VZ |
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 | ||
b46b1d59 | 37 | wxGSocketIOHandler::wxGSocketIOHandler(GSocket* socket) |
30c45bdd VZ |
38 | : m_socket(socket), |
39 | m_flags(0) | |
40 | { | |
41 | ||
b46b1d59 | 42 | } |
30c45bdd | 43 | |
b46b1d59 | 44 | void wxGSocketIOHandler::OnReadWaiting() |
30c45bdd VZ |
45 | { |
46 | m_socket->Detected_Read(); | |
b46b1d59 | 47 | } |
30c45bdd | 48 | |
b46b1d59 | 49 | void wxGSocketIOHandler::OnWriteWaiting() |
30c45bdd VZ |
50 | { |
51 | m_socket->Detected_Write(); | |
b46b1d59 | 52 | } |
30c45bdd | 53 | |
b46b1d59 | 54 | void wxGSocketIOHandler::OnExceptionWaiting() |
30c45bdd VZ |
55 | { |
56 | m_socket->Detected_Read(); | |
b46b1d59 | 57 | } |
30c45bdd | 58 | |
b46b1d59 | 59 | int wxGSocketIOHandler::GetFlags() const |
30c45bdd VZ |
60 | { |
61 | return m_flags; | |
b46b1d59 | 62 | } |
30c45bdd VZ |
63 | |
64 | ||
b46b1d59 | 65 | void wxGSocketIOHandler::RemoveFlag(wxFDIODispatcherEntryFlags flag) |
30c45bdd VZ |
66 | { |
67 | m_flags &= ~flag; | |
b46b1d59 | 68 | } |
30c45bdd | 69 | |
b46b1d59 | 70 | void wxGSocketIOHandler::AddFlag(wxFDIODispatcherEntryFlags flag) |
30c45bdd VZ |
71 | { |
72 | m_flags |= flag; | |
b46b1d59 | 73 | } |
30c45bdd VZ |
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); | |
b46b1d59 | 115 | const int fd = socket->m_fd; |
30c45bdd | 116 | |
b46b1d59 | 117 | if ( fd == -1 ) |
30c45bdd VZ |
118 | return; |
119 | ||
b46b1d59 | 120 | int c; |
30c45bdd VZ |
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 | ||
5e1eac14 | 130 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
b46b1d59 VZ |
131 | if ( !dispatcher ) |
132 | return; | |
133 | ||
5e1eac14 VZ |
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 ) | |
30c45bdd | 139 | { |
5e1eac14 VZ |
140 | registerHandler = false; |
141 | } | |
142 | else // no existing handler | |
143 | { | |
144 | registerHandler = true; | |
30c45bdd | 145 | handler = new wxGSocketIOHandler(socket); |
b46b1d59 | 146 | } |
30c45bdd VZ |
147 | |
148 | if (c == 0) | |
149 | { | |
b46b1d59 VZ |
150 | m_id[0] = fd; |
151 | handler->AddFlag(wxFDIO_INPUT); | |
30c45bdd VZ |
152 | } |
153 | else | |
154 | { | |
b46b1d59 VZ |
155 | m_id[1] = fd; |
156 | handler->AddFlag(wxFDIO_OUTPUT); | |
30c45bdd VZ |
157 | } |
158 | ||
5e1eac14 VZ |
159 | if ( registerHandler ) |
160 | dispatcher->RegisterFD(fd, handler, handler->GetFlags()); | |
161 | else | |
162 | dispatcher->ModifyFD(fd, handler, handler->GetFlags()); | |
30c45bdd VZ |
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 | ||
b46b1d59 VZ |
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 | ||
5e1eac14 | 188 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
b46b1d59 VZ |
189 | if ( !dispatcher ) |
190 | return; | |
191 | ||
5e1eac14 | 192 | wxGSocketIOHandler *& handler = socket->m_handler; |
b46b1d59 | 193 | if ( handler ) |
30c45bdd | 194 | { |
b46b1d59 VZ |
195 | handler->RemoveFlag(flag); |
196 | ||
197 | if ( !handler->GetFlags() ) | |
af57c51a VZ |
198 | { |
199 | dispatcher->UnregisterFD(fd); | |
30c45bdd | 200 | delete handler; |
af57c51a VZ |
201 | } |
202 | else | |
203 | { | |
204 | dispatcher->ModifyFD(fd, handler, handler->GetFlags()); | |
205 | } | |
206 | } | |
207 | else | |
208 | { | |
209 | dispatcher->UnregisterFD(fd); | |
30c45bdd | 210 | } |
30c45bdd VZ |
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 |