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
)
30 m_enabledCallbacks
= 0;
33 virtual void Shutdown();
34 virtual wxSocketImpl
*WaitConnection(wxSocketBase
& wxsocket
);
36 int Read(void *buffer
, int size
);
37 int Write(const void *buffer
, int size
);
38 //attach or detach from main loop
39 void Notify(bool flag
);
41 // wxFDIOHandler methods
42 virtual void OnReadWaiting();
43 virtual void OnWriteWaiting();
44 virtual void OnExceptionWaiting();
46 // Unix-specific functions
47 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks
!= 0; }
48 void EnableCallback(wxFDIODispatcherEntryFlags flag
)
49 { m_enabledCallbacks
|= flag
; }
50 void DisableCallback(wxFDIODispatcherEntryFlags flag
)
51 { m_enabledCallbacks
&= ~flag
; }
52 int GetEnabledCallbacks() const { return m_enabledCallbacks
; }
55 virtual wxSocketError
DoHandleConnect(int ret
);
56 virtual void DoClose()
58 wxSocketManager
* const manager
= wxSocketManager::Get();
61 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
62 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
68 virtual void UnblockAndRegisterWithEventLoop()
71 ioctl(m_fd
, FIONBIO
, &trueArg
);
76 // enable or disable notifications for socket input/output events but only
77 // if m_use_events is true; do nothing otherwise
78 virtual void EnableEvents()
87 DoEnableEvents(false);
90 // really enable or disable socket input/output events, regardless of
92 void DoEnableEvents(bool enable
);
95 // enable or disable events for the given event if m_use_events; do nothing
98 // notice that these functions also update m_detected: EnableEvent() clears
99 // the corresponding bit in it and DisableEvent() sets it
100 void EnableEvent(wxSocketNotify event
);
101 void DisableEvent(wxSocketNotify event
);
103 int Recv_Stream(void *buffer
, int size
);
104 int Recv_Dgram(void *buffer
, int size
);
105 int Send_Stream(const void *buffer
, int size
);
106 int Send_Dgram(const void *buffer
, int size
);
110 // true if socket should fire events
113 // descriptors for input and output event notification channels associated
117 // the events which are currently enabled for this socket, combination of
118 // wxFDIO_INPUT and wxFDIO_OUTPUT values
120 // TODO: this overlaps with m_detected but the semantics of the latter are
121 // very unclear so I don't dare to remove it right now
122 int m_enabledCallbacks
;
125 // notify the associated wxSocket about a change in socket state and shut
126 // down the socket if the event is wxSOCKET_LOST
127 void OnStateChange(wxSocketNotify event
);
129 // give it access to our m_fds
130 friend class wxSocketFDBasedManager
;
133 // A version of wxSocketManager which uses FDs for socket IO
134 class wxSocketFDBasedManager
: public wxSocketManager
137 // no special initialization/cleanup needed when using FDs
138 virtual bool OnInit() { return true; }
139 virtual void OnExit() { }
142 // identifies either input or output direction
144 // NB: the values of this enum shouldn't change
151 // get the FD index corresponding to the given wxSocketNotify
152 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
157 wxFAIL_MSG( "unexpected socket event" );
166 case wxSOCKET_OUTPUT
:
169 case wxSOCKET_CONNECTION
:
170 // FIXME: explain this?
171 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
175 // access the FDs we store
176 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
178 return socket
->m_fds
[d
];
182 // Common base class for all ports using X11-like (and hence implemented in
183 // X11, Motif and GTK) AddInput() and RemoveInput() functions
184 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
187 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
189 wxSocketImplUnix
* const
190 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
192 wxCHECK_RET( socket
->m_fd
!= -1,
193 "shouldn't be called on invalid socket" );
195 const SocketDir d
= GetDirForEvent(socket
, event
);
197 int& fd
= FD(socket
, d
);
201 fd
= AddInput(socket
, socket
->m_fd
, d
);
204 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
206 wxSocketImplUnix
* const
207 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
209 const SocketDir d
= GetDirForEvent(socket
, event
);
211 int& fd
= FD(socket
, d
);
220 // these functions map directly to XtAdd/RemoveInput() or
221 // gdk_input_add/remove()
222 virtual int AddInput(wxFDIOHandler
*handler
, int fd
, SocketDir d
) = 0;
223 virtual void RemoveInput(int fd
) = 0;
226 #endif /* _WX_UNIX_GSOCKUNX_H_ */