]>
Commit | Line | Data |
---|---|---|
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 | // Copyright: (c) 2006 Angel vidal | |
7 | // (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_SOCKETS | |
27 | ||
28 | #include "wx/app.h" | |
29 | #include "wx/apptrait.h" | |
30 | #include "wx/private/socket.h" | |
31 | #include "wx/link.h" | |
32 | ||
33 | // ============================================================================ | |
34 | // wxSocketFDBasedManager implementation | |
35 | // ============================================================================ | |
36 | ||
37 | bool wxSocketFDBasedManager::OnInit() | |
38 | { | |
39 | wxAppTraits * const traits = wxApp::GetTraitsIfExists(); | |
40 | if ( !traits ) | |
41 | return false; | |
42 | ||
43 | m_fdioManager = traits->GetFDIOManager(); | |
44 | return m_fdioManager != NULL; | |
45 | } | |
46 | ||
47 | void wxSocketFDBasedManager::Install_Callback(wxSocketImpl *socket_, | |
48 | wxSocketNotify event) | |
49 | { | |
50 | wxSocketImplUnix * const | |
51 | socket = static_cast<wxSocketImplUnix *>(socket_); | |
52 | ||
53 | wxCHECK_RET( socket->m_fd != -1, | |
54 | "shouldn't be called on invalid socket" ); | |
55 | ||
56 | const wxFDIOManager::Direction d = GetDirForEvent(socket, event); | |
57 | ||
58 | int& fd = FD(socket, d); | |
59 | if ( fd != -1 ) | |
60 | m_fdioManager->RemoveInput(socket, fd, d); | |
61 | ||
62 | fd = m_fdioManager->AddInput(socket, socket->m_fd, d); | |
63 | } | |
64 | ||
65 | void wxSocketFDBasedManager::Uninstall_Callback(wxSocketImpl *socket_, | |
66 | wxSocketNotify event) | |
67 | { | |
68 | wxSocketImplUnix * const | |
69 | socket = static_cast<wxSocketImplUnix *>(socket_); | |
70 | ||
71 | const wxFDIOManager::Direction d = GetDirForEvent(socket, event); | |
72 | ||
73 | int& fd = FD(socket, d); | |
74 | if ( fd != -1 ) | |
75 | { | |
76 | m_fdioManager->RemoveInput(socket, fd, d); | |
77 | fd = -1; | |
78 | } | |
79 | } | |
80 | ||
81 | wxFDIOManager::Direction | |
82 | wxSocketFDBasedManager::GetDirForEvent(wxSocketImpl *socket, | |
83 | wxSocketNotify event) | |
84 | { | |
85 | switch ( event ) | |
86 | { | |
87 | default: | |
88 | wxFAIL_MSG( "unknown socket event" ); | |
89 | return wxFDIOManager::INPUT; // we must return something | |
90 | ||
91 | case wxSOCKET_LOST: | |
92 | wxFAIL_MSG( "unexpected socket event" ); | |
93 | return wxFDIOManager::INPUT; // as above | |
94 | ||
95 | case wxSOCKET_INPUT: | |
96 | return wxFDIOManager::INPUT; | |
97 | ||
98 | case wxSOCKET_OUTPUT: | |
99 | return wxFDIOManager::OUTPUT; | |
100 | ||
101 | case wxSOCKET_CONNECTION: | |
102 | // for server sockets we're interested in events indicating | |
103 | // that a new connection is pending, i.e. that accept() will | |
104 | // succeed and this is indicated by socket becoming ready for | |
105 | // reading, while for the other ones we're interested in the | |
106 | // completion of non-blocking connect() which is indicated by | |
107 | // the socket becoming ready for writing | |
108 | return socket->IsServer() ? wxFDIOManager::INPUT | |
109 | : wxFDIOManager::OUTPUT; | |
110 | } | |
111 | } | |
112 | ||
113 | // set the wxBase variable to point to our wxSocketManager implementation | |
114 | // | |
115 | // see comments in wx/apptrait.h for the explanation of why do we do it | |
116 | // like this | |
117 | static struct ManagerSetter | |
118 | { | |
119 | ManagerSetter() | |
120 | { | |
121 | static wxSocketFDBasedManager s_manager; | |
122 | wxAppTraits::SetDefaultSocketManager(&s_manager); | |
123 | } | |
124 | } gs_managerSetter; | |
125 | ||
126 | ||
127 | // see the relative linker macro in socket.cpp | |
128 | wxFORCE_LINK_THIS_MODULE( socketiohandler ); | |
129 | ||
130 | #endif // wxUSE_SOCKETS |