Skip to content

Commit

Permalink
Merge branch '0.6-dev-issue-68-refactor-create-new-connection' into 0…
Browse files Browse the repository at this point in the history
….6-dev
  • Loading branch information
eao197 committed Jan 30, 2020
2 parents 2cdd856 + 718867c commit f048bf3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
9 changes: 5 additions & 4 deletions dev/restinio/impl/acceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,14 @@ class acceptor_t final
"do_accept_current_connection.create_and_init_connection",
[&] {
// Create new connection handler.
// NOTE: since v.0.6.3 this method throws in
// the case of an error. Because of that there is
// no need to check the value returned.
auto conn = factory->create_new_connection(
std::move(sock), std::move(ep) );

// If connection handler was created,
// then start waiting for request message.
if( conn )
conn->init();
// Start waiting for request message.
conn->init();
} );
};

Expand Down
30 changes: 10 additions & 20 deletions dev/restinio/impl/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1592,36 +1592,26 @@ class connection_factory_t
, m_logger{ *(m_connection_settings->m_logger ) }
{}

// NOTE: since v.0.6.3 it returns non-empty
// shared_ptr<connection_t<Traits>> or anexception is thrown in
// the case of an error.
auto
create_new_connection(
stream_socket_t socket,
endpoint_t remote_endpoint )
{
using connection_type_t = connection_t< Traits >;
std::shared_ptr< connection_type_t > result;
try
{
{
socket_options_t options{ socket.lowest_layer() };
(*m_socket_options_setter)( options );
}

result = std::make_shared< connection_type_t >(
m_connection_id_counter++,
std::move( socket ),
m_connection_settings,
std::move( remote_endpoint ) );
}
catch( const std::exception & ex )
{
m_logger.error( [&]{
return fmt::format(
"failed to create connection: {}",
ex.what() );
} );
socket_options_t options{ socket.lowest_layer() };
(*m_socket_options_setter)( options );
}

return result;
return std::make_shared< connection_type_t >(
m_connection_id_counter++,
std::move( socket ),
m_connection_settings,
std::move( remote_endpoint ) );
}

private:
Expand Down

0 comments on commit f048bf3

Please sign in to comment.