]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/sockunix.h
return wxStandardPaths, not wxStandardPathsBase, from wxStandardPathsBase::Get()...
[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 protected:
121 // identifies either input or output direction
122 //
123 // NB: the values of this enum shouldn't change
124 enum SocketDir
125 {
126 FD_INPUT,
127 FD_OUTPUT
128 };
129
130 // get the FD index corresponding to the given wxSocketNotify
131 SocketDir GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event)
132 {
133 switch ( event )
134 {
135 default:
136 wxFAIL_MSG( "unknown socket event" );
137 return FD_INPUT; // we must return something
138
139 case wxSOCKET_LOST:
140 wxFAIL_MSG( "unexpected socket event" );
141 return FD_INPUT; // as above
142
143 case wxSOCKET_INPUT:
144 return FD_INPUT;
145
146 case wxSOCKET_OUTPUT:
147 return FD_OUTPUT;
148
149 case wxSOCKET_CONNECTION:
150 // for server sockets we're interested in events indicating
151 // that a new connection is pending, i.e. that accept() will
152 // succeed and this is indicated by socket becoming ready for
153 // reading, while for the other ones we're interested in the
154 // completion of non-blocking connect() which is indicated by
155 // the socket becoming ready for writing
156 return socket->IsServer() ? FD_INPUT : FD_OUTPUT;
157 }
158 }
159
160 // access the FDs we store
161 int& FD(wxSocketImplUnix *socket, SocketDir d)
162 {
163 return socket->m_fds[d];
164 }
165 };
166
167 // Common base class for all ports using X11-like (and hence implemented in
168 // X11, Motif and GTK) AddInput() and RemoveInput() functions
169 class wxSocketInputBasedManager : public wxSocketFDBasedManager
170 {
171 public:
172 virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event)
173 {
174 wxSocketImplUnix * const
175 socket = static_cast<wxSocketImplUnix *>(socket_);
176
177 wxCHECK_RET( socket->m_fd != -1,
178 "shouldn't be called on invalid socket" );
179
180 const SocketDir d = GetDirForEvent(socket, event);
181
182 int& fd = FD(socket, d);
183 if ( fd != -1 )
184 RemoveInput(fd);
185
186 fd = AddInput(socket, socket->m_fd, d);
187 }
188
189 virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event)
190 {
191 wxSocketImplUnix * const
192 socket = static_cast<wxSocketImplUnix *>(socket_);
193
194 const SocketDir d = GetDirForEvent(socket, event);
195
196 int& fd = FD(socket, d);
197 if ( fd != -1 )
198 {
199 RemoveInput(fd);
200 fd = -1;
201 }
202 }
203
204 private:
205 // these functions map directly to XtAdd/RemoveInput() or
206 // gdk_input_add/remove()
207 virtual int AddInput(wxFDIOHandler *handler, int fd, SocketDir d) = 0;
208 virtual void RemoveInput(int fd) = 0;
209 };
210
211 #endif /* _WX_UNIX_GSOCKUNX_H_ */