1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/private/sockunix.h
3 // Purpose: wxSocketImpl implementation for Unix systems
4 // Authors: Guilhem Lavaux, Vadim Zeitlin
7 // Copyright: (c) 1997 Guilhem Lavaux
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_UNIX_GSOCKUNX_H_
13 #define _WX_UNIX_GSOCKUNX_H_
16 #include <sys/ioctl.h>
17 #include "wx/private/fdiodispatcher.h"
19 class wxSocketImplUnix
: public wxSocketImpl
,
23 wxSocketImplUnix(wxSocketBase
& wxsocket
)
24 : wxSocketImpl(wxsocket
)
29 m_enabledCallbacks
= 0;
32 virtual wxSocketError
GetLastError() const;
34 virtual void ReenableEvents(wxSocketEventFlags flags
)
36 // enable the notifications about input/output being available again in
37 // case they were disabled by OnRead/WriteWaiting()
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
51 // wxFDIOHandler methods
52 virtual void OnReadWaiting();
53 virtual void OnWriteWaiting();
54 virtual void OnExceptionWaiting();
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
; }
65 virtual void DoClose()
72 virtual void UnblockAndRegisterWithEventLoop()
75 ioctl(m_fd
, FIONBIO
, &trueArg
);
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); }
86 // really enable or disable socket input/output events
87 void DoEnableEvents(int flags
, bool enable
);
90 // descriptors for input and output event notification channels associated
94 // the events which are currently enabled for this socket, combination of
95 // wxFDIO_INPUT and wxFDIO_OUTPUT values
96 int m_enabledCallbacks
;
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
);
103 // check if there is any input available, return 1 if yes, 0 if no or -1 on
108 // give it access to our m_fds
109 friend class wxSocketFDBasedManager
;
112 // A version of wxSocketManager which uses FDs for socket IO
113 class wxSocketFDBasedManager
: public wxSocketManager
116 // no special initialization/cleanup needed when using FDs
117 virtual bool OnInit() { return true; }
118 virtual void OnExit() { }
121 // identifies either input or output direction
123 // NB: the values of this enum shouldn't change
130 // get the FD index corresponding to the given wxSocketNotify
131 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
136 wxFAIL_MSG( "unknown socket event" );
137 return FD_INPUT
; // we must return something
140 wxFAIL_MSG( "unexpected socket event" );
141 return FD_INPUT
; // as above
146 case wxSOCKET_OUTPUT
:
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
;
160 // access the FDs we store
161 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
163 return socket
->m_fds
[d
];
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
172 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
174 wxSocketImplUnix
* const
175 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
177 wxCHECK_RET( socket
->m_fd
!= -1,
178 "shouldn't be called on invalid socket" );
180 const SocketDir d
= GetDirForEvent(socket
, event
);
182 int& fd
= FD(socket
, d
);
186 fd
= AddInput(socket
, socket
->m_fd
, d
);
189 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
191 wxSocketImplUnix
* const
192 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
194 const SocketDir d
= GetDirForEvent(socket
, event
);
196 int& fd
= FD(socket
, d
);
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;
211 #endif /* _WX_UNIX_GSOCKUNX_H_ */