1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/socketiohandler.cpp
3 // Purpose: implementation of wxFDIOHandler for wxSocket
4 // Author: Angel Vidal, Lukasz Michalski
7 // Copyright: (c) 2006 Angel vidal
8 // (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER
25 #include "wx/apptrait.h"
26 #include "wx/private/socket.h"
28 // ============================================================================
30 // ============================================================================
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 class wxSocketImplFDIO
: public wxSocketImplUnix
39 wxSocketImplFDIO(wxSocketBase
& wxsocket
)
40 : wxSocketImplUnix(wxsocket
)
44 int GetFlags() const { return m_flags
; }
45 void RemoveFlag(wxFDIODispatcherEntryFlags flag
) { m_flags
&= ~flag
; }
46 void AddFlag(wxFDIODispatcherEntryFlags flag
) { m_flags
|= flag
; }
52 // ----------------------------------------------------------------------------
53 // wxSocketSelectManager
54 // ----------------------------------------------------------------------------
56 class wxSocketSelectManager
: public wxSocketFDBasedManager
59 virtual wxSocketImpl
*CreateSocket(wxSocketBase
& wxsocket
)
61 return new wxSocketImplFDIO(wxsocket
);
64 virtual void Install_Callback(wxSocketImpl
*socket
, wxSocketNotify event
);
65 virtual void Uninstall_Callback(wxSocketImpl
*socket
, wxSocketNotify event
);
68 void wxSocketSelectManager::Install_Callback(wxSocketImpl
*socket_
,
71 wxSocketImplFDIO
* const socket
= static_cast<wxSocketImplFDIO
*>(socket_
);
73 const int fd
= socket
->m_fd
;
78 const SocketDir d
= GetDirForEvent(socket
, event
);
80 wxFDIODispatcher
* const dispatcher
= wxFDIODispatcher::Get();
86 // register it when it's used for the first time, update it if it had been
87 // previously registered
88 const bool registerHandler
= socket
->GetFlags() == 0;
90 socket
->AddFlag(d
== FD_INPUT
? wxFDIO_INPUT
: wxFDIO_OUTPUT
);
92 if ( registerHandler
)
93 dispatcher
->RegisterFD(fd
, socket
, socket
->GetFlags());
95 dispatcher
->ModifyFD(fd
, socket
, socket
->GetFlags());
98 void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl
*socket_
,
101 wxSocketImplFDIO
* const socket
= static_cast<wxSocketImplFDIO
*>(socket_
);
103 const SocketDir d
= GetDirForEvent(socket
, event
);
105 const int fd
= FD(socket
, d
);
111 const wxFDIODispatcherEntryFlags
112 flag
= d
== FD_INPUT
? wxFDIO_INPUT
: wxFDIO_OUTPUT
;
114 wxFDIODispatcher
* const dispatcher
= wxFDIODispatcher::Get();
118 socket
->RemoveFlag(flag
);
120 if ( !socket
->GetFlags() )
122 dispatcher
->UnregisterFD(fd
);
126 dispatcher
->ModifyFD(fd
, socket
, socket
->GetFlags());
130 // set the wxBase variable to point to our wxSocketManager implementation
132 // see comments in wx/apptrait.h for the explanation of why do we do it
134 static struct ManagerSetter
138 static wxSocketSelectManager s_manager
;
139 wxAppTraits::SetDefaultSocketManager(&s_manager
);
143 #endif // wxUSE_SOCKETS