]>
Commit | Line | Data |
---|---|---|
30c45bdd | 1 | /////////////////////////////////////////////////////////////////////////////// |
86c5b12b | 2 | // Name: src/common/socketiohandler.cpp |
51fe4b60 | 3 | // Purpose: implementation of wxFDIOHandler for wxSocket |
30c45bdd | 4 | // Author: Angel Vidal, Lukasz Michalski |
30c45bdd VZ |
5 | // Created: 08.24.06 |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2006 Angel vidal | |
2804f77d | 8 | // (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org> |
30c45bdd VZ |
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 | ||
a1873279 | 23 | #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER |
30c45bdd | 24 | |
2804f77d | 25 | #include "wx/apptrait.h" |
30c45bdd | 26 | #include "wx/unix/private.h" |
86c5b12b | 27 | #include "wx/private/socketiohandler.h" |
30c45bdd VZ |
28 | |
29 | // ============================================================================ | |
30 | // implementation | |
31 | // ============================================================================ | |
32 | ||
33 | // ---------------------------------------------------------------------------- | |
51fe4b60 | 34 | // wxSocketImplFDIO |
30c45bdd VZ |
35 | // ---------------------------------------------------------------------------- |
36 | ||
51fe4b60 | 37 | class wxSocketImplFDIO : public wxSocketImplUnix |
30c45bdd | 38 | { |
2804f77d | 39 | public: |
51fe4b60 VZ |
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); | |
2804f77d | 68 | }; |
30c45bdd | 69 | |
51fe4b60 VZ |
70 | void wxSocketSelectManager::Install_Callback(wxSocketImpl *socket_, |
71 | wxSocketNotify event) | |
30c45bdd | 72 | { |
51fe4b60 VZ |
73 | wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_); |
74 | ||
2804f77d | 75 | const int fd = socket->m_fd; |
30c45bdd | 76 | |
2804f77d VZ |
77 | if ( fd == -1 ) |
78 | return; | |
30c45bdd | 79 | |
2804f77d | 80 | const SocketDir d = GetDirForEvent(socket, event); |
30c45bdd | 81 | |
2804f77d VZ |
82 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
83 | if ( !dispatcher ) | |
84 | return; | |
30c45bdd | 85 | |
51fe4b60 | 86 | wxSocketIOHandler *& handler = socket->m_handler; |
30c45bdd | 87 | |
2804f77d VZ |
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; | |
51fe4b60 | 97 | handler = new wxSocketIOHandler(socket); |
2804f77d | 98 | } |
30c45bdd | 99 | |
2804f77d VZ |
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 | } | |
30c45bdd | 109 | |
2804f77d VZ |
110 | if ( registerHandler ) |
111 | dispatcher->RegisterFD(fd, handler, handler->GetFlags()); | |
112 | else | |
113 | dispatcher->ModifyFD(fd, handler, handler->GetFlags()); | |
30c45bdd VZ |
114 | } |
115 | ||
51fe4b60 VZ |
116 | void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl *socket_, |
117 | wxSocketNotify event) | |
30c45bdd | 118 | { |
51fe4b60 VZ |
119 | wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_); |
120 | ||
2804f77d | 121 | const SocketDir d = GetDirForEvent(socket, event); |
30c45bdd | 122 | |
2804f77d VZ |
123 | const int fd = FD(socket, d); |
124 | if ( fd == -1 ) | |
125 | return; | |
30c45bdd | 126 | |
2804f77d | 127 | FD(socket, d) = -1; |
30c45bdd | 128 | |
2804f77d VZ |
129 | const wxFDIODispatcherEntryFlags |
130 | flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT; | |
30c45bdd | 131 | |
2804f77d VZ |
132 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
133 | if ( !dispatcher ) | |
134 | return; | |
30c45bdd | 135 | |
51fe4b60 | 136 | wxSocketIOHandler *& handler = socket->m_handler; |
2804f77d VZ |
137 | if ( handler ) |
138 | { | |
139 | handler->RemoveFlag(flag); | |
30c45bdd | 140 | |
2804f77d VZ |
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 | } | |
30c45bdd VZ |
156 | } |
157 | ||
86c5b12b VZ |
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 | |
30c45bdd | 163 | { |
86c5b12b VZ |
164 | ManagerSetter() |
165 | { | |
166 | static wxSocketSelectManager s_manager; | |
167 | wxAppTraits::SetDefaultSocketManager(&s_manager); | |
168 | } | |
169 | } gs_managerSetter; | |
30c45bdd VZ |
170 | |
171 | #endif // wxUSE_SOCKETS |