From f896b0e207e0b656109ef0e9f721f27ce53a836e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 28 Jun 2007 22:47:03 +0000 Subject: Client code is better, so is the socket, you can get addresses and other cool things from it. The plugger had yet another bugfix...plugger... --- src/client.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/client.h | 10 +++++++++- src/plugger.h | 6 +++--- src/socket.cpp | 12 ++++++++++++ src/socket.h | 2 ++ 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 0e48285..63822ba 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -10,7 +10,8 @@ Bu::Client::Client( Bu::Socket *pSocket ) : pSocket( pSocket ), - pProto( NULL ) + pProto( NULL ), + nRBOffset( 0 ) { } @@ -103,3 +104,54 @@ void Bu::Client::write( const char *pData, int nBytes ) sWriteBuf.append( pData, nBytes ); } +void Bu::Client::write( int8_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( int16_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( int32_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::write( int64_t nData ) +{ + sWriteBuf.append( (const char *)&nData, sizeof(nData) ); +} + +void Bu::Client::read( char *pData, int nBytes ) +{ + memcpy( pData, sReadBuf.getStr()+nRBOffset, nBytes ); + nRBOffset += nBytes; + if( sReadBuf.getSize()-nRBOffset == 0 ) + { + sReadBuf.clear(); + nRBOffset = 0; + } + // This is an experimental threshold, maybe I'll make this configurable + // later on. + else if( + (sReadBuf.getSize() >= 1024 && nRBOffset >= sReadBuf.getSize()/2) || + (nRBOffset >= sReadBuf.getSize()/4) + ) + { + sReadBuf.trimFront( nRBOffset ); + nRBOffset = 0; + } +} + +long Bu::Client::getInputSize() +{ + return sReadBuf.getSize()-nRBOffset; +} + +const Bu::Socket *Bu::Client::getSocket() const +{ + return pSocket; +} + diff --git a/src/client.h b/src/client.h index f228e96..b400bb3 100644 --- a/src/client.h +++ b/src/client.h @@ -25,7 +25,12 @@ namespace Bu Bu::FString &getInput(); Bu::FString &getOutput(); void write( const char *pData, int nBytes ); - void read( const char *pData, int nBytes ); + void write( int8_t nData ); + void write( int16_t nData ); + void write( int32_t nData ); + void write( int64_t nData ); + void read( char *pData, int nBytes ); + long getInputSize(); void setProtocol( Protocol *pProto ); Bu::Protocol *getProtocol(); @@ -33,10 +38,13 @@ namespace Bu bool isOpen(); + const Bu::Socket *getSocket() const; + private: Bu::Socket *pSocket; Bu::Protocol *pProto; Bu::FString sReadBuf; + int nRBOffset; Bu::FString sWriteBuf; }; } diff --git a/src/plugger.h b/src/plugger.h index 79a3271..615a662 100644 --- a/src/plugger.h +++ b/src/plugger.h @@ -37,7 +37,7 @@ namespace Bu { \ delete pCls; \ } \ - PluginInfo classname = { \ + Bu::PluginInfo classname = { \ #classname, name, ver, rev, \ create ##classname, destroy ##classname }; \ } @@ -52,7 +52,7 @@ namespace Bu { \ delete pCls; \ } \ - PluginInfo pluginname = { \ + Bu::PluginInfo pluginname = { \ #pluginname, name, ver, rev, \ (void *(*)())(create ##classname), \ (void (*)( void * ))(destroy ##classname) }; \ @@ -68,7 +68,7 @@ namespace Bu { \ delete pCls; \ } \ - PluginInfo structname = { \ + Bu::PluginInfo structname = { \ #pluginname, name, ver, rev, \ (void *(*)())(create ##classname), \ (void (*)( void * ))(destroy ##classname) }; \ diff --git a/src/socket.cpp b/src/socket.cpp index bd05024..5a3097c 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -276,3 +276,15 @@ bool Bu::Socket::isOpen() return bActive; } +Bu::FString Bu::Socket::getAddress() const +{ + struct sockaddr_in addr; + socklen_t len = sizeof(addr); + addr.sin_family = AF_INET; + getsockname( nSocket, (sockaddr *)(&addr), &len ); + char buf[150]; + sprintf( buf, "%s", inet_ntoa( addr.sin_addr ) ); + + return buf; +} + diff --git a/src/socket.h b/src/socket.h index 0ccee3b..9e36041 100644 --- a/src/socket.h +++ b/src/socket.h @@ -42,6 +42,8 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + Bu::FString getAddress() const; + private: int nSocket; bool bActive; -- cgit v1.2.3