-// --------------------------------------------------------------
-// wxSocketBase ctor and dtor
-// --------------------------------------------------------------
-
-wxSocketBase::wxSocketBase(wxSocketBase::wxSockFlags _flags,
- wxSocketBase::wxSockType _type) :
- wxEvtHandler(),
- m_socket(NULL), m_flags(_flags), m_type(_type),
- m_neededreq(0),
- m_lcount(0), m_timeout(600),
- m_unread(NULL), m_unrd_size(0), m_unrd_cur(0),
- m_cbk(NULL), m_cdata(NULL),
- m_connected(FALSE), m_establishing(FALSE),
- m_notify_state(FALSE), m_id(-1),
- m_defering(NO_DEFER), m_error(FALSE),
- m_states()
+// ==========================================================================
+// wxSocketBase
+// ==========================================================================
+
+// --------------------------------------------------------------------------
+// Initialization and shutdown
+// --------------------------------------------------------------------------
+
+// FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
+// to m_countInit with a crit section
+size_t wxSocketBase::m_countInit = 0;
+
+bool wxSocketBase::IsInitialized()
+{
+ return m_countInit > 0;
+}
+
+bool wxSocketBase::Initialize()
+{
+ if ( !m_countInit++ )
+ {
+ wxAppTraits *traits = wxAppConsole::GetInstance() ?
+ wxAppConsole::GetInstance()->GetTraits() : NULL;
+ GSocketGUIFunctionsTable *functions =
+ traits ? traits->GetSocketGUIFunctionsTable() : NULL;
+ GSocket_SetGUIFunctions(functions);
+
+ if ( !GSocket_Init() )
+ {
+ m_countInit--;
+
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+void wxSocketBase::Shutdown()
+{
+ // we should be initialized
+ wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") );
+ if ( !--m_countInit )
+ {
+ GSocket_Cleanup();
+ }
+}
+
+// --------------------------------------------------------------------------
+// Ctor and dtor
+// --------------------------------------------------------------------------
+
+void wxSocketBase::Init()
+{
+ m_socket = NULL;
+ m_type = wxSOCKET_UNINIT;
+
+ // state
+ m_flags = 0;
+ m_connected =
+ m_establishing =
+ m_reading =
+ m_writing =
+ m_error = FALSE;
+ m_lcount = 0;
+ m_timeout = 600;
+ m_beingDeleted = FALSE;
+
+ // pushback buffer
+ m_unread = NULL;
+ m_unrd_size = 0;
+ m_unrd_cur = 0;
+
+ // events
+ m_id = -1;
+ m_handler = NULL;
+ m_clientData = NULL;
+ m_notify = FALSE;
+ m_eventmask = 0;
+
+ if ( !IsInitialized() )
+ {
+ // this Initialize() will be undone by wxSocketModule::OnExit(), all the
+ // other calls to it should be matched by a call to Shutdown()
+ Initialize();
+ }
+}
+
+wxSocketBase::wxSocketBase()