32 #ifndef SIMPLE_SOCKET_H 33 #define SIMPLE_SOCKET_H 46 #if defined(SIMPLE_MESSAGE_LINUX) || defined(LINUXSOCKETS) 49 #include "sys/socket.h" 50 #include "arpa/inet.h" 52 #include "netinet/tcp.h" 59 #define SOCKET(domain, type, protocol) socket(domain, type, protocol) 60 #define BIND(sockfd, addr, addrlen) bind(sockfd, addr, addrlen) 61 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&(val)), sizeof(val)) 62 #define SET_REUSE_ADDR(sockfd, val) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&(val)), sizeof(val)) 63 #define LISTEN(sockfd, n) listen(sockfd, n) 64 #define ACCEPT(sockfd, addr, addrlen) accept(sockfd, addr, addrlen) 65 #define CONNECT(sockfd, dest_addr ,addrlen) connect(sockfd, dest_addr, addrlen) 66 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen) 67 #define SEND(sockfd, buf, len, flags) send(sockfd, buf, len, flags) 68 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, buf, len, flags, src_addr, addrlen) 69 #define RECV(sockfd, buf, len, flags) recv(sockfd, buf, len, flags) 70 #define SELECT(n, readfds, writefds, exceptfds, timeval) select(n, readfds, writefds, exceptfds, timeval) 72 #define CLOSE(fd) closesocket(fd) 74 #define CLOSE(fd) close(fd) 76 #ifndef HTONS // OSX defines HTONS 77 #define HTONS(num) htons(num) 79 #define INET_ADDR(str) inet_addr(str) 80 #define SOCKLEN_T socklen_t 81 #define GETADDRINFO(node, service, hints, results) getaddrinfo(node, service, hints, results) 85 #ifdef SIMPLE_MESSAGE_MOTOPLUS 94 extern "C" STATUS setsockopt (
102 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol) 103 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen) 108 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val)) 110 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function. 111 #define LISTEN(sockfd, n) mpListen(sockfd, n) 112 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen) 113 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen) 114 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen) 115 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags) 116 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen) 117 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags) 118 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval) 119 #define CLOSE(fd) mpClose(fd) 120 #define HTONS(num) mpHtons(num) 121 #define INET_ADDR(str) mpInetAddr(str) 122 #define SOCKLEN_T unsigned int 123 #define GETHOSTBYNAME(str) NULL 129 namespace simple_socket
137 namespace StandardSocketPorts
158 this->setSockHandle(this->SOCKET_FAIL);
159 memset(&this->sockaddr_, 0,
sizeof(this->sockaddr_));
160 this->setConnected(
false);
191 rawPoll(timeout, r, e);
215 static const int SOCKET_FAIL = -1;
221 static const int MAX_BUFFER_SIZE = 1024;
226 static const int SOCKET_POLL_TO = 10;
231 char buffer_[MAX_BUFFER_SIZE + 1];
240 this->sock_handle_ = sock_handle_;
251 __attribute__((deprecated(
252 "Please use: logSocketError(const char* msg, const int rc, const int error_no)")))
254 void logSocketError(
const char* msg,
int rc)
256 logSocketError(msg, rc, errno);
267 LOG_ERROR(
"%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(error_no), error_no);
278 virtual int rawSendBytes(
char *buffer,
280 virtual int rawReceiveBytes(
char *buffer,
291 virtual bool rawPoll(
int timeout,
bool & ready,
bool & error)=0;
294 this->connected_ = connected;
virtual void setConnected(bool connected)
Defines socket functions required for a simple connection type.
Defines an interface and common methods for sending simple messages (see simple_message). This interface makes a bare minimum of assumptions:
void logSocketError(const char *msg, const int rc, const int error_no)
Logs message to error log and reports associated socket system error.
#define LOG_ERROR(format,...)
sockaddr_in sockaddr_
address/port of remote socket
bool isReadyReceive(int timeout)
returns true if socket data is ready to receive
The byte array wraps a dynamic array of bytes (i.e. char).
bool connected_
flag indicating socket connection status
virtual ~SimpleSocket()
Destructor.
virtual void setDisconnected()
bool isConnected()
return connection status
SimpleSocket()
Constructor.
int sock_handle_
socket handle for sending/receiving data
int getSockHandle() const
void setSockHandle(int sock_handle_)