aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/hicn/transport/core/socket_connector.cc
diff options
context:
space:
mode:
authorMauro Sardara <msardara@cisco.com>2019-01-29 15:49:15 +0100
committerMichele Papalini <micpapal@cisco.com>2019-01-29 17:30:27 +0000
commit51ff9b669dff18c9300b9fe5bdef91e7040edac0 (patch)
tree286625282289580594c0ecf7dcf3df47a7ed8a18 /libtransport/src/hicn/transport/core/socket_connector.cc
parenta2e898eae8086cded2acfa96347184b3aa88d316 (diff)
[HICN-17] Add possibility to destroy connection directly from stopEventsLoop with an additional parameter.
Change-Id: I869a079a7b2f436768a62de66fd9281a7d1243cd Signed-off-by: Mauro Sardara <msardara@cisco.com>
Diffstat (limited to 'libtransport/src/hicn/transport/core/socket_connector.cc')
-rw-r--r--libtransport/src/hicn/transport/core/socket_connector.cc19
1 files changed, 17 insertions, 2 deletions
diff --git a/libtransport/src/hicn/transport/core/socket_connector.cc b/libtransport/src/hicn/transport/core/socket_connector.cc
index 332b87ec7..704d3badb 100644
--- a/libtransport/src/hicn/transport/core/socket_connector.cc
+++ b/libtransport/src/hicn/transport/core/socket_connector.cc
@@ -62,6 +62,7 @@ SocketConnector::SocketConnector(PacketReceivedCallback &&receive_callback,
is_connecting_(false),
is_reconnection_(false),
data_available_(false),
+ is_closed_(false),
receive_callback_(receive_callback),
on_reconnect_callback_(on_reconnect_callback),
app_name_(app_name) {}
@@ -102,7 +103,11 @@ void SocketConnector::send(const Packet::MemBufPtr &packet) {
}
void SocketConnector::close() {
- io_service_.post([this]() { socket_.close(); });
+ io_service_.dispatch([this]() {
+ is_closed_ = true;
+ socket_.shutdown(asio::ip::tcp::socket::shutdown_type::shutdown_both);
+ socket_.close();
+ });
}
void SocketConnector::doWrite() {
@@ -125,6 +130,9 @@ void SocketConnector::doWrite() {
if (!output_buffer_.empty()) {
doWrite();
}
+ } 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());
tryReconnect();
@@ -141,6 +149,9 @@ void SocketConnector::doReadBody(std::size_t body_length) {
if (TRANSPORT_EXPECT_TRUE(!ec)) {
receive_callback_(std::move(read_msg_));
doReadHeader();
+ } 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());
tryReconnect();
@@ -165,6 +176,9 @@ void SocketConnector::doReadHeader() {
} else {
TRANSPORT_LOGE("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());
tryReconnect();
@@ -173,11 +187,12 @@ void SocketConnector::doReadHeader() {
}
void SocketConnector::tryReconnect() {
- if (!is_connecting_) {
+ if (!is_connecting_ && !is_closed_) {
TRANSPORT_LOGE("Connection lost. Trying to reconnect...\n");
is_connecting_ = true;
is_reconnection_ = true;
io_service_.post([this]() {
+ socket_.shutdown(asio::ip::tcp::socket::shutdown_type::shutdown_both);
socket_.close();
startConnectionTimer();
doConnect();