20 #define AF_INET6_IS_AVAILABLE 30 #define WTREAD_GNUC5 ( __GNUC__ * 1000 ) + __GNUC_MINOR__ 31 #if WTREAD_GNUC5 < 4008 33 int WSAAPI
getaddrinfo(
const char*,
const char*,
const struct addrinfo*,
struct addrinfo**);
34 int WSAAPI
getnameinfo(
const struct sockaddr*,socklen_t,
char*,DWORD,
char*,DWORD,
int);
42 #define MSG_NOSIGNAL 0 45 #include <sys/socket.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 53 #define MSG_NOSIGNAL 0 57 #define MSG_NOSIGNAL 0 72 WORD wVersionRequested;
79 wVersionRequested = MAKEWORD(1,1);
81 wVersionRequested = MAKEWORD(2,0);
83 err = WSAStartup(wVersionRequested, &wsadata);
86 ::printf(
"Startup error=%d on windows\n",err);
97 int ret,bytes_left,first_byte;
100 if(socket == NULL)
return -1;
101 if(*socket == -1)
return -1;
106 while(bytes_left > 0)
108 ret = send(*socket,&cbuf[first_byte],bytes_left,
MSG_NOSIGNAL);
115 closesocket(*socket);
171 if(a == NULL)
return;
172 if((strlen(a)+1) >
sizeof(
adr))
return;
196 if(
s == -1)
return -1;
197 if(
select(timeout) == 0)
return 0;
203 ret = recv(
s,&cbuf[i],len-i,0);
217 if(
select(timeout) == 0)
return 0;
228 if(
s == -1)
return -1;
229 if(
select(timeout) == 0)
return 0;
239 ret = recv(
s,&buf[i],1,0);
243 if(WSAEWOULDBLOCK == WSAGetLastError())
goto tryagain;
269 int contentLength = 0;
274 int ret =
readStr(line, (
int)
sizeof(line)-1, timeout);
277 printf(
"ERROR in rlSocket::readHttpHeader() ret=%d\n", ret);
281 if(strstr(line,
"Content-Length:") != NULL)
283 sscanf(line,
"Content-Length: %d", &contentLength);
285 if(strlen(line) <= 2)
287 return contentLength;
294 int ret,bytes_left,first_byte;
297 if(
s == -1)
return -1;
306 while(bytes_left > 0)
326 size_t socklen =
sizeof(
struct sockaddr);
328 socklen_t socklen =
sizeof(
struct sockaddr);
330 struct sockaddr_in localAddr;
331 struct sockaddr_in remoteAddr;
332 struct hostent *host;
333 struct in_addr RemoteIpAddress;
334 #ifdef AF_INET6_IS_AVAILABLE 335 struct addrinfo hints0, hints1;
336 struct addrinfo *res, *ressave;
351 os = socket(AF_INET,SOCK_STREAM,0);
356 if(setsockopt(
os,SOL_SOCKET,SO_KEEPALIVE,&option,
sizeof(option)) < 0)
363 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,(
const char *) &option,
sizeof(option));
365 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,&option,
sizeof(option));
368 memset(&localAddr,0,
sizeof(localAddr));
369 localAddr.sin_port = htons((
short)
port);
370 localAddr.sin_family = AF_INET;
372 ret = bind(
os, (
struct sockaddr *) &localAddr,
sizeof(localAddr));
393 #ifdef AF_INET6_IS_AVAILABLE 396 memset(&hints0,0,
sizeof(hints0));
397 hints0.ai_flags = AI_PASSIVE;
399 hints0.ai_family = AF_INET6;
400 hints0.ai_socktype = SOCK_STREAM;
401 sprintf(portstr,
"%d",
port);
407 ::printf(
"rlSocket:tcp_listen error for %s port=%s : %s\n",
adr, portstr, gai_strerror(n));
416 os = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
420 if(setsockopt(
os,SOL_SOCKET,SO_KEEPALIVE,&option,
sizeof(option)) < 0)
427 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,(
const char *) &option,
sizeof(option));
429 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,&option,
sizeof(option));
431 if(bind(
os, res->ai_addr, res->ai_addrlen) == 0)
break;
435 while((res = res->ai_next) != NULL);
454 ::printf(
"rlSocket:ERROR IPV6 not available on this platform\n");
469 os = socket(AF_INET,SOCK_STREAM,0);
475 host = gethostbyname(
adr);
479 RemoteIpAddress.s_addr = inet_addr(
adr);
480 if(RemoteIpAddress.s_addr == INADDR_NONE)
490 memcpy(&RemoteIpAddress,host->h_addr,host->h_length);
493 memset(&remoteAddr,0,
sizeof(remoteAddr));
494 remoteAddr.sin_family = AF_INET;
495 remoteAddr.sin_port = htons((
short)
port);
496 remoteAddr.sin_addr = RemoteIpAddress;
509 #ifdef AF_INET6_IS_AVAILABLE 510 sprintf(portstr,
"%d",
port);
511 memset(&hints1, 0,
sizeof(hints1));
512 hints1.ai_family = AF_UNSPEC;
513 hints1.ai_socktype = SOCK_STREAM;
518 ::printf(
"rlSocket:tcp_connect error for %s port=%s : %s\n",
adr, portstr, gai_strerror(n));
525 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
527 if(::
connect(
s, res->ai_addr, res->ai_addrlen) == 0)
break;
530 while((res = res->ai_next) != NULL);
531 if(res == NULL)
::printf(
"rlSocket:tcp_connect error for %s port=%s\n",
adr, portstr);
534 ::printf(
"rlSocket:ERROR IPV6 not available on this platform\n");
561 if(
s == -1)
return 0;
567 struct timeval timout;
568 fd_set wset,rset,eset;
571 if(timeout == 0)
return 1;
578 timout.tv_sec = timeout / 1000;
579 timout.tv_usec = (timeout % 1000) * 1000;
581 ret =
::select(maxfdp1,&rset,&wset,&eset,&timout);
582 if(ret == 0)
return 0;
595 if(ret < 0)
return ret;
596 return write(message,strlen(message));
601 return printf(
"QPushButton(%d)\n",
id);
619 return getsockopt(sockfd, level, optname, (
char *) optval, optlen);
621 size_t len = *optlen;
622 int ret = getsockopt(sockfd, level, optname, optval, &len);
626 socklen_t len = *optlen;
627 int ret = getsockopt(sockfd, level, optname, optval, &len);
636 return setsockopt(sockfd, level, optname, (
const char *) optval, optlen);
638 return setsockopt(sockfd, level, optname, optval, optlen);
645 int len =
sizeof(option);
660 if(
readStr(line, (
int)
sizeof(line) - 1, timeout) < 1)
return -1;
661 if(strncmp(line,
"Content-Length:",15) == 0)
break;
664 sscanf(line,
"Content-Length: %d", &len);
665 if(
readStr(line, (
int)
sizeof(line) - 1, timeout) < 1)
return -1;
int setIPVersion(int version)
int write(const void *buf, int len)
void setActive(int active)
int readHttpHeader(rlString *header, int timeout=0)
int WSAAPI getaddrinfo(const char *, const char *, const struct addrinfo *, struct addrinfo **)
int rlScoketWrite(int *socket, const void *buf, int len)
int readStr(char *buf, int len, int timeout=0)
unsigned char sockaddr[16+48]
rlSocket(const char *adr, int port, int active)
int sendProcessViewBrowserButtonEvent(int id)
int read(void *buf, int len, int timeout=0)
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
static int rlSetsockopt(int sockfd, int level, int optname, const void *optval, int optlen)
int select(int timeout=0)
void WSAAPI freeaddrinfo(struct addrinfo *)
int WSAAPI getnameinfo(const struct sockaddr *, socklen_t, char *, DWORD, char *, DWORD, int)
static int rlGetsockopt(int sockfd, int level, int optname, void *optval, int *optlen)
int readHttpContentLength(int timeout)
void setAdr(const char *adr)
int rlwthread_sleep(long msec)
int printf(const char *format,...)