]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/sockunix.h
remove the badle defined and apparently unnecessary wxSocketImpl::m_detected field
[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 Shutdown();
35
36 virtual int Read(void *buffer, int size);
37 virtual int Write(const void *buffer, int size);
38
39 // wxFDIOHandler methods
40 virtual void OnReadWaiting();
41 virtual void OnWriteWaiting();
42 virtual void OnExceptionWaiting();
43
44 // Unix-specific functions
45 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks != 0; }
46 void EnableCallback(wxFDIODispatcherEntryFlags flag)
47 { m_enabledCallbacks |= flag; }
48 void DisableCallback(wxFDIODispatcherEntryFlags flag)
49 { m_enabledCallbacks &= ~flag; }
50 int GetEnabledCallbacks() const { return m_enabledCallbacks; }
51
52 private:
53 virtual void DoClose()
54 {
55 wxSocketManager * const manager = wxSocketManager::Get();
56 if ( manager )
57 {
58 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
59 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
60 }
61
62 close(m_fd);
63 }
64
65 virtual void UnblockAndRegisterWithEventLoop()
66 {
67 int trueArg = 1;
68 ioctl(m_fd, FIONBIO, &trueArg);
69
70 EnableEvents();
71 }
72
73 // enable or disable notifications for socket input/output events
74 void EnableEvents() { DoEnableEvents(true); }
75 void DisableEvents() { DoEnableEvents(false); }
76
77 // really enable or disable socket input/output events
78 void DoEnableEvents(bool enable);
79
80
81 // enable or disable events for the given event
82 void EnableEvent(wxSocketNotify event);
83 void DisableEvent(wxSocketNotify event);
84
85 int Recv_Stream(void *buffer, int size);
86 int Recv_Dgram(void *buffer, int size);
87 int Send_Stream(const void *buffer, int size);
88 int Send_Dgram(const void *buffer, int size);
89
90
91 protected:
92 // descriptors for input and output event notification channels associated
93 // with the socket
94 int m_fds[2];
95
96 // the events which are currently enabled for this socket, combination of
97 // wxFDIO_INPUT and wxFDIO_OUTPUT values
98 int m_enabledCallbacks;
99
100 private:
101 // notify the associated wxSocket about a change in socket state and shut
102 // down the socket if the event is wxSOCKET_LOST
103 void OnStateChange(wxSocketNotify event);
104
105 // give it access to our m_fds
106 friend class wxSocketFDBasedManager;
107 };
108
109 // A version of wxSocketManager which uses FDs for socket IO
110 class wxSocketFDBasedManager : public wxSocketManager
111 {
112 public:
113 // no special initialization/cleanup needed when using FDs
114 virtual bool OnInit() { return true; }
115 virtual void OnExit() { }
116
117 protected:
118 // identifies either input or output direction
119 //
120 // NB: the values of this enum shouldn't change
121 enum SocketDir
122 {
123 FD_INPUT,
124 FD_OUTPUT
125 };
126
127 // get the FD index corresponding to the given wxSocketNotify
128 SocketDir GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event)
129 {
130 switch ( event )
131 {
132 default:
133 wxFAIL_MSG( "unexpected socket event" );
134 // fall through
135
136 case wxSOCKET_LOST:
137 // fall through
138
139 case wxSOCKET_INPUT:
140 return FD_INPUT;
141
142 case wxSOCKET_OUTPUT:
143 return FD_OUTPUT;
144
145 case wxSOCKET_CONNECTION:
146 // FIXME: explain this?
147 return socket->IsServer() ? FD_INPUT : FD_OUTPUT;
148 }
149 }
150
151 // access the FDs we store
152 int& FD(wxSocketImplUnix *socket, SocketDir d)
153 {
154 return socket->m_fds[d];
155 }
156 };
157
158 // Common base class for all ports using X11-like (and hence implemented in
159 // X11, Motif and GTK) AddInput() and RemoveInput() functions
160 class wxSocketInputBasedManager : public wxSocketFDBasedManager
161 {
162 public:
163 virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event)
164 {
165 wxSocketImplUnix * const
166 socket = static_cast<wxSocketImplUnix *>(socket_);
167
168 wxCHECK_RET( socket->m_fd != -1,
169 "shouldn't be called on invalid socket" );
170
171 const SocketDir d = GetDirForEvent(socket, event);
172
173 int& fd = FD(socket, d);
174 if ( fd != -1 )
175 RemoveInput(fd);
176
177 fd = AddInput(socket, socket->m_fd, d);
178 }
179
180 virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event)
181 {
182 wxSocketImplUnix * const
183 socket = static_cast<wxSocketImplUnix *>(socket_);
184
185 const SocketDir d = GetDirForEvent(socket, event);
186
187 int& fd = FD(socket, d);
188 if ( fd != -1 )
189 {
190 RemoveInput(fd);
191 fd = -1;
192 }
193 }
194
195 private:
196 // these functions map directly to XtAdd/RemoveInput() or
197 // gdk_input_add/remove()
198 virtual int AddInput(wxFDIOHandler *handler, int fd, SocketDir d) = 0;
199 virtual void RemoveInput(int fd) = 0;
200 };
201
202 #endif /* _WX_UNIX_GSOCKUNX_H_ */