From d3a6d910f96d1509b17165b635663b608681b89a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 25 Jul 2011 17:25:28 +0000 Subject: In theory, after constructing a TcpSocket object, you can now take the operating system socket handle away and use it yourself, after doing so, the TcpSocket will not close the socket when it exits, or be able to effect it ever again. --- src/tcpsocket.cpp | 17 +++++++++++++++-- src/tcpsocket.h | 20 +++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/tcpsocket.cpp b/src/tcpsocket.cpp index 399ec44..b9b215c 100644 --- a/src/tcpsocket.cpp +++ b/src/tcpsocket.cpp @@ -31,7 +31,7 @@ namespace Bu { subExceptionDef( TcpSocketException ) } -Bu::TcpSocket::TcpSocket( int nTcpSocket ) : +Bu::TcpSocket::TcpSocket( handle nTcpSocket ) : nTcpSocket( nTcpSocket ), bActive( true ), bBlocking( true ) @@ -443,11 +443,24 @@ Bu::String Bu::TcpSocket::getAddress() const return sAddress; } -Bu::TcpSocket::operator int() const +Bu::TcpSocket::operator Bu::TcpSocket::handle() const { return nTcpSocket; } +Bu::TcpSocket::handle Bu::TcpSocket::getHandle() const +{ + return nTcpSocket; +} + +Bu::TcpSocket::handle Bu::TcpSocket::takeHandle() +{ + handle nRet = nTcpSocket; + bActive = false; + nTcpSocket = 0; + return nRet; +} + Bu::size Bu::TcpSocket::getSize() const { throw UnsupportedException(); diff --git a/src/tcpsocket.h b/src/tcpsocket.h index dbaaa5e..52218bd 100644 --- a/src/tcpsocket.h +++ b/src/tcpsocket.h @@ -60,7 +60,13 @@ namespace Bu class TcpSocket : public Stream { public: - TcpSocket( int nTcpSocket ); +#ifdef WIN32 + typedef unsigned int handle; +#else + typedef int handle; +#endif + + TcpSocket( handle nTcpSocket ); TcpSocket( const String &sAddr, int nPort, int nTimeout=30, bool bBlocking=true ); virtual ~TcpSocket(); @@ -96,7 +102,10 @@ namespace Bu virtual void setSize( size iSize ); Bu::String getAddress() const; - operator int() const; + operator handle() const; + + handle getHandle() const; + handle takeHandle(); virtual size getSize() const; virtual size getBlockSize() const; @@ -105,11 +114,8 @@ namespace Bu private: void setAddress(); -#ifdef WIN32 - unsigned int nTcpSocket; -#else - int nTcpSocket; -#endif + handle nTcpSocket; + bool bActive; bool bBlocking; String sReadBuf; -- cgit v1.2.3