]> git.saurik.com Git - wxWidgets.git/blob - src/common/gsocketiohandler.cpp
move more socket functions common to Winsock and BSD implementations to common code
[wxWidgets.git] / src / common / gsocketiohandler.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/gsocketiohandler.cpp
3 // Purpose: implementation of wxFDIOHandler for GSocket
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/unix/private.h"
27 #include "wx/private/gsocketiohandler.h"
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 // ----------------------------------------------------------------------------
34 // GSocketSelectManager
35 // ----------------------------------------------------------------------------
36
37 class GSocketSelectManager : public GSocketFDBasedManager
38 {
39 public:
40 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
41 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
42 };
43
44 void GSocketSelectManager::Install_Callback(GSocket *socket,
45 GSocketEvent event)
46 {
47 const int fd = socket->m_fd;
48
49 if ( fd == -1 )
50 return;
51
52 const SocketDir d = GetDirForEvent(socket, event);
53
54 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
55 if ( !dispatcher )
56 return;
57
58 wxGSocketIOHandler *& handler = socket->m_handler;
59
60 // we should register the new handlers but modify the existing ones in place
61 bool registerHandler;
62 if ( handler )
63 {
64 registerHandler = false;
65 }
66 else // no existing handler
67 {
68 registerHandler = true;
69 handler = new wxGSocketIOHandler(socket);
70 }
71
72 FD(socket, d) = fd;
73 if (d == FD_INPUT)
74 {
75 handler->AddFlag(wxFDIO_INPUT);
76 }
77 else
78 {
79 handler->AddFlag(wxFDIO_OUTPUT);
80 }
81
82 if ( registerHandler )
83 dispatcher->RegisterFD(fd, handler, handler->GetFlags());
84 else
85 dispatcher->ModifyFD(fd, handler, handler->GetFlags());
86 }
87
88 void GSocketSelectManager::Uninstall_Callback(GSocket *socket,
89 GSocketEvent event)
90 {
91 const SocketDir d = GetDirForEvent(socket, event);
92
93 const int fd = FD(socket, d);
94 if ( fd == -1 )
95 return;
96
97 FD(socket, d) = -1;
98
99 const wxFDIODispatcherEntryFlags
100 flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
101
102 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
103 if ( !dispatcher )
104 return;
105
106 wxGSocketIOHandler *& handler = socket->m_handler;
107 if ( handler )
108 {
109 handler->RemoveFlag(flag);
110
111 if ( !handler->GetFlags() )
112 {
113 dispatcher->UnregisterFD(fd);
114 delete handler;
115 socket->m_handler = NULL;
116 }
117 else
118 {
119 dispatcher->ModifyFD(fd, handler, handler->GetFlags());
120 }
121 }
122 else
123 {
124 dispatcher->UnregisterFD(fd);
125 }
126 }
127
128 GSocketManager *wxAppTraits::GetSocketManager()
129 {
130 static GSocketSelectManager s_manager;
131
132 return &s_manager;
133 }
134
135 #endif // wxUSE_SOCKETS