1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/gsockunx.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 wxSocketIOHandler
;
20 class wxSocketImplUnix
: public wxSocketImpl
23 wxSocketImplUnix(wxSocketBase
& wxsocket
);
25 virtual void Shutdown();
26 virtual wxSocketImpl
*WaitConnection(wxSocketBase
& wxsocket
);
28 int Read(char *buffer
, int size
);
29 int Write(const char *buffer
, int size
);
30 //attach or detach from main loop
31 void Notify(bool flag
);
33 void Detected_Write();
36 virtual wxSocketError
DoHandleConnect(int ret
);
37 virtual void DoClose()
39 wxSocketManager
* const manager
= wxSocketManager::Get();
42 manager
->Uninstall_Callback(this, wxSOCKET_INPUT
);
43 manager
->Uninstall_Callback(this, wxSOCKET_OUTPUT
);
49 virtual void UnblockAndRegisterWithEventLoop()
52 ioctl(m_fd
, FIONBIO
, &trueArg
);
57 // enable or disable notifications for socket input/output events but only
58 // if m_use_events is true; do nothing otherwise
59 virtual void EnableEvents()
68 DoEnableEvents(false);
71 // really enable or disable socket input/output events, regardless of
73 void DoEnableEvents(bool enable
);
76 // enable or disable events for the given event if m_use_events; do nothing
79 // notice that these functions also update m_detected: EnableEvent() clears
80 // the corresponding bit in it and DisableEvent() sets it
81 void EnableEvent(wxSocketNotify event
);
82 void DisableEvent(wxSocketNotify event
);
85 wxSocketError
Input_Timeout();
86 wxSocketError
Output_Timeout();
87 int Recv_Stream(char *buffer
, int size
);
88 int Recv_Dgram(char *buffer
, int size
);
89 int Send_Stream(const char *buffer
, int size
);
90 int Send_Dgram(const char *buffer
, int size
);
93 // true if socket should fire events
96 // descriptors for input and output event notification channels associated
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() { }
117 // allocate/free the storage we need
118 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
120 return new wxSocketImplUnix(wxsocket
);
124 // identifies either input or output direction
126 // NB: the values of this enum shouldn't change
133 // get the FD index corresponding to the given wxSocketNotify
134 SocketDir
GetDirForEvent(wxSocketImpl
*socket
, wxSocketNotify event
)
139 wxFAIL_MSG( "unexpected socket event" );
148 case wxSOCKET_OUTPUT
:
151 case wxSOCKET_CONNECTION
:
152 // FIXME: explain this?
153 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
157 // access the FDs we store
158 int& FD(wxSocketImpl
*socket
, SocketDir d
)
160 return static_cast<wxSocketImplUnix
*>(socket
)->m_fds
[d
];
164 // Common base class for all ports using X11-like (and hence implemented in
165 // X11, Motif and GTK) AddInput() and RemoveInput() functions
166 class wxSocketInputBasedManager
: public wxSocketFDBasedManager
169 virtual void Install_Callback(wxSocketImpl
*socket
, wxSocketNotify event
)
171 wxCHECK_RET( socket
->m_fd
!= -1,
172 "shouldn't be called on invalid socket" );
174 const SocketDir d
= GetDirForEvent(socket
, event
);
176 int& fd
= FD(socket
, d
);
180 fd
= AddInput(socket
, d
);
183 virtual void Uninstall_Callback(wxSocketImpl
*socket
, wxSocketNotify event
)
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(wxSocketImpl
*socket
, SocketDir d
) = 0;
199 virtual void RemoveInput(int fd
) = 0;
202 #endif /* _WX_UNIX_GSOCKUNX_H_ */