|
@@ -79,7 +79,7 @@ static int block_error() {
|
|
return -2;
|
|
return -2;
|
|
}
|
|
}
|
|
|
|
|
|
-void hl_socket_init() {
|
|
|
|
|
|
+HL_PRIM void hl_socket_init() {
|
|
#ifdef HL_WIN
|
|
#ifdef HL_WIN
|
|
if( !init_done ) {
|
|
if( !init_done ) {
|
|
WSAStartup(MAKEWORD(2,0),&init_data);
|
|
WSAStartup(MAKEWORD(2,0),&init_data);
|
|
@@ -88,7 +88,7 @@ void hl_socket_init() {
|
|
#endif
|
|
#endif
|
|
}
|
|
}
|
|
|
|
|
|
-hl_socket *hl_socket_new( bool udp ) {
|
|
|
|
|
|
+HL_PRIM hl_socket *hl_socket_new( bool udp ) {
|
|
SOCKET s;
|
|
SOCKET s;
|
|
if( udp )
|
|
if( udp )
|
|
s = socket(AF_INET,SOCK_DGRAM,0);
|
|
s = socket(AF_INET,SOCK_DGRAM,0);
|
|
@@ -113,12 +113,12 @@ hl_socket *hl_socket_new( bool udp ) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void hl_socket_close( hl_socket *s ) {
|
|
|
|
|
|
+HL_PRIM void hl_socket_close( hl_socket *s ) {
|
|
closesocket(s->sock);
|
|
closesocket(s->sock);
|
|
s->sock = INVALID_SOCKET;
|
|
s->sock = INVALID_SOCKET;
|
|
}
|
|
}
|
|
|
|
|
|
-int hl_socket_send_char( hl_socket *s, int c ) {
|
|
|
|
|
|
+HL_PRIM int hl_socket_send_char( hl_socket *s, int c ) {
|
|
unsigned char cc;
|
|
unsigned char cc;
|
|
cc = (unsigned char)c;
|
|
cc = (unsigned char)c;
|
|
if( send(s->sock,&cc,1,MSG_NOSIGNAL) == SOCKET_ERROR )
|
|
if( send(s->sock,&cc,1,MSG_NOSIGNAL) == SOCKET_ERROR )
|
|
@@ -126,7 +126,7 @@ int hl_socket_send_char( hl_socket *s, int c ) {
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|
|
-int hl_socket_send( hl_socket *s, vbyte *buf, int pos, int len ) {
|
|
|
|
|
|
+HL_PRIM int hl_socket_send( hl_socket *s, vbyte *buf, int pos, int len ) {
|
|
int r = send(s->sock, (char*)buf + pos, len, MSG_NOSIGNAL);
|
|
int r = send(s->sock, (char*)buf + pos, len, MSG_NOSIGNAL);
|
|
if( len == SOCKET_ERROR )
|
|
if( len == SOCKET_ERROR )
|
|
return block_error();
|
|
return block_error();
|
|
@@ -134,14 +134,14 @@ int hl_socket_send( hl_socket *s, vbyte *buf, int pos, int len ) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-int hl_socket_recv( hl_socket *s, vbyte *buf, int pos, int len ) {
|
|
|
|
|
|
+HL_PRIM int hl_socket_recv( hl_socket *s, vbyte *buf, int pos, int len ) {
|
|
int ret = recv(s->sock, (char*)buf + pos, len, MSG_NOSIGNAL);
|
|
int ret = recv(s->sock, (char*)buf + pos, len, MSG_NOSIGNAL);
|
|
if( ret == SOCKET_ERROR )
|
|
if( ret == SOCKET_ERROR )
|
|
return block_error();
|
|
return block_error();
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
-int hl_socket_recv_char( hl_socket *s ) {
|
|
|
|
|
|
+HL_PRIM int hl_socket_recv_char( hl_socket *s ) {
|
|
unsigned char cc;
|
|
unsigned char cc;
|
|
int ret = recv(s->sock,&cc,1,MSG_NOSIGNAL);
|
|
int ret = recv(s->sock,&cc,1,MSG_NOSIGNAL);
|
|
if( ret == SOCKET_ERROR || ret == 0 )
|
|
if( ret == SOCKET_ERROR || ret == 0 )
|
|
@@ -149,7 +149,7 @@ int hl_socket_recv_char( hl_socket *s ) {
|
|
return cc;
|
|
return cc;
|
|
}
|
|
}
|
|
|
|
|
|
-int hl_host_resolve( vbyte *host ) {
|
|
|
|
|
|
+HL_PRIM int hl_host_resolve( vbyte *host ) {
|
|
unsigned int ip;
|
|
unsigned int ip;
|
|
ip = inet_addr((char*)host);
|
|
ip = inet_addr((char*)host);
|
|
if( ip == INADDR_NONE ) {
|
|
if( ip == INADDR_NONE ) {
|
|
@@ -169,13 +169,13 @@ int hl_host_resolve( vbyte *host ) {
|
|
return ip;
|
|
return ip;
|
|
}
|
|
}
|
|
|
|
|
|
-vbyte *hl_host_to_string( int ip ) {
|
|
|
|
|
|
+HL_PRIM vbyte *hl_host_to_string( int ip ) {
|
|
struct in_addr i;
|
|
struct in_addr i;
|
|
*(int*)&i = ip;
|
|
*(int*)&i = ip;
|
|
return (vbyte*)inet_ntoa(i);
|
|
return (vbyte*)inet_ntoa(i);
|
|
}
|
|
}
|
|
|
|
|
|
-vbyte *hl_host_reverse( int ip ) {
|
|
|
|
|
|
+HL_PRIM vbyte *hl_host_reverse( int ip ) {
|
|
struct hostent *h;
|
|
struct hostent *h;
|
|
# if defined(HL_WIN) || defined(HL_MAC)
|
|
# if defined(HL_WIN) || defined(HL_MAC)
|
|
h = gethostbyaddr((char *)&ip,4,AF_INET);
|
|
h = gethostbyaddr((char *)&ip,4,AF_INET);
|
|
@@ -190,14 +190,14 @@ vbyte *hl_host_reverse( int ip ) {
|
|
return (vbyte*)h->h_name;
|
|
return (vbyte*)h->h_name;
|
|
}
|
|
}
|
|
|
|
|
|
-vbyte *hl_host_local() {
|
|
|
|
|
|
+HL_PRIM vbyte *hl_host_local() {
|
|
char buf[256];
|
|
char buf[256];
|
|
if( gethostname(buf,256) == SOCKET_ERROR )
|
|
if( gethostname(buf,256) == SOCKET_ERROR )
|
|
return NULL;
|
|
return NULL;
|
|
return hl_copy_bytes((vbyte*)buf,(int)strlen(buf)+1);
|
|
return hl_copy_bytes((vbyte*)buf,(int)strlen(buf)+1);
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_connect( hl_socket *s, int host, int port ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_connect( hl_socket *s, int host, int port ) {
|
|
struct sockaddr_in addr;
|
|
struct sockaddr_in addr;
|
|
memset(&addr,0,sizeof(addr));
|
|
memset(&addr,0,sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_family = AF_INET;
|
|
@@ -211,11 +211,11 @@ bool hl_socket_connect( hl_socket *s, int host, int port ) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_listen( hl_socket *s, int n ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_listen( hl_socket *s, int n ) {
|
|
return listen(s->sock,n) != SOCKET_ERROR;
|
|
return listen(s->sock,n) != SOCKET_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_bind( hl_socket *s, int host, int port ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_bind( hl_socket *s, int host, int port ) {
|
|
int opt = 1;
|
|
int opt = 1;
|
|
struct sockaddr_in addr;
|
|
struct sockaddr_in addr;
|
|
memset(&addr,0,sizeof(addr));
|
|
memset(&addr,0,sizeof(addr));
|
|
@@ -228,7 +228,7 @@ bool hl_socket_bind( hl_socket *s, int host, int port ) {
|
|
return bind(s->sock,(struct sockaddr*)&addr,sizeof(addr)) != SOCKET_ERROR;
|
|
return bind(s->sock,(struct sockaddr*)&addr,sizeof(addr)) != SOCKET_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
-hl_socket *hl_socket_accept( hl_socket *s ) {
|
|
|
|
|
|
+HL_PRIM hl_socket *hl_socket_accept( hl_socket *s ) {
|
|
struct sockaddr_in addr;
|
|
struct sockaddr_in addr;
|
|
unsigned int addrlen = sizeof(addr);
|
|
unsigned int addrlen = sizeof(addr);
|
|
SOCKET nsock;
|
|
SOCKET nsock;
|
|
@@ -241,7 +241,7 @@ hl_socket *hl_socket_accept( hl_socket *s ) {
|
|
return hs;
|
|
return hs;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_peer( hl_socket *s, int *host, int *port ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_peer( hl_socket *s, int *host, int *port ) {
|
|
struct sockaddr_in addr;
|
|
struct sockaddr_in addr;
|
|
unsigned int addrlen = sizeof(addr);
|
|
unsigned int addrlen = sizeof(addr);
|
|
if( getpeername(s->sock,(struct sockaddr*)&addr,&addrlen) == SOCKET_ERROR )
|
|
if( getpeername(s->sock,(struct sockaddr*)&addr,&addrlen) == SOCKET_ERROR )
|
|
@@ -251,7 +251,7 @@ bool hl_socket_peer( hl_socket *s, int *host, int *port ) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_host( hl_socket *s, int *host, int *port ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_host( hl_socket *s, int *host, int *port ) {
|
|
struct sockaddr_in addr;
|
|
struct sockaddr_in addr;
|
|
unsigned int addrlen = sizeof(addr);
|
|
unsigned int addrlen = sizeof(addr);
|
|
if( getsockname(s->sock,(struct sockaddr*)&addr,&addrlen) == SOCKET_ERROR )
|
|
if( getsockname(s->sock,(struct sockaddr*)&addr,&addrlen) == SOCKET_ERROR )
|
|
@@ -261,7 +261,7 @@ bool hl_socket_host( hl_socket *s, int *host, int *port ) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_set_timeout( hl_socket *s, double t ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_set_timeout( hl_socket *s, double t ) {
|
|
#ifdef HL_WIN
|
|
#ifdef HL_WIN
|
|
int time = (int)(t * 1000);
|
|
int time = (int)(t * 1000);
|
|
#else
|
|
#else
|
|
@@ -275,13 +275,13 @@ bool hl_socket_set_timeout( hl_socket *s, double t ) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_shutdown( hl_socket *s, bool r, bool w ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_shutdown( hl_socket *s, bool r, bool w ) {
|
|
if( !r && !w )
|
|
if( !r && !w )
|
|
return true;
|
|
return true;
|
|
return shutdown(s->sock,r?(w?SHUT_RDWR:SHUT_RD):SHUT_WR) == 0;
|
|
return shutdown(s->sock,r?(w?SHUT_RDWR:SHUT_RD):SHUT_WR) == 0;
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_set_blocking( hl_socket *s, bool b ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_set_blocking( hl_socket *s, bool b ) {
|
|
#ifdef HL_WIN
|
|
#ifdef HL_WIN
|
|
unsigned long arg = b?0:1;
|
|
unsigned long arg = b?0:1;
|
|
return ioctlsocket(s->sock,FIONBIO,&arg) == 0;
|
|
return ioctlsocket(s->sock,FIONBIO,&arg) == 0;
|
|
@@ -297,7 +297,7 @@ bool hl_socket_set_blocking( hl_socket *s, bool b ) {
|
|
#endif
|
|
#endif
|
|
}
|
|
}
|
|
|
|
|
|
-bool hl_socket_set_fast_send( hl_socket *s, bool b ) {
|
|
|
|
|
|
+HL_PRIM bool hl_socket_set_fast_send( hl_socket *s, bool b ) {
|
|
int fast = b;
|
|
int fast = b;
|
|
return setsockopt(s->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&fast,sizeof(fast)) == 0;
|
|
return setsockopt(s->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&fast,sizeof(fast)) == 0;
|
|
}
|
|
}
|