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);
212 if(
select(timeout) == 0)
return 0;
223 if(
s == -1)
return -1;
224 if(
select(timeout) == 0)
return 0;
232 ret = recv(
s,&buf[i],1,0);
236 if(WSAEWOULDBLOCK == WSAGetLastError())
goto tryagain;
259 int contentLength = 0;
264 int ret =
readStr(line, (
int)
sizeof(line)-1, timeout);
267 printf(
"ERROR in rlSocket::readHttpHeader() ret=%d\n", ret);
271 if(strstr(line,
"Content-Length:") != NULL)
273 sscanf(line,
"Content-Length: %d", &contentLength);
275 if(strlen(line) <= 2)
277 return contentLength;
284 int ret,bytes_left,first_byte;
287 if(
s == -1)
return -1;
292 while(bytes_left > 0)
312 size_t socklen =
sizeof(
struct sockaddr);
314 socklen_t socklen =
sizeof(
struct sockaddr);
316 struct sockaddr_in localAddr;
317 struct sockaddr_in remoteAddr;
318 struct hostent *host;
319 struct in_addr RemoteIpAddress;
320 #ifdef AF_INET6_IS_AVAILABLE 321 struct addrinfo hints0, hints1;
322 struct addrinfo *res, *ressave;
337 os = socket(AF_INET,SOCK_STREAM,0);
342 if(setsockopt(
os,SOL_SOCKET,SO_KEEPALIVE,&option,
sizeof(option)) < 0)
349 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,(
const char *) &option,
sizeof(option));
351 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,&option,
sizeof(option));
354 memset(&localAddr,0,
sizeof(localAddr));
355 localAddr.sin_port = htons((
short)
port);
356 localAddr.sin_family = AF_INET;
358 ret = bind(
os, (
struct sockaddr *) &localAddr,
sizeof(localAddr));
379 #ifdef AF_INET6_IS_AVAILABLE 382 memset(&hints0,0,
sizeof(hints0));
383 hints0.ai_flags = AI_PASSIVE;
385 hints0.ai_family = AF_INET6;
386 hints0.ai_socktype = SOCK_STREAM;
387 sprintf(portstr,
"%d",
port);
393 ::printf(
"rlSocket:tcp_listen error for %s port=%s : %s\n",
adr, portstr, gai_strerror(n));
402 os = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
406 if(setsockopt(
os,SOL_SOCKET,SO_KEEPALIVE,&option,
sizeof(option)) < 0)
413 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,(
const char *) &option,
sizeof(option));
415 setsockopt(
os,SOL_SOCKET,SO_REUSEADDR,&option,
sizeof(option));
417 if(bind(
os, res->ai_addr, res->ai_addrlen) == 0)
break;
421 while((res = res->ai_next) != NULL);
440 ::printf(
"rlSocket:ERROR IPV6 not available on this platform\n");
455 os = socket(AF_INET,SOCK_STREAM,0);
461 host = gethostbyname(
adr);
465 RemoteIpAddress.s_addr = inet_addr(
adr);
466 if(RemoteIpAddress.s_addr == INADDR_NONE)
474 memcpy(&RemoteIpAddress,host->h_addr,host->h_length);
477 memset(&remoteAddr,0,
sizeof(remoteAddr));
478 remoteAddr.sin_family = AF_INET;
479 remoteAddr.sin_port = htons((
short)
port);
480 remoteAddr.sin_addr = RemoteIpAddress;
493 #ifdef AF_INET6_IS_AVAILABLE 494 sprintf(portstr,
"%d",
port);
495 memset(&hints1, 0,
sizeof(hints1));
496 hints1.ai_family = AF_UNSPEC;
497 hints1.ai_socktype = SOCK_STREAM;
502 ::printf(
"rlSocket:tcp_connect error for %s port=%s : %s\n",
adr, portstr, gai_strerror(n));
509 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
511 if(::
connect(
s, res->ai_addr, res->ai_addrlen) == 0)
break;
514 while((res = res->ai_next) != NULL);
515 if(res == NULL)
::printf(
"rlSocket:tcp_connect error for %s port=%s\n",
adr, portstr);
518 ::printf(
"rlSocket:ERROR IPV6 not available on this platform\n");
545 if(
s == -1)
return 0;
551 struct timeval timout;
552 fd_set wset,rset,eset;
555 if(timeout == 0)
return 1;
562 timout.tv_sec = timeout / 1000;
563 timout.tv_usec = (timeout % 1000) * 1000;
565 ret =
::select(maxfdp1,&rset,&wset,&eset,&timout);
566 if(ret == 0)
return 0;
579 if(ret < 0)
return ret;
580 return write(message,strlen(message));
585 return printf(
"QPushButton(%d)\n",
id);
603 return getsockopt(sockfd, level, optname, (
char *) optval, optlen);
605 size_t len = *optlen;
606 int ret = getsockopt(sockfd, level, optname, optval, &len);
610 socklen_t len = *optlen;
611 int ret = getsockopt(sockfd, level, optname, optval, &len);
620 return setsockopt(sockfd, level, optname, (
const char *) optval, optlen);
622 return setsockopt(sockfd, level, optname, optval, optlen);
629 int len =
sizeof(option);
644 if(
readStr(line, (
int)
sizeof(line) - 1, timeout) < 1)
return -1;
645 if(strncmp(line,
"Content-Length:",15) == 0)
break;
648 sscanf(line,
"Content-Length: %d", &len);
649 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,...)