class GSocketBase
{
public:
+ // static factory function
+ static GSocket *Create();
+
+ virtual ~GSocketBase();
+
GSocketEventFlags Select(GSocketEventFlags flags);
+ virtual void Close() = 0;
+ virtual void Shutdown();
+
#ifdef __WINDOWS__
SOCKET m_fd;
#else
int m_fd;
#endif
- bool m_ok;
int m_initialRecvBufferSize;
int m_initialSendBufferSize;
GSocketEventFlags m_detected;
GSocketCallback m_cbacks[GSOCK_MAX_EVENT];
char *m_data[GSOCK_MAX_EVENT];
+
+protected:
+ GSocketBase();
};
#if defined(__WINDOWS__)
#include "wx/unix/gsockunx.h"
#endif
-
/* Global initializers */
/* GSocket_Init() must be called at the beginning (but after calling
void GSocket_Cleanup();
-/* Constructors / Destructors */
-
-GSocket *GSocket_new();
-
-
/* GAddress */
// Represents a socket endpoint, i.e. -- in spite of its name -- not an address
#define SOCKOPTLEN_T int
#endif
+/*
+ * MSW defines this, Unices don't.
+ */
+#ifndef INVALID_SOCKET
+#define INVALID_SOCKET (-1)
+#endif
+
#endif /* wxUSE_SOCKETS */
#endif /* _WX_GSOCKET_H_ */
class GSocket : public GSocketBase
{
public:
- GSocket();
- ~GSocket();
- bool IsOk() { return m_ok; }
- void Close();
- void Shutdown();
+ GSocket() : GSocketBase() { m_msgnumber = 0; }
+
+ virtual void Close();
+
GSocketError SetLocal(GAddress *address);
GSocketError SetPeer(GAddress *address);
GAddress *GetLocal();
{
public:
GSocket();
- virtual ~GSocket();
- bool IsOk() { return m_ok; }
- void Close();
- void Shutdown();
+ ~GSocket();
+ virtual void Close();
+ virtual void Shutdown();
GSocketError SetLocal(GAddress *address);
GSocketError SetPeer(GAddress *address);
GAddress *GetLocal();
const void *optval, int optlen);
//attach or detach from main loop
void Notify(bool flag);
- virtual void Detected_Read();
- virtual void Detected_Write();
+ void Detected_Read();
+ void Detected_Write();
void SetInitialSocketBuffers(int recv, int send)
{
m_initialRecvBufferSize = recv;
ms_manager = app->GetTraits()->GetSocketManager();
}
+// ==========================================================================
+// GSocketBase
+// ==========================================================================
+
+/* static */
+GSocket *GSocketBase::Create()
+{
+ GSocket * const newsocket = new GSocket();
+ if ( !GSocketManager::Get()->Init_Socket(newsocket) )
+ {
+ delete newsocket;
+ return NULL;
+ }
+
+ return newsocket;
+}
+
+GSocketBase::GSocketBase()
+{
+ m_fd = INVALID_SOCKET;
+ m_detected = 0;
+ m_local = NULL;
+ m_peer = NULL;
+ m_error = GSOCK_NOERROR;
+ m_server = false;
+ m_stream = true;
+ m_non_blocking = false;
+#ifdef __WINDOWS__
+ m_timeout.tv_sec = 10 * 60; /* 10 minutes */
+ m_timeout.tv_usec = 0;
+#else
+ m_timeout = 10*60*1000; /* 10 minutes * 60 sec * 1000 millisec */
+#endif
+
+ m_establishing = false;
+ m_reusable = false;
+ m_broadcast = false;
+ m_dobind = true;
+ m_initialRecvBufferSize = -1;
+ m_initialSendBufferSize = -1;
+
+ for ( int i = 0; i < GSOCK_MAX_EVENT; i++ )
+ m_cbacks[i] = NULL;
+}
+
+GSocketBase::~GSocketBase()
+{
+ if (m_fd != INVALID_SOCKET)
+ Shutdown();
+
+ if (m_local)
+ GAddress_destroy(m_local);
+
+ if (m_peer)
+ GAddress_destroy(m_peer);
+
+ // cast is ok as all GSocketBase objects we have in our code are really
+ // GSockets
+ GSocketManager::Get()->Destroy_Socket(static_cast<GSocket *>(this));
+}
+
+/* GSocket_Shutdown:
+ * Disallow further read/write operations on this socket, close
+ * the fd and disable all callbacks.
+ */
+void GSocketBase::Shutdown()
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ shutdown(m_fd, 1 /* SD_SEND */);
+ Close();
+ }
+
+ /* Disable GUI callbacks */
+ for ( int evt = 0; evt < GSOCK_MAX_EVENT; evt++ )
+ m_cbacks[evt] = NULL;
+
+ m_detected = GSOCK_LOST_FLAG;
+}
+
// ==========================================================================
// wxSocketBase
// ==========================================================================
{
wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
- m_socket = GSocket_new();
+ m_socket = GSocket::Create();
if (!m_socket)
{
delete m_socket;
}
- m_socket = GSocket_new();
+ m_socket = GSocket::Create();
m_connected = false;
m_establishing = false;
: wxSocketBase( flags, wxSOCKET_DATAGRAM )
{
// Create the socket
- m_socket = GSocket_new();
+ m_socket = GSocket::Create();
if (!m_socket)
{
/* Constructors / Destructors for GSocket */
-GSocket::GSocket()
-{
- int i;
-
- m_fd = INVALID_SOCKET;
- for (i = 0; i < GSOCK_MAX_EVENT; i++)
- {
- m_cbacks[i] = NULL;
- }
- m_detected = 0;
- m_local = NULL;
- m_peer = NULL;
- m_error = GSOCK_NOERROR;
- m_server = false;
- m_stream = true;
- m_non_blocking = false;
- m_timeout.tv_sec = 10 * 60; /* 10 minutes */
- m_timeout.tv_usec = 0;
- m_establishing = false;
- m_reusable = false;
- m_broadcast = false;
- m_dobind = true;
- m_initialRecvBufferSize = -1;
- m_initialSendBufferSize = -1;
-
- m_ok = GSocketManager::Get()->Init_Socket(this);
-}
-
void GSocket::Close()
{
GSocketManager::Get()->Disable_Events(this);
m_fd = INVALID_SOCKET;
}
-GSocket::~GSocket()
-{
- GSocketManager::Get()->Destroy_Socket(this);
-
- /* Check that the socket is really shutdowned */
- if (m_fd != INVALID_SOCKET)
- Shutdown();
-
- /* Destroy private addresses */
- if (m_local)
- GAddress_destroy(m_local);
-
- if (m_peer)
- GAddress_destroy(m_peer);
-}
-
-/* GSocket_Shutdown:
- * Disallow further read/write operations on this socket, close
- * the fd and disable all callbacks.
- */
-void GSocket::Shutdown()
-{
- int evt;
-
- /* If socket has been created, shutdown it */
- if (m_fd != INVALID_SOCKET)
- {
- shutdown(m_fd, 1 /* SD_SEND */);
- Close();
- }
-
- /* Disable GUI callbacks */
- for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
- m_cbacks[evt] = NULL;
-
- m_detected = GSOCK_LOST_FLAG;
-}
-
/* Address handling */
/* GSocket_SetLocal:
}
/* Create a GSocket object for the new connection */
- connection = GSocket_new();
+ connection = GSocket::Create();
if (!connection)
{
return ret;
}
-/* Compatibility functions for GSocket */
-GSocket *GSocket_new()
-{
- GSocket *newsocket = new GSocket();
- if(newsocket->IsOk())
- return newsocket;
- delete newsocket;
- return NULL;
-}
-
-
/*
* -------------------------------------------------------------------------
* GAddress
{
/* Remove the socket from the list */
EnterCriticalSection(&critical);
- if ( socket->IsOk() )
- {
- const int msgnum = socket->m_msgnumber;
+ const int msgnum = socket->m_msgnumber;
+ if ( msgnum )
+ {
// we need to remove any pending messages for this socket to avoid having
// them sent to a new socket which could reuse the same message number as
// soon as we destroy this one
socketList[msgnum - WM_USER] = NULL;
}
+ //else: the socket has never been created successfully
LeaveCriticalSection(&critical);
}
#define SOCKOPTLEN_T WX_SOCKLEN_T
#endif
-/*
- * MSW defines this, Unices don't.
- */
-#ifndef INVALID_SOCKET
-#define INVALID_SOCKET -1
-#endif
-
/* UnixWare reportedly needs this for FIONBIO definition */
#ifdef __UNIXWARE__
#include <sys/filio.h>
GSocket::GSocket()
{
- int i;
-
- m_fd = INVALID_SOCKET;
m_handler = NULL;
- for (i=0;i<GSOCK_MAX_EVENT;i++)
- {
- m_cbacks[i] = NULL;
- }
- m_detected = 0;
- m_local = NULL;
- m_peer = NULL;
- m_error = GSOCK_NOERROR;
- m_server = false;
- m_stream = true;
m_gui_dependent = NULL;
- m_non_blocking = false;
- m_reusable = false;
- m_broadcast = false;
- m_dobind = true;
- m_timeout = 10*60*1000;
- /* 10 minutes * 60 sec * 1000 millisec */
- m_establishing = false;
m_use_events = false;
- m_initialRecvBufferSize = -1;
- m_initialSendBufferSize = -1;
-
- m_ok = GSocketManager::Get()->Init_Socket(this);
}
void GSocket::Close()
GSocket::~GSocket()
{
- assert(this);
-
- /* Check that the socket is really shutdowned */
- if (m_fd != INVALID_SOCKET)
- Shutdown();
-
- GSocketManager::Get()->Destroy_Socket(this);
-
delete m_handler;
-
- /* Destroy private addresses */
- if (m_local)
- GAddress_destroy(m_local);
-
- if (m_peer)
- GAddress_destroy(m_peer);
-
}
/* GSocket_Shutdown:
*/
void GSocket::Shutdown()
{
- int evt;
-
- assert(this);
-
- /* Don't allow events to fire after socket has been closed */
- if (m_use_events)
- DisableEvents();
-
- /* If socket has been created, shutdown it */
- if (m_fd != INVALID_SOCKET)
- {
- shutdown(m_fd, 1);
- Close();
- }
-
- /* Disable GUI callbacks */
- for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
- m_cbacks[evt] = NULL;
+ /* Don't allow events to fire after socket has been closed */
+ if (m_use_events)
+ DisableEvents();
- m_detected = GSOCK_LOST_FLAG;
+ GSocketBase::Shutdown();
}
/* Address handling */
}
/* Create a GSocket object for the new connection */
- connection = GSocket_new();
+ connection = GSocket::Create();
if (!connection)
{
}
}
-/* Compatibility functions for GSocket */
-GSocket *GSocket_new(void)
-{
- GSocket *newsocket = new GSocket();
- if (newsocket->IsOk())
- return newsocket;
-
- delete newsocket;
-
- return NULL;
-}
-
/*
* -------------------------------------------------------------------------
* GAddress