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 #ifdef LINUXSOCKETS
46 
47 #include "sys/socket.h"
48 #include "arpa/inet.h"
49 #include "string.h"
50 #include "unistd.h"
51 #include "netinet/tcp.h"
52 #include "errno.h"
53 
54 #define SOCKET(domain, type, protocol) socket(domain, type, protocol)
55 #define BIND(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
56 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &(val), sizeof(val))
57 #define SET_REUSE_ADDR(sockfd, val) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(val), sizeof(val))
58 #define LISTEN(sockfd, n) listen(sockfd, n)
59 #define ACCEPT(sockfd, addr, addrlen) accept(sockfd, addr, addrlen)
60 #define CONNECT(sockfd, dest_addr ,addrlen) connect(sockfd, dest_addr, addrlen)
61 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen)
62 #define SEND(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
63 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, buf, len, flags, src_addr, addrlen)
64 #define RECV(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
65 #define SELECT(n, readfds, writefds, exceptfds, timeval) select(n, readfds, writefds, exceptfds, timeval)
66 #define CLOSE(fd) close(fd)
67 #ifndef HTONS // OSX defines HTONS
68 #define HTONS(num) htons(num)
69 #endif
70 #define INET_ADDR(str) inet_addr(str)
71 #define SOCKLEN_T socklen_t
72 #define GETADDRINFO(node, service, hints, results) getaddrinfo(node, service, hints, results)
73 
74 #endif
75 
76 #ifdef MOTOPLUS
77 
78 #include "motoPlus.h"
79 
80 #include "errno.h"
81 
82 // Including os defintion for set socket option. The motoplus wrappers do not give access to socket
83 // options. In order to remove system delays the nagel algorithm must be disabled using the
84 // TPC_NO_DELAY option
85 extern "C" STATUS setsockopt ( /* remove "extern C", if you're using C instead of C++ */
86  int s, /* target socket */
87  int level, /* protocol level of option */
88  int optname, /* option name */
89  char * optval, /* pointer to option value */
90  int optlen /* option length */
91  );
92 
93 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
94 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
95 
96 // Motoplus compliant version (i.e. a NOOP)
97 // #define SET_NO_DELAY(sockfd, val) -1 //MOTOPLUS does not allow for setting the "no delay" socket option
98 // Raw OS call, not Motoplus compliant and might not be allowed in future versions. (taking a risk at this point)
99 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
100 
101 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
102 #define LISTEN(sockfd, n) mpListen(sockfd, n)
103 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
104 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
105 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
106 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
107 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
108 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
109 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
110 #define CLOSE(fd) mpClose(fd)
111 #define HTONS(num) mpHtons(num)
112 #define INET_ADDR(str) mpInetAddr(str)
113 #define SOCKLEN_T unsigned int
114 #define GETHOSTBYNAME(str) NULL
115 
116 #endif
117 
118 namespace industrial
119 {
120 namespace simple_socket
121 {
122 
128 namespace StandardSocketPorts
129 {
131 {
132  MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
133 };
134 }
136 
141 {
142 public:
143 
148  {
149  this->setSockHandle(this->SOCKET_FAIL);
150  memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
151  this->setConnected(false);
152  }
153 
157  virtual ~SimpleSocket(){}
158 
159  bool isConnected()
160  {
161  return connected_;
162  }
163 
164  // Internally set the state of the connection to be disconnected.
165  // This is needed in UDP connections to signal when a timeout has occurred
166  // and the connection needs to be reestablished using the handshake protocol.
167  virtual void setDisconnected()
168  {
169  setConnected(false);
170  }
171 
179  bool isReadyReceive(int timeout)
180  {
181  bool r, e;
182  rawPoll(timeout, r, e);
183  return r;
184  }
185 
186 protected:
187 
192 
196  sockaddr_in sockaddr_;
197 
202 
206  static const int SOCKET_FAIL = -1;
207 
212  static const int MAX_BUFFER_SIZE = 1024;
213 
217  static const int SOCKET_POLL_TO = 1000;
218 
222  char buffer_[MAX_BUFFER_SIZE + 1];
223 
224  int getSockHandle() const
225  {
226  return sock_handle_;
227  }
228 
229  void setSockHandle(int sock_handle_)
230  {
231  this->sock_handle_ = sock_handle_;
232  }
233 
241  __attribute__((deprecated(
242  "Please use: logSocketError(const char* msg, const int rc, const int error_no)")))
243  void logSocketError(const char* msg, int rc)
244  {
245  logSocketError(msg, rc, errno);
246  }
247 
254  void logSocketError(const char* msg, const int rc, const int error_no)
255  {
256  LOG_ERROR("%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(error_no), error_no);
257  }
258 
259  // Send/Receive functions (inherited classes should override raw methods
260  // Virtual
261  bool sendBytes(industrial::byte_array::ByteArray & buffer);
262  bool receiveBytes(industrial::byte_array::ByteArray & buffer,
264  // Virtual
265  virtual int rawSendBytes(char *buffer,
267  virtual int rawReceiveBytes(char *buffer,
278  virtual bool rawPoll(int timeout, bool & ready, bool & error)=0;
279  virtual void setConnected(bool connected)
280  {
281  this->connected_ = connected;
282  }
283 
284 };
285 
286 } //simple_socket
287 } //industrial
288 
289 #endif /* SIMPLE_SOCKET_H */
virtual void setConnected(bool connected)
Defines socket functions required for a simple connection type.
Defines an interface and common methods for sending simple messages (see simple_message). This interface makes a bare minimum of assumptions:
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
bool isReadyReceive(int timeout)
returns true if socket data is ready to receive
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
bool connected_
flag indicating socket connection status
bool isConnected()
return connection status
int sock_handle_
socket handle for sending/receiving data


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