Comprehensive error handling

I have a special network code. I use asio, but it really doesn't matter for this question. I guess there is no way to untie a socket other than closing it. The problem is that open(), bind()and listen()they can throw it away system_error. So I processed the code simple try/catch. Code written broken.

using namespace boost::asio;

class Thing
{
public:

   ip::tcp::endpoint m_address;

   ip::tcp::acceptor m_acceptor;

   /// connect should handle all of its exceptions internally.
   bool connect()
   {
      try
      {
         m_acceptor.open( m_address.protocol() );
         m_acceptor.set_option( tcp::acceptor::reuse_address(true) );

         m_acceptor.bind( m_address );
         m_acceptor.listen();

         m_acceptor.async_accept( /*stuff*/ );
      }
      catch( const boost::system::system_error& error )
      {
         assert(acceptor.is_open());
         m_acceptor.close();
         return false;
      }
      return true;
   }

   /// don't call disconnect unless connect previously succeeded.
   void disconnect()
   {
      // other stuff needed to disconnect is ommited
      m_acceptor.close();
   }
};

The error is that the socket will not be able to connect, it will try to close it in the catch block and throw another system_error into the closure of the acceptor, which was never opened.

if( acceptor.is_open() ) catch, . , C - c++. , - open().

boost::system::error_code error;
acceptor.open( address.protocol, error );
if( ! error )
{
    try
    {
       acceptor.set_option( tcp::acceptor::reuse_address(true) );

       acceptor.bind( address );
       acceptor.listen();

       acceptor.async_accept( /*stuff*/ );
    }
    catch( const boost::system::system_error& error )
    {
       assert(acceptor.is_open());
       acceptor.close();
       return false;
    }
}
return !error;

RAII try/catch?

, if( error condition ) ?

+3
2

open, :

bool connect()
{
  try {
     m_acceptor.open( m_address.protocol() );
  } catch( const boost::system::system_error& error ) {
     return false;
  }

  try {
     m_acceptor.set_option( tcp::acceptor::reuse_address(true) );

     m_acceptor.bind( m_address );
     m_acceptor.listen();

     m_acceptor.async_accept( /*stuff*/ );
  } catch( const boost::system::system_error& error ) {
     m_acceptor.close();
     return false;
  }
  return true;
}
+3

try-catch , system_error error_code, . , error_code catch.

RAI, , ,

acceptor.async_accept( /*stuff*/ );

, , . Thing th;

{
 Connector conn(th); / connect on constructor
 // ... th.async_accept  
 // do some work while connected
}
 // disconnect on destructor

, - is_open, acceptor.open().

   Connector::Connector(...)
   : ...
   , is_open(false)
   {
     m_acceptor.open( m_address.protocol() );
     is_open=true;
     m_acceptor.set_option( tcp::acceptor::reuse_address(true) );

     m_acceptor.bind( m_address );
     m_acceptor.listen();

     m_acceptor.async_accept( /*stuff*/ );
   }

   Connector::~Connector(...)
   {
     // other stuff needed to disconnect is omitted
     if (is_open) m_acceptor.close();
   }
+2

Source: https://habr.com/ru/post/1743204/


All Articles