r1 - 02 Feb 2007 - 00:42:39 - BobKrzaczekYou are here: TWiki >  Sandbox Web  >  TWikiUsers > BobKrzaczek > KrzSyntax
Tags:
create new tag
, view all tags

udpbroad.hh

#ifndef _udpbroad_hh
#define _udpbroad_hh
#include <lt/LT_export.hh>
#include <ace/INET_Addr.h>
#include <ace/SOCK_Dgram_Bcast.h>
#include <ace/Log_Msg.h>

namespace LT {

/// UDP_Brocaster provides a facade for broadcast sockets. Under
/// Mac OS X (and possibly
/// others), the datagram broadcasting socket has an interesting
/// quirk: because it's written to broadcast some data from every
/// single interface on the local machine, it tries to set up
/// Bcast_nodes for all kinds of protocols that it can't yet currently
/// support (\em e.g., Appletalk, IPv6).  The errors that result are
/// harmless, but get sent to the ACE logger as debug messages.
/// Therefore, we'll create this class to manage the socket and its
/// source address for us, and does so with the logger's debug
/// messages disabled when necessary.  I imagine this can go away in
/// some future release of ACE.
class LT_Export UDP_Broadcaster 
{
private:
     ACE_INET_Addr *a_;         ///< The local source address for broadcasts.
     ACE_SOCK_Dgram_Bcast *b_;  ///< The broadcasting socket we manage.
public:
     UDP_Broadcaster();
     ~UDP_Broadcaster();
     bool open();
     ssize_t send( const void *buf,
                   size_t buflen,
                   u_short portnum,
                   int flags = 0 ) const;
};

};      // namespace LT
#endif

udpbroad.cc

#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 );
}

};
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions

tip TWiki Tip of the Day
Inline search
To embed a search in a topic, use the %SEARCH{ parameters }% TWiki Variable. The search variable ... Read on Read more

 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback