]> git.saurik.com Git - wxWidgets.git/blame - src/common/socketiohandler.cpp
Added clipboard sample
[wxWidgets.git] / src / common / socketiohandler.cpp
CommitLineData
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"
30c45bdd
VZ
27
28// ============================================================================
29// implementation
30// ============================================================================
31
51fe4b60
VZ
32// ----------------------------------------------------------------------------
33// wxSocketSelectManager
34// ----------------------------------------------------------------------------
35
36class wxSocketSelectManager : public wxSocketFDBasedManager
37{
38public:
51fe4b60
VZ
39 virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
40 virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
2804f77d 41};
30c45bdd 42
51fe4b60
VZ
43void wxSocketSelectManager::Install_Callback(wxSocketImpl *socket_,
44 wxSocketNotify event)
30c45bdd 45{
acd523a9 46 wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_);
51fe4b60 47
2804f77d 48 const int fd = socket->m_fd;
30c45bdd 49
2804f77d
VZ
50 if ( fd == -1 )
51 return;
30c45bdd 52
2804f77d 53 const SocketDir d = GetDirForEvent(socket, event);
30c45bdd 54
2804f77d
VZ
55 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
56 if ( !dispatcher )
57 return;
30c45bdd 58
a9d859df 59 FD(socket, d) = fd;
30c45bdd 60
a9d859df
VZ
61 // register it when it's used for the first time, update it if it had been
62 // previously registered
acd523a9 63 const bool alreadyRegistered = socket->HasAnyEnabledCallbacks();
30c45bdd 64
acd523a9 65 socket->EnableCallback(d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT);
30c45bdd 66
acd523a9
VZ
67 if ( alreadyRegistered )
68 dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks());
2804f77d 69 else
acd523a9 70 dispatcher->RegisterFD(fd, socket, socket->GetEnabledCallbacks());
30c45bdd
VZ
71}
72
51fe4b60
VZ
73void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl *socket_,
74 wxSocketNotify event)
30c45bdd 75{
acd523a9 76 wxSocketImplUnix * const socket = static_cast<wxSocketImplUnix *>(socket_);
51fe4b60 77
2804f77d 78 const SocketDir d = GetDirForEvent(socket, event);
30c45bdd 79
2804f77d
VZ
80 const int fd = FD(socket, d);
81 if ( fd == -1 )
82 return;
30c45bdd 83
2804f77d 84 FD(socket, d) = -1;
30c45bdd 85
2804f77d
VZ
86 const wxFDIODispatcherEntryFlags
87 flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
30c45bdd 88
2804f77d
VZ
89 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
90 if ( !dispatcher )
91 return;
30c45bdd 92
acd523a9 93 socket->DisableCallback(flag);
a9d859df 94
acd523a9 95 if ( !socket->HasAnyEnabledCallbacks() )
a9d859df 96 dispatcher->UnregisterFD(fd);
2804f77d 97 else
acd523a9 98 dispatcher->ModifyFD(fd, socket, socket->GetEnabledCallbacks());
30c45bdd
VZ
99}
100
86c5b12b
VZ
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
105static struct ManagerSetter
30c45bdd 106{
86c5b12b
VZ
107 ManagerSetter()
108 {
109 static wxSocketSelectManager s_manager;
110 wxAppTraits::SetDefaultSocketManager(&s_manager);
111 }
112} gs_managerSetter;
30c45bdd
VZ
113
114#endif // wxUSE_SOCKETS