Socket: Automatically create an internal timer to handle connection timeout.

[GB.NET]
* NEW: Socket: Automatically create an internal timer to handle connection timeout.
* NEW: Net.ConnectionTimeout is a new status constant indicating a connection timeout.
This commit is contained in:
gambas 2020-09-11 00:35:58 +02:00
parent 6e3f56be4e
commit 687708e1b3
4 changed files with 21 additions and 6 deletions

View file

@ -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),

View file

@ -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

View file

@ -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));
}
/*******************************************************************

View file

@ -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;