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 void Shutdown();
33 virtual wxSocketImpl
*WaitConnection(wxSocketBase
& wxsocket
);
35 int Read(void *buffer
, int size
);
36 int Write(const void *buffer
, int size
);
38 // wxFDIOHandler methods
39 virtual void OnReadWaiting();
40 virtual void OnWriteWaiting();
41 virtual void OnExceptionWaiting();
43 // Unix-specific functions
44 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks
!= 0; }
45 void EnableCallback(wxFDIODispatcherEntryFlags flag
)
46 { m_enabledCallbacks
|= flag
; }
47 void DisableCallback(wxFDIODispatcherEntryFlags flag
)
48 { m_enabledCallbacks
&= ~flag
; }
49 int GetEnabledCallbacks() const { return m_enabledCallbacks
; }
52 virtual wxSocketError
DoHandleConnect(int ret
);
53 virtual void DoClose()
55 wxSocketManager
* const manager
= wxSocketManager::Get();
58 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
59 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
65 virtual void UnblockAndRegisterWithEventLoop()
68 ioctl(m_fd
, FIONBIO
, &trueArg
);
73 // enable or disable notifications for socket input/output events
74 void EnableEvents() { DoEnableEvents(true); }
75 void DisableEvents() { DoEnableEvents(false);
78 // really enable or disable socket input/output events
79 void DoEnableEvents(bool enable
);
82 // enable or disable events for the given event
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
);
89 int Recv_Stream(void *buffer
, int size
);
90 int Recv_Dgram(void *buffer
, int size
);
91 int Send_Stream(const void *buffer
, int size
);
92 int Send_Dgram(const void *buffer
, int size
);
96 // descriptors for input and output event notification channels associated
100 // the events which are currently enabled for this socket, combination of
101 // wxFDIO_INPUT and wxFDIO_OUTPUT values
103 // TODO: this overlaps with m_detected but the semantics of the latter are
104 // very unclear so I don't dare to remove it right now
105 int m_enabledCallbacks
;
108 // notify the associated wxSocket about a change in socket state and shut
109 // down the socket if the event is wxSOCKET_LOST
110 void OnStateChange(wxSocketNotify event
);
112 // give it access to our m_fds
113 friend class wxSocketFDBasedManager
;
116 // A version of wxSocketManager which uses FDs for socket IO
117 class wxSocketFDBasedManager
: public wxSocketManager
120 // no special initialization/cleanup needed when using FDs
121 virtual bool OnInit() { return true; }
122 virtual void OnExit() { }
125 // identifies either input or output direction
127 // NB: the values of this enum shouldn't change
134 // get the FD index corresponding to the given wxSocketNotify
135 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
140 wxFAIL_MSG( "unexpected socket event" );
149 case wxSOCKET_OUTPUT
:
152 case wxSOCKET_CONNECTION
:
153 // FIXME: explain this?
154 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
158 // access the FDs we store
159 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
161 return socket
->m_fds
[d
];
165 // Common base class for all ports using X11-like (and hence implemented in
166 // X11, Motif and GTK) AddInput() and RemoveInput() functions
167 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
170 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
172 wxSocketImplUnix
* const
173 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
175 wxCHECK_RET( socket
->m_fd
!= -1,
176 "shouldn't be called on invalid socket" );
178 const SocketDir d
= GetDirForEvent(socket
, event
);
180 int& fd
= FD(socket
, d
);
184 fd
= AddInput(socket
, socket
->m_fd
, d
);
187 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
189 wxSocketImplUnix
* const
190 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
192 const SocketDir d
= GetDirForEvent(socket
, event
);
194 int& fd
= FD(socket
, d
);
203 // these functions map directly to XtAdd/RemoveInput() or
204 // gdk_input_add/remove()
205 virtual int AddInput(wxFDIOHandler
*handler
, int fd
, SocketDir d
) = 0;
206 virtual void RemoveInput(int fd
) = 0;
209 #endif /* _WX_UNIX_GSOCKUNX_H_ */