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 #ifdef ROS
00036 
00037 #include "sys/socket.h"
00038 #include "arpa/inet.h"
00039 #include "string.h"
00040 #include "unistd.h"
00041 #include "netinet/tcp.h"
00042 #include "errno.h"
00043 
00044 #include "simple_message/log_wrapper.h"
00045 #include "simple_message/shared_types.h"
00046 #include "simple_message/smpl_msg_connection.h"
00047 
00048 #define SOCKET(domain, type, protocol) socket(domain, type, protocol)
00049 #define BIND(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
00050 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val))
00051 #define SET_REUSE_ADDR(sockfd, val) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))
00052 #define LISTEN(sockfd, n) listen(sockfd, n)
00053 #define ACCEPT(sockfd, addr, addrlen) accept(sockfd, addr, addrlen)
00054 #define CONNECT(sockfd, dest_addr ,addrlen) connect(sockfd, dest_addr, addrlen)
00055 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen)
00056 #define SEND(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
00057 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, buf, len, flags, src_addr, addrlen)
00058 #define RECV(sockfd, buf, len, flags) recv(sockfd, buf, len, flags)
00059 #define SELECT(n, readfds, writefds, exceptfds, timeval) select(n, readfds, writefds, exceptfds, timeval)
00060 #define CLOSE(fd) close(fd)
00061 #ifndef HTONS // OSX defines HTONS
00062 #define HTONS(num) htons(num)
00063 #endif
00064 #define INET_ADDR(str) inet_addr(str)
00065 #define SOCKLEN_T socklen_t
00066 
00067 #endif
00068 
00069 #ifdef MOTOPLUS
00070 
00071 #include "motoPlus.h"
00072 
00073 #include "errno.h"
00074 #include "log_wrapper.h"
00075 #include "shared_types.h"
00076 #include "smpl_msg_connection.h"
00077 
00078 // Including os defintion for set socket option.  The motoplus wrappers do not give access to socket
00079 // options.  In order to remove system delays the nagel algorithm must be disabled using the
00080 // TPC_NO_DELAY option
00081 extern "C" STATUS setsockopt (   /* remove "extern C", if you're using C instead of C++ */
00082     int    s,                 /* target socket */
00083     int    level,             /* protocol level of option */
00084     int    optname,           /* option name */
00085     char * optval,            /* pointer to option value */
00086     int    optlen             /* option length */
00087     );
00088 
00089 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00090 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00091 
00092 // Motoplus compliant version (i.e. a NOOP)
00093 // #define SET_NO_DELAY(sockfd, val) -1 //MOTOPLUS does not allow for setting the "no delay" socket option
00094 // Raw OS call, not Motoplus compliant and might not be allowed in future versions. (taking a risk at this point)
00095 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
00096 
00097 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
00098 #define LISTEN(sockfd, n) mpListen(sockfd, n)
00099 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
00100 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
00101 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
00102 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
00103 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
00104 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
00105 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
00106 #define CLOSE(fd) mpClose(fd)
00107 #define HTONS(num) mpHtons(num)
00108 #define INET_ADDR(str) mpInetAddr(str)
00109 #define SOCKLEN_T unsigned int
00110 
00111 #endif
00112 
00113 namespace industrial
00114 {
00115 namespace simple_socket
00116 {
00117 
00123 namespace StandardSocketPorts
00124 {
00125 enum StandardSocketPort
00126 {
00127   MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
00128 };
00129 }
00130 typedef StandardSocketPorts::StandardSocketPort StandardSocketPort;
00131 
00135 class SimpleSocket : public industrial::smpl_msg_connection::SmplMsgConnection
00136 {
00137 public:
00138 
00142   SimpleSocket(){}
00143 
00147   virtual ~SimpleSocket(){}
00148 
00149   bool isConnected()
00150   {
00151     return connected_;
00152   }
00153 
00154 protected:
00155 
00159   int sock_handle_;
00160 
00164   sockaddr_in sockaddr_;
00165   
00169   bool connected_;
00170 
00174   static const int SOCKET_FAIL = -1;
00175 
00180   static const int MAX_BUFFER_SIZE = 1024;
00184   char buffer_[MAX_BUFFER_SIZE + 1];
00185 
00186   int  getSockHandle() const
00187   {
00188     return sock_handle_;
00189   }
00190 
00191   void setSockHandle(int sock_handle_)
00192   {
00193     this->sock_handle_ = sock_handle_;
00194   }
00195   
00196   virtual void setConnected(bool connected)
00197   {
00198     this->connected_ = connected;
00199   }
00200 
00201   void logSocketError(char* msg, int rc)
00202   {
00203     LOG_ERROR("%s, rc: %d, errno: %d", msg, rc, errno);
00204   }
00205   
00213   bool isReadyReceive(int timeout);
00214   
00215   // Send/Receive functions (inherited classes should override raw methods
00216   // Virtual
00217   bool sendBytes(industrial::byte_array::ByteArray & buffer);
00218   bool receiveBytes(industrial::byte_array::ByteArray & buffer,
00219       industrial::shared_types::shared_int num_bytes);
00220   // Virtual
00221   virtual int rawSendBytes(char *buffer,
00222       industrial::shared_types::shared_int num_bytes)=0;
00223   virtual int rawReceiveBytes(char *buffer,
00224       industrial::shared_types::shared_int num_bytes)=0;
00225 
00226 };
00227 
00228 } //simple_socket
00229 } //industrial
00230 
00231 #endif /* SIMPLE_SOCKET_H */


simple_message
Author(s): Shaun Edwards
autogenerated on Mon Oct 6 2014 00:54:18