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


simple_message
Author(s): Shaun Edwards
autogenerated on Sat Sep 21 2019 03:30:09