Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

cabal_udpsocket.h

00001 /*
00002    cabal_udpsocket.h
00003    Basic UDP socket object for cabal
00004 
00005    $Id: cabal_udpsocket.h,v 1.4 2004/04/10 15:28:16 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : Thu, 11 Mar 2004 03:05:18 +0100
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 #ifndef CABAL_UDPSOCKET_H
00015 #define CABAL_UDPSOCKET_H
00016 
00017 #ifdef _MSC_VER
00018    #pragma warning ( disable : 4786 )
00019 #endif
00020 
00021 #include <cabal_socket.h>
00022 
00023 namespace Cabal
00024 {
00025 
00031 class UDPSocket: public Socket
00032 {
00033    void create( const Address *local=0, const Address *remote=0 ) throw( InitError );
00034    void localCreate() throw( InitError )
00035    {
00036       m_skCom = Net::createUDPSocket();
00037       if ( ! Net::validSocket( m_skCom ) )
00038          throw InitError( "Low level socket not created", Net::errorCode() );
00039    
00040       Net::setNonBlocking( m_skCom );
00041    }
00042    
00043    void localBind() throw( InitError )
00044    {
00045       if( ! Net::bind( m_skCom, m_local.address() ) )
00046          throw InitError( "Can't bind local address.", Net::errorCode() );
00047    }
00048 
00049 protected:
00050    virtual int rawRecv( void *data, const int len )
00051    {
00052       int remlen = sizeof( SockAddrInet );   
00053       // Teorethically, we should not always use varAddress; recvFrom may return 0, and it
00054       // would not change the address in remote. But having to recalculate the remote
00055       // 
00056       return Net::recvfrom( m_skCom, data, len, 0, &(m_remote.varAddress()), &remlen );
00057    }
00058 
00059 
00060    virtual int rawSend( const void *data, const int len )
00061    {
00062       return Net::sendto( m_skCom, static_cast<const char *>(data), len, 0,
00063          &(m_remote.address()), sizeof( SockAddrInet ) );
00064    }
00065 
00066    
00067 public:
00068    UDPSocket( const long timeout, const Address *local=0, const Address *remote=0 ) throw( InitError )
00069          :Socket( ST_UDP ) 
00070    {
00071       create( local, remote );
00072       m_timeout = timeout;
00073    }
00074    
00075    UDPSocket( const Address *local=0, const Address *remote=0  ) throw( InitError )
00076          :Socket( ST_UDP ) 
00077    {
00078       create( local, remote );
00079       m_timeout = -1;
00080    }
00081    
00082    UDPSocket( const char *ip_local, const int port_local ) throw( InitError )
00083          :Socket( ST_UDP, ip_local, port_local ) 
00084    {
00085       localCreate();
00086       localBind();
00087       m_timeout = -1;
00088    }
00089    
00090    UDPSocket( const char *ip_local, const int port_local, 
00091               const char *ip_remote, const int port_remote  ) throw( InitError )
00092          :Socket( ST_UDP, ip_local, port_local, ip_remote, port_remote ) 
00093    {
00094       localCreate();
00095       localBind();
00096       m_timeout = -1;
00097    }
00098    
00106    bool setBroadcast();
00107    
00112    int sendTo( const void *data, const int count, const Address &remote);      
00113    
00118    int recvFrom( void *data, const int count, Address &remote);
00119 };
00120 
00121 }
00122 
00123 #endif
00124 
00125 /* end of cabal_udpsocket.h */

Generated on Sat Apr 10 17:41:49 2004 for Cabal by doxygen 1.3.5