]>
Commit | Line | Data |
---|---|---|
30c45bdd | 1 | /////////////////////////////////////////////////////////////////////////////// |
86c5b12b | 2 | // Name: src/common/socketiohandler.cpp |
51fe4b60 | 3 | // Purpose: implementation of wxFDIOHandler for wxSocket |
30c45bdd | 4 | // Author: Angel Vidal, Lukasz Michalski |
30c45bdd VZ |
5 | // Created: 08.24.06 |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2006 Angel vidal | |
2804f77d | 8 | // (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org> |
30c45bdd VZ |
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 | ||
a1873279 | 23 | #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER |
30c45bdd | 24 | |
2804f77d | 25 | #include "wx/apptrait.h" |
a9d859df | 26 | #include "wx/private/socket.h" |
54e757fc | 27 | #include "wx/link.h" |
30c45bdd VZ |
28 | |
29 | // ============================================================================ | |
30 | // implementation | |
31 | // ============================================================================ | |
32 | ||
51fe4b60 | 33 | // ---------------------------------------------------------------------------- |
39b61b05 | 34 | // wxSocketFDIOManager: socket manager using wxFDIODispatcher |
51fe4b60 VZ |
35 | // ---------------------------------------------------------------------------- |
36 | ||
39b61b05 | 37 | class wxSocketFDIOManager : public wxSocketFDBasedManager |
51fe4b60 VZ |
38 | { |
39 | public: | |
51fe4b60 VZ |
40 | virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event); |
41 | virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event); | |
2804f77d | 42 | }; |
30c45bdd | 43 | |
39b61b05 | 44 | void wxSocketFDIOManager::Install_Callback(wxSocketImpl *socket_, |
51fe4b60 | 45 | wxSocketNotify event) |
30c45bdd | 46 | { |
acd523a9 | 47 | wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_); |
51fe4b60 | 48 | |
2804f77d | 49 | const int fd = socket->m_fd; |
30c45bdd | 50 | |
2804f77d VZ |
51 | if ( fd == -1 ) |
52 | return; | |
30c45bdd | 53 | |
2804f77d | 54 | const SocketDir d = GetDirForEvent(socket, event); |
30c45bdd | 55 | |
2804f77d VZ |
56 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
57 | if ( !dispatcher ) | |
58 | return; | |
30c45bdd | 59 | |
a9d859df | 60 | FD(socket, d) = fd; |
30c45bdd | 61 | |
a9d859df VZ |
62 | // register it when it's used for the first time, update it if it had been |
63 | // previously registered | |
acd523a9 | 64 | const bool alreadyRegistered = socket->HasAnyEnabledCallbacks(); |
30c45bdd | 65 | |
acd523a9 | 66 | socket->EnableCallback(d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT); |
30c45bdd | 67 | |
acd523a9 VZ |
68 | if ( alreadyRegistered ) |
69 | dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks()); | |
2804f77d | 70 | else |
acd523a9 | 71 | dispatcher->RegisterFD(fd, socket, socket->GetEnabledCallbacks()); |
30c45bdd VZ |
72 | } |
73 | ||
39b61b05 | 74 | void wxSocketFDIOManager::Uninstall_Callback(wxSocketImpl *socket_, |
51fe4b60 | 75 | wxSocketNotify event) |
30c45bdd | 76 | { |
acd523a9 | 77 | wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_); |
51fe4b60 | 78 | |
2804f77d | 79 | const SocketDir d = GetDirForEvent(socket, event); |
30c45bdd | 80 | |
2804f77d VZ |
81 | const int fd = FD(socket, d); |
82 | if ( fd == -1 ) | |
83 | return; | |
30c45bdd | 84 | |
2804f77d | 85 | FD(socket, d) = -1; |
30c45bdd | 86 | |
2804f77d VZ |
87 | const wxFDIODispatcherEntryFlags |
88 | flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT; | |
30c45bdd | 89 | |
2804f77d VZ |
90 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
91 | if ( !dispatcher ) | |
92 | return; | |
30c45bdd | 93 | |
acd523a9 | 94 | socket->DisableCallback(flag); |
a9d859df | 95 | |
acd523a9 | 96 | if ( !socket->HasAnyEnabledCallbacks() ) |
a9d859df | 97 | dispatcher->UnregisterFD(fd); |
2804f77d | 98 | else |
acd523a9 | 99 | dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks()); |
30c45bdd VZ |
100 | } |
101 | ||
86c5b12b VZ |
102 | // set the wxBase variable to point to our wxSocketManager implementation |
103 | // | |
104 | // see comments in wx/apptrait.h for the explanation of why do we do it | |
105 | // like this | |
106 | static struct ManagerSetter | |
30c45bdd | 107 | { |
86c5b12b VZ |
108 | ManagerSetter() |
109 | { | |
39b61b05 | 110 | static wxSocketFDIOManager s_manager; |
86c5b12b VZ |
111 | wxAppTraits::SetDefaultSocketManager(&s_manager); |
112 | } | |
113 | } gs_managerSetter; | |
30c45bdd | 114 | |
54e757fc FM |
115 | |
116 | // see the relative linker macro in socket.cpp | |
117 | wxFORCE_LINK_THIS_MODULE( socketiohandler ); | |
118 | ||
30c45bdd | 119 | #endif // wxUSE_SOCKETS |