#include "lt/udpbroad.hh"
namespace LT {
/// Constructs a new broadcaster from scratch.
UDP_Broadcaster::UDP_Broadcaster() : a_(0), b_(0)
{
LT_TRACE( "UDP_Broadcaster::UDP_Broadcaster" );
open();
}
/// Clean up this broadcaster, and the address and socket
/// it manages.
/// Delete the broadcast socket first, because it contains a reference
/// to the INET_Addr at a.
UDP_Broadcaster::~UDP_Broadcaster()
{
LT_TRACE( "UDP_Broadcaster::~UDP_Broadcaster" );
delete b_;
delete a_;
}
/// Initialize a new broadcaster and make it ready to
/// support send().
/// Creates a new source address but doesn't "fill in" any of its
/// attributes; ACE will default this address to "any port", the
/// INET_ANY address, and so on. Any debug messages sent to the ACE
/// Logger during the construction of the underlying SOCK_Bcast_Dgram
/// will be suppressed; this works around the silly OS X warnings that
/// are harmless, but distracting:
/// SOCK_Dgram_Bcast's open() will call an internal function
/// mk_broadcast() that creates a list of BCast_nodes representing
/// every network interface on the system. However, Mac OS X will
/// return all kinds of interfaces in its network layer, including
/// things that ACE doesn't really understand (like AF_Appletalk).
/// The resulting warning messages (sent to the logger as LM_DEBUG
/// messages) are harmless, but a pain in the neck. So, we'll
/// temporarily mask off the Logger's debug messages and hope we don't
/// miss anything important during open().
bool
UDP_Broadcaster::open()
{
LT_TRACE( "UDP_Broadcaster::open" );
u_long o = ACE_LOG_MSG->priority_mask( ACE_Log_Msg::PROCESS );
u_long n = o & ~LM_DEBUG;
ACE_LOG_MSG->priority_mask( n, ACE_Log_Msg::PROCESS );
ACE_NEW_RETURN( a_, ACE_INET_Addr, false );
ACE_NEW_RETURN( b_, ACE_SOCK_Dgram_Bcast( *a_ ), false );
ACE_LOG_MSG->priority_mask( o, ACE_Log_Msg::PROCESS );
return true;
}
/// Broadcast some data to all connected networks, using the
/// given port number. Returns the average number of bytes sent
/// across all interfaces.
/// Really, all we need to do is pass our arguments to the
/// SOCK_Dgram_Bcast we're hiding
/// behind our facade.
ssize_t
UDP_Broadcaster::send( const void *buf,
size_t buflen,
u_short portnum,
int flags ) const
{
LT_TRACE( "UDP_Broadcaster::send" );
return b_->send( buf, buflen, portnum, flags );
}
}; |