more fixes for using wxSocket in console applications (patch 1756260)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48723 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-09-16 14:29:42 +00:00
parent 7ed0328efd
commit 22b6de6a70
21 changed files with 428 additions and 299 deletions

View File

@@ -21,6 +21,7 @@
#ifndef __GSOCKET_STANDALONE__
#include "wx/defs.h"
#include "wx/private/gsocketiohandler.h"
#include "wx/thread.h"
#endif
#if defined(__VISAGECPP__)
@@ -455,38 +456,6 @@ struct servent *wxGetservbyname_r(const char *port, const char *protocol,
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)
@@ -498,7 +467,7 @@ int GSocket_Init(void)
{
if (!gs_gui_functions)
{
static GSocketGUIFunctionsTableNull table;
static GSocketGUIFunctionsTableConcrete table;
gs_gui_functions = &table;
}
if ( !gs_gui_functions->OnInit() )
@@ -541,6 +510,7 @@ GSocket::GSocket()
m_timeout = 10*60*1000;
/* 10 minutes * 60 sec * 1000 millisec */
m_establishing = false;
m_use_events = false;
assert(gs_gui_functions);
/* Per-socket GUI-specific initialization */
@@ -549,7 +519,8 @@ GSocket::GSocket()
void GSocket::Close()
{
gs_gui_functions->Disable_Events(this);
if (m_use_events)
DisableEvents();
/* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
#if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
close(m_fd);
@@ -566,7 +537,8 @@ GSocket::~GSocket()
Shutdown();
/* Per-socket GUI-specific cleanup */
gs_gui_functions->Destroy_Socket(this);
if (m_use_events)
gs_gui_functions->Destroy_Socket(this);
delete m_handler;
@@ -576,6 +548,7 @@ GSocket::~GSocket()
if (m_peer)
GAddress_destroy(m_peer);
}
/* GSocket_Shutdown:
@@ -589,7 +562,8 @@ void 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)
@@ -773,7 +747,8 @@ GSocketError GSocket::SetServer()
#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.
@@ -892,11 +867,30 @@ GSocket *GSocket::WaitConnection()
#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;
// it is not safe to attach or detach i/o descriptor in child thread
wxASSERT_MSG(wxThread::IsMain(),wxT("wxSocketBase::Notify can be called in main thread only"));
m_use_events = flag;
EnableEvents(flag);
}
void GSocket::EnableEvents(bool flag)
{
if ( flag )
gs_gui_functions->Enable_Events(this);
else
gs_gui_functions->Disable_Events(this);
}
bool GSocket::SetReusable()
{
/* socket must not be null, and must not be in use/already bound */
@@ -1029,8 +1023,8 @@ GSocketError GSocket::Connect(GSocketStream stream)
* 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)
{
@@ -1055,8 +1049,8 @@ GSocketError GSocket::Connect(GSocketStream stream)
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;
@@ -1136,7 +1130,8 @@ GSocketError GSocket::SetNonOriented()
#else
ioctl(m_fd, FIONBIO, &arg);
#endif
gs_gui_functions->Enable_Events(this);
if ( m_use_events )
EnableEvents();
if (m_reusable)
{
@@ -1208,9 +1203,12 @@ int GSocket::Read(char *buffer, int size)
if (ret == 0)
{
/* 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)
{
@@ -1538,14 +1536,20 @@ GSocketError GSocket::SetSockOpt(int level, int optname,
void GSocket::Enable(GSocketEvent event)
{
m_detected &= ~(1 << event);
gs_gui_functions->Install_Callback(this, event);
if ( m_use_events )
{
m_detected &= ~(1 << event);
gs_gui_functions->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);
gs_gui_functions->Uninstall_Callback(this, event);
}
}
/* _GSocket_Input_Timeout: