diff --git a/gb.net/src/CNet.c b/gb.net/src/CNet.c index c3499a1c9..182e1ba78 100644 --- a/gb.net/src/CNet.c +++ b/gb.net/src/CNet.c @@ -142,6 +142,7 @@ GB_DESC CNetDesc[] = GB_CONSTANT("CannotListen", "i", NET_CANNOT_LISTEN), GB_CONSTANT("CannotBindInterface", "i", NET_CANNOT_BIND_INTERFACE), GB_CONSTANT("CannotAuthenticate", "i", NET_CANNOT_AUTHENTICATE), + GB_CONSTANT("ConnectionTimeout", "i", NET_CONNECTION_TIMEOUT), /* SeverSocket, type */ GB_CONSTANT("Internet", "i", NET_TYPE_INTERNET), GB_CONSTANT("Local", "i", NET_TYPE_LOCAL), diff --git a/gb.net/src/CNet.h b/gb.net/src/CNet.h index b0a124675..746a44323 100644 --- a/gb.net/src/CNet.h +++ b/gb.net/src/CNet.h @@ -71,7 +71,8 @@ enum NET_CANNOT_BIND_SOCKET = -10, NET_CANNOT_LISTEN = -14, NET_CANNOT_BIND_INTERFACE = -15, - NET_CANNOT_AUTHENTICATE = -16 // For gb.net.pop3 + NET_CANNOT_AUTHENTICATE = -16, // For gb.net.pop3 + NET_CONNECTION_TIMEOUT = -17 }; enum diff --git a/gb.net/src/CSocket.c b/gb.net/src/CSocket.c index 7ea867653..9cbc7a28e 100644 --- a/gb.net/src/CSocket.c +++ b/gb.net/src/CSocket.c @@ -177,10 +177,18 @@ static void CSocket_close(CSOCKET *_object) set_status(THIS, NET_INACTIVE); } + if (THIS->timer) + GB.Unref(POINTER(&THIS->timer)); + if (THIS->OnClose) THIS->OnClose(_object); } +static int connect_timeout(void *_object) +{ + CSocket_stream_internal_error(THIS, NET_CONNECTION_TIMEOUT, TRUE); + return TRUE; +} /* This function is called by DnsClient to inform @@ -220,6 +228,8 @@ void CSocket_CallBackFromDns(void *_object) if (!myval || errno == EINPROGRESS) // Rhis is the good answer : connect in progress { set_status(THIS, NET_CONNECTING); + if (SOCKET->timeout > 0) + THIS->timer = GB.Every(SOCKET->timeout, (GB_TIMER_CALLBACK)connect_timeout, (intptr_t)THIS); GB.Watch(SOCKET->socket, GB_WATCH_WRITE, (void *)CSocket_CallBackConnecting, (intptr_t)THIS); } else @@ -294,6 +304,8 @@ void CSocket_CallBackConnecting(int t_sock,int type,intptr_t param) GB.Ref(THIS); GB.Post(CSocket_post_connected,(intptr_t)THIS); + + GB.Unref(POINTER(&THIS->timer)); } /******************************************************************* diff --git a/gb.net/src/CSocket.h b/gb.net/src/CSocket.h index 64564585d..9d4990311 100644 --- a/gb.net/src/CSocket.h +++ b/gb.net/src/CSocket.h @@ -64,19 +64,20 @@ typedef CSOCKET_COMMON common; struct sockaddr_in Server; /* struct for TCP connections */ struct sockaddr_un UServer; /* struct for UNIX connections */ - int iUsePort; - int iPort; - int iLocalPort; - int conn_type; + unsigned short iUsePort; + unsigned short iPort; + unsigned short iLocalPort; + char conn_type; + bool watch_write; char *sPath; char *sLocalHostIP; char *sRemoteHostIP; char *Host; char *Path; CDNSCLIENT *DnsTool; + GB_TIMER *timer; void *parent; void (*OnClose)(void *sck); - bool watch_write; } CSOCKET;