32 #ifndef SIMPLE_SOCKET_H 33 #define SIMPLE_SOCKET_H 47 #include "sys/socket.h" 48 #include "arpa/inet.h" 51 #include "netinet/tcp.h" 54 #define SOCKET(domain, type, protocol) socket(domain, type, protocol) 55 #define BIND(sockfd, addr, addrlen) bind(sockfd, addr, addrlen) 56 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &(val), sizeof(val)) 57 #define SET_REUSE_ADDR(sockfd, val) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(val), sizeof(val)) 58 #define LISTEN(sockfd, n) listen(sockfd, n) 59 #define ACCEPT(sockfd, addr, addrlen) accept(sockfd, addr, addrlen) 60 #define CONNECT(sockfd, dest_addr ,addrlen) connect(sockfd, dest_addr, addrlen) 61 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen) 62 #define SEND(sockfd, buf, len, flags) send(sockfd, buf, len, flags) 63 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, buf, len, flags, src_addr, addrlen) 64 #define RECV(sockfd, buf, len, flags) recv(sockfd, buf, len, flags) 65 #define SELECT(n, readfds, writefds, exceptfds, timeval) select(n, readfds, writefds, exceptfds, timeval) 66 #define CLOSE(fd) close(fd) 67 #ifndef HTONS // OSX defines HTONS 68 #define HTONS(num) htons(num) 70 #define INET_ADDR(str) inet_addr(str) 71 #define SOCKLEN_T socklen_t 72 #define GETADDRINFO(node, service, hints, results) getaddrinfo(node, service, hints, results) 85 extern "C" STATUS setsockopt (
93 #define SOCKET(domain, type, protocol) mpSocket(domain, type, protocol) 94 #define BIND(sockfd, addr, addrlen) mpBind(sockfd, addr, addrlen) 99 #define SET_NO_DELAY(sockfd, val) setsockopt(sockfd, SOL_SOCKET, TCP_NODELAY, (char *)&val, sizeof(val)) 101 #define SET_REUSE_ADDR(sockfd, val) -1 //MOTOPLUS does not support this function. 102 #define LISTEN(sockfd, n) mpListen(sockfd, n) 103 #define ACCEPT(sockfd, addr, addrlen) mpAccept(sockfd, addr, addrlen) 104 #define CONNECT(sockfd, dest_addr ,addrlen) mpConnect(sockfd, dest_addr, addrlen) 105 #define SEND_TO(sockfd, buf, len, flags, dest_addr, addrlen) mpSendTo(sockfd, buf, len, flags, dest_addr, addrlen) 106 #define SEND(sockfd, buf, len, flags) mpSend(sockfd, buf, len, flags) 107 #define RECV_FROM(sockfd, buf, len, flags, src_addr, addrlen) mpRecvFrom(sockfd, buf, len, flags, src_addr, (int*)addrlen) 108 #define RECV(sockfd, buf, len, flags) mpRecv(sockfd, buf, len, flags) 109 #define SELECT(n, readfds, writefds, exceptfds, timeval) mpSelect(n, readfds, writefds, exceptfds, timeval) 110 #define CLOSE(fd) mpClose(fd) 111 #define HTONS(num) mpHtons(num) 112 #define INET_ADDR(str) mpInetAddr(str) 113 #define SOCKLEN_T unsigned int 114 #define GETHOSTBYNAME(str) NULL 120 namespace simple_socket
128 namespace StandardSocketPorts
149 this->setSockHandle(this->SOCKET_FAIL);
150 memset(&this->sockaddr_, 0,
sizeof(this->sockaddr_));
151 this->setConnected(
false);
182 rawPoll(timeout, r, e);
206 static const int SOCKET_FAIL = -1;
212 static const int MAX_BUFFER_SIZE = 1024;
217 static const int SOCKET_POLL_TO = 1000;
222 char buffer_[MAX_BUFFER_SIZE + 1];
231 this->sock_handle_ = sock_handle_;
241 __attribute__((deprecated(
242 "Please use: logSocketError(const char* msg, const int rc, const int error_no)")))
243 void logSocketError(
const char* msg,
int rc)
245 logSocketError(msg, rc, errno);
256 LOG_ERROR(
"%s, rc: %d. Error: '%s' (errno: %d)", msg, rc, strerror(error_no), error_no);
265 virtual int rawSendBytes(
char *buffer,
267 virtual int rawReceiveBytes(
char *buffer,
278 virtual bool rawPoll(
int timeout,
bool & ready,
bool & error)=0;
281 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 getSockHandle() const
int sock_handle_
socket handle for sending/receiving data
void setSockHandle(int sock_handle_)