// Copyright: (C) 1999-1997, Guilhem Lavaux
// (C) 2000-1999, Guillermo Rodriguez Garcia
// RCS_ID: $Id$
-// License: see wxWindows license
+// License: see wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ==========================================================================
#if wxUSE_SOCKETS
#include "wx/app.h"
+#include "wx/apptrait.h"
#include "wx/defs.h"
#include "wx/object.h"
#include "wx/string.h"
#include "wx/intl.h"
#include "wx/event.h"
-#if wxUSE_GUI
- #include "wx/gdicmn.h" // for wxPendingDelete
-#endif // wxUSE_GUI
-
#include "wx/sckaddr.h"
#include "wx/socket.h"
// discard buffer
#define MAX_DISCARD_SIZE (10 * 1024)
-// what to do within waits: in wxBase we don't do anything as we don't have
-// the event loop anyhow (for now). In GUI apps we have 2 cases: from the main
-// thread itself we have to call wxYield() to let the events (including the
-// GUI events and the low-level (not wxWindows) events from GSocket) be
-// processed. From another thread it is enough to just call wxThread::Yield()
-// which will give away the rest of our time slice: the explanation is that
-// the events will be processed by the main thread anyhow, without calling
-// wxYield(), but we don't want to eat the CPU time uselessly while sitting
-// in the loop waiting for the data
-#if wxUSE_GUI
- #if wxUSE_THREADS
- #define PROCESS_EVENTS() \
- { \
- if ( wxThread::IsMain() ) \
- wxYield(); \
- else \
- wxThread::Yield(); \
- }
- #else // !wxUSE_THREADS
- #define PROCESS_EVENTS() wxYield()
- #endif // wxUSE_THREADS/!wxUSE_THREADS
-#else // !wxUSE_GUI
- #define PROCESS_EVENTS()
-#endif // wxUSE_GUI/!wxUSE_GUI
+// what to do within waits: we have 2 cases: from the main thread itself we
+// have to call wxYield() to let the events (including the GUI events and the
+// low-level (not wxWindows) events from GSocket) be processed. From another
+// thread it is enough to just call wxThread::Yield() which will give away the
+// rest of our time slice: the explanation is that the events will be processed
+// by the main thread anyhow, without calling wxYield(), but we don't want to
+// eat the CPU time uselessly while sitting in the loop waiting for the data
+#if wxUSE_THREADS
+ #define PROCESS_EVENTS() \
+ { \
+ if ( wxThread::IsMain() ) \
+ wxYield(); \
+ else \
+ wxThread::Yield(); \
+ }
+#else // !wxUSE_THREADS
+ #define PROCESS_EVENTS() wxYield()
+#endif // wxUSE_THREADS/!wxUSE_THREADS
+
+#define wxTRACE_Socket _T("wxSocket")
// --------------------------------------------------------------------------
// wxWin macros
public:
wxSocketState() : wxObject() {}
+
+ DECLARE_NO_COPY_CLASS(wxSocketState)
};
// ==========================================================================
{
// Just in case the app called Destroy() *and* then deleted
// the socket immediately: don't leave dangling pointers.
-#if wxUSE_GUI
- wxPendingDelete.DeleteObject(this);
-#endif
+ wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
+ if ( traits )
+ traits->RemoveFromPendingDelete(this);
// Shutdown and close the socket
if (!m_beingDeleted)
// Supress events from now on
Notify(FALSE);
-#if wxUSE_GUI
- if ( !wxPendingDelete.Member(this) )
- wxPendingDelete.Append(this);
-#else
- delete this;
-#endif
+ // schedule this object for deletion
+ wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
+ if ( traits )
+ {
+ // let the traits object decide what to do with us
+ traits->ScheduleForDestroy(this);
+ }
+ else // no app or no traits
+ {
+ // in wxBase we might have no app object at all, don't leak memory
+ delete this;
+ }
return TRUE;
}
return FALSE;
peer = GSocket_GetPeer(m_socket);
+
+ // copying a null address would just trigger an assert anyway
+
+ if (!peer)
+ return FALSE;
+
addr_man.SetAddress(peer);
GAddress_destroy(peer);
void wxSocketBase::RestoreState()
{
- wxNode *node;
+ wxList::compatibility_iterator node;
wxSocketState *state;
- node = m_states.Last();
+ node = m_states.GetLast();
if (!node)
return;
- state = (wxSocketState *)node->Data();
+ state = (wxSocketState *)node->GetData();
m_flags = state->m_flags;
m_notify = state->m_notify;
m_cdata = state->m_cdata;
#endif // WXWIN_COMPATIBILITY
- delete node;
+ m_states.Erase(node);
delete state;
}
wxSocketFlags flags)
: wxSocketBase(flags, wxSOCKET_SERVER)
{
- // Create the socket
- m_socket = GSocket_new();
+ wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
- if (!m_socket)
- return;
+ m_socket = GSocket_new();
- // Setup the socket as server
- GSocket_SetLocal(m_socket, addr_man.GetAddress());
- if (GSocket_SetServer(m_socket) != GSOCK_NOERROR)
- {
- GSocket_destroy(m_socket);
- m_socket = NULL;
- return;
- }
+ if (!m_socket)
+ {
+ wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") );
+ return;
+ }
- GSocket_SetTimeout(m_socket, m_timeout * 1000);
- GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
- GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
- wx_socket_callback, (char *)this);
+ // Setup the socket as server
+ GSocket_SetLocal(m_socket, addr_man.GetAddress());
+ if (GSocket_SetServer(m_socket) != GSOCK_NOERROR)
+ {
+ GSocket_destroy(m_socket);
+ m_socket = NULL;
+
+ wxLogTrace( wxTRACE_Socket, _T("*** GSocket_SetServer failed") );
+ return;
+ }
+
+ GSocket_SetTimeout(m_socket, m_timeout * 1000);
+ GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
+ GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
+ wx_socket_callback, (char *)this);
}
// --------------------------------------------------------------------------
// wxSocketModule
// ==========================================================================
-class WXDLLEXPORT wxSocketModule : public wxModule
+class wxSocketModule : public wxModule
{
public:
virtual bool OnInit()
#endif
// wxUSE_SOCKETS
+
+// vi:sts=4:sw=4:et