]> git.saurik.com Git - wxWidgets.git/blame - src/common/socketiohandler.cpp
fixing osx_cocoa
[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>
526954c5 9// Licence: wxWindows licence
30c45bdd
VZ
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
b4715d08
VZ
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
6bcc1145 27#if wxUSE_SOCKETS
30c45bdd 28
6bcc1145 29#include "wx/app.h"
2804f77d 30#include "wx/apptrait.h"
a9d859df 31#include "wx/private/socket.h"
54e757fc 32#include "wx/link.h"
30c45bdd
VZ
33
34// ============================================================================
6bcc1145 35// wxSocketFDBasedManager implementation
30c45bdd
VZ
36// ============================================================================
37
6bcc1145 38bool wxSocketFDBasedManager::OnInit()
30c45bdd 39{
6bcc1145
VZ
40 wxAppTraits * const traits = wxApp::GetTraitsIfExists();
41 if ( !traits )
42 return false;
51fe4b60 43
6bcc1145
VZ
44 m_fdioManager = traits->GetFDIOManager();
45 return m_fdioManager != NULL;
46}
30c45bdd 47
6bcc1145
VZ
48void wxSocketFDBasedManager::Install_Callback(wxSocketImpl *socket_,
49 wxSocketNotify event)
50{
51 wxSocketImplUnix * const
52 socket = static_cast<wxSocketImplUnix *>(socket_);
30c45bdd 53
6bcc1145
VZ
54 wxCHECK_RET( socket->m_fd != -1,
55 "shouldn't be called on invalid socket" );
30c45bdd 56
6bcc1145 57 const wxFDIOManager::Direction d = GetDirForEvent(socket, event);
30c45bdd 58
6bcc1145
VZ
59 int& fd = FD(socket, d);
60 if ( fd != -1 )
61 m_fdioManager->RemoveInput(socket, fd, d);
30c45bdd 62
6bcc1145 63 fd = m_fdioManager->AddInput(socket, socket->m_fd, d);
30c45bdd
VZ
64}
65
6bcc1145
VZ
66void wxSocketFDBasedManager::Uninstall_Callback(wxSocketImpl *socket_,
67 wxSocketNotify event)
30c45bdd 68{
6bcc1145
VZ
69 wxSocketImplUnix * const
70 socket = static_cast<wxSocketImplUnix *>(socket_);
30c45bdd 71
6bcc1145 72 const wxFDIOManager::Direction d = GetDirForEvent(socket, event);
30c45bdd 73
6bcc1145
VZ
74 int& fd = FD(socket, d);
75 if ( fd != -1 )
76 {
77 m_fdioManager->RemoveInput(socket, fd, d);
78 fd = -1;
79 }
80}
a9d859df 81
6bcc1145
VZ
82wxFDIOManager::Direction
83wxSocketFDBasedManager::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 }
30c45bdd
VZ
112}
113
86c5b12b
VZ
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
118static struct ManagerSetter
30c45bdd 119{
86c5b12b
VZ
120 ManagerSetter()
121 {
6bcc1145 122 static wxSocketFDBasedManager s_manager;
86c5b12b
VZ
123 wxAppTraits::SetDefaultSocketManager(&s_manager);
124 }
125} gs_managerSetter;
30c45bdd 126
54e757fc
FM
127
128// see the relative linker macro in socket.cpp
129wxFORCE_LINK_THIS_MODULE( socketiohandler );
130
30c45bdd 131#endif // wxUSE_SOCKETS