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
00079
00080
00081 extern "C" STATUS setsockopt (
00082 int s,
00083 int level,
00084 int optname,
00085 char * optval,
00086 int optlen
00087 );
00088
00089 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00090 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00091
00092
00093
00094
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
00216
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
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 }
00229 }
00230
00231 #endif