From f7f45e9630912cfca6b3a6b60577f02924c3dbc9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Jul 2007 00:50:59 +0000 Subject: Ah, that explains much, I did the big reorg -> trunk move and forgot to commit some things, so here they are, after being manually copied. --- build.conf | 3 ++- src/exceptionbase.h | 2 +- src/plugger.h | 4 +--- src/socket.cpp | 23 +++++++++++++++++++--- src/socket.h | 3 ++- src/tests/fstrstd.cpp | 9 +++++++++ src/tests/socketblock.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 src/tests/fstrstd.cpp create mode 100644 src/tests/socketblock.cpp diff --git a/build.conf b/build.conf index d994977..e82cc29 100644 --- a/build.conf +++ b/build.conf @@ -39,7 +39,8 @@ filesIn("src/tests") filter regexp("^src/tests/(.*)\\.cpp$", "tests/{re:1}"): set "LDFLAGS" += "-L. -lbu++", input "src/{target}.cpp" -["tests/itoqueue1", "tests/itoqueue2"]: set "LDFLAGS" += "-lpthread" +["tests/itoqueue1", "tests/itoqueue2", "tests/socketblock"]: + set "LDFLAGS" += "-lpthread" filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"): rule "exe", diff --git a/src/exceptionbase.h b/src/exceptionbase.h index 33e79b2..0a251e4 100644 --- a/src/exceptionbase.h +++ b/src/exceptionbase.h @@ -81,7 +81,7 @@ namespace Bu } #define subExceptionDecl( name ) \ -class name : public Bu::ExceptionBase \ +class name : public Bu::ExceptionBase \ { \ public: \ name( const char *sFormat, ... ) throw (); \ diff --git a/src/plugger.h b/src/plugger.h index 2124b7a..88628a9 100644 --- a/src/plugger.h +++ b/src/plugger.h @@ -158,9 +158,7 @@ namespace Bu bool hasPlugin( const char *lpName ) { - if( hPlugin[lpName] == NULL ) - return false; - return true; + return hPlugin.has( lpName ); } void destroy( T *pPlug ) diff --git a/src/socket.cpp b/src/socket.cpp index e567061..221d4c2 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -174,9 +174,26 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) return nRead; } -//size_t Bu::Socket::read( void *pBuf, size_t nBytes, uint32_t nTimeout ) -//{ -//} +size_t Bu::Socket::read( void *pBuf, size_t nBytes, + uint32_t nSec, uint32_t nUSec ) +{ + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(nSocket, &rfds); + struct timeval tv = { nSec, nUSec }; + int retval = select( nSocket+1, &rfds, NULL, NULL, &tv ); + if( retval == -1 ) + throw ConnectionException( + excodeBadReadError, + "Bad Read error" + ); + if( !FD_ISSET( nSocket, &rfds ) ) + throw ConnectionException( + excodeSocketTimeout, + "Socket timout on read" + ); + return read( pBuf, nBytes ); +} size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) { diff --git a/src/socket.h b/src/socket.h index c291549..9f82456 100644 --- a/src/socket.h +++ b/src/socket.h @@ -21,7 +21,8 @@ namespace Bu 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 nTimeout ); + 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 long tell(); diff --git a/src/tests/fstrstd.cpp b/src/tests/fstrstd.cpp new file mode 100644 index 0000000..8dffcb6 --- /dev/null +++ b/src/tests/fstrstd.cpp @@ -0,0 +1,9 @@ +#include +#include "bu/fstring.h" + +int main() +{ + Bu::FString s("Hey there, dude.\n"); + + std::cout << s << 5; +} diff --git a/src/tests/socketblock.cpp b/src/tests/socketblock.cpp new file mode 100644 index 0000000..443b07e --- /dev/null +++ b/src/tests/socketblock.cpp @@ -0,0 +1,50 @@ +#include "bu/ito.h" +#include "bu/socket.h" +#include "bu/serversocket.h" +#include + +class TstServer : public Bu::Ito +{ +public: + TstServer() : + s( 55678 ) + { + } + + virtual void *run() + { + Bu::Socket c = s.accept( 45, 0 ); + printf("TstServer: Accetped connection.\n"); fflush( stdout ); + + sleep( 1 ); + printf("TstServer: Trying to read 10 bytes...\n"); fflush( stdout ); + + char buf[10]; + size_t nRead = c.read( buf, 10 ); + printf("TstServer: Got %d bytes...\n", nRead ); fflush( stdout ); + + printf("TstServer: Closing connection...\n"); fflush( stdout ); + c.close(); + + return NULL; + } + + Bu::ServerSocket s; +}; + +int main() +{ + TstServer ts; + + ts.start(); + + printf("main: Connecting to server.\n"); fflush( stdout ); + Bu::Socket s( "localhost", 55678 ); + + printf("main: Sending 4 bytes.\n"); fflush( stdout ); + s.write( "aoeu", 4 ); + + printf("main: Sleeping 10 seconds for good measure.\n"); fflush( stdout ); + sleep( 10 ); +} + -- cgit v1.2.3