00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00200
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
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 }
00213 }
00214
00215 #endif