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 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00079 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00080 #define SET_NO_DELAY(sockfd, val) -1 //MOTOPLUS does not allow for setting the "no delay" socket option
00081 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
00082 #define LISTEN(sockfd, n) mpListen(sockfd, n)
00083 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
00084 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
00085 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
00086 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
00087 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
00088 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
00089 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
00090 #define CLOSE(fd) mpClose(fd)
00091 #define HTONS(num) mpHtons(num)
00092 #define INET_ADDR(str) mpInetAddr(str)
00093 #define SOCKLEN_T unsigned int
00094 
00095 #endif
00096 
00097 namespace industrial
00098 {
00099 namespace simple_socket
00100 {
00101 
00107 namespace StandardSocketPorts
00108 {
00109 enum StandardSocketPort
00110 {
00111   MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
00112 };
00113 }
00114 typedef StandardSocketPorts::StandardSocketPort StandardSocketPort;
00115 
00119 class SimpleSocket : public industrial::smpl_msg_connection::SmplMsgConnection
00120 {
00121 public:
00122 
00126   SimpleSocket(){}
00127 
00131   virtual ~SimpleSocket(){}
00132 
00133   bool isConnected()
00134   {
00135     return connected_;
00136   }
00137 
00138 protected:
00139 
00143   int sock_handle_;
00144 
00148   sockaddr_in sockaddr_;
00149   
00153   bool connected_;
00154 
00158   static const int SOCKET_FAIL = -1;
00159 
00164   static const int MAX_BUFFER_SIZE = 1024;
00168   char buffer_[MAX_BUFFER_SIZE + 1];
00169 
00170   int  getSockHandle() const
00171   {
00172     return sock_handle_;
00173   }
00174 
00175   void setSockHandle(int sock_handle_)
00176   {
00177     this->sock_handle_ = sock_handle_;
00178   }
00179   
00180   virtual void setConnected(bool connected)
00181   {
00182     this->connected_ = connected;
00183   }
00184 
00185   void logSocketError(char* msg, int rc)
00186   {
00187     LOG_ERROR("%s, rc: %d, errno: %d", msg, rc, errno);
00188   }
00189   
00197   bool isReadyReceive(int timeout);
00198   
00199   // Send/Receive functions (inherited classes should override raw methods
00200   // Virtual
00201   bool sendBytes(industrial::byte_array::ByteArray & buffer);
00202   bool receiveBytes(industrial::byte_array::ByteArray & buffer,
00203       industrial::shared_types::shared_int num_bytes);
00204   // Virtual
00205   virtual int rawSendBytes(char *buffer,
00206       industrial::shared_types::shared_int num_bytes)=0;
00207   virtual int rawReceiveBytes(char *buffer,
00208       industrial::shared_types::shared_int num_bytes)=0;
00209 
00210 };
00211 
00212 } //simple_socket
00213 } //industrial
00214 
00215 #endif /* SIMPLE_SOCKET_H */


simple_message
Author(s): Shaun Edwards
autogenerated on Fri Jan 3 2014 11:26:56