You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/11/12 19:29:02 UTC
svn commit: r594249 - in
/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net: BufferedSocket.cpp
TcpSocket.cpp
Author: tabish
Date: Mon Nov 12 10:29:00 2007
New Revision: 594249
URL: http://svn.apache.org/viewvc?rev=594249&view=rev
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Changing the close behavior of the Scoket classes. We don't want to delete streams in the socket close methods as there could be someone using them in a read thread. We want to close them in close and delete them in the dtor.
Modified:
activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/BufferedSocket.cpp
activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/BufferedSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/BufferedSocket.cpp?rev=594249&r1=594248&r2=594249&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/BufferedSocket.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/BufferedSocket.cpp Mon Nov 12 10:29:00 2007
@@ -30,14 +30,14 @@
int inputBufferSize,
int outputBufferSize,
bool own ) :
- socket(NULL),
- own(false),
- inputStream(NULL),
- outputStream(NULL),
- inputBufferSize(0),
- outputBufferSize(0)
-{
- if(inputBufferSize < 0 || outputBufferSize < 0 ) {
+ socket( NULL ),
+ own( false ),
+ inputStream( NULL ),
+ outputStream( NULL ),
+ inputBufferSize( 0 ),
+ outputBufferSize( 0 ) {
+
+ if( inputBufferSize < 0 || outputBufferSize < 0 ) {
throw IllegalArgumentException(
__FILE__, __LINE__,
"BufferedSocket::BufferedSocket - buffer sizes must be >=0! "
@@ -46,7 +46,7 @@
outputBufferSize );
}
- if(socket == NULL) {
+ if( socket == NULL ) {
throw IllegalArgumentException(
__FILE__, __LINE__,
"BufferedSocket::BufferedSocket - Constructed with NULL Socket");
@@ -61,7 +61,26 @@
////////////////////////////////////////////////////////////////////////////////
BufferedSocket::~BufferedSocket() {
try {
+
close();
+
+ // delete the streams first as they may try and close or flush the
+ // contained streams when destoryed.
+ if( outputStream != NULL ) {
+ delete outputStream;
+ outputStream = NULL;
+ }
+
+ if( inputStream != NULL ){
+ delete inputStream;
+ inputStream = NULL;
+ }
+
+ // if we own it, delete it.
+ if( own ) {
+ delete socket;
+ }
+ socket = NULL;
}
DECAF_CATCH_NOTHROW( Exception )
DECAF_CATCHALL_NOTHROW()
@@ -97,17 +116,18 @@
try {
- if( outputStream != NULL ) {
- // Ensure all data is written
- outputStream->flush();
+ // We close and flush streams here but do not delete as there may be
+ // others classes ( and threads ) using them and we don't want to pull
+ // the rug out from under them, we delete the streams in the dtor and
+ // and that point its the users responsibility to ensure that they have
+ // removed all referenced to the streams provided by Sokcet.
- delete outputStream;
- outputStream = NULL;
+ if( outputStream != NULL ) {
+ outputStream->close();
}
if( inputStream != NULL ){
- delete inputStream;
- inputStream = NULL;
+ inputStream->close();
}
if( socket != NULL ){
@@ -115,12 +135,6 @@
try{
socket->close();
} catch( lang::Exception& ex ){ /* Absorb */ }
-
- // if we own it, delete it.
- if( own ) {
- delete socket;
- }
- socket = NULL;
}
}
DECAF_CATCH_RETHROW( SocketException )
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp?rev=594249&r1=594248&r2=594249&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp Mon Nov 12 10:29:00 2007
@@ -29,22 +29,18 @@
////////////////////////////////////////////////////////////////////////////////
TcpSocket::TcpSocket() throw ( SocketException )
- :
- socketHandle( INVALID_SOCKET_HANDLE ),
+ : socketHandle( INVALID_SOCKET_HANDLE ),
inputStream( NULL ),
- outputStream( NULL )
-{
+ outputStream( NULL ) {
}
////////////////////////////////////////////////////////////////////////////////
TcpSocket::TcpSocket( SocketHandle socketHandle )
- :
- socketHandle( INVALID_SOCKET_HANDLE ),
+ : socketHandle( INVALID_SOCKET_HANDLE ),
inputStream( NULL ),
outputStream( NULL ) {
try {
-
this->socketHandle = socketHandle;
this->inputStream = new SocketInputStream( socketHandle );
this->outputStream = new SocketOutputStream( socketHandle );
@@ -55,8 +51,26 @@
////////////////////////////////////////////////////////////////////////////////
TcpSocket::~TcpSocket() {
- // No shutdown, just close - dont want blocking destructor.
- close();
+
+ try{
+
+ // No shutdown, just close - dont want blocking destructor.
+ close();
+
+ // Destroy the input stream.
+ if( inputStream != NULL ){
+ delete inputStream;
+ inputStream = NULL;
+ }
+
+ // Destroy the output stream.
+ if( outputStream != NULL ){
+ delete outputStream;
+ outputStream = NULL;
+ }
+ }
+ DECAF_CATCH_NOTHROW( Exception )
+ DECAF_CATCHALL_NOTHROW()
}
////////////////////////////////////////////////////////////////////////////////
@@ -121,27 +135,28 @@
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::close() throw( lang::Exception )
-{
- // Destroy the input stream.
- if( inputStream != NULL ){
- delete inputStream;
- inputStream = NULL;
- }
-
- // Destroy the output stream.
- if( outputStream != NULL ){
- delete outputStream;
- outputStream = NULL;
- }
-
- // When connected we first shutdown, which breaks our reads and writes
- // then we close to free APR resources.
- if( isConnected() ) {
- apr_socket_close( socketHandle );
- socketHandle = INVALID_SOCKET_HANDLE;
+void TcpSocket::close() throw( lang::Exception ) {
+
+ try{
+ // Destroy the input stream.
+ if( inputStream != NULL ){
+ inputStream->close();
+ }
+
+ // Destroy the output stream.
+ if( outputStream != NULL ){
+ outputStream->close();
+ }
+
+ // When connected we first shutdown, which breaks our reads and writes
+ // then we close to free APR resources.
+ if( isConnected() ) {
+ apr_socket_close( socketHandle );
+ socketHandle = INVALID_SOCKET_HANDLE;
+ }
}
-
+ DECAF_CATCH_RETHROW( lang::Exception )
+ DECAF_CATCHALL_THROW( lang::Exception )
}
////////////////////////////////////////////////////////////////////////////////
@@ -257,8 +272,8 @@
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::setSoTimeout ( const int millisecs ) throw ( SocketException )
-{
+void TcpSocket::setSoTimeout ( const int millisecs ) throw ( SocketException ) {
+
try{
// Time is in microseconds so multiply by 1000.
checkResult( apr_socket_timeout_set( socketHandle, millisecs * 1000 ) );
@@ -268,8 +283,8 @@
}
////////////////////////////////////////////////////////////////////////////////
-int TcpSocket::getSoTimeout() const throw( SocketException )
-{
+int TcpSocket::getSoTimeout() const throw( SocketException ) {
+
try{
// Time is in microseconds so divide by 1000.
apr_interval_time_t value = 0;
@@ -304,7 +319,7 @@
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::checkResult( apr_status_t value ) const throw (SocketException) {
+void TcpSocket::checkResult( apr_status_t value ) const throw ( SocketException ) {
if( value != APR_SUCCESS ){
throw SocketException(