added support for broadcasting to UDP sockets (patch 1740266)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47126 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-07-04 21:33:11 +00:00
parent bf511d579f
commit 60edcf4535
14 changed files with 180 additions and 23 deletions

View File

@@ -177,6 +177,8 @@ GSocket::GSocket()
m_timeout.tv_usec = 0;
m_establishing = false;
m_reusable = false;
m_broadcast = false;
m_dobind = true;
assert(gs_gui_functions);
/* Per-socket GUI-specific initialization */
@@ -529,6 +531,34 @@ bool GSocket::SetReusable()
return false;
}
/* GSocket_SetBroadcast:
* Simply sets the m_broadcast flag on the socket. GSocket_SetServer will
* make the appropriate setsockopt() call.
* Implemented as a GSocket function because clients (ie, wxSocketServer)
* don't have access to the GSocket struct information.
* Returns true if the flag was set correctly, false if an error occurred
* (ie, if the parameter was NULL)
*/
bool GSocket::SetBroadcast()
{
/* socket must not be in use/already bound */
if (m_fd == INVALID_SOCKET) {
m_broadcast = true;
return true;
}
return false;
}
bool GSocket::DontDoBind()
{
/* socket must not be in use/already bound */
if (m_fd == INVALID_SOCKET) {
m_dobind = false;
return true;
}
return false;
}
/* Client specific parts */
/* GSocket_Connect:
@@ -706,18 +736,24 @@ GSocketError GSocket::SetNonOriented()
{
setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
}
/* Bind to the local address,
* and retrieve the actual address bound.
*/
if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
(getsockname(m_fd,
m_local->m_addr,
(WX_SOCKLEN_T *)&m_local->m_len) != 0))
if (m_broadcast)
{
Close();
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST, (const char*)&arg, sizeof(arg));
}
if (m_dobind)
{
/* Bind to the local address,
* and retrieve the actual address bound.
*/
if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
(getsockname(m_fd,
m_local->m_addr,
(WX_SOCKLEN_T *)&m_local->m_len) != 0))
{
Close();
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
}
return GSOCK_NOERROR;
@@ -1413,6 +1449,11 @@ GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
return GSOCK_NOERROR;
}
GSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
{
return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
}
GSocketError GAddress_INET_SetAnyAddress(GAddress *address)
{
return GAddress_INET_SetHostAddress(address, INADDR_ANY);