libnavajo
web server for static and dynamic pages developement in C++
nvjSocket.h
Go to the documentation of this file.
1 //********************************************************
12 //********************************************************
13 
14 #ifndef NVJSOCKET_H_
15 #define NVJSOCKET_H_
16 
17 #ifdef WIN32
18 
19 #define usleep(n) Sleep(n/1000>=1 ? n/1000 : 1)
20 
21 #define snprintf _snprintf_s
22 #define vsnprintf _vsnprintf
23 #define strcasecmp _stricmp
24 #define strncasecmp _strnicmp
25 #define close closesocket
26 #define SHUT_RDWR SD_BOTH
27 #define ushort USHORT
28 #define poll(x,y,z) WSAPoll((x),(y),(z))
29 #define setsockoptCompat(a,b,c,d,e) setsockopt(a,b,c,(const char *)d,(int)e)
30 #define sendCompat(f,b,l,o) send((f),(const char *)(b),(int)(l),(o))
31 
32 
33 #else
34 
35 #include <sys/socket.h>
36 #include <netinet/tcp.h>
37 #include <unistd.h>
38 #include <strings.h>
39 #include <netdb.h>
40 #include <sys/poll.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 
44 #define setsockoptCompat setsockopt
45 #define sendCompat send
46 #endif
47 
48 /***********************************************************************
49 * setSocketNoSigpipe: Place a timeout on the socket
50 * @param socket - socket descriptor
51 * diabled generation of SIGPIPE for the socket
52 * \return result of setsockopt (successfull: 0, otherwise -1)
53 ***********************************************************************/
54 
55 inline bool setSocketNoSigpipe(int socket, bool sigpipe = false)
56 {
57 #ifndef LINUX
58  int set = sigpipe ? 1 : 0;
59  return setsockoptCompat( socket, SOL_SOCKET, SO_NOSIGPIPE, (void *) &set, sizeof(int) ) == 0;
60 #else
61  return true;
62 #endif
63 }
64 
65 /***********************************************************************
66 * setSocketKeepAlive: Place a timeout on the socket
67 * @param socket - socket descriptor
68 * diabled generation of SIGPIPE for the socket
69 * \return result of setsockopt (successfull: 0, otherwise -1)
70 ***********************************************************************/
71 
72 inline int setSocketKeepAlive(int socket, bool keepAlive)
73 {
74  int set = keepAlive ? 1 : 0;
75  return setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (void *)&set, sizeof(int) );
76 }
77 
78 /***********************************************************************
79 * setSocketDoLinger: Place a timeout on the socket
80 * @param socket - socket descriptor
81 * @param seconds - number of seconds
82 * @param useconds - number of useconds
83 * \return result of setsockopt (successfull: 0, otherwise -1)
84 ***********************************************************************/
85 
86 inline int setSocketDoLinger(int socket, bool dolinger)
87 {
88  linger value;
89  value.l_onoff = dolinger != 0;
90  value.l_linger = dolinger;
91  return setsockopt( socket, SOL_SOCKET, SO_LINGER, (char*)&value, sizeof (linger) );
92 }
93 /***********************************************************************
94 * setSocketSndRcvTimeout: Place a timeout on the socket
95 * @param socket - socket descriptor
96 * @param seconds - number of seconds
97 * @param useconds - number of useconds
98 * \return result of setsockopt (successfull: 0, otherwise -1)
99 ***********************************************************************/
100 
101 inline bool setSocketSndRcvTimeout(int socket, time_t seconds, long int useconds=0)
102 {
103 #ifdef WIN32
104  int nTimeout = seconds * 1000 + useconds;
105  if ( (setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO,(const char*)&nTimeout, sizeof(int)) == 0 )
106  && (setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO,(const char*)&nTimeout, sizeof(int)) == 0 )
107  return true;
108 #else
109 
110  struct timeval tv,tv2;
111  tv.tv_sec = seconds ;
112  tv.tv_usec = useconds ;
113  tv2.tv_sec = seconds ;
114  tv2.tv_usec = useconds ;
115 
116  if ( (setsockoptCompat (socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) == 0)
117  && (setsockoptCompat (socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv2, sizeof(struct timeval)) == 0))
118  return true;
119 #endif
120 
121  return false;
122 }
123 
124 /***********************************************************************
125 * setSocketTcpAckTimeout: Place a timeout on the tcp acknowledgement
126 * @param socket - socket descriptor
127 * @param seconds - number of seconds
128 * @param milliseconds - number of milliseconds
129 * \return true if no error, otherwise return false
130 ***********************************************************************/
131 
132 inline bool setSocketTcpAckTimeout(int socket, int seconds, int milliseconds)
133 {
134 #if defined(SOL_TCP) && defined(TCP_USER_TIMEOUT)
135  int timeout = 1000 * seconds + milliseconds; // user timeout in milliseconds [ms]
136  return setsockopt (socket, SOL_TCP, TCP_USER_TIMEOUT, (char*) &timeout, sizeof (timeout)) == 0;
137 #else
138  return false;
139 #endif
140 }
141 
142 /***********************************************************************
143 * setSocketNagleAlgo:
144 * @param socket - socket descriptor
145 * @param enabled - use Nagle Algorithm
146 * \return true if no error, otherwise return false
147 ***********************************************************************/
148 
149 inline int setSocketNagleAlgo(int socket, bool naggle = false)
150 {
151  int flag = naggle ? 1 : 0;
152  return setsockopt(socket,IPPROTO_TCP,TCP_NODELAY,(char *)&flag,sizeof(flag)) == 0;
153 }
154 
155 #endif