void protocol::handle_accept(const std::error_code& ec, channel_ptr node, acceptor_ptr accept) { accept->accept( strand_.wrap(std::bind(&protocol::handle_accept, this, _1, _2, accept))); if (ec) { log_error(LOG_PROTOCOL) << "Problem accepting connection: " << ec.message(); return; } accepted_channels_.push_back(node); log_info(LOG_PROTOCOL) << "Accepted connection: " << accepted_channels_.size(); auto handshake_complete = [this, node](const std::error_code& ec) { if (ec) { log_error(LOG_PROTOCOL) << "Problem with handshake: " << ec.message(); return; } // Remove channel from list of connections node->subscribe_stop( strand_.wrap(std::bind(&protocol::inbound_channel_stopped, this, _1, node))); setup_new_channel(node); }; handshake_.ready(node, handshake_complete); }
void protocol::handle_connect( const std::error_code& ec, channel_ptr node, const network_address_type& address, slot_index slot) { BITCOIN_ASSERT(connect_states_[slot] == connect_state::connecting); connect_states_[slot] = connect_state::established; BITCOIN_ASSERT(connections_.size() <= max_outbound_); if (ec) { log_warning(LOG_PROTOCOL) << "Unable to connect to " << pretty(address.ip) << ":" << address.port << " - " << ec.message(); // Retry another connection // Still in same strand. try_connect_once(slot); return; } connections_.push_back({address, node}); log_info(LOG_PROTOCOL) << "Connected to " << pretty(address.ip) << ":" << address.port << " (" << connections_.size() << " connections)"; // Remove channel from list of connections node->subscribe_stop( strand_.wrap(std::bind(&protocol::outbound_channel_stopped, this, _1, node, slot))); setup_new_channel(node); }
void handle_handshake(const std::error_code& ec, channel_ptr node, handshake_ptr hs) { if (ec) error_exit(ec.message()); log_info() << "Connected"; channode = node; node->subscribe_stop(handle_stop); }
void protocol::setup_new_channel(channel_ptr node) { // Remove channel from list of connections node->subscribe_stop( strand_.wrap(std::bind(&protocol::channel_stopped, this, _1, node))); subscribe_address(node); node->send(get_address_type(), handle_send); // Notify subscribers channel_subscribe_->relay(std::error_code(), node); }
void accepted_connection(const std::error_code& ec, channel_ptr node, acceptor_ptr accept) { if (ec) { log_error() << "Accept: " << ec.message(); return; } log_info() << "Accepted connection!"; node->subscribe_stop(node_stopped); // Now we need to keep it alive otherwise the connection is closed. node->subscribe_version( std::bind(version_received, _1, _2, node)); // Keep accepting more connections. accept->accept( std::bind(accepted_connection, _1, _2, accept)); }
void protocol::handle_manual_connect( const std::error_code& ec, channel_ptr node, const std::string& hostname, uint16_t port) { if (ec) { log_warning(LOG_PROTOCOL) << "Manual connection to " << hostname << ":" << port << " failed with " << ec.message(); maintain_connection(hostname, port); return; } manual_connections_.push_back(node); // Connected! log_info(LOG_PROTOCOL) << "Manual connection established: " << hostname << ":" << port; // Remove channel from list of connections node->subscribe_stop(strand_.wrap( &protocol::manual_channel_stopped, this, _1, node, hostname, port)); setup_new_channel(node); }