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>
18 class wxSocketImplUnix
: public wxSocketImpl
21 wxSocketImplUnix(wxSocketBase
& wxsocket
)
22 : wxSocketImpl(wxsocket
)
30 virtual void Shutdown();
31 virtual wxSocketImpl
*WaitConnection(wxSocketBase
& wxsocket
);
33 int Read(char *buffer
, int size
);
34 int Write(const char *buffer
, int size
);
35 //attach or detach from main loop
36 void Notify(bool flag
);
38 void Detected_Write();
41 virtual wxSocketError
DoHandleConnect(int ret
);
42 virtual void DoClose()
44 wxSocketManager
* const manager
= wxSocketManager::Get();
47 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
48 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
54 virtual void UnblockAndRegisterWithEventLoop()
57 ioctl(m_fd
, FIONBIO
, &trueArg
);
62 // enable or disable notifications for socket input/output events but only
63 // if m_use_events is true; do nothing otherwise
64 virtual void EnableEvents()
73 DoEnableEvents(false);
76 // really enable or disable socket input/output events, regardless of
78 void DoEnableEvents(bool enable
);
81 // enable or disable events for the given event if m_use_events; do nothing
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
);
90 wxSocketError
Input_Timeout();
91 wxSocketError
Output_Timeout();
92 int Recv_Stream(char *buffer
, int size
);
93 int Recv_Dgram(char *buffer
, int size
);
94 int Send_Stream(const char *buffer
, int size
);
95 int Send_Dgram(const char *buffer
, int size
);
98 // true if socket should fire events
101 // descriptors for input and output event notification channels associated
106 // notify the associated wxSocket about a change in socket state and shut
107 // down the socket if the event is wxSOCKET_LOST
108 void OnStateChange(wxSocketNotify event
);
110 // give it access to our m_fds
111 friend class wxSocketFDBasedManager
;
114 // A version of wxSocketManager which uses FDs for socket IO
115 class wxSocketFDBasedManager
: public wxSocketManager
118 // no special initialization/cleanup needed when using FDs
119 virtual bool OnInit() { return true; }
120 virtual void OnExit() { }
122 // allocate/free the storage we need
123 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
125 return new wxSocketImplUnix(wxsocket
);
129 // identifies either input or output direction
131 // NB: the values of this enum shouldn't change
138 // get the FD index corresponding to the given wxSocketNotify
139 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
144 wxFAIL_MSG( "unexpected socket event" );
153 case wxSOCKET_OUTPUT
:
156 case wxSOCKET_CONNECTION
:
157 // FIXME: explain this?
158 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
162 // access the FDs we store
163 int& FD(wxSocketImpl
*socket
, SocketDir d
)
165 return static_cast<wxSocketImplUnix
*>(socket
)->m_fds
[d
];
169 // Common base class for all ports using X11-like (and hence implemented in
170 // X11, Motif and GTK) AddInput() and RemoveInput() functions
171 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
174 virtual void Install_Callback(wxSocketImpl
*socket
, wxSocketNotify event
)
176 wxCHECK_RET( socket
->m_fd
!= -1,
177 "shouldn't be called on invalid socket" );
179 const SocketDir d
= GetDirForEvent(socket
, event
);
181 int& fd
= FD(socket
, d
);
185 fd
= AddInput(socket
, d
);
188 virtual void Uninstall_Callback(wxSocketImpl
*socket
, wxSocketNotify event
)
190 const SocketDir d
= GetDirForEvent(socket
, event
);
192 int& fd
= FD(socket
, d
);
201 // these functions map directly to XtAdd/RemoveInput() or
202 // gdk_input_add/remove()
203 virtual int AddInput(wxSocketImpl
*socket
, SocketDir d
) = 0;
204 virtual void RemoveInput(int fd
) = 0;
207 #endif /* _WX_UNIX_GSOCKUNX_H_ */