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

cabal_udpsocket.cpp

00001 /*
00002    CABAL Cached Balanced Lan
00003    UDPSocket class implementation
00004 
00005    $Id: cabal_udpsocket.cpp,v 1.4 2004/03/29 14:17:28 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : Thu, 11 Mar 2004 03:32:52 +0100
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 
00015 #include <cabal_udpsocket.h>
00016 #include <fcntl.h>
00017 #include <string>
00018 #include <cassert>
00019 
00020 namespace Cabal {
00021 
00022 void UDPSocket::create( const Address *local, const Address *remote  ) throw( InitError )
00023 {
00024    localCreate();
00025 
00026    if ( local ) {
00027       m_local = *local;
00028       localBind();
00029    }
00030    
00031    if ( remote )
00032       m_remote = *remote;
00033 }
00034 
00035 bool UDPSocket::setBroadcast()
00036 {
00037    int iOpt = 1;
00038    if ( setsockopt( m_skCom, SOL_SOCKET,
00039          SO_BROADCAST, (const char *) &iOpt, sizeof( iOpt )) == 0 )
00040       return true;
00041       
00042    m_osError = Net::errorCode();
00043    return false;
00044 }
00045 
00046 
00047 int UDPSocket::sendTo( const void *data, const int count, const Address &remote )
00048 {
00049    if( selectFor( true ) ) //write
00050    {
00051       int len = Net::sendto( m_skCom, data, count, MSG_NOSIGNAL, &(remote.address()), sizeof(SockAddrInet)  );
00052 
00053       if ( len <  0 ) {
00054          m_osError = Net::errorCode();
00055       }
00056       else
00057          m_osError = 0;
00058       m_bTout = false;
00059       return len;
00060    }
00061    else {
00062       m_osError = 0;
00063       m_bTout = true;
00064       return 0;
00065    }
00066 }
00067 
00068 int UDPSocket::recvFrom( void *data, const int count, Address &remote )
00069 {
00070    if( selectFor( false ) ) //read
00071    {
00072       int size = sizeof(SockAddrInet);
00073       int len = Net::recvfrom( m_skCom, data, count, MSG_NOSIGNAL, &(remote.varAddress()), &size  );
00074 
00075       if ( len <  0 ) {
00076          m_osError = Net::errorCode();
00077       }
00078       else
00079          m_osError = 0;
00080       m_bTout = false;
00081       return len;
00082    }
00083    else {
00084       m_osError = 0;
00085       m_bTout = true;
00086       return 0;
00087    }
00088 }
00089 
00090 };
00091 
00092 /* end of cabal_udpsocket.cpp */

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