/* Table of GUI-related functions. We must call them indirectly because
* of wxBase and GUI separation: */
-static GSocketGUIFunctionsTable *gs_gui_functions;
-
-class GSocketGUIFunctionsTableNull: public GSocketGUIFunctionsTable
-{
-public:
- virtual bool OnInit();
- virtual void OnExit();
- virtual bool CanUseEventLoop();
- virtual bool Init_Socket(GSocket *socket);
- virtual void Destroy_Socket(GSocket *socket);
- virtual void Install_Callback(GSocket *socket, GSocketEvent event);
- virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
- virtual void Enable_Events(GSocket *socket);
- virtual void Disable_Events(GSocket *socket);
-};
-
-bool GSocketGUIFunctionsTableNull::OnInit()
-{ return true; }
-void GSocketGUIFunctionsTableNull::OnExit()
-{}
-bool GSocketGUIFunctionsTableNull::CanUseEventLoop()
-{ return false; }
-bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket *WXUNUSED(socket))
-{ return true; }
-void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket *WXUNUSED(socket))
-{}
-void GSocketGUIFunctionsTableNull::Install_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event))
-{}
-void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event))
-{}
-void GSocketGUIFunctionsTableNull::Enable_Events(GSocket *WXUNUSED(socket))
-{}
-void GSocketGUIFunctionsTableNull::Disable_Events(GSocket *WXUNUSED(socket))
-{}
-/* Global initialisers */
-
-void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc)
-{
- gs_gui_functions = guifunc;
-}
-
-int GSocket_Init(void)
+bool GSocket_Init()
{
- if (!gs_gui_functions)
- {
- static GSocketGUIFunctionsTableNull table;
- gs_gui_functions = &table;
- }
- if ( !gs_gui_functions->OnInit() )
- return 0;
- return 1;
+ GSocketManager * const manager = GSocketManager::Get();
+ return manager && manager->OnInit();
}
-void GSocket_Cleanup(void)
+void GSocket_Cleanup()
{
- if (gs_gui_functions)
- {
- gs_gui_functions->OnExit();
- }
+ GSocketManager * const manager = GSocketManager::Get();
+ if ( manager )
+ manager->OnExit();
}
/* Constructors / Destructors for GSocket */
m_timeout = 10*60*1000;
/* 10 minutes * 60 sec * 1000 millisec */
m_establishing = false;
+ m_use_events = false;
m_initialRecvBufferSize = -1;
m_initialSendBufferSize = -1;
- assert(gs_gui_functions);
- /* Per-socket GUI-specific initialization */
- m_ok = gs_gui_functions->Init_Socket(this);
+ m_ok = GSocketManager::Get()->Init_Socket(this);
}
void GSocket::Close()
{
- gs_gui_functions->Disable_Events(this);
+ if (m_use_events)
+ DisableEvents();
/* When running on OS X, the gsockosx implementation of GSocketGUIFunctionsTable
will close the socket during Disable_Events. However, it will only do this
if (m_fd != INVALID_SOCKET)
Shutdown();
- /* Per-socket GUI-specific cleanup */
- gs_gui_functions->Destroy_Socket(this);
+ GSocketManager::Get()->Destroy_Socket(this);
delete m_handler;
if (m_peer)
GAddress_destroy(m_peer);
+
}
/* GSocket_Shutdown:
assert(this);
/* Don't allow events to fire after socket has been closed */
- gs_gui_functions->Disable_Events(this);
+ if (m_use_events)
+ DisableEvents();
/* If socket has been created, shutdown it */
if (m_fd != INVALID_SOCKET)
#endif
ioctl(m_fd, FIONBIO, &arg);
- gs_gui_functions->Enable_Events(this);
+ if (m_use_events)
+ EnableEvents();
/* allow a socket to re-bind if the socket is in the TIME_WAIT
state after being previously closed.
#else
ioctl(connection->m_fd, FIONBIO, &arg);
#endif
- gs_gui_functions->Enable_Events(connection);
+ if (m_use_events)
+ connection->Notify(true);
return connection;
}
+void GSocket::Notify(bool flag)
+{
+ if (flag == m_use_events)
+ return;
+ m_use_events = flag;
+ EnableEvents(flag);
+}
+
+void GSocket::EnableEvents(bool flag)
+{
+ if (flag)
+ GSocketManager::Get()->Enable_Events(this);
+ else
+ GSocketManager::Get()->Disable_Events(this);
+}
+
bool GSocket::SetReusable()
{
/* socket must not be null, and must not be in use/already bound */
* call to Enable_Events now.
*/
- if (m_non_blocking || ret == 0)
- gs_gui_functions->Enable_Events(this);
+ if (m_use_events && (m_non_blocking || ret == 0))
+ EnableEvents();
if (ret == -1)
{
SOCKOPTLEN_T len = sizeof(error);
getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
-
- gs_gui_functions->Enable_Events(this);
+ if (m_use_events)
+ EnableEvents();
if (!error)
return GSOCK_NOERROR;
#else
ioctl(m_fd, FIONBIO, &arg);
#endif
- gs_gui_functions->Enable_Events(this);
+ if (m_use_events)
+ EnableEvents();
if (m_reusable)
{
else
ret = Recv_Dgram(buffer, size);
- /* If recv returned zero, then the connection has been gracefully closed.
- * Otherwise, recv has returned an error (-1), in which case we have lost the
- * socket only if errno does _not_ indicate that there may be more data to read.
+ /*
+ * If recv returned zero for a TCP socket (if m_stream == NULL, it's an UDP
+ * socket and empty datagrams are possible), then the connection has been
+ * gracefully closed.
+ *
+ * Otherwise, recv has returned an error (-1), in which case we have lost
+ * the socket only if errno does _not_ indicate that there may be more data
+ * to read.
*/
- if (ret == 0)
+ if ((ret == 0) && m_stream)
{
/* Make sure wxSOCKET_LOST event gets sent and shut down the socket */
- m_detected = GSOCK_LOST_FLAG;
- Detected_Read();
- return 0;
+ if (m_use_events)
+ {
+ m_detected = GSOCK_LOST_FLAG;
+ Detected_Read();
+ return 0;
+ }
}
else if (ret == -1)
{
*/
GSocketEventFlags GSocket::Select(GSocketEventFlags flags)
{
- if (!gs_gui_functions->CanUseEventLoop())
- {
-
- GSocketEventFlags result = 0;
- fd_set readfds;
- fd_set writefds;
- fd_set exceptfds;
- struct timeval tv;
+ assert(this);
- assert(this);
+ GSocketEventFlags result = 0;
+ fd_set readfds;
+ fd_set writefds;
+ fd_set exceptfds;
+ struct timeval tv;
- if (m_fd == -1)
- return (GSOCK_LOST_FLAG & flags);
+ if (m_fd == -1)
+ return (GSOCK_LOST_FLAG & flags);
- /* Do not use a static struct, Linux can garble it */
- tv.tv_sec = m_timeout / 1000;
- tv.tv_usec = (m_timeout % 1000) * 1000;
+ /* Do not use a static struct, Linux can garble it */
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
- wxFD_ZERO(&readfds);
- wxFD_ZERO(&writefds);
- wxFD_ZERO(&exceptfds);
- wxFD_SET(m_fd, &readfds);
- if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
- wxFD_SET(m_fd, &writefds);
- wxFD_SET(m_fd, &exceptfds);
+ wxFD_ZERO(&readfds);
+ wxFD_ZERO(&writefds);
+ wxFD_ZERO(&exceptfds);
+ wxFD_SET(m_fd, &readfds);
+ if (flags & GSOCK_OUTPUT_FLAG || flags & GSOCK_CONNECTION_FLAG)
+ wxFD_SET(m_fd, &writefds);
+ wxFD_SET(m_fd, &exceptfds);
- /* Check 'sticky' CONNECTION flag first */
- result |= (GSOCK_CONNECTION_FLAG & m_detected);
+ /* Check 'sticky' CONNECTION flag first */
+ result |= GSOCK_CONNECTION_FLAG & m_detected;
- /* If we have already detected a LOST event, then don't try
- * to do any further processing.
- */
- if ((m_detected & GSOCK_LOST_FLAG) != 0)
- {
- m_establishing = false;
+ /* If we have already detected a LOST event, then don't try
+ * to do any further processing.
+ */
+ if ((m_detected & GSOCK_LOST_FLAG) != 0)
+ {
+ m_establishing = false;
+ return (GSOCK_LOST_FLAG & flags);
+ }
- return (GSOCK_LOST_FLAG & flags);
- }
+ /* Try select now */
+ if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) < 0)
+ {
+ /* What to do here? */
+ return (result & flags);
+ }
- /* Try select now */
- if (select(m_fd + 1, &readfds, &writefds, &exceptfds, &tv) <= 0)
- {
- /* What to do here? */
- return (result & flags);
- }
+ /* Check for exceptions and errors */
+ if (wxFD_ISSET(m_fd, &exceptfds))
+ {
+ m_establishing = false;
+ m_detected = GSOCK_LOST_FLAG;
- /* Check for exceptions and errors */
- if (wxFD_ISSET(m_fd, &exceptfds))
- {
- m_establishing = false;
- m_detected = GSOCK_LOST_FLAG;
+ /* LOST event: Abort any further processing */
+ return (GSOCK_LOST_FLAG & flags);
+ }
- /* LOST event: Abort any further processing */
- return (GSOCK_LOST_FLAG & flags);
- }
+ /* Check for readability */
+ if (wxFD_ISSET(m_fd, &readfds))
+ {
+ result |= GSOCK_INPUT_FLAG;
- /* Check for readability */
- if (wxFD_ISSET(m_fd, &readfds))
+ if (m_server && m_stream)
{
- result |= GSOCK_INPUT_FLAG;
-
- if (m_server && m_stream)
- {
- /* This is a TCP server socket that detected a connection.
- While the INPUT_FLAG is also set, it doesn't matter on
- this kind of sockets, as we can only Accept() from them. */
- result |= GSOCK_CONNECTION_FLAG;
- m_detected |= GSOCK_CONNECTION_FLAG;
- }
+ /* This is a TCP server socket that detected a connection.
+ While the INPUT_FLAG is also set, it doesn't matter on
+ this kind of sockets, as we can only Accept() from them. */
+ m_detected |= GSOCK_CONNECTION_FLAG;
}
+ }
- /* Check for writability */
- if (wxFD_ISSET(m_fd, &writefds))
+ /* Check for writability */
+ if (wxFD_ISSET(m_fd, &writefds))
+ {
+ if (m_establishing && !m_server)
{
- if (m_establishing && !m_server)
- {
- int error;
- SOCKOPTLEN_T len = sizeof(error);
-
- m_establishing = false;
-
- getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
+ int error;
+ SOCKOPTLEN_T len = sizeof(error);
+ m_establishing = false;
+ getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
- if (error)
- {
- m_detected = GSOCK_LOST_FLAG;
+ if (error)
+ {
+ m_detected = GSOCK_LOST_FLAG;
- /* LOST event: Abort any further processing */
- return (GSOCK_LOST_FLAG & flags);
- }
- else
- {
- result |= GSOCK_CONNECTION_FLAG;
- m_detected |= GSOCK_CONNECTION_FLAG;
- }
+ /* LOST event: Abort any further processing */
+ return (GSOCK_LOST_FLAG & flags);
}
else
{
- result |= GSOCK_OUTPUT_FLAG;
+ m_detected |= GSOCK_CONNECTION_FLAG;
}
}
-
- return (result & flags);
-
- }
- else
- {
- assert(this);
- return flags & m_detected;
+ else
+ {
+ result |= GSOCK_OUTPUT_FLAG;
+ }
}
+
+ return (result | m_detected) & flags;
}
/* Flags */
void GSocket::Enable(GSocketEvent event)
{
- m_detected &= ~(1 << event);
- gs_gui_functions->Install_Callback(this, event);
+ if (m_use_events)
+ {
+ m_detected &= ~(1 << event);
+ GSocketManager::Get()->Install_Callback(this, event);
+ }
}
void GSocket::Disable(GSocketEvent event)
{
- m_detected |= (1 << event);
- gs_gui_functions->Uninstall_Callback(this, event);
+ if (m_use_events)
+ {
+ m_detected |= (1 << event);
+ GSocketManager::Get()->Uninstall_Callback(this, event);
+ }
}
/* _GSocket_Input_Timeout:
}
else if (num == 0)
{
- /* graceful shutdown */
- CALL_CALLBACK(this, GSOCK_LOST);
- Shutdown();
+ if (m_stream)
+ {
+ /* graceful shutdown */
+ CALL_CALLBACK(this, GSOCK_LOST);
+ Shutdown();
+ }
+ else
+ {
+ /* Empty datagram received */
+ CALL_CALLBACK(this, GSOCK_INPUT);
+ }
}
else
{