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
)
32 virtual void Shutdown();
33 virtual wxSocketImpl
*WaitConnection(wxSocketBase
& wxsocket
);
35 int Read(char *buffer
, int size
);
36 int Write(const char *buffer
, int size
);
37 //attach or detach from main loop
38 void Notify(bool flag
);
40 // wxFDIOHandler methods
41 virtual void OnReadWaiting();
42 virtual void OnWriteWaiting();
43 virtual void OnExceptionWaiting();
46 virtual wxSocketError
DoHandleConnect(int ret
);
47 virtual void DoClose()
49 wxSocketManager
* const manager
= wxSocketManager::Get();
52 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
53 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
59 virtual void UnblockAndRegisterWithEventLoop()
62 ioctl(m_fd
, FIONBIO
, &trueArg
);
67 // enable or disable notifications for socket input/output events but only
68 // if m_use_events is true; do nothing otherwise
69 virtual void EnableEvents()
78 DoEnableEvents(false);
81 // really enable or disable socket input/output events, regardless of
83 void DoEnableEvents(bool enable
);
86 // enable or disable events for the given event if m_use_events; do nothing
89 // notice that these functions also update m_detected: EnableEvent() clears
90 // the corresponding bit in it and DisableEvent() sets it
91 void EnableEvent(wxSocketNotify event
);
92 void DisableEvent(wxSocketNotify event
);
95 wxSocketError
Input_Timeout();
96 wxSocketError
Output_Timeout();
97 int Recv_Stream(char *buffer
, int size
);
98 int Recv_Dgram(char *buffer
, int size
);
99 int Send_Stream(const char *buffer
, int size
);
100 int Send_Dgram(const char *buffer
, int size
);
103 // true if socket should fire events
106 // descriptors for input and output event notification channels associated
111 // notify the associated wxSocket about a change in socket state and shut
112 // down the socket if the event is wxSOCKET_LOST
113 void OnStateChange(wxSocketNotify event
);
115 // give it access to our m_fds
116 friend class wxSocketFDBasedManager
;
119 // A version of wxSocketManager which uses FDs for socket IO
120 class wxSocketFDBasedManager
: public wxSocketManager
123 // no special initialization/cleanup needed when using FDs
124 virtual bool OnInit() { return true; }
125 virtual void OnExit() { }
127 // allocate/free the storage we need
128 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
130 return new wxSocketImplUnix(wxsocket
);
134 // identifies either input or output direction
136 // NB: the values of this enum shouldn't change
143 // get the FD index corresponding to the given wxSocketNotify
144 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
149 wxFAIL_MSG( "unexpected socket event" );
158 case wxSOCKET_OUTPUT
:
161 case wxSOCKET_CONNECTION
:
162 // FIXME: explain this?
163 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
167 // access the FDs we store
168 int& FD(wxSocketImplUnix
*socket
, SocketDir d
)
170 return socket
->m_fds
[d
];
174 // Common base class for all ports using X11-like (and hence implemented in
175 // X11, Motif and GTK) AddInput() and RemoveInput() functions
176 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
179 virtual void Install_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
181 wxSocketImplUnix
* const
182 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
184 wxCHECK_RET( socket
->m_fd
!= -1,
185 "shouldn't be called on invalid socket" );
187 const SocketDir d
= GetDirForEvent(socket
, event
);
189 int& fd
= FD(socket
, d
);
193 fd
= AddInput(socket
, socket
->m_fd
, d
);
196 virtual void Uninstall_Callback(wxSocketImpl
*socket_
, wxSocketNotify event
)
198 wxSocketImplUnix
* const
199 socket
= static_cast<wxSocketImplUnix
*>(socket_
);
201 const SocketDir d
= GetDirForEvent(socket
, event
);
203 int& fd
= FD(socket
, d
);
212 // these functions map directly to XtAdd/RemoveInput() or
213 // gdk_input_add/remove()
214 virtual int AddInput(wxFDIOHandler
*handler
, int fd
, SocketDir d
) = 0;
215 virtual void RemoveInput(int fd
) = 0;
218 #endif /* _WX_UNIX_GSOCKUNX_H_ */