]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/sockunix.h
no real changes, just moved wxSocketImplUnix ctor inline
[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
18 class wxSocketImplUnix : public wxSocketImpl
19 {
20 public:
21 wxSocketImplUnix(wxSocketBase& wxsocket)
22 : wxSocketImpl(wxsocket)
23 {
24 m_fds[0] =
25 m_fds[1] = -1;
26
27 m_use_events = false;
28 }
29
30 virtual void Shutdown();
31 virtual wxSocketImpl *WaitConnection(wxSocketBase& wxsocket);
32
33 int Read(char *buffer, int size);
34 int Write(const char *buffer, int size);
35 //attach or detach from main loop
36 void Notify(bool flag);
37 void Detected_Read();
38 void Detected_Write();
39
40 private:
41 virtual wxSocketError DoHandleConnect(int ret);
42 virtual void DoClose()
43 {
44 wxSocketManager * const manager = wxSocketManager::Get();
45 if ( manager )
46 {
47 manager->Uninstall_Callback(this, wxSOCKET_INPUT);
48 manager->Uninstall_Callback(this, wxSOCKET_OUTPUT);
49 }
50
51 close(m_fd);
52 }
53
54 virtual void UnblockAndRegisterWithEventLoop()
55 {
56 int trueArg = 1;
57 ioctl(m_fd, FIONBIO, &trueArg);
58
59 EnableEvents();
60 }
61
62 // enable or disable notifications for socket input/output events but only
63 // if m_use_events is true; do nothing otherwise
64 virtual void EnableEvents()
65 {
66 if ( m_use_events )
67 DoEnableEvents(true);
68 }
69
70 void DisableEvents()
71 {
72 if ( m_use_events )
73 DoEnableEvents(false);
74 }
75
76 // really enable or disable socket input/output events, regardless of
77 // m_use_events value
78 void DoEnableEvents(bool enable);
79
80
81 // enable or disable events for the given event if m_use_events; do nothing
82 // otherwise
83 //
84 // notice that these functions also update m_detected: EnableEvent() clears
85 // the corresponding bit in it and DisableEvent() sets it
86 void EnableEvent(wxSocketNotify event);
87 void DisableEvent(wxSocketNotify event);
88
89
90 wxSocketError Input_Timeout();
91 wxSocketError Output_Timeout();
92 int Recv_Stream(char *buffer, int size);
93 int Recv_Dgram(char *buffer, int size);
94 int Send_Stream(const char *buffer, int size);
95 int Send_Dgram(const char *buffer, int size);
96
97 protected:
98 // true if socket should fire events
99 bool m_use_events;
100
101 // descriptors for input and output event notification channels associated
102 // with the socket
103 int m_fds[2];
104
105 private:
106 // notify the associated wxSocket about a change in socket state and shut
107 // down the socket if the event is wxSOCKET_LOST
108 void OnStateChange(wxSocketNotify event);
109
110 // give it access to our m_fds
111 friend class wxSocketFDBasedManager;
112 };
113
114 // A version of wxSocketManager which uses FDs for socket IO
115 class wxSocketFDBasedManager : public wxSocketManager
116 {
117 public:
118 // no special initialization/cleanup needed when using FDs
119 virtual bool OnInit() { return true; }
120 virtual void OnExit() { }
121
122 // allocate/free the storage we need
123 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
124 {
125 return new wxSocketImplUnix(wxsocket);
126 }
127
128 protected:
129 // identifies either input or output direction
130 //
131 // NB: the values of this enum shouldn't change
132 enum SocketDir
133 {
134 FD_INPUT,
135 FD_OUTPUT
136 };
137
138 // get the FD index corresponding to the given wxSocketNotify
139 SocketDir GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event)
140 {
141 switch ( event )
142 {
143 default:
144 wxFAIL_MSG( "unexpected socket event" );
145 // fall through
146
147 case wxSOCKET_LOST:
148 // fall through
149
150 case wxSOCKET_INPUT:
151 return FD_INPUT;
152
153 case wxSOCKET_OUTPUT:
154 return FD_OUTPUT;
155
156 case wxSOCKET_CONNECTION:
157 // FIXME: explain this?
158 return socket->m_server ? FD_INPUT : FD_OUTPUT;
159 }
160 }
161
162 // access the FDs we store
163 int& FD(wxSocketImpl *socket, SocketDir d)
164 {
165 return static_cast<wxSocketImplUnix *>(socket)->m_fds[d];
166 }
167 };
168
169 // Common base class for all ports using X11-like (and hence implemented in
170 // X11, Motif and GTK) AddInput() and RemoveInput() functions
171 class wxSocketInputBasedManager : public wxSocketFDBasedManager
172 {
173 public:
174 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event)
175 {
176 wxCHECK_RET( socket->m_fd != -1,
177 "shouldn't be called on invalid socket" );
178
179 const SocketDir d = GetDirForEvent(socket, event);
180
181 int& fd = FD(socket, d);
182 if ( fd != -1 )
183 RemoveInput(fd);
184
185 fd = AddInput(socket, d);
186 }
187
188 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event)
189 {
190 const SocketDir d = GetDirForEvent(socket, event);
191
192 int& fd = FD(socket, d);
193 if ( fd != -1 )
194 {
195 RemoveInput(fd);
196 fd = -1;
197 }
198 }
199
200 private:
201 // these functions map directly to XtAdd/RemoveInput() or
202 // gdk_input_add/remove()
203 virtual int AddInput(wxSocketImpl *socket, SocketDir d) = 0;
204 virtual void RemoveInput(int fd) = 0;
205 };
206
207 #endif /* _WX_UNIX_GSOCKUNX_H_ */