simple_socket.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Southwest Research Institute
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *       * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *       * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *       * Neither the name of the Southwest Research Institute, nor the names
00016  *       of its contributors may be used to endorse or promote products derived
00017  *       from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #ifndef SIMPLE_SOCKET_H
00033 #define SIMPLE_SOCKET_H
00034 
00035 #ifndef FLATHEADERS
00036 #include "simple_message/log_wrapper.h"
00037 #include "simple_message/shared_types.h"
00038 #include "simple_message/smpl_msg_connection.h"
00039 #else
00040 #include "log_wrapper.h"
00041 #include "shared_types.h"
00042 #include "smpl_msg_connection.h"
00043 #endif
00044 
00045 #ifdef LINUXSOCKETS
00046 
00047 #include "sys/socket.h"
00048 #include "arpa/inet.h"
00049 #include "string.h"
00050 #include "unistd.h"
00051 #include "netinet/tcp.h"
00052 #include "errno.h"
00053 
00054 #define SOCKET(domain, type, protocol) socket(domain, type, protocol)
00055 #define BIND(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
00056 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val))
00057 #define SET_REUSE_ADDR(sockfd, val) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))
00058 #define LISTEN(sockfd, n) listen(sockfd, n)
00059 #define ACCEPT(sockfd, addr, addrlen) accept(sockfd, addr, addrlen)
00060 #define CONNECT(sockfd, dest_addr ,addrlen) connect(sockfd, dest_addr, addrlen)
00061 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen)
00062 #define SEND(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
00063 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, buf, len, flags, src_addr, addrlen)
00064 #define RECV(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
00065 #define SELECT(n, readfds, writefds, exceptfds, timeval) select(n, readfds, writefds, exceptfds, timeval)
00066 #define CLOSE(fd) close(fd)
00067 #ifndef HTONS // OSX defines HTONS
00068 #define HTONS(num) htons(num)
00069 #endif
00070 #define INET_ADDR(str) inet_addr(str)
00071 #define SOCKLEN_T socklen_t
00072 
00073 #endif
00074 
00075 #ifdef MOTOPLUS
00076 
00077 #include "motoPlus.h"
00078 
00079 #include "errno.h"
00080 
00081 // Including os defintion for set socket option.  The motoplus wrappers do not give access to socket
00082 // options.  In order to remove system delays the nagel algorithm must be disabled using the
00083 // TPC_NO_DELAY option
00084 extern "C" STATUS setsockopt (   /* remove "extern C", if you're using C instead of C++ */
00085     int    s,                 /* target socket */
00086     int    level,             /* protocol level of option */
00087     int    optname,           /* option name */
00088     char * optval,            /* pointer to option value */
00089     int    optlen             /* option length */
00090     );
00091 
00092 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00093 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00094 
00095 // Motoplus compliant version (i.e. a NOOP)
00096 // #define SET_NO_DELAY(sockfd, val) -1 //MOTOPLUS does not allow for setting the "no delay" socket option
00097 // Raw OS call, not Motoplus compliant and might not be allowed in future versions. (taking a risk at this point)
00098 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
00099 
00100 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
00101 #define LISTEN(sockfd, n) mpListen(sockfd, n)
00102 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
00103 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
00104 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
00105 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
00106 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
00107 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
00108 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
00109 #define CLOSE(fd) mpClose(fd)
00110 #define HTONS(num) mpHtons(num)
00111 #define INET_ADDR(str) mpInetAddr(str)
00112 #define SOCKLEN_T unsigned int
00113 
00114 #endif
00115 
00116 namespace industrial
00117 {
00118 namespace simple_socket
00119 {
00120 
00126 namespace StandardSocketPorts
00127 {
00128 enum StandardSocketPort
00129 {
00130   MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
00131 };
00132 }
00133 typedef StandardSocketPorts::StandardSocketPort StandardSocketPort;
00134 
00138 class SimpleSocket : public industrial::smpl_msg_connection::SmplMsgConnection
00139 {
00140 public:
00141 
00145   SimpleSocket(){}
00146 
00150   virtual ~SimpleSocket(){}
00151 
00152   bool isConnected()
00153   {
00154     return connected_;
00155   }
00156   
00164   bool isReadyReceive(int timeout)
00165   {
00166     bool r, e;
00167     return poll(timeout, r, e);
00168   }
00169 
00170 protected:
00171 
00175   int sock_handle_;
00176 
00180   sockaddr_in sockaddr_;
00181   
00185   bool connected_;
00186 
00190   static const int SOCKET_FAIL = -1;
00191 
00196   static const int MAX_BUFFER_SIZE = 1024;
00197 
00201   static const int SOCKET_POLL_TO = 1000;
00202 
00206   char buffer_[MAX_BUFFER_SIZE + 1];
00207 
00208   int  getSockHandle() const
00209   {
00210     return sock_handle_;
00211   }
00212 
00213   void setSockHandle(int sock_handle_)
00214   {
00215     this->sock_handle_ = sock_handle_;
00216   }
00217   
00218   virtual void setConnected(bool connected)
00219   {
00220     this->connected_ = connected;
00221   }
00222 
00223   void logSocketError(const char* msg, int rc)
00224   {
00225     int errno_ = errno;
00226     LOG_ERROR("%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(errno_), errno_);
00227   }
00228   
00238   bool poll(int timeout, bool & ready, bool & error);
00239   
00240   // Send/Receive functions (inherited classes should override raw methods
00241   // Virtual
00242   bool sendBytes(industrial::byte_array::ByteArray & buffer);
00243   bool receiveBytes(industrial::byte_array::ByteArray & buffer,
00244       industrial::shared_types::shared_int num_bytes);
00245   // Virtual
00246   virtual int rawSendBytes(char *buffer,
00247       industrial::shared_types::shared_int num_bytes)=0;
00248   virtual int rawReceiveBytes(char *buffer,
00249       industrial::shared_types::shared_int num_bytes)=0;
00250 
00251 };
00252 
00253 } //simple_socket
00254 } //industrial
00255 
00256 #endif /* SIMPLE_SOCKET_H */


simple_message
Author(s): Shaun Edwards
autogenerated on Fri Aug 28 2015 11:11:56