]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/sockunix.h
Virtualize wxSocketImpl creation by routing it via wxSocketManager.
[wxWidgets.git] / include / wx / unix / private / sockunix.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/private/sockunix.h
3 // Purpose: wxSocketImpl implementation for Unix systems
4 // Authors: Guilhem Lavaux, Vadim Zeitlin
5 // Created: April 1997
6 // RCS-ID: $Id$
7 // Copyright: (c) 1997 Guilhem Lavaux
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_UNIX_GSOCKUNX_H_
13 #define _WX_UNIX_GSOCKUNX_H_
14
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include "wx/private/fdiodispatcher.h"
18
19 class wxSocketImplUnix : public wxSocketImpl,
20 public wxFDIOHandler
21 {
22 public:
23 wxSocketImplUnix(wxSocketBase& wxsocket)
24 : wxSocketImpl(wxsocket)
25 {
26 m_fds[0] =
27 m_fds[1] = -1;
28
29 m_enabledCallbacks = 0;
30 }
31
32 virtual wxSocketError GetLastError() const;
33
34 virtual void ReenableEvents(wxSocketEventFlags flags)
35 {
36 // enable the notifications about input/output being available again in
37 // case they were disabled by OnRead/WriteWaiting()
38 //
39 // notice that we'd like to enable the events here only if there is
40 // nothing more left on the socket right now as otherwise we're going
41 // to get a "ready for whatever" notification immediately (well, during
42 // the next event loop iteration) and disable the event back again
43 // which is rather inefficient but unfortunately doing it like this
44 // doesn't work because the existing code (e.g. src/common/sckipc.cpp)
45 // expects to keep getting notifications about the data available from
46 // the socket even if it didn't read all the data the last time, so we
47 // absolutely have to continue generating them
48 EnableEvents(flags);
49 }
50
51 // wxFDIOHandler methods
52 virtual void OnReadWaiting();
53 virtual void OnWriteWaiting();
54 virtual void OnExceptionWaiting();
55
56 // Unix-specific functions used by wxSocketFDIOManager only
57 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks != 0; }
58 void EnableCallback(wxFDIODispatcherEntryFlags flag)
59 { m_enabledCallbacks |= flag; }
60 void DisableCallback(wxFDIODispatcherEntryFlags flag)
61 { m_enabledCallbacks &= ~flag; }
62 int GetEnabledCallbacks() const { return m_enabledCallbacks; }
63
64 private:
65 virtual void DoClose()
66 {
67 DisableEvents();
68
69 close(m_fd);
70 }
71
72 virtual void UnblockAndRegisterWithEventLoop()
73 {
74 int trueArg = 1;
75 ioctl(m_fd, FIONBIO, &trueArg);
76
77 EnableEvents();
78 }
79
80 // enable or disable notifications for socket input/output events
81 void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)
82 { DoEnableEvents(flags, true); }
83 void DisableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)
84 { DoEnableEvents(flags, false); }
85
86 // really enable or disable socket input/output events
87 void DoEnableEvents(int flags, bool enable);
88
89 protected:
90 // descriptors for input and output event notification channels associated
91 // with the socket
92 int m_fds[2];
93
94 // the events which are currently enabled for this socket, combination of
95 // wxFDIO_INPUT and wxFDIO_OUTPUT values
96 int m_enabledCallbacks;
97
98 private:
99 // notify the associated wxSocket about a change in socket state and shut
100 // down the socket if the event is wxSOCKET_LOST
101 void OnStateChange(wxSocketNotify event);
102
103 // check if there is any input available, return 1 if yes, 0 if no or -1 on
104 // error
105 int CheckForInput();
106
107
108 // give it access to our m_fds
109 friend class wxSocketFDBasedManager;
110 };
111
112 // A version of wxSocketManager which uses FDs for socket IO
113 class wxSocketFDBasedManager : public wxSocketManager
114 {
115 public:
116 // no special initialization/cleanup needed when using FDs
117 virtual bool OnInit() { return true; }
118 virtual void OnExit() { }
119
120 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
121 {
122 return new wxSocketImplUnix(wxsocket);
123 }
124
125 protected:
126 // identifies either input or output direction
127 //
128 // NB: the values of this enum shouldn't change
129 enum SocketDir
130 {
131 FD_INPUT,
132 FD_OUTPUT
133 };
134
135 // get the FD index corresponding to the given wxSocketNotify
136 SocketDir GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event)
137 {
138 switch ( event )
139 {
140 default:
141 wxFAIL_MSG( "unknown socket event" );
142 return FD_INPUT; // we must return something
143
144 case wxSOCKET_LOST:
145 wxFAIL_MSG( "unexpected socket event" );
146 return FD_INPUT; // as above
147
148 case wxSOCKET_INPUT:
149 return FD_INPUT;
150
151 case wxSOCKET_OUTPUT:
152 return FD_OUTPUT;
153
154 case wxSOCKET_CONNECTION:
155 // for server sockets we're interested in events indicating
156 // that a new connection is pending, i.e. that accept() will
157 // succeed and this is indicated by socket becoming ready for
158 // reading, while for the other ones we're interested in the
159 // completion of non-blocking connect() which is indicated by
160 // the socket becoming ready for writing
161 return socket->IsServer() ? FD_INPUT : FD_OUTPUT;
162 }
163 }
164
165 // access the FDs we store
166 int& FD(wxSocketImplUnix *socket, SocketDir d)
167 {
168 return socket->m_fds[d];
169 }
170 };
171
172 // Common base class for all ports using X11-like (and hence implemented in
173 // X11, Motif and GTK) AddInput() and RemoveInput() functions
174 class wxSocketInputBasedManager : public wxSocketFDBasedManager
175 {
176 public:
177 virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event)
178 {
179 wxSocketImplUnix * const
180 socket = static_cast<wxSocketImplUnix *>(socket_);
181
182 wxCHECK_RET( socket->m_fd != -1,
183 "shouldn't be called on invalid socket" );
184
185 const SocketDir d = GetDirForEvent(socket, event);
186
187 int& fd = FD(socket, d);
188 if ( fd != -1 )
189 RemoveInput(fd);
190
191 fd = AddInput(socket, socket->m_fd, d);
192 }
193
194 virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event)
195 {
196 wxSocketImplUnix * const
197 socket = static_cast<wxSocketImplUnix *>(socket_);
198
199 const SocketDir d = GetDirForEvent(socket, event);
200
201 int& fd = FD(socket, d);
202 if ( fd != -1 )
203 {
204 RemoveInput(fd);
205 fd = -1;
206 }
207 }
208
209 private:
210 // these functions map directly to XtAdd/RemoveInput() or
211 // gdk_input_add/remove()
212 virtual int AddInput(wxSocketImplUnix *handler, int fd, SocketDir d) = 0;
213 virtual void RemoveInput(int fd) = 0;
214 };
215
216 #endif /* _WX_UNIX_GSOCKUNX_H_ */