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 int Read(void *buffer
, int size
);
35 virtual int Write(const void *buffer
, int size
);
37 // wxFDIOHandler methods
38 virtual void OnReadWaiting();
39 virtual void OnWriteWaiting();
40 virtual void OnExceptionWaiting();
42 // Unix-specific functions
43 bool HasAnyEnabledCallbacks() const { return m_enabledCallbacks
!= 0; }
44 void EnableCallback(wxFDIODispatcherEntryFlags flag
)
45 { m_enabledCallbacks
|= flag
; }
46 void DisableCallback(wxFDIODispatcherEntryFlags flag
)
47 { m_enabledCallbacks
&= ~flag
; }
48 int GetEnabledCallbacks() const { return m_enabledCallbacks
; }
51 virtual void DoClose()
58 virtual void UnblockAndRegisterWithEventLoop()
61 ioctl(m_fd
, FIONBIO
, &trueArg
);
66 // enable or disable notifications for socket input/output events
67 void EnableEvents() { DoEnableEvents(true); }
68 void DisableEvents() { DoEnableEvents(false); }
70 // really enable or disable socket input/output events
71 void DoEnableEvents(bool enable
);
74 int Recv_Stream(void *buffer
, int size
);
75 int Recv_Dgram(void *buffer
, int size
);
76 int Send_Stream(const void *buffer
, int size
);
77 int Send_Dgram(const void *buffer
, int size
);
81 // descriptors for input and output event notification channels associated
85 // the events which are currently enabled for this socket, combination of
86 // wxFDIO_INPUT and wxFDIO_OUTPUT values
87 int m_enabledCallbacks
;
90 // notify the associated wxSocket about a change in socket state and shut
91 // down the socket if the event is wxSOCKET_LOST
92 void OnStateChange(wxSocketNotify event
);
94 // give it access to our m_fds
95 friend class wxSocketFDBasedManager
;
98 // A version of wxSocketManager which uses FDs for socket IO
99 class wxSocketFDBasedManager
: public wxSocketManager
102 // no special initialization/cleanup needed when using FDs
103 virtual bool OnInit() { return true; }
104 virtual void OnExit() { }
107 // identifies either input or output direction
109 // NB: the values of this enum shouldn't change
116 // get the FD index corresponding to the given wxSocketNotify
117 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
122 wxFAIL_MSG( "unexpected socket event" );
131 case wxSOCKET_OUTPUT
:
134 case wxSOCKET_CONNECTION
:
135 // FIXME: explain this?
136 return socket
->IsServer() ? FD_INPUT
: FD_OUTPUT
;
140 // access the FDs we store
141 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
143 return socket
->m_fds
[d
];
147 // Common base class for all ports using X11-like (and hence implemented in
148 // X11, Motif and GTK) AddInput() and RemoveInput() functions
149 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
152 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
154 wxSocketImplUnix
* const
155 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
157 wxCHECK_RET( socket
->m_fd
!= -1,
158 "shouldn't be called on invalid socket" );
160 const SocketDir d
= GetDirForEvent(socket
, event
);
162 int& fd
= FD(socket
, d
);
166 fd
= AddInput(socket
, socket
->m_fd
, d
);
169 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
171 wxSocketImplUnix
* const
172 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
174 const SocketDir d
= GetDirForEvent(socket
, event
);
176 int& fd
= FD(socket
, d
);
185 // these functions map directly to XtAdd/RemoveInput() or
186 // gdk_input_add/remove()
187 virtual int AddInput(wxFDIOHandler
*handler
, int fd
, SocketDir d
) = 0;
188 virtual void RemoveInput(int fd
) = 0;
191 #endif /* _WX_UNIX_GSOCKUNX_H_ */