]> git.saurik.com Git - wxWidgets.git/blob - src/common/socketiohandler.cpp
also rename gsocketiohandler.* to socketiohandler.* and move it to wxNet where it...
[wxWidgets.git] / src / common / socketiohandler.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/socketiohandler.cpp
3 // Purpose: implementation of wxFDIOHandler for wxSocket
4 // Author: Angel Vidal, Lukasz Michalski
5 // Created: 08.24.06
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Angel vidal
8 // (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER
24
25 #include "wx/apptrait.h"
26 #include "wx/unix/private.h"
27 #include "wx/private/socketiohandler.h"
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 // ----------------------------------------------------------------------------
34 // wxSocketImplFDIO
35 // ----------------------------------------------------------------------------
36
37 class wxSocketImplFDIO : public wxSocketImplUnix
38 {
39 public:
40 wxSocketImplFDIO(wxSocketBase& wxsocket)
41 : wxSocketImplUnix(wxsocket)
42 {
43 m_handler = NULL;
44 }
45
46 virtual ~wxSocketImplFDIO()
47 {
48 delete m_handler;
49 }
50
51 wxSocketIOHandler *m_handler;
52 };
53
54 // ----------------------------------------------------------------------------
55 // wxSocketSelectManager
56 // ----------------------------------------------------------------------------
57
58 class wxSocketSelectManager : public wxSocketFDBasedManager
59 {
60 public:
61 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
62 {
63 return new wxSocketImplFDIO(wxsocket);
64 }
65
66 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
67 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
68 };
69
70 void wxSocketSelectManager::Install_Callback(wxSocketImpl *socket_,
71 wxSocketNotify event)
72 {
73 wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_);
74
75 const int fd = socket->m_fd;
76
77 if ( fd == -1 )
78 return;
79
80 const SocketDir d = GetDirForEvent(socket, event);
81
82 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
83 if ( !dispatcher )
84 return;
85
86 wxSocketIOHandler *& handler = socket->m_handler;
87
88 // we should register the new handlers but modify the existing ones in place
89 bool registerHandler;
90 if ( handler )
91 {
92 registerHandler = false;
93 }
94 else // no existing handler
95 {
96 registerHandler = true;
97 handler = new wxSocketIOHandler(socket);
98 }
99
100 FD(socket, d) = fd;
101 if (d == FD_INPUT)
102 {
103 handler->AddFlag(wxFDIO_INPUT);
104 }
105 else
106 {
107 handler->AddFlag(wxFDIO_OUTPUT);
108 }
109
110 if ( registerHandler )
111 dispatcher->RegisterFD(fd, handler, handler->GetFlags());
112 else
113 dispatcher->ModifyFD(fd, handler, handler->GetFlags());
114 }
115
116 void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl *socket_,
117 wxSocketNotify event)
118 {
119 wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_);
120
121 const SocketDir d = GetDirForEvent(socket, event);
122
123 const int fd = FD(socket, d);
124 if ( fd == -1 )
125 return;
126
127 FD(socket, d) = -1;
128
129 const wxFDIODispatcherEntryFlags
130 flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
131
132 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
133 if ( !dispatcher )
134 return;
135
136 wxSocketIOHandler *& handler = socket->m_handler;
137 if ( handler )
138 {
139 handler->RemoveFlag(flag);
140
141 if ( !handler->GetFlags() )
142 {
143 dispatcher->UnregisterFD(fd);
144 delete handler;
145 socket->m_handler = NULL;
146 }
147 else
148 {
149 dispatcher->ModifyFD(fd, handler, handler->GetFlags());
150 }
151 }
152 else
153 {
154 dispatcher->UnregisterFD(fd);
155 }
156 }
157
158 // set the wxBase variable to point to our wxSocketManager implementation
159 //
160 // see comments in wx/apptrait.h for the explanation of why do we do it
161 // like this
162 static struct ManagerSetter
163 {
164 ManagerSetter()
165 {
166 static wxSocketSelectManager s_manager;
167 wxAppTraits::SetDefaultSocketManager(&s_manager);
168 }
169 } gs_managerSetter;
170
171 #endif // wxUSE_SOCKETS