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 #define GETHOSTBYNAME(str) gethostbyname(str)
00073
00074 #endif
00075
00076 #ifdef MOTOPLUS
00077
00078 #include "motoPlus.h"
00079
00080 #include "errno.h"
00081
00082
00083
00084
00085 extern "C" STATUS setsockopt (
00086 int s,
00087 int level,
00088 int optname,
00089 char * optval,
00090 int optlen
00091 );
00092
00093 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol)
00094 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen)
00095
00096
00097
00098
00099 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val))
00100
00101 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function.
00102 #define LISTEN(sockfd, n) mpListen(sockfd, n)
00103 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen)
00104 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen)
00105 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen)
00106 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags)
00107 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen)
00108 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags)
00109 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval)
00110 #define CLOSE(fd) mpClose(fd)
00111 #define HTONS(num) mpHtons(num)
00112 #define INET_ADDR(str) mpInetAddr(str)
00113 #define SOCKLEN_T unsigned int
00114 #define GETHOSTBYNAME(str) NULL
00115
00116 #endif
00117
00118 namespace industrial
00119 {
00120 namespace simple_socket
00121 {
00122
00128 namespace StandardSocketPorts
00129 {
00130 enum StandardSocketPort
00131 {
00132 MOTION = 11000, SYSTEM = 11001, STATE = 11002, IO = 11003
00133 };
00134 }
00135 typedef StandardSocketPorts::StandardSocketPort StandardSocketPort;
00136
00140 class SimpleSocket : public industrial::smpl_msg_connection::SmplMsgConnection
00141 {
00142 public:
00143
00147 SimpleSocket()
00148 {
00149 this->setSockHandle(this->SOCKET_FAIL);
00150 memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00151 this->setConnected(false);
00152 }
00153
00157 virtual ~SimpleSocket(){}
00158
00159 bool isConnected()
00160 {
00161 return connected_;
00162 }
00163
00164
00165
00166
00167 virtual void setDisconnected()
00168 {
00169 setConnected(false);
00170 }
00171
00179 bool isReadyReceive(int timeout)
00180 {
00181 bool r, e;
00182 rawPoll(timeout, r, e);
00183 return r;
00184 }
00185
00186 protected:
00187
00191 int sock_handle_;
00192
00196 sockaddr_in sockaddr_;
00197
00201 bool connected_;
00202
00206 static const int SOCKET_FAIL = -1;
00207
00212 static const int MAX_BUFFER_SIZE = 1024;
00213
00217 static const int SOCKET_POLL_TO = 1000;
00218
00222 char buffer_[MAX_BUFFER_SIZE + 1];
00223
00224 int getSockHandle() const
00225 {
00226 return sock_handle_;
00227 }
00228
00229 void setSockHandle(int sock_handle_)
00230 {
00231 this->sock_handle_ = sock_handle_;
00232 }
00233
00241 __attribute__((deprecated(
00242 "Please use: logSocketError(const char* msg, const int rc, const int error_no)")))
00243 void logSocketError(const char* msg, int rc)
00244 {
00245 logSocketError(msg, rc, errno);
00246 }
00247
00254 void logSocketError(const char* msg, const int rc, const int error_no)
00255 {
00256 LOG_ERROR("%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(error_no), error_no);
00257 }
00258
00259
00260
00261 bool sendBytes(industrial::byte_array::ByteArray & buffer);
00262 bool receiveBytes(industrial::byte_array::ByteArray & buffer,
00263 industrial::shared_types::shared_int num_bytes);
00264
00265 virtual int rawSendBytes(char *buffer,
00266 industrial::shared_types::shared_int num_bytes)=0;
00267 virtual int rawReceiveBytes(char *buffer,
00268 industrial::shared_types::shared_int num_bytes)=0;
00278 virtual bool rawPoll(int timeout, bool & ready, bool & error)=0;
00279 virtual void setConnected(bool connected)
00280 {
00281 this->connected_ = connected;
00282 }
00283
00284 };
00285
00286 }
00287 }
00288
00289 #endif