///////////////////////////////////////////////////////////////////////////////
// Name: src/common/gsocketiohandler.cpp
-// Purpose: implementation of wxFDIOHandler for GSocket
+// Purpose: implementation of wxFDIOHandler for wxSocket
// Author: Angel Vidal, Lukasz Michalski
// Created: 08.24.06
// RCS-ID: $Id$
// Copyright: (c) 2006 Angel vidal
+// (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
// License: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#if wxUSE_SOCKETS && wxUSE_SELECT_DISPATCHER
-#include "wx/private/gsocketiohandler.h"
+#include "wx/apptrait.h"
#include "wx/unix/private.h"
-#include "wx/gsocket.h"
-#include "wx/unix/gsockunx.h"
+#include "wx/private/gsocketiohandler.h"
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
-// wxGSocketIOHandler
+// wxSocketImplFDIO
// ----------------------------------------------------------------------------
-wxGSocketIOHandler::wxGSocketIOHandler(GSocket* socket)
- : m_socket(socket),
- m_flags(0)
+class wxSocketImplFDIO : public wxSocketImplUnix
{
+public:
+ wxSocketImplFDIO(wxSocketBase& wxsocket)
+ : wxSocketImplUnix(wxsocket)
+ {
+ m_handler = NULL;
+ }
-}
+ virtual ~wxSocketImplFDIO()
+ {
+ delete m_handler;
+ }
-void wxGSocketIOHandler::OnReadWaiting()
-{
- m_socket->Detected_Read();
-}
+ wxSocketIOHandler *m_handler;
+};
-void wxGSocketIOHandler::OnWriteWaiting()
-{
- m_socket->Detected_Write();
-}
+// ----------------------------------------------------------------------------
+// wxSocketSelectManager
+// ----------------------------------------------------------------------------
-void wxGSocketIOHandler::OnExceptionWaiting()
+class wxSocketSelectManager : public wxSocketFDBasedManager
{
- m_socket->Detected_Read();
-}
+public:
+ virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
+ {
+ return new wxSocketImplFDIO(wxsocket);
+ }
+
+ virtual void Install_Callback(wxSocketImpl *socket, wxSocketNotify event);
+ virtual void Uninstall_Callback(wxSocketImpl *socket, wxSocketNotify event);
+};
-int wxGSocketIOHandler::GetFlags() const
+void wxSocketSelectManager::Install_Callback(wxSocketImpl *socket_,
+ wxSocketNotify event)
{
- return m_flags;
-}
+ wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_);
+ const int fd = socket->m_fd;
-void wxGSocketIOHandler::RemoveFlag(wxFDIODispatcherEntryFlags flag)
-{
- m_flags &= ~flag;
-}
+ if ( fd == -1 )
+ return;
-void wxGSocketIOHandler::AddFlag(wxFDIODispatcherEntryFlags flag)
-{
- m_flags |= flag;
-}
+ const SocketDir d = GetDirForEvent(socket, event);
-// ----------------------------------------------------------------------------
-// GSocket interface
-// ----------------------------------------------------------------------------
+ wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
+ if ( !dispatcher )
+ return;
-bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
-{
- return true;
-}
+ wxSocketIOHandler *& handler = socket->m_handler;
-bool GSocketGUIFunctionsTableConcrete::OnInit()
-{
- return true;
-}
+ // we should register the new handlers but modify the existing ones in place
+ bool registerHandler;
+ if ( handler )
+ {
+ registerHandler = false;
+ }
+ else // no existing handler
+ {
+ registerHandler = true;
+ handler = new wxSocketIOHandler(socket);
+ }
-void GSocketGUIFunctionsTableConcrete::OnExit()
-{
+ FD(socket, d) = fd;
+ if (d == FD_INPUT)
+ {
+ handler->AddFlag(wxFDIO_INPUT);
+ }
+ else
+ {
+ handler->AddFlag(wxFDIO_OUTPUT);
+ }
+
+ if ( registerHandler )
+ dispatcher->RegisterFD(fd, handler, handler->GetFlags());
+ else
+ dispatcher->ModifyFD(fd, handler, handler->GetFlags());
}
-bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
+void wxSocketSelectManager::Uninstall_Callback(wxSocketImpl *socket_,
+ wxSocketNotify event)
{
- int *m_id;
+ wxSocketImplFDIO * const socket = static_cast<wxSocketImplFDIO *>(socket_);
- socket->m_gui_dependent = (char *)malloc(sizeof(int)*2);
- m_id = (int *)(socket->m_gui_dependent);
+ const SocketDir d = GetDirForEvent(socket, event);
- m_id[0] = -1;
- m_id[1] = -1;
+ const int fd = FD(socket, d);
+ if ( fd == -1 )
+ return;
- return true;
-}
+ FD(socket, d) = -1;
-void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
-{
- free(socket->m_gui_dependent);
-}
+ const wxFDIODispatcherEntryFlags
+ flag = d == FD_INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
-void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket,
- GSocketEvent event)
-{
- int *m_id = (int *)(socket->m_gui_dependent);
- const int fd = socket->m_fd;
-
- if ( fd == -1 )
- return;
-
- int c;
- switch (event)
- {
- case GSOCK_LOST: /* fall-through */
- case GSOCK_INPUT: c = 0; break;
- case GSOCK_OUTPUT: c = 1; break;
- case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
- default: return;
- }
-
- wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
- if ( !dispatcher )
- return;
-
- wxGSocketIOHandler *& handler = socket->m_handler;
-
- // we should register the new handlers but modify the existing ones in place
- bool registerHandler;
- if ( handler )
- {
- registerHandler = false;
- }
- else // no existing handler
- {
- registerHandler = true;
- handler = new wxGSocketIOHandler(socket);
- }
-
- if (c == 0)
- {
- m_id[0] = fd;
- handler->AddFlag(wxFDIO_INPUT);
- }
- else
- {
- m_id[1] = fd;
- handler->AddFlag(wxFDIO_OUTPUT);
- }
-
- if ( registerHandler )
- dispatcher->RegisterFD(fd, handler, handler->GetFlags());
- else
- dispatcher->ModifyFD(fd, handler, handler->GetFlags());
-}
+ wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
+ if ( !dispatcher )
+ return;
-void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket,
- GSocketEvent event)
-{
- int *m_id = (int *)(socket->m_gui_dependent);
- int c;
-
- switch (event)
- {
- case GSOCK_LOST: /* fall-through */
- case GSOCK_INPUT: c = 0; break;
- case GSOCK_OUTPUT: c = 1; break;
- case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
- default: return;
- }
-
- if ( m_id[c] == -1 )
- return;
-
- int fd = m_id[c];
- m_id[c] = -1;
-
- const wxFDIODispatcherEntryFlags flag = c == 0 ? wxFDIO_INPUT : wxFDIO_OUTPUT;
-
- wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
- if ( !dispatcher )
- return;
-
- wxGSocketIOHandler *& handler = socket->m_handler;
- if ( handler )
- {
- handler->RemoveFlag(flag);
-
- if ( !handler->GetFlags() )
- {
- dispatcher->UnregisterFD(fd);
- delete handler;
- }
- else
- {
- dispatcher->ModifyFD(fd, handler, handler->GetFlags());
- }
- }
- else
- {
- dispatcher->UnregisterFD(fd);
- }
-}
+ wxSocketIOHandler *& handler = socket->m_handler;
+ if ( handler )
+ {
+ handler->RemoveFlag(flag);
-void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
-{
- Install_Callback(socket, GSOCK_INPUT);
- Install_Callback(socket, GSOCK_OUTPUT);
+ if ( !handler->GetFlags() )
+ {
+ dispatcher->UnregisterFD(fd);
+ delete handler;
+ socket->m_handler = NULL;
+ }
+ else
+ {
+ dispatcher->ModifyFD(fd, handler, handler->GetFlags());
+ }
+ }
+ else
+ {
+ dispatcher->UnregisterFD(fd);
+ }
}
-void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
+wxSocketManager *wxAppTraits::GetSocketManager()
{
- Uninstall_Callback(socket, GSOCK_INPUT);
- Uninstall_Callback(socket, GSOCK_OUTPUT);
+ static wxSocketSelectManager s_manager;
+
+ return &s_manager;
}
#endif // wxUSE_SOCKETS