#include "wx/sckaddr.h"
#include "wx/socket.h"
-#include "wx/stopwatch.h"
+#include "wx/datetime.h"
// DLL options compatibility check:
#include "wx/build.h"
public:
wxSocketState() : wxObject() {}
- DECLARE_NO_COPY_CLASS(wxSocketState)
+ DECLARE_NO_COPY_CLASS(wxSocketState)
};
// ==========================================================================
{
// we should be initialized
wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") );
- if ( !--m_countInit )
+ if ( --m_countInit == 0 )
{
GSocket_Cleanup();
}
else
timeout = m_timeout * 1000;
-#if !defined(wxUSE_GUI) || !wxUSE_GUI
- m_socket->SetTimeout(timeout);
-#endif
+ bool has_event_loop = wxTheApp ? (wxTheApp->GetTraits() ? true : false) : false;
// Wait in an active polling loop.
//
// Do this at least once (important if timeout == 0, when
// we are just polling). Also, if just polling, do not yield.
- wxStopWatch chrono;
+ wxDateTime current_time = wxDateTime::UNow();
+ unsigned int time_limit = (current_time.GetTicks() * 1000) + current_time.GetMillisecond() + timeout;
bool done = false;
+ bool valid_result = false;
+
+ if (!has_event_loop)
+ {
+ // This is used to avoid a busy loop on wxBase - having a select
+ // timeout of 50 ms per iteration should be enough.
+ if (timeout > 50)
+ m_socket->SetTimeout(50);
+ else
+ m_socket->SetTimeout(timeout);
+ }
while (!done)
{
{
m_connected = true;
m_establishing = false;
- return true;
+ valid_result = true;
+ break;
}
// Data available or output buffer ready
if ((result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG))
{
- return true;
+ valid_result = true;
+ break;
}
// Connection lost
{
m_connected = false;
m_establishing = false;
- return (flags & GSOCK_LOST_FLAG) != 0;
+ valid_result = ((flags & GSOCK_LOST_FLAG) != 0);
+ break;
}
// Wait more?
- if ((!timeout) || (chrono.Time() > timeout) || (m_interrupt))
+ current_time = wxDateTime::UNow();
+ int time_left = time_limit - ((current_time.GetTicks() * 1000) + current_time.GetMillisecond());
+ if ((!timeout) || (time_left <= 0) || (m_interrupt))
done = true;
else
- PROCESS_EVENTS();
+ {
+ if (has_event_loop)
+ {
+ PROCESS_EVENTS();
+ }
+ else
+ {
+ // If there's less than 50 ms left, just call select with that timeout.
+ if (time_left < 50)
+ m_socket->SetTimeout(time_left);
+ }
+ }
}
- return false;
+ // Set timeout back to original value (we overwrote it for polling)
+ if (!has_event_loop)
+ m_socket->SetTimeout(m_timeout*1000);
+
+ return valid_result;
}
bool wxSocketBase::Wait(long seconds, long milliseconds)
return true;
// Note that GSOCK_INPUT_LOST has to be explicitly passed to
- // _Wait becuase of the semantics of WaitForRead: a return
+ // _Wait because of the semantics of WaitForRead: a return
// value of true means that a GSocket_Read call will return
// immediately, not that there is actually data to read.
// Ctor
// --------------------------------------------------------------------------
-wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
+wxSocketServer::wxSocketServer(const wxSockAddress& addr_man,
wxSocketFlags flags)
: wxSocketBase(flags, wxSOCKET_SERVER)
{
return true;
}
+bool wxSocketBase::SetLocal(wxIPV4address& local)
+{
+ GAddress* la = local.GetAddress();
+
+ // If the address is valid, save it for use when we call Connect
+ if (la && la->m_addr)
+ {
+ m_localAddress = local;
+
+ return true;
+ }
+
+ return false;
+}
+
// ==========================================================================
// wxSocketClient
// ==========================================================================
// Connect
// --------------------------------------------------------------------------
-bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
+bool wxSocketClient::DoConnect(wxSockAddress& addr_man, wxSockAddress* local, bool wait)
{
GSocketError err;
if (!wait)
m_socket->SetNonBlocking(1);
+ // Reuse makes sense for clients too, if we are trying to rebind to the same port
+ if (GetFlags() & wxSOCKET_REUSEADDR)
+ {
+ m_socket->SetReusable();
+ }
+
+ // If no local address was passed and one has been set, use the one that was Set
+ if (!local && m_localAddress.GetAddress())
+ {
+ local = &m_localAddress;
+ }
+
+ // Bind to the local IP address and port, when provided
+ if (local)
+ {
+ GAddress* la = local->GetAddress();
+
+ if (la && la->m_addr)
+ m_socket->SetLocal(la);
+ }
+
m_socket->SetPeer(addr_man.GetAddress());
err = m_socket->Connect(GSOCK_STREAMED);
return true;
}
+bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
+{
+ return (DoConnect(addr_man, NULL, wait));
+}
+
+bool wxSocketClient::Connect(wxSockAddress& addr_man, wxSockAddress& local, bool wait)
+{
+ return (DoConnect(addr_man, &local, wait));
+}
+
bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds)
{
if (m_connected) // Already connected
/* NOTE: experimental stuff - might change */
-wxDatagramSocket::wxDatagramSocket( wxSockAddress& addr,
+wxDatagramSocket::wxDatagramSocket( const wxSockAddress& addr,
wxSocketFlags flags )
: wxSocketBase( flags, wxSOCKET_DATAGRAM )
{
return (*this);
}
-wxDatagramSocket& wxDatagramSocket::SendTo( wxSockAddress& addr,
+wxDatagramSocket& wxDatagramSocket::SendTo( const wxSockAddress& addr,
const void* buf,
wxUint32 nBytes )
{