void connection::complete_connection(const boost::system::error_code& error, std::size_t bytes_transferred) { if (error) { stillborn(); return; } link_.received(bytes_transferred); const boost::uint8_t (*supported_protocols)[2] = buffer_cast<const boost::uint8_t(*)[2]>(link_.received_buffer()); for (std::vector<protocol_id>::iterator protocol = supported_protocols_.begin(); protocol != supported_protocols_.end(); ++protocol) { *protocol = u16(supported_protocols[protocol - supported_protocols_.begin()]); } link_.consume_receive_buffer(supported_protocols_.size() * 2); lifecycle_ = connected; established_ = boost::posix_time::second_clock::universal_time(); DLOG(INFO) << "Connected with cipher: " << ::SSL_CIPHER_get_name(::SSL_get_current_cipher(link_.socket.impl()->ssl)); //<< " and compression: " << ::SSL_COMP_get_name(::SSL_get_current_compression(link_.socket.impl()->ssl)); node_->register_connection(shared_from_this()); if (accepts_ib_traffic()) outstanding_non_packet_frames_ |= frame_bit_successor_request; send_next_frame(); receive_next_frame(); }
void NetworkQueue::start() { if( !is_running() && !m_transactions.isEmpty() ) { m_current_index = 0; send_next_frame(); } }
void connection::send_next_frame(int send_non_packet_frame) { if (!outstanding_non_packet_frames_ && packet_queue_.empty() && lifecycle_ == connected) { outstanding_non_packet_frames_ |= send_non_packet_frame; send_next_frame(); } else { outstanding_non_packet_frames_ |= send_non_packet_frame; } }
void connection::send(packet::ptr_t pkt, std::size_t oob_threshold_override) { packet_queue_.push_back(queued_packet(pkt, oob_threshold_override)); if (!outstanding_non_packet_frames_ && packet_queue_.size() == 1 && lifecycle_ == connected) { send_next_frame(); } else DLOG(INFO) << "Queued packet from " << std::string(node_->id()) << " to " << std::string(remote_id()); }
void connection::update_oob_threshold() { #ifdef FORCE_OOB_THRESHOLD oob_threshold_ = FORCE_OOB_THRESHOLD; #else oob_threshold_ = std::min(remote_oob_threshold_, boost::uint32_t(local_oob_threshold_)); if (oob_threshold_ < min_oob_threshold) oob_threshold_ = min_oob_threshold; #endif node_->update_threshold_stats(); send_next_frame(frame_bit_oob_threshold_update); }
void connection::frame_sent(frame_bits frame_bit, const boost::system::error_code& error, std::size_t bytes_transfered) { // DLOG(INFO) << "Sent " << bytes_transfered << " bytes for frame type " << frame_bit; if (!(frame_bit & frame_bit_oob_threshold_update && ack_sends_needed_)) outstanding_non_packet_frames_ &= ~frame_bit; if (error) { send_failure(); return; } send_next_frame(); }
void NetworkQueue::signal_ack ( common::SignalArgs & args ) { // if( isRunning() ) { SignalOptions & options = args.options(); std::string frameid = options.value<std::string>( "frameid" ); // if( m_currentFrameID == frameid ) { bool success = options.value<bool>( "success" ); if( !success ) { std::string message = options.value<std::string>( "message" ); QString msg("Error while running the script.\n\nThe following error occured:\n %1" "\n\nWhen executing the signal:\n\n%2"); XML::SignalFrame f = m_transactions[m_current_index]->actions[0].node; NLog::global()->add_exception( msg.arg(message.c_str()).arg(f.to_script().c_str()) ); // if the signal was comming from a script, we close the script file if ( m_transactions[m_current_index]->from_script ) m_script_file->close(); m_transactions.removeAt( m_current_index ); if( m_transactions.isEmpty() ) m_current_index = -1; } else { m_transactions.removeAt( m_current_index ); if( m_transactions.isEmpty() ) m_current_index = -1; if( !m_transactions.isEmpty() ) send_next_frame(); else if( m_script_file->isOpen() ) send_next_command(); } } // else // NLog::global()->addWarning(QString("Bad uuid! Received \"%1\" but \"%2\" was excpeted") // .arg(frameid.c_str()).arg(m_currentFrameID.c_str())); } // else // NLog::global()->addWarning(QString("Received ACK while not running.")); }
void connection::packet_sent(const boost::system::error_code& error, std::size_t bytes_transfered) { if (error) { send_failure(); return; } DLOG(INFO) << std::string(node_->id()) << " Sent " << bytes_transfered << " bytes of packet content"; node_->sent_content(remote_id(), bytes_transfered); packet_queue_.pop_front(); send_next_frame(); }
void connection::send_delayed_frame(const boost::system::error_code& error) { if (!error) send_next_frame(); }
void connection::frame_head_received(const boost::system::error_code& error, std::size_t bytes_transfered) { if (error) { DLOG(INFO) << "Error receiving frame"; node_->receive_failure(shared_from_this()); return; } link_.received(bytes_transfered); DLOG(INFO) << std::string(node_->id()) << " Recieved " << bytes_transfered << " bytes, total buffered: " << link_.valid_received_bytes(); while (link_.valid_received_bytes()) { int frame_type(buffer_cast<const boost::uint8_t*>(link_.received_buffer())[0]); switch (frame_type) { case frame_network_packet: { // DLOG(INFO) << "Receiving network packet frame"; packet::ptr_t pkt(boost::make_shared<packet>()); pkt->receive(link_, boost::protect(boost::bind(&connection::incoming_packet, shared_from_this(), _1, _2))); return; } case frame_oob_threshold_update: if (link_.valid_received_bytes() >= oob_threshold_size()) { parse_oob_threshold(); break; } boost::asio::async_read(link_.socket, mutable_buffers_1(link_.receive_buffer()), boost::asio::transfer_at_least(oob_threshold_size() - link_.valid_received_bytes()), boost::bind(&connection::frame_head_received, shared_from_this(), placeholders::error, placeholders::bytes_transferred)); return; case frame_successor_request: { if (link_.valid_received_bytes() >= 4) { DLOG(INFO) << std::string(node_->id()) << "Sending successor"; send_next_frame(frame_bit_successor); link_.consume_receive_buffer(4); break; } boost::asio::async_read(link_.socket, mutable_buffers_1(link_.receive_buffer()), boost::asio::transfer_at_least(4 - link_.valid_received_bytes()), boost::bind(&connection::frame_head_received, shared_from_this(), placeholders::error, placeholders::bytes_transferred)); return; } case frame_successor: if (link_.valid_received_bytes() >= successor_size()) { parse_successor(); break; } boost::asio::async_read(link_.socket, mutable_buffers_1(link_.receive_buffer()), boost::asio::transfer_at_least(oob_threshold_size() - link_.valid_received_bytes()), boost::bind(&connection::frame_head_received, shared_from_this(), placeholders::error, placeholders::bytes_transferred)); return; default: { DLOG(INFO) << "Unknown frame type: " << buffer_cast<const boost::uint8_t*>(link_.received_buffer())[0]; google::FlushLogFiles(google::INFO); node_->receive_failure(shared_from_this()); return; } } } receive_next_frame(); }