From 400f12a9d100d48509a48188fcff7f1b820d5826 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 20 Jan 2011 15:36:24 +0000 Subject: Ummm...it's a udpsocket, mostly. It seems like it should work, but early tests are failing... --- src/tests/udpsocket.cpp | 41 +++++++++++++++++++++++++ src/udpsocket.cpp | 79 ++++++++++++++++++++++++------------------------- src/udpsocket.h | 13 +++++++- 3 files changed, 91 insertions(+), 42 deletions(-) create mode 100644 src/tests/udpsocket.cpp diff --git a/src/tests/udpsocket.cpp b/src/tests/udpsocket.cpp new file mode 100644 index 0000000..63aa576 --- /dev/null +++ b/src/tests/udpsocket.cpp @@ -0,0 +1,41 @@ +#include "bu/udpsocket.h" +#include "bu/sio.h" + +using namespace Bu; + +int main( int argc, char *argv[] ) +{ + if( argv[1][0] == 'l' ) + { + sio << "Listening..." << sio.nl; + Bu::UdpSocket udp( "255.255.255.255", 6688, UdpSocket::Read|UdpSocket::Broadcast ); + + for(;;) + { + char buf[1501]; + int iRead = udp.read( buf, 1500 ); + buf[iRead] = '\0'; + sio << "Read(" << iRead << "): '" << buf << "'"; + } + } + else if( argv[1][0] == 'b' ) + { + sio << "Broadcasting..." << sio.nl; + Bu::UdpSocket udp("255.255.255.255", 6688, + UdpSocket::Write|UdpSocket::Broadcast ); + + for(;;) + { + udp.write("hello", 5 ); + usleep( 250000 ); + } + } + else + { + sio << "Options are 'l' for listening and 'b' for broadcasting." + << sio.nl; + } + + return 0; +} + diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp index d836690..7b03b78 100644 --- a/src/udpsocket.cpp +++ b/src/udpsocket.cpp @@ -8,17 +8,20 @@ namespace Bu { subExceptionDef( UdpSocketException ) } +#define saTarget ( *((struct sockaddr_in *)paTarget) ) + Bu::UdpSocket::UdpSocket( int iUdpSocket ) : - iUdpSocket( iUdpSocket ) + iUdpSocket( iUdpSocket ), + paTarget( NULL ), + bBound( false ) { } -Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, - bool bBroadcast ) : - iUdpSocket( 0 ) +Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) : + iUdpSocket( 0 ), + paTarget( NULL ), + bBound( false ) { - struct sockaddr_in name; - iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 ); if( iUdpSocket < 0 ) { @@ -27,7 +30,7 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, ); } - if( bBroadcast ) + if( (iFlags&Broadcast) ) { int broadcast = 1; if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST, @@ -39,42 +42,29 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, } } - name.sin_family = AF_INET; - name.sin_port = htons( iPort ); - name.sin_addr.s_addr = INADDR_ANY; - memset( name.sin_zero, '\0', sizeof(name.sin_zero) ); - - if( bind( iUdpSocket, (struct sockaddr*) &name, sizeof(name) ) == -1 ) - { - throw UdpSocketException("Couldn't bind port to udp socket: %s", - strerror( errno ) - ); - } + paTarget = new struct sockaddr_in; + saTarget.sin_family = AF_INET; + saTarget.sin_port = htons( iPort ); + saTarget.sin_addr.s_addr = inet_addr( sAddr.getStr() ); // INADDR_ANY; + memset( saTarget.sin_zero, '\0', sizeof(saTarget.sin_zero) ); - name.sin_family = AF_INET; - name.sin_port = htons( iPort ); - name.sin_addr.s_addr = inet_addr( sAddr.getStr() ); - memset( name.sin_zero, '\0', sizeof(name.sin_zero) ); -/* - while( true ) + if( (iFlags&Read) ) { - nbytes = sendto( iUdpSocket, "12345", 5, 0, - (struct sockaddr *)&name, size ); - if( nbytes < 0 ) - { - perror("sendto"); -// exit( 0 ); - } - - printf("Client wrote something\n"); - int nQueen = sockServe.accept( 3, 0 ); - if( nQueen >= 0 ) + if( bind( iUdpSocket, (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) ) + == -1 ) { - close( iUdpSocket ); - return nQueen; + throw UdpSocketException("Couldn't bind port to udp socket: %s", + strerror( errno ) + ); } + bBound = true; } - */ +} + +Bu::UdpSocket::~UdpSocket() +{ + delete (struct sockaddr_in *)paTarget; + paTarget = NULL; } void Bu::UdpSocket::close() @@ -84,6 +74,7 @@ void Bu::UdpSocket::close() size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes ) { + return recvfrom( iUdpSocket, pBuf, nBytes, 0, NULL, 0 ); } size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, @@ -93,9 +84,15 @@ size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) { -// name, the destination address, needs to be a class variable... -// return sendto( iUdpSocket, pBuf, nBytes, 0, -// (struct sockaddr *)&name, size ); + if( bBound ) + { + return sendto( iUdpSocket, pBuf, nBytes, 0, NULL, 0 ); + } + else + { + return sendto( iUdpSocket, pBuf, nBytes, 0, + (struct sockaddr*)paTarget, sizeof(struct sockaddr_in) ); + } } size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes, diff --git a/src/udpsocket.h b/src/udpsocket.h index 4452458..0f81c38 100644 --- a/src/udpsocket.h +++ b/src/udpsocket.h @@ -13,7 +13,7 @@ namespace Bu { public: UdpSocket( int iUdpSocket ); - UdpSocket( const Bu::FString &sAddr, int iPort, bool bBroadcast ); + UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ); virtual ~UdpSocket(); virtual void close(); @@ -46,12 +46,23 @@ namespace Bu virtual void setSize( long iSize ); + enum { + // Flags + Read = 0x01, ///< Open udp socket for reading + Write = 0x02, ///< Open udp socket for writing + ReadWrite = 0x03, ///< Open for both read and write + Broadcast = 0x04, ///< Open for broadcast + }; + + private: #ifdef WIN32 unsigned int iUdpSocket; #else int iUdpSocket; #endif + void *paTarget; + bool bBound; }; }; -- cgit v1.2.3