From c259f95bd0e58b247940a339bb9b4b401b4e9438 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 20 Jan 2011 17:48:45 +0000 Subject: UdpSocket is pretty much working. Non-blocking mode isn't, which is odd, but we can figure that out later. --- src/tests/udpsocket.cpp | 43 +++++++++++++++++++++++--- src/udpsocket.cpp | 80 +++++++++++++++++++++++++++++++++++++++++-------- src/udpsocket.h | 8 +++-- 3 files changed, 112 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/tests/udpsocket.cpp b/src/tests/udpsocket.cpp index 3038560..18c7bb9 100644 --- a/src/tests/udpsocket.cpp +++ b/src/tests/udpsocket.cpp @@ -2,22 +2,57 @@ #include "bu/sio.h" #include +#include +#include +#include +#include using namespace Bu; int main( int argc, char *argv[] ) { - if( argv[1][0] == 'l' ) + sio << Fmt::hex(8) << INADDR_ANY << sio.nl << Fmt::hex(8) << inet_addr("0.0.0.0") << sio.nl; + if( argc == 1 ) + { + sio << "Options are 'l' for listening and 'b' for broadcasting." + << sio.nl; + } + else if( argv[1][0] == 'l' ) { sio << "Listening..." << sio.nl; - Bu::UdpSocket udp( "255.255.255.255", 6688, UdpSocket::Read|UdpSocket::Broadcast ); + Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast ); for(;;) { char buf[1501]; int iRead = udp.read( buf, 1500 ); - buf[iRead] = '\0'; - sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl; + if( iRead >= 0 ) + { + buf[iRead] = '\0'; + sio << "Read(" << iRead << "): '" << buf << "'" << sio.nl; + } + else + { + sio << "Got " << iRead << ": " << strerror( errno ) << sio.nl; + } + } + } + else if( argv[1][0] == 'L' ) + { + sio << "Listening..." << sio.nl; + Bu::UdpSocket udp( "0.0.0.0", 6688, UdpSocket::Read|UdpSocket::Broadcast ); + + for(;;) + { + char buf[1501]; + Bu::UdpSocket::addr aHost; + int iPort; + int iRead = udp.read( buf, 1500, aHost, iPort ); + if( iRead >= 0 ) + { + buf[iRead] = '\0'; + sio << "Read(" << iRead << ") from " << Bu::UdpSocket::addrToStr( aHost ) << ":" << iPort << ": '" << buf << "'" << sio.nl; + } } } else if( argv[1][0] == 'b' ) diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp index 7b03b78..da57b8d 100644 --- a/src/udpsocket.cpp +++ b/src/udpsocket.cpp @@ -1,5 +1,9 @@ #include "bu/udpsocket.h" +#include "bu/sio.h" +using namespace Bu; +#include + #include #include #include @@ -41,7 +45,7 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) : ); } } - + paTarget = new struct sockaddr_in; saTarget.sin_family = AF_INET; saTarget.sin_port = htons( iPort ); @@ -63,10 +67,24 @@ Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ) : Bu::UdpSocket::~UdpSocket() { + close(); delete (struct sockaddr_in *)paTarget; paTarget = NULL; } +Bu::FString Bu::UdpSocket::addrToStr( const addr &a ) +{ + Bu::FString sOut; + sOut.format("%d.%d.%d.%d", + (a&0xff), + (a&0xff00)>>8, + (a&0xff0000)>>16, + (a&0xff000000)>>24 + ); + + return sOut; +} + void Bu::UdpSocket::close() { ::close( iUdpSocket ); @@ -74,12 +92,19 @@ void Bu::UdpSocket::close() size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes ) { - return recvfrom( iUdpSocket, pBuf, nBytes, 0, NULL, 0 ); + return recv( iUdpSocket, pBuf, nBytes, 0 ); } size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, - uint32_t nSec, uint32_t nUSec ) + Bu::UdpSocket::addr &aHost, int &iPort ) { + sockaddr_in name; + size_t size = sizeof(name); + size_t ret = recvfrom( iUdpSocket, pBuf, nBytes, 0, + (struct sockaddr *)&name, &size ); + aHost = name.sin_addr.s_addr; + iPort = ntohs(name.sin_port); + return ret; } size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) @@ -95,33 +120,34 @@ size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) } } -size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes, - uint32_t nSec, uint32_t nUSec ) -{ -} - long Bu::UdpSocket::tell() { + throw Bu::UnsupportedException(); } -void Bu::UdpSocket::seek( long offset ) +void Bu::UdpSocket::seek( long ) { + throw Bu::UnsupportedException(); } -void Bu::UdpSocket::setPos( long pos ) +void Bu::UdpSocket::setPos( long ) { + throw Bu::UnsupportedException(); } -void Bu::UdpSocket::setPosEnd( long pos ) +void Bu::UdpSocket::setPosEnd( long ) { + throw Bu::UnsupportedException(); } bool Bu::UdpSocket::isEos() { + return false; } bool Bu::UdpSocket::isOpen() { + return true; } void Bu::UdpSocket::flush() @@ -130,33 +156,63 @@ void Bu::UdpSocket::flush() bool Bu::UdpSocket::canRead() { + return bBound; } bool Bu::UdpSocket::canWrite() { + return true; } bool Bu::UdpSocket::isReadable() { + return bBound; } bool Bu::UdpSocket::isWritable() { + return true; } bool Bu::UdpSocket::isSeekable() { + return false; } bool Bu::UdpSocket::isBlocking() { + return true; } void Bu::UdpSocket::setBlocking( bool bBlocking ) { +#ifndef WIN32 + if( bBlocking ) + { + fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) & (~O_NONBLOCK) ); + } + else + { + fcntl( iUdpSocket, F_SETFL, fcntl( iUdpSocket, F_GETFL, 0 ) | O_NONBLOCK ); + } +#else + u_long iMode; + if( bBlocking ) + iMode = 0; + else + iMode = 1; + //------------------------- + // Set the socket I/O mode: In this case FIONBIO + // enables or disables the blocking mode for the + // socket based on the numerical value of iMode. + // If iMode = 0, blocking is enabled; + // If iMode != 0, non-blocking mode is enabled. + bu_ioctlsocket(iUdpSocket, FIONBIO, &iMode); +#endif } -void Bu::UdpSocket::setSize( long iSize ) +void Bu::UdpSocket::setSize( long ) { + throw Bu::UnsupportedException(); } diff --git a/src/udpsocket.h b/src/udpsocket.h index 0f81c38..253839a 100644 --- a/src/udpsocket.h +++ b/src/udpsocket.h @@ -16,13 +16,15 @@ namespace Bu UdpSocket( const Bu::FString &sAddr, int iPort, int iFlags ); virtual ~UdpSocket(); + typedef uint32_t addr; + + static Bu::FString addrToStr( const addr &a ); + virtual void close(); virtual size_t read( void *pBuf, size_t nBytes ); virtual size_t read( void *pBuf, size_t nBytes, - uint32_t nSec, uint32_t nUSec=0 ); + addr &sHost, int &iPort ); virtual size_t write( const void *pBuf, size_t nBytes ); - virtual size_t write( const void *pBuf, size_t nBytes, - uint32_t nSec, uint32_t nUSec=0 ); using Stream::write; virtual long tell(); -- cgit v1.2.3