Add SetLocal method; overload Connect to accept a local address and move common Connect code into DoConnect; DoConnect handles wxSOCKET_REUSEADDR flag and setting local address [ Heavily modified and expanded patch 1415505 ]
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37506 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -121,6 +121,7 @@ public:
|
|||||||
// addresses
|
// addresses
|
||||||
virtual bool GetLocal(wxSockAddress& addr_man) const;
|
virtual bool GetLocal(wxSockAddress& addr_man) const;
|
||||||
virtual bool GetPeer(wxSockAddress& addr_man) const;
|
virtual bool GetPeer(wxSockAddress& addr_man) const;
|
||||||
|
virtual bool SetLocal(wxSockAddress& local);
|
||||||
|
|
||||||
// base IO
|
// base IO
|
||||||
virtual bool Close();
|
virtual bool Close();
|
||||||
@@ -217,7 +218,7 @@ private:
|
|||||||
// the initialization count, GSocket is initialized if > 0
|
// the initialization count, GSocket is initialized if > 0
|
||||||
static size_t m_countInit;
|
static size_t m_countInit;
|
||||||
|
|
||||||
DECLARE_NO_COPY_CLASS(wxSocketBase)
|
DECLARE_NO_COPY_CLASS(wxSocketBase)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -254,9 +255,13 @@ public:
|
|||||||
virtual ~wxSocketClient();
|
virtual ~wxSocketClient();
|
||||||
|
|
||||||
virtual bool Connect(wxSockAddress& addr, bool wait = true);
|
virtual bool Connect(wxSockAddress& addr, bool wait = true);
|
||||||
|
virtual bool Connect(wxSockAddress& addr, wxSockAddress& local, bool wait = true);
|
||||||
|
|
||||||
bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
|
bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool DoConnect(wxSockAddress& addr, wxSockAddress* local, bool wait = true);
|
||||||
|
|
||||||
DECLARE_NO_COPY_CLASS(wxSocketClient)
|
DECLARE_NO_COPY_CLASS(wxSocketClient)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -1198,6 +1198,20 @@ bool wxSocketBase::SetOption(int level, int optname, const void *optval,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool wxSocketBase::SetLocal(wxSockAddress& local)
|
||||||
|
{
|
||||||
|
GAddress* la = local.GetAddress();
|
||||||
|
|
||||||
|
if (la && la->m_addr)
|
||||||
|
{
|
||||||
|
m_socket->SetLocal(la);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
// wxSocketClient
|
// wxSocketClient
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
@@ -1219,7 +1233,7 @@ wxSocketClient::~wxSocketClient()
|
|||||||
// Connect
|
// Connect
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
|
bool wxSocketClient::DoConnect(wxSockAddress& addr_man, wxSockAddress* local, bool wait)
|
||||||
{
|
{
|
||||||
GSocketError err;
|
GSocketError err;
|
||||||
|
|
||||||
@@ -1249,6 +1263,21 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
|
|||||||
if (!wait)
|
if (!wait)
|
||||||
m_socket->SetNonBlocking(1);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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());
|
m_socket->SetPeer(addr_man.GetAddress());
|
||||||
err = m_socket->Connect(GSOCK_STREAMED);
|
err = m_socket->Connect(GSOCK_STREAMED);
|
||||||
|
|
||||||
@@ -1267,6 +1296,16 @@ bool wxSocketClient::Connect(wxSockAddress& addr_man, bool wait)
|
|||||||
return true;
|
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)
|
bool wxSocketClient::WaitOnConnect(long seconds, long milliseconds)
|
||||||
{
|
{
|
||||||
if (m_connected) // Already connected
|
if (m_connected) // Already connected
|
||||||
|
Reference in New Issue
Block a user