00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00054
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