make wxSocketImplUnix inherit from wxFDIOHandler as they're used for almost the same...
[wxWidgets.git] / src / common / socketiohandler.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/socketiohandler.cpp
3 // Purpose: implementation of wxFDIOHandler for wxSocket
4 // Author: Angel Vidal, Lukasz Michalski
5 // Created: 08.24.06
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Angel vidal
8 // (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER
24
25 #include "wx/apptrait.h"
26 #include "wx/private/socket.h"
27
28 // ============================================================================
29 // implementation
30 // ============================================================================
31
32 // ----------------------------------------------------------------------------
33 // wxSocketImplFDIO
34 // ----------------------------------------------------------------------------
35
36 class wxSocketImplFDIO : public wxSocketImplUnix
37 {
38 public:
39 wxSocketImplFDIO(wxSocketBase& wxsocket)
40 : wxSocketImplUnix(wxsocket)
41 {
42 }
43
44 int GetFlags() const { return m_flags; }
45 void RemoveFlag(wxFDIODispatcherEntryFlags flag) { m_flags &= ~flag; }
46 void AddFlag(wxFDIODispatcherEntryFlags flag) { m_flags |= flag; }
47
48 private:
49 int m_flags;
50 };
51
52 // ----------------------------------------------------------------------------
53 // wxSocketSelectManager
54 // ----------------------------------------------------------------------------
55
56 class wxSocketSelectManager : public wxSocketFDBasedManager
57 {
58 public:
59 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
60 {
61 return new wxSocketImplFDIO(wxsocket);
62 }
63
64 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
65 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
66 };
67
68 void wxSocketSelectManager::Install_Callback(wxSocketImpl *socket_,
69 wxSocketNotify event)
70 {
71 wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_);
72
73 const int fd = socket->m_fd;
74
75 if ( fd == -1 )
76 return;
77
78 const SocketDir d = GetDirForEvent(socket, event);
79
80 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
81 if ( !dispatcher )
82 return;
83
84 FD(socket, d) = fd;
85
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;
89
90 socket->AddFlag(d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT);
91
92 if ( registerHandler )
93 dispatcher->RegisterFD(fd, socket, socket->GetFlags());
94 else
95 dispatcher->ModifyFD(fd, socket, socket->GetFlags());
96 }
97
98 void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl *socket_,
99 wxSocketNotify event)
100 {
101 wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_);
102
103 const SocketDir d = GetDirForEvent(socket, event);
104
105 const int fd = FD(socket, d);
106 if ( fd == -1 )
107 return;
108
109 FD(socket, d) = -1;
110
111 const wxFDIODispatcherEntryFlags
112 flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
113
114 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
115 if ( !dispatcher )
116 return;
117
118 socket->RemoveFlag(flag);
119
120 if ( !socket->GetFlags() )
121 {
122 dispatcher->UnregisterFD(fd);
123 }
124 else
125 {
126 dispatcher->ModifyFD(fd, socket, socket->GetFlags());
127 }
128 }
129
130 // set the wxBase variable to point to our wxSocketManager implementation
131 //
132 // see comments in wx/apptrait.h for the explanation of why do we do it
133 // like this
134 static struct ManagerSetter
135 {
136 ManagerSetter()
137 {
138 static wxSocketSelectManager s_manager;
139 wxAppTraits::SetDefaultSocketManager(&s_manager);
140 }
141 } gs_managerSetter;
142
143 #endif // wxUSE_SOCKETS