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 Shutdown();
36 virtual int Read(void *buffer
, int size
);
37 virtual int Write(const void *buffer
, int size
);
39 // wxFDIOHandler methods
40 virtual void OnReadWaiting();
41 virtual void OnWriteWaiting();
42 virtual void OnExceptionWaiting();
44 // Unix-specific functions
45 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks
!= 0; }
46 void EnableCallback(wxFDIODispatcherEntryFlags flag
)
47 { m_enabledCallbacks
|= flag
; }
48 void DisableCallback(wxFDIODispatcherEntryFlags flag
)
49 { m_enabledCallbacks
&= ~flag
; }
50 int GetEnabledCallbacks() const { return m_enabledCallbacks
; }
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); }
77 // really enable or disable socket input/output events
78 void DoEnableEvents(bool enable
);
81 // enable or disable events for the given event
82 void EnableEvent(wxSocketNotify event
);
83 void DisableEvent(wxSocketNotify event
);
85 int Recv_Stream(void *buffer
, int size
);
86 int Recv_Dgram(void *buffer
, int size
);
87 int Send_Stream(const void *buffer
, int size
);
88 int Send_Dgram(const void *buffer
, int size
);
92 // descriptors for input and output event notification channels associated
96 // the events which are currently enabled for this socket, combination of
97 // wxFDIO_INPUT and wxFDIO_OUTPUT values
98 int m_enabledCallbacks
;
101 // notify the associated wxSocket about a change in socket state and shut
102 // down the socket if the event is wxSOCKET_LOST
103 void OnStateChange(wxSocketNotify event
);
105 // give it access to our m_fds
106 friend class wxSocketFDBasedManager
;
109 // A version of wxSocketManager which uses FDs for socket IO
110 class wxSocketFDBasedManager
: public wxSocketManager
113 // no special initialization/cleanup needed when using FDs
114 virtual bool OnInit() { return true; }
115 virtual void OnExit() { }
118 // identifies either input or output direction
120 // NB: the values of this enum shouldn't change
127 // get the FD index corresponding to the given wxSocketNotify
128 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
133 wxFAIL_MSG( "unexpected socket event" );
142 case wxSOCKET_OUTPUT
:
145 case wxSOCKET_CONNECTION
:
146 // FIXME: explain this?
147 return socket
->IsServer() ? FD_INPUT
: FD_OUTPUT
;
151 // access the FDs we store
152 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
154 return socket
->m_fds
[d
];
158 // Common base class for all ports using X11-like (and hence implemented in
159 // X11, Motif and GTK) AddInput() and RemoveInput() functions
160 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
163 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
165 wxSocketImplUnix
* const
166 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
168 wxCHECK_RET( socket
->m_fd
!= -1,
169 "shouldn't be called on invalid socket" );
171 const SocketDir d
= GetDirForEvent(socket
, event
);
173 int& fd
= FD(socket
, d
);
177 fd
= AddInput(socket
, socket
->m_fd
, d
);
180 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
182 wxSocketImplUnix
* const
183 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
185 const SocketDir d
= GetDirForEvent(socket
, event
);
187 int& fd
= FD(socket
, d
);
196 // these functions map directly to XtAdd/RemoveInput() or
197 // gdk_input_add/remove()
198 virtual int AddInput(wxFDIOHandler
*handler
, int fd
, SocketDir d
) = 0;
199 virtual void RemoveInput(int fd
) = 0;
202 #endif /* _WX_UNIX_GSOCKUNX_H_ */