]> git.saurik.com Git - wxWidgets.git/blob - src/common/socketiohandler.cpp
same as r57054 in 2.8
[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 // wxSocketSelectManager
34 // ----------------------------------------------------------------------------
35
36 class wxSocketSelectManager : public wxSocketFDBasedManager
37 {
38 public:
39 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
40 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
41 };
42
43 void wxSocketSelectManager::Install_Callback(wxSocketImpl *socket_,
44 wxSocketNotify event)
45 {
46 wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_);
47
48 const int fd = socket->m_fd;
49
50 if ( fd == -1 )
51 return;
52
53 const SocketDir d = GetDirForEvent(socket, event);
54
55 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
56 if ( !dispatcher )
57 return;
58
59 FD(socket, d) = fd;
60
61 // register it when it's used for the first time, update it if it had been
62 // previously registered
63 const bool alreadyRegistered = socket->HasAnyEnabledCallbacks();
64
65 socket->EnableCallback(d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT);
66
67 if ( alreadyRegistered )
68 dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks());
69 else
70 dispatcher->RegisterFD(fd, socket, socket->GetEnabledCallbacks());
71 }
72
73 void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl *socket_,
74 wxSocketNotify event)
75 {
76 wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_);
77
78 const SocketDir d = GetDirForEvent(socket, event);
79
80 const int fd = FD(socket, d);
81 if ( fd == -1 )
82 return;
83
84 FD(socket, d) = -1;
85
86 const wxFDIODispatcherEntryFlags
87 flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
88
89 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
90 if ( !dispatcher )
91 return;
92
93 socket->DisableCallback(flag);
94
95 if ( !socket->HasAnyEnabledCallbacks() )
96 dispatcher->UnregisterFD(fd);
97 else
98 dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks());
99 }
100
101 // set the wxBase variable to point to our wxSocketManager implementation
102 //
103 // see comments in wx/apptrait.h for the explanation of why do we do it
104 // like this
105 static struct ManagerSetter
106 {
107 ManagerSetter()
108 {
109 static wxSocketSelectManager s_manager;
110 wxAppTraits::SetDefaultSocketManager(&s_manager);
111 }
112 } gs_managerSetter;
113
114 #endif // wxUSE_SOCKETS