]> git.saurik.com Git - wxWidgets.git/blame - src/common/gsocketiohandler.cpp
rename old wxAppConsole to wxAppConsoleBase and wxAppConsoleUnix to wxAppConsole...
[wxWidgets.git] / src / common / gsocketiohandler.cpp
CommitLineData
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
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
b46b1d59 37wxGSocketIOHandler::wxGSocketIOHandler(GSocket* socket)
30c45bdd
VZ
38 : m_socket(socket),
39 m_flags(0)
40{
41
b46b1d59 42}
30c45bdd 43
b46b1d59 44void wxGSocketIOHandler::OnReadWaiting()
30c45bdd
VZ
45{
46 m_socket->Detected_Read();
b46b1d59 47}
30c45bdd 48
b46b1d59 49void wxGSocketIOHandler::OnWriteWaiting()
30c45bdd
VZ
50{
51 m_socket->Detected_Write();
b46b1d59 52}
30c45bdd 53
b46b1d59 54void wxGSocketIOHandler::OnExceptionWaiting()
30c45bdd
VZ
55{
56 m_socket->Detected_Read();
b46b1d59 57}
30c45bdd 58
b46b1d59 59int wxGSocketIOHandler::GetFlags() const
30c45bdd
VZ
60{
61 return m_flags;
b46b1d59 62}
30c45bdd
VZ
63
64
b46b1d59 65void wxGSocketIOHandler::RemoveFlag(wxFDIODispatcherEntryFlags flag)
30c45bdd
VZ
66{
67 m_flags &= ~flag;
b46b1d59 68}
30c45bdd 69
b46b1d59 70void wxGSocketIOHandler::AddFlag(wxFDIODispatcherEntryFlags flag)
30c45bdd
VZ
71{
72 m_flags |= flag;
b46b1d59 73}
30c45bdd
VZ
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);
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
b46b1d59
VZ
130 wxSelectDispatcher * const dispatcher = wxSelectDispatcher::Get();
131 if ( !dispatcher )
132 return;
133
134 wxGSocketIOHandler *
135 handler = (wxGSocketIOHandler*)dispatcher->FindHandler(fd);
136 if ( !handler )
30c45bdd
VZ
137 {
138 handler = new wxGSocketIOHandler(socket);
b46b1d59 139 }
30c45bdd
VZ
140
141 if (c == 0)
142 {
b46b1d59
VZ
143 m_id[0] = fd;
144 handler->AddFlag(wxFDIO_INPUT);
30c45bdd
VZ
145 }
146 else
147 {
b46b1d59
VZ
148 m_id[1] = fd;
149 handler->AddFlag(wxFDIO_OUTPUT);
30c45bdd
VZ
150 }
151
b46b1d59 152 dispatcher->RegisterFD(fd, handler, handler->GetFlags());
30c45bdd
VZ
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
b46b1d59
VZ
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 )
30c45bdd 185 {
b46b1d59
VZ
186 handler->RemoveFlag(flag);
187
188 if ( !handler->GetFlags() )
30c45bdd
VZ
189 delete handler;
190 }
30c45bdd
VZ
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