diff options
Diffstat (limited to 'libtransport/src/core/tcp_socket_connector.cc')
-rw-r--r-- | libtransport/src/core/tcp_socket_connector.cc | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/libtransport/src/core/tcp_socket_connector.cc b/libtransport/src/core/tcp_socket_connector.cc index 20b3d6ce6..7758e2cf2 100644 --- a/libtransport/src/core/tcp_socket_connector.cc +++ b/libtransport/src/core/tcp_socket_connector.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -18,8 +18,8 @@ #include <hicn/transport/portability/win_portability.h> #endif +#include <glog/logging.h> #include <hicn/transport/errors/errors.h> -#include <hicn/transport/utils/log.h> #include <hicn/transport/utils/object_pool.h> #include <thread> @@ -33,6 +33,8 @@ namespace { class NetworkMessage { public: static constexpr std::size_t fixed_header_length = 10; + static constexpr std::uint8_t ccnx_flag = 102; + static constexpr std::size_t ccnx_packet_length = 44; static std::size_t decodeHeader(const uint8_t *packet) { // General checks @@ -40,11 +42,12 @@ class NetworkMessage { uint8_t first_byte = packet[0]; uint8_t ip_format = (packet[0] & 0xf0) >> 4; - if (TRANSPORT_EXPECT_FALSE(first_byte == 102)) { + if (TRANSPORT_EXPECT_FALSE(first_byte == ccnx_flag)) { // Get packet length - return 44; + return ccnx_packet_length; } else if (TRANSPORT_EXPECT_TRUE(ip_format == 6 || ip_format == 4)) { - Packet::Format format = Packet::getFormatFromBuffer(packet); + Packet::Format format = + Packet::getFormatFromBuffer(packet, fixed_header_length); return Packet::getHeaderSizeFromBuffer(format, packet) + Packet::getPayloadSizeFromBuffer(format, packet); } @@ -82,8 +85,10 @@ void TcpSocketConnector::send(const uint8_t *packet, std::size_t len, const PacketSentCallback &packet_sent) { if (packet_sent != 0) { asio::async_write(socket_, asio::buffer(packet, len), - [packet_sent](std::error_code ec, - std::size_t /*length*/) { packet_sent(); }); + [packet_sent](const std::error_code &ec) + std::size_t /*length*/) { + packet_sent(); + }); } else { if (state_ == ConnectorState::CONNECTED) { asio::write(socket_, asio::buffer(packet, len)); @@ -148,7 +153,7 @@ void TcpSocketConnector::doWrite() { asio::async_write( socket_, std::move(array), - [this, packet_store = std::move(packet_store)](std::error_code ec, + [this, packet_store = std::move(packet_store)](const std::error_code &ec, std::size_t length) { if (TRANSPORT_EXPECT_TRUE(!ec)) { if (!output_buffer_.empty()) { @@ -159,7 +164,7 @@ void TcpSocketConnector::doWrite() { // The connection has been closed by the application. return; } else { - TRANSPORT_LOGE("%d %s", ec.value(), ec.message().c_str()); + LOG(ERROR) << ec.value() << ":" << ec.message(); tryReconnect(); } }); @@ -169,7 +174,7 @@ void TcpSocketConnector::doReadBody(std::size_t body_length) { asio::async_read( socket_, asio::buffer(read_msg_->writableTail(), body_length), asio::transfer_exactly(body_length), - [this](std::error_code ec, std::size_t length) { + [this](const std::error_code &ec, std::size_t length) { read_msg_->append(length); if (TRANSPORT_EXPECT_TRUE(!ec)) { receive_callback_(std::move(read_msg_)); @@ -179,7 +184,7 @@ void TcpSocketConnector::doReadBody(std::size_t body_length) { // The connection has been closed by the application. return; } else { - TRANSPORT_LOGE("%d %s", ec.value(), ec.message().c_str()); + LOG(ERROR) << ec.value() << " " << ec.message(); tryReconnect(); } }); @@ -192,7 +197,7 @@ void TcpSocketConnector::doReadHeader() { asio::buffer(read_msg_->writableData(), NetworkMessage::fixed_header_length), asio::transfer_exactly(NetworkMessage::fixed_header_length), - [this](std::error_code ec, std::size_t length) { + [this](const std::error_code &ec, std::size_t length) { if (TRANSPORT_EXPECT_TRUE(!ec)) { read_msg_->append(NetworkMessage::fixed_header_length); std::size_t body_length = 0; @@ -200,23 +205,23 @@ void TcpSocketConnector::doReadHeader() { 0) { doReadBody(body_length - length); } else { - TRANSPORT_LOGE("Decoding error. Ignoring packet."); + LOG(ERROR) << "Decoding error. Ignoring packet."; } } else if (ec.value() == static_cast<int>(std::errc::operation_canceled)) { // The connection has been closed by the application. return; } else { - TRANSPORT_LOGE("%d %s", ec.value(), ec.message().c_str()); + LOG(ERROR) << ec.value() << " " << ec.message(); tryReconnect(); } }); } void TcpSocketConnector::tryReconnect() { - if (state_ == ConnectorState::CONNECTED) { - TRANSPORT_LOGE("Connection lost. Trying to reconnect...\n"); - state_ = ConnectorState::CONNECTING; + if (state_ == Connector::State::CONNECTED) { + LOG(ERROR) << "Connection lost. Trying to reconnect..."; + state_ = Connector::State::CONNECTING; is_reconnection_ = true; io_service_.post([this]() { if (socket_.is_open()) { @@ -232,7 +237,7 @@ void TcpSocketConnector::tryReconnect() { void TcpSocketConnector::doConnect() { asio::async_connect( socket_, endpoint_iterator_, - [this](std::error_code ec, tcp::resolver::iterator) { + [this](const std::error_code &ec, tcp::resolver::iterator) { if (!ec) { timer_.cancel(); state_ = ConnectorState::CONNECTED; @@ -247,7 +252,7 @@ void TcpSocketConnector::doConnect() { if (is_reconnection_) { is_reconnection_ = false; - TRANSPORT_LOGI("Connection recovered!\n"); + LOG(INFO) << "Connection recovered!"; on_reconnect_callback_(); } } else { @@ -271,7 +276,7 @@ void TcpSocketConnector::handleDeadline(const std::error_code &ec) { if (!ec) { io_service_.post([this]() { socket_.close(); - TRANSPORT_LOGE("Error connecting. Is the forwarder running?\n"); + LOG(ERROR) << "Error connecting. Is the forwarder running?"; io_service_.stop(); }); } |