From b4ab27db8a97f0a56a61bd56480cfbe08a1f3399 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 26 Mar 2009 17:00:35 +0000 Subject: Added some more constructors to the ServerSocket, and also switched the write function in Bu::Formatter to take a void *, what was I thinking? --- src/formatter.cpp | 2 +- src/formatter.h | 2 +- src/serversocket.cpp | 37 +++++++++++++++++++++++++++++++++++-- src/serversocket.h | 3 +++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/formatter.cpp b/src/formatter.cpp index 6eb3b86..2fadd96 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp @@ -16,7 +16,7 @@ void Bu::Formatter::write( const Bu::FString &sStr ) rOut.write( sStr ); } -void Bu::Formatter::write( const char *sStr, int iLen ) +void Bu::Formatter::write( const void *sStr, int iLen ) { rOut.write( sStr, iLen ); } diff --git a/src/formatter.h b/src/formatter.h index 693c3ed..5dd7622 100644 --- a/src/formatter.h +++ b/src/formatter.h @@ -83,7 +83,7 @@ namespace Bu } Fmt; void write( const Bu::FString &sStr ); - void write( const char *sStr, int iLen ); + void write( const void *sStr, int iLen ); void writeAligned( const Bu::FString &sStr ); void writeAligned( const char *sStr, int iLen ); diff --git a/src/serversocket.cpp b/src/serversocket.cpp index 89fef7e..b3a8c2d 100644 --- a/src/serversocket.cpp +++ b/src/serversocket.cpp @@ -65,9 +65,37 @@ Bu::ServerSocket::ServerSocket(const FString &sAddr,int nPort, int nPoolSize) : startServer( name, nPoolSize ); } +Bu::ServerSocket::ServerSocket( int nServer, bool bInit, int nPoolSize ) : + nServer( nServer ), + nPort( 0 ) +{ + if( bInit ) + { + struct sockaddr name; + socklen_t namelen = sizeof(name); + getpeername( nServer, &name, &namelen ); + + initServer( *((sockaddr_in *)&name), nPoolSize ); + } + else + { + FD_ZERO( &fdActive ); + FD_SET( nServer, &fdActive ); + } +} + +Bu::ServerSocket::ServerSocket( const ServerSocket &rSrc ) +{ + nServer = dup( rSrc.nServer ); + nPort = rSrc.nPort; + FD_ZERO( &fdActive ); + FD_SET( nServer, &fdActive ); +} + Bu::ServerSocket::~ServerSocket() { - ::close( nServer ); + if( nServer > -1 ) + ::close( nServer ); } void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) @@ -79,7 +107,7 @@ void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) { throw Bu::ServerSocketException("Couldn't create a listen socket."); } - + int opt = 1; bu_setsockopt( nServer, @@ -89,6 +117,11 @@ void Bu::ServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) sizeof( opt ) ); + initServer( name, nPoolSize ); +} + +void Bu::ServerSocket::initServer( struct sockaddr_in &name, int nPoolSize ) +{ if( bu_bind( nServer, (struct sockaddr *) &name, sizeof(name) ) < 0 ) { throw Bu::ServerSocketException("Couldn't bind to the listen socket."); diff --git a/src/serversocket.h b/src/serversocket.h index 0860972..88d09ec 100644 --- a/src/serversocket.h +++ b/src/serversocket.h @@ -39,6 +39,8 @@ namespace Bu public: ServerSocket( int nPort, int nPoolSize=40 ); ServerSocket( const FString &sAddr, int nPort, int nPoolSize=40 ); + ServerSocket( int nSocket, bool bInit, int nPoolSize=40 ); + ServerSocket( const ServerSocket &rSrc ); virtual ~ServerSocket(); int accept( int nTimeoutSec=0, int nTimeoutUSec=0 ); @@ -47,6 +49,7 @@ namespace Bu private: void startServer( struct sockaddr_in &name, int nPoolSize ); + void initServer( struct sockaddr_in &name, int nPoolSize ); fd_set fdActive; #ifdef WIN32 -- cgit v1.2.3