]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/gsocketiohandler.cpp
added explanatory comment
[wxWidgets.git] / src / common / gsocketiohandler.cpp
... / ...
CommitLineData
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
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
37wxGSocketIOHandler::wxGSocketIOHandler(GSocket* socket)
38 : m_socket(socket),
39 m_flags(0)
40{
41
42}
43
44void wxGSocketIOHandler::OnReadWaiting()
45{
46 m_socket->Detected_Read();
47}
48
49void wxGSocketIOHandler::OnWriteWaiting()
50{
51 m_socket->Detected_Write();
52}
53
54void wxGSocketIOHandler::OnExceptionWaiting()
55{
56 m_socket->Detected_Read();
57}
58
59int wxGSocketIOHandler::GetFlags() const
60{
61 return m_flags;
62}
63
64
65void wxGSocketIOHandler::RemoveFlag(wxFDIODispatcherEntryFlags flag)
66{
67 m_flags &= ~flag;
68}
69
70void wxGSocketIOHandler::AddFlag(wxFDIODispatcherEntryFlags flag)
71{
72 m_flags |= flag;
73}
74
75// ----------------------------------------------------------------------------
76// GSocket interface
77// ----------------------------------------------------------------------------
78
79bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
80{
81 return true;
82}
83
84bool GSocketGUIFunctionsTableConcrete::OnInit()
85{
86 return true;
87}
88
89void GSocketGUIFunctionsTableConcrete::OnExit()
90{
91}
92
93bool 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
106void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
107{
108 free(socket->m_gui_dependent);
109}
110
111void 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 wxSelectDispatcher * const dispatcher = wxSelectDispatcher::Get();
131 if ( !dispatcher )
132 return;
133
134 wxGSocketIOHandler *
135 handler = (wxGSocketIOHandler*)dispatcher->FindHandler(fd);
136 if ( !handler )
137 {
138 handler = new wxGSocketIOHandler(socket);
139 }
140
141 if (c == 0)
142 {
143 m_id[0] = fd;
144 handler->AddFlag(wxFDIO_INPUT);
145 }
146 else
147 {
148 m_id[1] = fd;
149 handler->AddFlag(wxFDIO_OUTPUT);
150 }
151
152 dispatcher->RegisterFD(fd, handler, handler->GetFlags());
153}
154
155void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket,
156 GSocketEvent event)
157{
158 int *m_id = (int *)(socket->m_gui_dependent);
159 int c;
160
161 switch (event)
162 {
163 case GSOCK_LOST: /* fall-through */
164 case GSOCK_INPUT: c = 0; break;
165 case GSOCK_OUTPUT: c = 1; break;
166 case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
167 default: return;
168 }
169
170 if ( m_id[c] == -1 )
171 return;
172
173 int fd = m_id[c];
174 m_id[c] = -1;
175
176 const wxFDIODispatcherEntryFlags flag = c == 0 ? wxFDIO_INPUT : wxFDIO_OUTPUT;
177
178 wxSelectDispatcher * const dispatcher = wxSelectDispatcher::Get();
179 if ( !dispatcher )
180 return;
181
182 wxGSocketIOHandler * const
183 handler = (wxGSocketIOHandler*)dispatcher->UnregisterFD(fd, flag);
184 if ( handler )
185 {
186 handler->RemoveFlag(flag);
187
188 if ( !handler->GetFlags() )
189 delete handler;
190 }
191}
192
193void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
194{
195 Install_Callback(socket, GSOCK_INPUT);
196 Install_Callback(socket, GSOCK_OUTPUT);
197}
198
199void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
200{
201 Uninstall_Callback(socket, GSOCK_INPUT);
202 Uninstall_Callback(socket, GSOCK_OUTPUT);
203}
204
205#endif // wxUSE_SOCKETS