1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/gsocketiohandler.cpp
3 // Purpose: implementation of wxFDIOHandler for GSocket
4 // Author: Angel Vidal, Lukasz Michalski
7 // Copyright: (c) 2006 Angel vidal
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER
24 #include "wx/private/gsocketiohandler.h"
25 #include "wx/unix/private.h"
26 #include "wx/gsocket.h"
27 #include "wx/unix/gsockunx.h"
29 // ============================================================================
31 // ============================================================================
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 wxGSocketIOHandler::wxGSocketIOHandler(GSocket
* socket
)
44 void wxGSocketIOHandler::OnReadWaiting()
46 m_socket
->Detected_Read();
49 void wxGSocketIOHandler::OnWriteWaiting()
51 m_socket
->Detected_Write();
54 void wxGSocketIOHandler::OnExceptionWaiting()
56 m_socket
->Detected_Read();
59 int wxGSocketIOHandler::GetFlags() const
65 void wxGSocketIOHandler::RemoveFlag(wxFDIODispatcherEntryFlags flag
)
70 void wxGSocketIOHandler::AddFlag(wxFDIODispatcherEntryFlags flag
)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
84 bool GSocketGUIFunctionsTableConcrete::OnInit()
89 void GSocketGUIFunctionsTableConcrete::OnExit()
93 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket
*socket
)
97 socket
->m_gui_dependent
= (char *)malloc(sizeof(int)*2);
98 m_id
= (int *)(socket
->m_gui_dependent
);
106 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket
*socket
)
108 free(socket
->m_gui_dependent
);
111 void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket
*socket
,
114 int *m_id
= (int *)(socket
->m_gui_dependent
);
115 const int fd
= socket
->m_fd
;
123 case GSOCK_LOST
: /* fall-through */
124 case GSOCK_INPUT
: c
= 0; break;
125 case GSOCK_OUTPUT
: c
= 1; break;
126 case GSOCK_CONNECTION
: c
= ((socket
->m_server
) ? 0 : 1); break;
130 wxFDIODispatcher
* const dispatcher
= wxFDIODispatcher::Get();
134 wxGSocketIOHandler
*& handler
= socket
->m_handler
;
136 // we should register the new handlers but modify the existing ones in place
137 bool registerHandler
;
140 registerHandler
= false;
142 else // no existing handler
144 registerHandler
= true;
145 handler
= new wxGSocketIOHandler(socket
);
151 handler
->AddFlag(wxFDIO_INPUT
);
156 handler
->AddFlag(wxFDIO_OUTPUT
);
159 if ( registerHandler
)
160 dispatcher
->RegisterFD(fd
, handler
, handler
->GetFlags());
162 dispatcher
->ModifyFD(fd
, handler
, handler
->GetFlags());
165 void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket
*socket
,
168 int *m_id
= (int *)(socket
->m_gui_dependent
);
173 case GSOCK_LOST
: /* fall-through */
174 case GSOCK_INPUT
: c
= 0; break;
175 case GSOCK_OUTPUT
: c
= 1; break;
176 case GSOCK_CONNECTION
: c
= ((socket
->m_server
) ? 0 : 1); break;
186 const wxFDIODispatcherEntryFlags flag
= c
== 0 ? wxFDIO_INPUT
: wxFDIO_OUTPUT
;
188 wxFDIODispatcher
* const dispatcher
= wxFDIODispatcher::Get();
192 wxGSocketIOHandler
*& handler
= socket
->m_handler
;
195 handler
->RemoveFlag(flag
);
197 if ( !handler
->GetFlags() )
199 dispatcher
->UnregisterFD(fd
);
204 dispatcher
->ModifyFD(fd
, handler
, handler
->GetFlags());
209 dispatcher
->UnregisterFD(fd
);
213 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket
*socket
)
215 Install_Callback(socket
, GSOCK_INPUT
);
216 Install_Callback(socket
, GSOCK_OUTPUT
);
219 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket
*socket
)
221 Uninstall_Callback(socket
, GSOCK_INPUT
);
222 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
225 #endif // wxUSE_SOCKETS