X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ef57d8662f4a9838adad363d6d3cdf512a7e924f..e1208c3103734c8581c9854c344b97323e381897:/src/common/socket.cpp diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 98ce67a4b9..9436c636b6 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -35,7 +35,10 @@ #include "wx/module.h" #include "wx/log.h" #include "wx/intl.h" -#include "wx/gdicmn.h" // for wxPendingDelete + +#if wxUSE_GUI + #include "wx/gdicmn.h" // for wxPendingDelete +#endif // wxUSE_GUI #include "wx/sckaddr.h" #include "wx/socket.h" @@ -89,7 +92,8 @@ wxSocketBase::wxSocketBase(wxSockFlags _flags, wxSockType _type) : m_neededreq(0), m_notify_state(FALSE), m_connected(FALSE), m_establishing(FALSE), m_reading(FALSE), m_writing(FALSE), - m_error(FALSE), m_lcount(0), m_timeout(600), m_states(), + m_error(FALSE), m_lcount(0), m_timeout(600), + m_states(), m_beingDeleted(FALSE), m_unread(NULL), m_unrd_size(0), m_unrd_cur(0), m_cbk(NULL), m_cdata(NULL) { @@ -102,7 +106,8 @@ wxSocketBase::wxSocketBase() : m_neededreq(0), m_notify_state(FALSE), m_connected(FALSE), m_establishing(FALSE), m_reading(FALSE), m_writing(FALSE), - m_error(FALSE), m_lcount(0), m_timeout(600), m_states(), + m_error(FALSE), m_lcount(0), m_timeout(600), + m_states(), m_beingDeleted(FALSE), m_unread(NULL), m_unrd_size(0), m_unrd_cur(0), m_cbk(NULL), m_cdata(NULL) { @@ -110,33 +115,45 @@ wxSocketBase::wxSocketBase() : wxSocketBase::~wxSocketBase() { - if (m_unread) - free(m_unread); + // 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 // Shutdown and close the socket - Close(); + if (!m_beingDeleted) + Close(); // Destroy the GSocket object if (m_socket) GSocket_destroy(m_socket); + + // Free the pushback buffer + if (m_unread) + free(m_unread); } -/* bool wxSocketBase::Destroy() { // Delayed destruction: the socket will be deleted during the next // idle loop iteration. This ensures that all pending events have // been processed. - m_beingDeleted = TRUE; + + // Shutdown and close the socket Close(); - if ( !wxPendingDelete.Member(this) ) +#if wxUSE_GUI + if ( wxPendingDelete.Member(this) ) wxPendingDelete.Append(this); +#else + delete this; +#endif return TRUE; } -*/ + // -------------------------------------------------------------------------- // Basic IO operations @@ -548,6 +565,8 @@ wxSocketBase& wxSocketBase::Discard() // timeout elapses. The polling loop calls PROCESS_EVENTS(), so // this won't block the GUI. +#if wxUSE_GUI + class _wxSocketInternalTimer: public wxTimer { public: @@ -560,11 +579,17 @@ public: } }; +#endif // wxUSE_GUI + bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags flags) { GSocketEventFlags result; +#if wxUSE_GUI _wxSocketInternalTimer timer; + wxTimerRunner runTimer(timer); +#endif // wxUSE_GUI + long timeout; int state = -1; @@ -584,9 +609,11 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, // Activate timer if (timeout) { +#if wxUSE_GUI timer.m_state = &state; timer.m_new_val = 0; - timer.Start((int)timeout, TRUE); + runTimer.Start((int)timeout, TRUE); +#endif // wxUSE_GUI } // Active polling (without using events) @@ -608,7 +635,6 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, // Incoming connection (server) or connection established (client) if (result & GSOCK_CONNECTION_FLAG) { - timer.Stop(); m_connected = TRUE; m_establishing = FALSE; return TRUE; @@ -617,14 +643,12 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, // Data available or output buffer ready if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG)) { - timer.Stop(); return TRUE; } // Connection lost if (result & GSOCK_LOST_FLAG) { - timer.Stop(); m_connected = FALSE; m_establishing = FALSE; return (flags & GSOCK_LOST_FLAG); @@ -637,7 +661,6 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, PROCESS_EVENTS(); } - timer.Stop(); return FALSE; } @@ -872,11 +895,13 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt) event.m_skevt = req_evt; if (m_evt_handler) + { #if USE_DELAYED_EVENTS wxPostEvent(m_evt_handler, event); #else ProcessEvent(event); #endif + } OldOnNotify(req_evt); if (m_cbk) @@ -1114,7 +1139,8 @@ bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds) if (!m_establishing || !m_socket) // No connection in progress return FALSE; - return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG); + return _Wait(seconds, milliseconds, GSOCK_CONNECTION_FLAG | + GSOCK_LOST_FLAG); } // ==========================================================================