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