From de9e89c6a4e188d74d6a1256a23611bd3babd1d9 Mon Sep 17 00:00:00 2001 From: 98aa713aa8355e307a05fba5721b6ed31d764bb9 Date: Wed, 21 Aug 2019 10:30:10 -0400 Subject: [PATCH 1/3] Issue-238 Fix tested on indigo & Fanuc --- .../simple_message/socket/tcp_client.h | 3 + simple_message/src/socket/tcp_client.cpp | 129 ++++++++++-------- 2 files changed, 74 insertions(+), 58 deletions(-) diff --git a/simple_message/include/simple_message/socket/tcp_client.h b/simple_message/include/simple_message/socket/tcp_client.h index 035128c0..29706d74 100644 --- a/simple_message/include/simple_message/socket/tcp_client.h +++ b/simple_message/include/simple_message/socket/tcp_client.h @@ -76,6 +76,9 @@ class TcpClient : public industrial::tcp_socket::TcpSocket // Overrides bool makeConnect(); +private: + bool connectSocketHandle(); + }; diff --git a/simple_message/src/socket/tcp_client.cpp b/simple_message/src/socket/tcp_client.cpp index 279eb0e1..5b40f04d 100644 --- a/simple_message/src/socket/tcp_client.cpp +++ b/simple_message/src/socket/tcp_client.cpp @@ -53,56 +53,38 @@ TcpClient::~TcpClient() bool TcpClient::init(char *buff, int port_num) { - - int rc; - bool rtn; - int disableNodeDelay = 1; addrinfo *result; addrinfo hints = {}; - rc = SOCKET(AF_INET, SOCK_STREAM, 0); - if (this->SOCKET_FAIL != rc) + if (!_connectSocketHandle()) { - this->setSockHandle(rc); - - // The set no delay disables the NAGEL algorithm - rc = SET_NO_DELAY(this->getSockHandle(), disableNodeDelay); - if (this->SOCKET_FAIL == rc) - { - LOG_WARN("Failed to set no socket delay, sending data can be delayed by up to 250ms"); - } - - // Initialize address data structure - memset(&this->sockaddr_, 0, sizeof(this->sockaddr_)); - this->sockaddr_.sin_family = AF_INET; - - // Check for 'buff' as hostname, and use that, otherwise assume IP address - hints.ai_family = AF_INET; // Allow IPv4 - hints.ai_socktype = SOCK_STREAM; // TCP socket - hints.ai_flags = 0; // No flags - hints.ai_protocol = 0; // Any protocol - hints.ai_canonname = NULL; - hints.ai_addr = NULL; - hints.ai_next = NULL; - if (0 == GETADDRINFO(buff, NULL, &hints, &result)) - { - this->sockaddr_ = *((sockaddr_in *)result->ai_addr); - } - else - { - this->sockaddr_.sin_addr.s_addr = INET_ADDR(buff); - } - this->sockaddr_.sin_port = HTONS(port_num); - - rtn = true; + return false; + } + + // Initialize address data structure + memset(&this->sockaddr_, 0, sizeof(this->sockaddr_)); + this->sockaddr_.sin_family = AF_INET; + + // Check for 'buff' as hostname, and use that, otherwise assume IP address + hints.ai_family = AF_INET; // Allow IPv4 + hints.ai_socktype = SOCK_STREAM; // TCP socket + hints.ai_flags = 0; // No flags + hints.ai_protocol = 0; // Any protocol + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + if (0 == GETADDRINFO(buff, NULL, &hints, &result)) + { + this->sockaddr_ = *((sockaddr_in *)result->ai_addr); } else { - LOG_ERROR("Failed to create socket, rc: %d", rc); - rtn = false; + this->sockaddr_.sin_addr.s_addr = INET_ADDR(buff); } - return rtn; + this->sockaddr_.sin_port = HTONS(port_num); + + return true; } bool TcpClient::makeConnect() @@ -111,32 +93,63 @@ bool TcpClient::makeConnect() int rc = this->SOCKET_FAIL; SOCKLEN_T addrSize = 0; - if (!this->isConnected()) + if (isConnected()) { - addrSize = sizeof(this->sockaddr_); - rc = CONNECT(this->getSockHandle(), (sockaddr *)&this->sockaddr_, addrSize); - if (this->SOCKET_FAIL != rc) - { - LOG_INFO("Connected to server"); - this->setConnected(true); - rtn = true; - } - else - { - this->logSocketError("Failed to connect to server", rc, errno); - rtn = false; - } + LOG_WARN("Tried to connect when socket already in connected state"); + return false; } - else + if (!connectSocketHandle()) { - LOG_WARN("Tried to connect when socket already in connected state"); + // Logging handled by connectSocketHandle() + return false; } - return rtn; + int rc = CONNECT(this->getSockHandle(), (sockaddr *)&sockaddr_, sizeof(sockaddr_)); + if (SOCKET_FAIL == rc) + { + logSocketError("Failed to connect to server", rc, errno); + return false; + } + + LOG_INFO("Connected to server"); + setConnected(true); + return true; } +bool TcpClient::_connectSocketHandle() +{ + if (isConnected()) + { + // Already connected, nothing to do + return true; + } + + int sock_handle = getSockHandle(); + + if (sock_handle != SOCKET_FAIL) + { + // Handle is stale close old handle + CLOSE(sock_handle); + } + + sock_handle = SOCKET(AF_INET, SOCK_STREAM, 0); + setSockHandle(sock_handle); + if (SOCKET_FAIL == sock_handle) + { + LOG_ERROR("Failed to create socket"); + return false; + } + + int disableNodeDelay = 1; + // The set no delay disables the NAGEL algorithm + if (SOCKET_FAIL == SET_NO_DELAY(sock_handle, disableNodeDelay)) + { + LOG_WARN("Failed to set no socket delay, sending data can be delayed by up to 250ms"); + } + return true; +} } //tcp_client } //industrial From ab18eeb3f360e7d1d9dcbb295a99ca483cf2c563 Mon Sep 17 00:00:00 2001 From: 98aa713aa8355e307a05fba5721b6ed31d764bb9 Date: Wed, 21 Aug 2019 11:15:42 -0400 Subject: [PATCH 2/3] Issue-238 Cleanup that didn't get pushed --- simple_message/src/socket/tcp_client.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/simple_message/src/socket/tcp_client.cpp b/simple_message/src/socket/tcp_client.cpp index 5b40f04d..40f99471 100644 --- a/simple_message/src/socket/tcp_client.cpp +++ b/simple_message/src/socket/tcp_client.cpp @@ -56,7 +56,7 @@ bool TcpClient::init(char *buff, int port_num) addrinfo *result; addrinfo hints = {}; - if (!_connectSocketHandle()) + if (!connectSocketHandle()) { return false; } @@ -89,10 +89,6 @@ bool TcpClient::init(char *buff, int port_num) bool TcpClient::makeConnect() { - bool rtn = false; - int rc = this->SOCKET_FAIL; - SOCKLEN_T addrSize = 0; - if (isConnected()) { LOG_WARN("Tried to connect when socket already in connected state"); @@ -118,7 +114,7 @@ bool TcpClient::makeConnect() return true; } -bool TcpClient::_connectSocketHandle() +bool TcpClient::connectSocketHandle() { if (isConnected()) { From 648eb3b57648131b00448f3790fd01fbd6902768 Mon Sep 17 00:00:00 2001 From: 98aa713aa8355e307a05fba5721b6ed31d764bb9 Date: Wed, 28 Aug 2019 10:07:18 -0400 Subject: [PATCH 3/3] Issue-238 Add copyright for mods --- simple_message/include/simple_message/socket/tcp_client.h | 1 + simple_message/src/socket/tcp_client.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/simple_message/include/simple_message/socket/tcp_client.h b/simple_message/include/simple_message/socket/tcp_client.h index 29706d74..6e96f3e6 100644 --- a/simple_message/include/simple_message/socket/tcp_client.h +++ b/simple_message/include/simple_message/socket/tcp_client.h @@ -2,6 +2,7 @@ * Software License Agreement (BSD License) * * Copyright (c) 2011, Southwest Research Institute + * Copyright (c) 2019, READY Robotics Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/simple_message/src/socket/tcp_client.cpp b/simple_message/src/socket/tcp_client.cpp index 40f99471..feb564f4 100644 --- a/simple_message/src/socket/tcp_client.cpp +++ b/simple_message/src/socket/tcp_client.cpp @@ -2,6 +2,7 @@ * Software License Agreement (BSD License) * * Copyright (c) 2011, Southwest Research Institute + * Copyright (c) 2019, READY Robotics Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without