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 #define GETHOSTBYNAME(str) gethostbyname(str)
00073 
00074 #endif
00075 
00076 #ifdef MOTOPLUS
00077 
00078 #include "motoPlus.h"
00079 
00080 #include "errno.h"
00081 
00082 // Including os defintion for set socket option.  The motoplus wrappers do not give access to socket
00083 // options.  In order to remove system delays the nagel algorithm must be disabled using the
00084 // TPC_NO_DELAY option
00085 extern "C" STATUS setsockopt (   /* remove "extern C", if you're using C instead of C++ */
00086     int    s,                 /* target socket */
00087     int    level,             /* protocol level of option */
00088     int    optname,           /* option name */
00089     char * optval,            /* pointer to option value */
00090     int    optlen             /* option length */
00091     );
00092 
00093 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00094 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00095 
00096 // Motoplus compliant version (i.e. a NOOP)
00097 // #define SET_NO_DELAY(sockfd, val) -1 //MOTOPLUS does not allow for setting the "no delay" socket option
00098 // Raw OS call, not Motoplus compliant and might not be allowed in future versions. (taking a risk at this point)
00099 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
00100 
00101 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
00102 #define LISTEN(sockfd, n) mpListen(sockfd, n)
00103 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
00104 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
00105 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
00106 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
00107 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
00108 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
00109 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
00110 #define CLOSE(fd) mpClose(fd)
00111 #define HTONS(num) mpHtons(num)
00112 #define INET_ADDR(str) mpInetAddr(str)
00113 #define SOCKLEN_T unsigned int
00114 #define GETHOSTBYNAME(str) NULL
00115 
00116 #endif
00117 
00118 namespace industrial
00119 {
00120 namespace simple_socket
00121 {
00122 
00128 namespace StandardSocketPorts
00129 {
00130 enum StandardSocketPort
00131 {
00132   MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
00133 };
00134 }
00135 typedef StandardSocketPorts::StandardSocketPort StandardSocketPort;
00136 
00140 class SimpleSocket : public industrial::smpl_msg_connection::SmplMsgConnection
00141 {
00142 public:
00143 
00147   SimpleSocket()
00148   {
00149     this->setSockHandle(this->SOCKET_FAIL);
00150     memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00151     this->setConnected(false);
00152   }
00153 
00157   virtual ~SimpleSocket(){}
00158 
00159   bool isConnected()
00160   {
00161     return connected_;
00162   }
00163   
00164   // Internally set the state of the connection to be disconnected.
00165   // This is needed in UDP connections to signal when a timeout has occurred 
00166   // and the connection needs to be reestablished using the handshake protocol.
00167   virtual void setDisconnected()
00168   {
00169     setConnected(false);
00170   }
00171   
00179   bool isReadyReceive(int timeout)
00180   {
00181     bool r, e;
00182     rawPoll(timeout, r, e);
00183     return r;
00184   }
00185 
00186 protected:
00187 
00191   int sock_handle_;
00192 
00196   sockaddr_in sockaddr_;
00197   
00201   bool connected_;
00202 
00206   static const int SOCKET_FAIL = -1;
00207 
00212   static const int MAX_BUFFER_SIZE = 1024;
00213 
00217   static const int SOCKET_POLL_TO = 1000;
00218 
00222   char buffer_[MAX_BUFFER_SIZE + 1];
00223 
00224   int  getSockHandle() const
00225   {
00226     return sock_handle_;
00227   }
00228 
00229   void setSockHandle(int sock_handle_)
00230   {
00231     this->sock_handle_ = sock_handle_;
00232   }
00233 
00241   __attribute__((deprecated(
00242                    "Please use: logSocketError(const char* msg, const int rc, const int error_no)")))
00243   void logSocketError(const char* msg, int rc)
00244   {
00245     logSocketError(msg, rc, errno);
00246   }
00247 
00254   void logSocketError(const char* msg, const int rc, const int error_no)
00255   {
00256     LOG_ERROR("%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(error_no), error_no);
00257   }
00258   
00259   // Send/Receive functions (inherited classes should override raw methods
00260   // Virtual
00261   bool sendBytes(industrial::byte_array::ByteArray & buffer);
00262   bool receiveBytes(industrial::byte_array::ByteArray & buffer,
00263       industrial::shared_types::shared_int num_bytes);
00264   // Virtual
00265   virtual int rawSendBytes(char *buffer,
00266       industrial::shared_types::shared_int num_bytes)=0;
00267   virtual int rawReceiveBytes(char *buffer,
00268       industrial::shared_types::shared_int num_bytes)=0;
00278   virtual bool rawPoll(int timeout, bool & ready, bool & error)=0;
00279   virtual void setConnected(bool connected)
00280   {
00281     this->connected_ = connected;
00282   }
00283 
00284 };
00285 
00286 } //simple_socket
00287 } //industrial
00288 
00289 #endif /* SIMPLE_SOCKET_H */


simple_message
Author(s): Shaun Edwards
autogenerated on Tue Jan 17 2017 21:10:02