| 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 | #include "wx/link.h" |
| 28 | |
| 29 | // ============================================================================ |
| 30 | // implementation |
| 31 | // ============================================================================ |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | // wxSocketFDIOManager: socket manager using wxFDIODispatcher |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | class wxSocketFDIOManager : public wxSocketFDBasedManager |
| 38 | { |
| 39 | public: |
| 40 | virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event); |
| 41 | virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event); |
| 42 | }; |
| 43 | |
| 44 | void wxSocketFDIOManager::Install_Callback(wxSocketImpl *socket_, |
| 45 | wxSocketNotify event) |
| 46 | { |
| 47 | wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_); |
| 48 | |
| 49 | const int fd = socket->m_fd; |
| 50 | |
| 51 | if ( fd == -1 ) |
| 52 | return; |
| 53 | |
| 54 | const SocketDir d = GetDirForEvent(socket, event); |
| 55 | |
| 56 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
| 57 | if ( !dispatcher ) |
| 58 | return; |
| 59 | |
| 60 | FD(socket, d) = fd; |
| 61 | |
| 62 | // register it when it's used for the first time, update it if it had been |
| 63 | // previously registered |
| 64 | const bool alreadyRegistered = socket->HasAnyEnabledCallbacks(); |
| 65 | |
| 66 | socket->EnableCallback(d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT); |
| 67 | |
| 68 | if ( alreadyRegistered ) |
| 69 | dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks()); |
| 70 | else |
| 71 | dispatcher->RegisterFD(fd, socket, socket->GetEnabledCallbacks()); |
| 72 | } |
| 73 | |
| 74 | void wxSocketFDIOManager::Uninstall_Callback(wxSocketImpl *socket_, |
| 75 | wxSocketNotify event) |
| 76 | { |
| 77 | wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_); |
| 78 | |
| 79 | const SocketDir d = GetDirForEvent(socket, event); |
| 80 | |
| 81 | const int fd = FD(socket, d); |
| 82 | if ( fd == -1 ) |
| 83 | return; |
| 84 | |
| 85 | FD(socket, d) = -1; |
| 86 | |
| 87 | const wxFDIODispatcherEntryFlags |
| 88 | flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT; |
| 89 | |
| 90 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
| 91 | if ( !dispatcher ) |
| 92 | return; |
| 93 | |
| 94 | socket->DisableCallback(flag); |
| 95 | |
| 96 | if ( !socket->HasAnyEnabledCallbacks() ) |
| 97 | dispatcher->UnregisterFD(fd); |
| 98 | else |
| 99 | dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks()); |
| 100 | } |
| 101 | |
| 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 |
| 107 | { |
| 108 | ManagerSetter() |
| 109 | { |
| 110 | static wxSocketFDIOManager s_manager; |
| 111 | wxAppTraits::SetDefaultSocketManager(&s_manager); |
| 112 | } |
| 113 | } gs_managerSetter; |
| 114 | |
| 115 | |
| 116 | // see the relative linker macro in socket.cpp |
| 117 | wxFORCE_LINK_THIS_MODULE( socketiohandler ); |
| 118 | |
| 119 | #endif // wxUSE_SOCKETS |