simple_socket.h
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 
32 #ifndef SIMPLE_SOCKET_H
33 #define SIMPLE_SOCKET_H
34 
35 #ifndef FLATHEADERS
39 #else
40 #include "log_wrapper.h"
41 #include "shared_types.h"
42 #include "smpl_msg_connection.h"
43 #endif
44 
45 // remove LINUXSOCKETS after Melodic (bw compat for #262)
46 #if defined(SIMPLE_MESSAGE_LINUX) || defined(LINUXSOCKETS)
47 
48 #ifndef _WIN32
49 #include "sys/socket.h"
50 #include "arpa/inet.h"
51 #include "unistd.h"
52 #include "netinet/tcp.h"
53 #else
54 #include <ws2tcpip.h>
55 #endif
56 #include "string.h"
57 #include "errno.h"
58 
59 #define SOCKET(domain, type, protocol) socket(domain, type, protocol)
60 #define BIND(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
61 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&(val)), sizeof(val))
62 #define SET_REUSE_ADDR(sockfd, val) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&(val)), sizeof(val))
63 #define LISTEN(sockfd, n) listen(sockfd, n)
64 #define ACCEPT(sockfd, addr, addrlen) accept(sockfd, addr, addrlen)
65 #define CONNECT(sockfd, dest_addr ,addrlen) connect(sockfd, dest_addr, addrlen)
66 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen)
67 #define SEND(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
68 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, buf, len, flags, src_addr, addrlen)
69 #define RECV(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
70 #define SELECT(n, readfds, writefds, exceptfds, timeval) select(n, readfds, writefds, exceptfds, timeval)
71 #ifdef _WIN32
72 #define CLOSE(fd) closesocket(fd)
73 #else
74 #define CLOSE(fd) close(fd)
75 #endif
76 #ifndef HTONS // OSX defines HTONS
77 #define HTONS(num) htons(num)
78 #endif
79 #define INET_ADDR(str) inet_addr(str)
80 #define SOCKLEN_T socklen_t
81 #define GETADDRINFO(node, service, hints, results) getaddrinfo(node, service, hints, results)
82 
83 #endif
84 
85 #ifdef SIMPLE_MESSAGE_MOTOPLUS
86 
87 #include "motoPlus.h"
88 
89 #include "errno.h"
90 
91 // Including os defintion for set socket option. The motoplus wrappers do not give access to socket
92 // options. In order to remove system delays the nagel algorithm must be disabled using the
93 // TPC_NO_DELAY option
94 extern "C" STATUS setsockopt ( /* remove "extern C", if you're using C instead of C++ */
95  int s, /* target socket */
96  int level, /* protocol level of option */
97  int optname, /* option name */
98  char * optval, /* pointer to option value */
99  int optlen /* option length */
100  );
101 
102 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
103 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
104 
105 // Motoplus compliant version (i.e. a NOOP)
106 // #define SET_NO_DELAY(sockfd, val) -1 //MOTOPLUS does not allow for setting the "no delay" socket option
107 // Raw OS call, not Motoplus compliant and might not be allowed in future versions. (taking a risk at this point)
108 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
109 
110 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
111 #define LISTEN(sockfd, n) mpListen(sockfd, n)
112 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
113 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
114 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
115 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
116 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
117 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
118 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
119 #define CLOSE(fd) mpClose(fd)
120 #define HTONS(num) mpHtons(num)
121 #define INET_ADDR(str) mpInetAddr(str)
122 #define SOCKLEN_T unsigned int
123 #define GETHOSTBYNAME(str) NULL
124 
125 #endif
126 
127 namespace industrial
128 {
129 namespace simple_socket
130 {
131 
137 namespace StandardSocketPorts
138 {
140 {
141  MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
142 };
143 }
145 
150 {
151 public:
152 
157  {
158  this->setSockHandle(this->SOCKET_FAIL);
159  memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
160  this->setConnected(false);
161  }
162 
166  virtual ~SimpleSocket(){}
167 
168  bool isConnected()
169  {
170  return connected_;
171  }
172 
173  // Internally set the state of the connection to be disconnected.
174  // This is needed in UDP connections to signal when a timeout has occurred
175  // and the connection needs to be reestablished using the handshake protocol.
176  virtual void setDisconnected()
177  {
178  setConnected(false);
179  }
180 
188  bool isReadyReceive(int timeout)
189  {
190  bool r, e;
191  rawPoll(timeout, r, e);
192  return r;
193  }
194 
195 protected:
196 
201 
205  sockaddr_in sockaddr_;
206 
211 
215  static const int SOCKET_FAIL = -1;
216 
221  static const int MAX_BUFFER_SIZE = 1024;
222 
226  static const int SOCKET_POLL_TO = 10;
227 
232 
233  int getSockHandle() const
234  {
235  return sock_handle_;
236  }
237 
239  {
240  this->sock_handle_ = sock_handle_;
241  }
242 
250 #ifndef _MSC_VER
251  __attribute__((deprecated(
252  "Please use: logSocketError(const char* msg, const int rc, const int error_no)")))
253 #endif
254  void logSocketError(const char* msg, int rc)
255  {
256  logSocketError(msg, rc, errno);
257  }
258 
265  void logSocketError(const char* msg, const int rc, const int error_no)
266  {
267  LOG_ERROR("%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(error_no), error_no);
268  }
269 
270  // Send/Receive functions (inherited classes should override raw methods
271  // Virtual
276 
277  // Virtual
278  virtual int rawSendBytes(char *buffer,
280  virtual int rawReceiveBytes(char *buffer,
291  virtual bool rawPoll(int timeout, bool & ready, bool & error)=0;
292  virtual void setConnected(bool connected)
293  {
294  this->connected_ = connected;
295  }
296 
297 };
298 
299 } //simple_socket
300 } //industrial
301 
302 #endif /* SIMPLE_SOCKET_H */
industrial::simple_socket::SimpleSocket::receiveBytes
bool receiveBytes(industrial::byte_array::ByteArray &buffer, industrial::shared_types::shared_int num_bytes, industrial::shared_types::shared_int timeout_ms)
Method used by receive message interface method. This should be overridden for the specific connectio...
Definition: simple_socket.cpp:98
industrial::simple_socket::SimpleSocket::connected_
bool connected_
flag indicating socket connection status
Definition: simple_socket.h:210
industrial::simple_socket::SimpleSocket::SOCKET_POLL_TO
static const int SOCKET_POLL_TO
socket ready polling timeout (ms)
Definition: simple_socket.h:226
industrial::simple_socket::StandardSocketPorts::StandardSocketPort
StandardSocketPort
Definition: simple_socket.h:139
industrial::simple_socket::SimpleSocket::MAX_BUFFER_SIZE
static const int MAX_BUFFER_SIZE
maximum size of buffer for receiving data (fixed memory size used in order to avoid dynamic memory al...
Definition: simple_socket.h:221
industrial
Definition: byte_array.h:45
industrial::simple_socket::SimpleSocket::~SimpleSocket
virtual ~SimpleSocket()
Destructor.
Definition: simple_socket.h:166
industrial::simple_socket::SimpleSocket::rawSendBytes
virtual int rawSendBytes(char *buffer, industrial::shared_types::shared_int num_bytes)=0
industrial::simple_socket::StandardSocketPort
StandardSocketPorts::StandardSocketPort StandardSocketPort
Definition: simple_socket.h:144
industrial::simple_socket::SimpleSocket::getSockHandle
int getSockHandle() const
Definition: simple_socket.h:233
industrial::simple_socket::SimpleSocket::isConnected
bool isConnected()
return connection status
Definition: simple_socket.h:168
industrial::simple_socket::SimpleSocket::rawReceiveBytes
virtual int rawReceiveBytes(char *buffer, industrial::shared_types::shared_int num_bytes)=0
industrial::simple_socket::StandardSocketPorts::SYSTEM
@ SYSTEM
Definition: simple_socket.h:141
industrial::simple_socket::SimpleSocket::__attribute__
__attribute__((deprecated("Please use: logSocketError(const char* msg, const int rc, const int error_no)"))) void logSocketError(const char *msg
Logs message to error log and reports associated socket system error.
LOG_ERROR
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:134
industrial::simple_socket::SimpleSocket::isReadyReceive
bool isReadyReceive(int timeout)
returns true if socket data is ready to receive
Definition: simple_socket.h:188
industrial::simple_socket::SimpleSocket::sockaddr_
sockaddr_in sockaddr_
address/port of remote socket
Definition: simple_socket.h:205
industrial::simple_socket::SimpleSocket::logSocketError
void logSocketError(const char *msg, const int rc, const int error_no)
Logs message to error log and reports associated socket system error.
Definition: simple_socket.h:265
industrial::simple_socket::StandardSocketPorts::MOTION
@ MOTION
Definition: simple_socket.h:141
industrial::byte_array::ByteArray
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
industrial::simple_socket::SimpleSocket::setDisconnected
virtual void setDisconnected()
Definition: simple_socket.h:176
industrial::simple_socket::SimpleSocket::buffer_
char buffer_[MAX_BUFFER_SIZE+1]
internal data buffer for receiving
Definition: simple_socket.h:231
industrial::simple_socket::SimpleSocket::SimpleSocket
SimpleSocket()
Constructor.
Definition: simple_socket.h:156
shared_types.h
industrial::simple_socket::SimpleSocket::setSockHandle
void setSockHandle(int sock_handle_)
Definition: simple_socket.h:238
industrial::simple_socket::StandardSocketPorts::STATE
@ STATE
Definition: simple_socket.h:141
log_wrapper.h
industrial::smpl_msg_connection::SmplMsgConnection
Defines an interface and common methods for sending simple messages (see simple_message)....
Definition: smpl_msg_connection.h:61
industrial::shared_types::shared_int
int shared_int
Definition: shared_types.h:62
industrial::simple_socket::SimpleSocket::sock_handle_
int sock_handle_
socket handle for sending/receiving data
Definition: simple_socket.h:200
industrial::simple_socket::StandardSocketPorts::IO
@ IO
Definition: simple_socket.h:141
industrial::simple_socket::SimpleSocket::SOCKET_FAIL
static const int SOCKET_FAIL
socket fail return value
Definition: simple_socket.h:215
industrial::simple_socket::SimpleSocket::rawPoll
virtual bool rawPoll(int timeout, bool &ready, bool &error)=0
polls socket for data or error
industrial::simple_message::StandardMsgTypes::STATUS
@ STATUS
Definition: simple_message.h:72
industrial::simple_socket::SimpleSocket::sendBytes
bool sendBytes(industrial::byte_array::ByteArray &buffer)
Method used by send message interface method. This should be overridden for the specific connection t...
Definition: simple_socket.cpp:48
industrial::simple_socket::SimpleSocket
Defines socket functions required for a simple connection type.
Definition: simple_socket.h:149
industrial::simple_socket::SimpleSocket::rc
int rc
Definition: simple_socket.h:255
smpl_msg_connection.h
industrial::simple_socket::SimpleSocket::setConnected
virtual void setConnected(bool connected)
Definition: simple_socket.h:292


simple_message
Author(s): Shaun Edwards
autogenerated on Wed Mar 2 2022 00:24:53