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