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 // wxFDIOHandler methods
35 virtual void OnReadWaiting();
36 virtual void OnWriteWaiting();
37 virtual void OnExceptionWaiting();
39 // Unix-specific functions
40 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks
!= 0; }
41 void EnableCallback(wxFDIODispatcherEntryFlags flag
)
42 { m_enabledCallbacks
|= flag
; }
43 void DisableCallback(wxFDIODispatcherEntryFlags flag
)
44 { m_enabledCallbacks
&= ~flag
; }
45 int GetEnabledCallbacks() const { return m_enabledCallbacks
; }
48 virtual void DoClose()
55 virtual void UnblockAndRegisterWithEventLoop()
58 ioctl(m_fd
, FIONBIO
, &trueArg
);
63 // enable or disable notifications for socket input/output events
64 void EnableEvents() { DoEnableEvents(true); }
65 void DisableEvents() { DoEnableEvents(false); }
67 // really enable or disable socket input/output events
68 void DoEnableEvents(bool enable
);
71 // descriptors for input and output event notification channels associated
75 // the events which are currently enabled for this socket, combination of
76 // wxFDIO_INPUT and wxFDIO_OUTPUT values
77 int m_enabledCallbacks
;
80 // notify the associated wxSocket about a change in socket state and shut
81 // down the socket if the event is wxSOCKET_LOST
82 void OnStateChange(wxSocketNotify event
);
84 // give it access to our m_fds
85 friend class wxSocketFDBasedManager
;
88 // A version of wxSocketManager which uses FDs for socket IO
89 class wxSocketFDBasedManager
: public wxSocketManager
92 // no special initialization/cleanup needed when using FDs
93 virtual bool OnInit() { return true; }
94 virtual void OnExit() { }
97 // identifies either input or output direction
99 // NB: the values of this enum shouldn't change
106 // get the FD index corresponding to the given wxSocketNotify
107 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
112 wxFAIL_MSG( "unexpected socket event" );
121 case wxSOCKET_OUTPUT
:
124 case wxSOCKET_CONNECTION
:
125 // FIXME: explain this?
126 return socket
->IsServer() ? FD_INPUT
: FD_OUTPUT
;
130 // access the FDs we store
131 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
133 return socket
->m_fds
[d
];
137 // Common base class for all ports using X11-like (and hence implemented in
138 // X11, Motif and GTK) AddInput() and RemoveInput() functions
139 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
142 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
144 wxSocketImplUnix
* const
145 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
147 wxCHECK_RET( socket
->m_fd
!= -1,
148 "shouldn't be called on invalid socket" );
150 const SocketDir d
= GetDirForEvent(socket
, event
);
152 int& fd
= FD(socket
, d
);
156 fd
= AddInput(socket
, socket
->m_fd
, d
);
159 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
161 wxSocketImplUnix
* const
162 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
164 const SocketDir d
= GetDirForEvent(socket
, event
);
166 int& fd
= FD(socket
, d
);
175 // these functions map directly to XtAdd/RemoveInput() or
176 // gdk_input_add/remove()
177 virtual int AddInput(wxFDIOHandler
*handler
, int fd
, SocketDir d
) = 0;
178 virtual void RemoveInput(int fd
) = 0;
181 #endif /* _WX_UNIX_GSOCKUNX_H_ */