tcp_client.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Southwest Research Institute
5  * Copyright (c) 2019, READY Robotics Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * * Neither the name of the Southwest Research Institute, nor the names
17  * of its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef FLATHEADERS
35 #else
36 #include "tcp_client.h"
37 #include "log_wrapper.h"
38 #endif
39 
40 namespace industrial
41 {
42 namespace tcp_client
43 {
44 
46 {
47 
48 }
49 
51 {
52  LOG_DEBUG("Destructing TCPClient");
53 }
54 
55 bool TcpClient::init(char *buff, int port_num)
56 {
57  addrinfo *result;
58  addrinfo hints = {};
59 
60  if (!connectSocketHandle())
61  {
62  return false;
63  }
64 
65  // Initialize address data structure
66  memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
67  this->sockaddr_.sin_family = AF_INET;
68 
69 
70  // Check for 'buff' as hostname, and use that, otherwise assume IP address
71  hints.ai_family = AF_INET; // Allow IPv4
72  hints.ai_socktype = SOCK_STREAM; // TCP socket
73  hints.ai_flags = 0; // No flags
74  hints.ai_protocol = 0; // Any protocol
75  hints.ai_canonname = NULL;
76  hints.ai_addr = NULL;
77  hints.ai_next = NULL;
78  if (0 == GETADDRINFO(buff, NULL, &hints, &result))
79  {
80  this->sockaddr_ = *((sockaddr_in *)result->ai_addr);
81  }
82  else
83  {
84  this->sockaddr_.sin_addr.s_addr = INET_ADDR(buff);
85  }
86  this->sockaddr_.sin_port = HTONS(port_num);
87 
88  return true;
89 }
90 
92 {
93  if (isConnected())
94  {
95  LOG_WARN("Tried to connect when socket already in connected state");
96  return false;
97  }
98 
99  if (!connectSocketHandle())
100  {
101  // Logging handled by connectSocketHandle()
102  return false;
103  }
104 
105  int rc = CONNECT(this->getSockHandle(), (sockaddr *)&sockaddr_, sizeof(sockaddr_));
106  if (SOCKET_FAIL == rc)
107  {
108  logSocketError("Failed to connect to server", rc, errno);
109  return false;
110  }
111 
112  LOG_INFO("Connected to server");
113  setConnected(true);
114 
115  return true;
116 }
117 
119 {
120  if (isConnected())
121  {
122  // Already connected, nothing to do
123  return true;
124  }
125 
126  int sock_handle = getSockHandle();
127 
128  if (sock_handle != SOCKET_FAIL)
129  {
130  // Handle is stale close old handle
131  CLOSE(sock_handle);
132  }
133 
134  sock_handle = SOCKET(AF_INET, SOCK_STREAM, 0);
135  setSockHandle(sock_handle);
136  if (SOCKET_FAIL == sock_handle)
137  {
138  LOG_ERROR("Failed to create socket");
139  return false;
140  }
141 
142  int disableNodeDelay = 1;
143  // The set no delay disables the NAGEL algorithm
144  if (SOCKET_FAIL == SET_NO_DELAY(sock_handle, disableNodeDelay))
145  {
146  LOG_WARN("Failed to set no socket delay, sending data can be delayed by up to 250ms");
147  }
148  return true;
149 }
150 } //tcp_client
151 } //industrial
152 
bool init(char *buff, int port_num)
initializes TCP client socket. Object can either be a client OR a server, NOT BOTH.
Definition: tcp_client.cpp:55
virtual void setConnected(bool connected)
#define LOG_WARN(format,...)
Definition: log_wrapper.h:133
void logSocketError(const char *msg, const int rc, const int error_no)
Logs message to error log and reports associated socket system error.
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
sockaddr_in sockaddr_
address/port of remote socket
#define LOG_INFO(format,...)
Definition: log_wrapper.h:132
#define LOG_DEBUG(format,...)
Definition: log_wrapper.h:131
bool isConnected()
return connection status
static const int SOCKET_FAIL
socket fail return value
bool makeConnect()
connects to the remote host
Definition: tcp_client.cpp:91


simple_message
Author(s): Shaun Edwards
autogenerated on Mon Jul 19 2021 12:45:19