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 #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
00073 #endif
00074
00075 #ifdef MOTOPLUS
00076
00077 #include "motoPlus.h"
00078
00079 #include "errno.h"
00080
00081
00082
00083
00084 extern "C" STATUS setsockopt (
00085 int s,
00086 int level,
00087 int optname,
00088 char * optval,
00089 int optlen
00090 );
00091
00092 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00093 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00094
00095
00096
00097
00098 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
00099
00100 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
00101 #define LISTEN(sockfd, n) mpListen(sockfd, n)
00102 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
00103 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
00104 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
00105 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
00106 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
00107 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
00108 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
00109 #define CLOSE(fd) mpClose(fd)
00110 #define HTONS(num) mpHtons(num)
00111 #define INET_ADDR(str) mpInetAddr(str)
00112 #define SOCKLEN_T unsigned int
00113
00114 #endif
00115
00116 namespace industrial
00117 {
00118 namespace simple_socket
00119 {
00120
00126 namespace StandardSocketPorts
00127 {
00128 enum StandardSocketPort
00129 {
00130 MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
00131 };
00132 }
00133 typedef StandardSocketPorts::StandardSocketPort StandardSocketPort;
00134
00138 class SimpleSocket : public industrial::smpl_msg_connection::SmplMsgConnection
00139 {
00140 public:
00141
00145 SimpleSocket(){}
00146
00150 virtual ~SimpleSocket(){}
00151
00152 bool isConnected()
00153 {
00154 return connected_;
00155 }
00156
00164 bool isReadyReceive(int timeout)
00165 {
00166 bool r, e;
00167 return poll(timeout, r, e);
00168 }
00169
00170 protected:
00171
00175 int sock_handle_;
00176
00180 sockaddr_in sockaddr_;
00181
00185 bool connected_;
00186
00190 static const int SOCKET_FAIL = -1;
00191
00196 static const int MAX_BUFFER_SIZE = 1024;
00197
00201 static const int SOCKET_POLL_TO = 1000;
00202
00206 char buffer_[MAX_BUFFER_SIZE + 1];
00207
00208 int getSockHandle() const
00209 {
00210 return sock_handle_;
00211 }
00212
00213 void setSockHandle(int sock_handle_)
00214 {
00215 this->sock_handle_ = sock_handle_;
00216 }
00217
00218 virtual void setConnected(bool connected)
00219 {
00220 this->connected_ = connected;
00221 }
00222
00223 void logSocketError(const char* msg, int rc)
00224 {
00225 int errno_ = errno;
00226 LOG_ERROR("%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(errno_), errno_);
00227 }
00228
00238 bool poll(int timeout, bool & ready, bool & error);
00239
00240
00241
00242 bool sendBytes(industrial::byte_array::ByteArray & buffer);
00243 bool receiveBytes(industrial::byte_array::ByteArray & buffer,
00244 industrial::shared_types::shared_int num_bytes);
00245
00246 virtual int rawSendBytes(char *buffer,
00247 industrial::shared_types::shared_int num_bytes)=0;
00248 virtual int rawReceiveBytes(char *buffer,
00249 industrial::shared_types::shared_int num_bytes)=0;
00250
00251 };
00252
00253 }
00254 }
00255
00256 #endif