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/udpsocket.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 12 deletions(-) (limited to 'src/udpsocket.cpp') 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(); } -- cgit v1.2.3