From 9edd5b50c2b928be9fc7569170103a41cbc80e5f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 14 Jan 2011 23:14:13 +0000 Subject: Here's a udp socket. It's not done yet, it can just about send data. --- src/tcpsocket.h | 1 - src/udpsocket.cpp | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/udpsocket.h | 58 +++++++++++++++++++ 3 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 src/udpsocket.cpp create mode 100644 src/udpsocket.h (limited to 'src') diff --git a/src/tcpsocket.h b/src/tcpsocket.h index 3361e84..ce15172 100644 --- a/src/tcpsocket.h +++ b/src/tcpsocket.h @@ -65,7 +65,6 @@ namespace Bu virtual ~TcpSocket(); virtual void close(); - //virtual void read(); 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 ); diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp new file mode 100644 index 0000000..b612b25 --- /dev/null +++ b/src/udpsocket.cpp @@ -0,0 +1,164 @@ +#include "bu/udpsocket.h" + +#include +#include +#include +#include +#include + +namespace Bu { subExceptionDef( UdpSocketException ) } + +Bu::UdpSocket::UdpSocket( int iUdpSocket ) : + iUdpSocket( iUdpSocket ) +{ +} + +Bu::UdpSocket::UdpSocket( const Bu::FString &sAddr, int iPort, + bool bBroadcast ) : + iUdpSocket( 0 ) +{ + struct sockaddr_in name; + + iUdpSocket = socket( PF_INET, SOCK_DGRAM, 0 ); + if( iUdpSocket < 0 ) + { + throw UdpSocketException("Couldn't open udp socket: %s", + strerror( errno ) + ); + } + + if( bBroadcast ) + { + int broadcast = 1; + if( (setsockopt( iUdpSocket, SOL_SOCKET, SO_BROADCAST, + &broadcast, sizeof(broadcast) )) == -1) + { + throw UdpSocketException("Couldn't set udp socket to broadcast: %s", + strerror( errno ) + ); + } + } + + 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 ) + ); + } + + 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 ) + { + 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 ) + { + close( iUdpSocket ); + return nQueen; + } + } + */ +} + +void Bu::UdpSocket::close() +{ + ::close( iUdpSocket ); +} + +size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes ) +{ +} + +size_t Bu::UdpSocket::read( void *pBuf, size_t nBytes, + uint32_t nSec, uint32_t nUSec=0 ) +{ +} + +size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes ) +{ + return sendto( iUdpSocket, pBuf, nBytes, 0, + (struct sockaddr *)&name, size ); +} + +size_t Bu::UdpSocket::write( const void *pBuf, size_t nBytes, + uint32_t nSec, uint32_t nUSec=0 ) +{ +} + +long Bu::UdpSocket::tell() +{ +} + +void Bu::UdpSocket::seek( long offset ) +{ +} + +void Bu::UdpSocket::setPos( long pos ) +{ +} + +void Bu::UdpSocket::setPosEnd( long pos ) +{ +} + +bool Bu::UdpSocket::isEos() +{ +} + +bool Bu::UdpSocket::isOpen() +{ +} + +void Bu::UdpSocket::flush() +{ +} + +bool Bu::UdpSocket::canRead() +{ +} + +bool Bu::UdpSocket::canWrite() +{ +} + +bool Bu::UdpSocket::isReadable() +{ +} + +bool Bu::UdpSocket::isWritable() +{ +} + +bool Bu::UdpSocket::isSeekable() +{ +} + +bool Bu::UdpSocket::isBlocking() +{ +} + +void Bu::UdpSocket::setBlocking( bool bBlocking=true ) +{ +} + +void Bu::UdpSocket::setSize( long iSize ) +{ +} + diff --git a/src/udpsocket.h b/src/udpsocket.h new file mode 100644 index 0000000..4452458 --- /dev/null +++ b/src/udpsocket.h @@ -0,0 +1,58 @@ +#ifndef BU_UDP_SOCKET_H +#define BU_UDP_SOCKET_H + +#include + +#include "bu/stream.h" + +namespace Bu +{ + subExceptionDecl( UdpSocketException ); + + class UdpSocket : public Stream + { + public: + UdpSocket( int iUdpSocket ); + UdpSocket( const Bu::FString &sAddr, int iPort, bool bBroadcast ); + virtual ~UdpSocket(); + + 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 ); + 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(); + virtual void seek( long offset ); + virtual void setPos( long pos ); + virtual void setPosEnd( long pos ); + virtual bool isEos(); + virtual bool isOpen(); + + virtual void flush(); + + virtual bool canRead(); + virtual bool canWrite(); + + virtual bool isReadable(); + virtual bool isWritable(); + virtual bool isSeekable(); + + virtual bool isBlocking(); + virtual void setBlocking( bool bBlocking=true ); + + virtual void setSize( long iSize ); + + private: +#ifdef WIN32 + unsigned int iUdpSocket; +#else + int iUdpSocket; +#endif + }; +}; + +#endif -- cgit v1.2.3