From 3a6b6702460e40f2703353b3d964a09d5eeadd30 Mon Sep 17 00:00:00 2001 From: Di-Shi Sun Date: Fri, 19 Jul 2024 14:22:05 -0400 Subject: [PATCH] Added separated connect timeout. --- RELNOTES.txt | 3 +- include/osp/ospcomm.h | 3 ++ include/osp/ospproviderapi.h | 2 ++ src/ospcomm.c | 30 ++++++++++++++++++ src/ospproviderapi.c | 61 ++++++++++++++++++++++++++++++++++++ src/ospsocket.c | 14 ++++++--- test/Makefile | 1 + test/test_app.c | 30 +++++++++++++++++- 8 files changed, 138 insertions(+), 6 deletions(-) diff --git a/RELNOTES.txt b/RELNOTES.txt index ffaefdf..9349e2b 100644 --- a/RELNOTES.txt +++ b/RELNOTES.txt @@ -798,6 +798,7 @@ Version 4.28.0 - 2024-07-08 * Enabled without token element. * Fixed fcntl typo. -Version 4.29.0 - 2024-0x-xx +Version 4.29.0 - 2024-07-19 * Updated to support setting OSPC_DEFAULT_BLOCKING_FLAG. +* Added separated connect timeout. diff --git a/include/osp/ospcomm.h b/include/osp/ospcomm.h index 20482aa..bf89f6d 100644 --- a/include/osp/ospcomm.h +++ b/include/osp/ospcomm.h @@ -73,6 +73,7 @@ typedef struct { unsigned HttpPersistence; unsigned HttpRetryDelay; unsigned HttpRetryLimit; + unsigned ConnectTimeout; unsigned HttpTimeout; OSPTUINT64 ConnSelectionTimeout; OSPTSVCPT *ServicePointList; @@ -98,10 +99,12 @@ extern "C" { int OSPPCommGetPersistence(OSPTCOMM *, unsigned *); int OSPPCommGetRetryDelay(OSPTCOMM *, unsigned *); int OSPPCommGetRetryLimit(OSPTCOMM *, unsigned *); + int OSPPCommGetConnectTimeout(OSPTCOMM *, unsigned *); int OSPPCommGetTimeout(OSPTCOMM *, unsigned *); int OSPPCommSetPersistence(OSPTCOMM *, unsigned); int OSPPCommSetRetryDelay(OSPTCOMM *, unsigned); int OSPPCommSetRetryLimit(OSPTCOMM *, unsigned); + int OSPPCommSetConnectTimeout(OSPTCOMM *, unsigned); int OSPPCommSetTimeout(OSPTCOMM *, unsigned); int OSPPCommSetConnSelectionTimeout(OSPTCOMM *, OSPTUINT64); int OSPPCommGetMaxConnections(OSPTCOMM *, unsigned *); diff --git a/include/osp/ospproviderapi.h b/include/osp/ospproviderapi.h index 5f11c19..5b38686 100644 --- a/include/osp/ospproviderapi.h +++ b/include/osp/ospproviderapi.h @@ -33,6 +33,7 @@ extern "C" { int OSPPProviderGetHTTPPersistence(OSPTPROVHANDLE, unsigned *); int OSPPProviderGetHTTPRetryDelay(OSPTPROVHANDLE, unsigned *); int OSPPProviderGetHTTPRetryLimit(OSPTPROVHANDLE, unsigned *); + int OSPPProviderGetConnectTimeout(OSPTPROVHANDLE, unsigned *); int OSPPProviderGetHTTPTimeout(OSPTPROVHANDLE, unsigned *); int OSPPProviderGetLocalKeys(OSPTPROVHANDLE, OSPTPRIVATEKEY *, unsigned, void *); int OSPPProviderGetLocalValidation(OSPTPROVHANDLE, unsigned *); @@ -47,6 +48,7 @@ extern "C" { int OSPPProviderSetHTTPPersistence(OSPTPROVHANDLE, unsigned); int OSPPProviderSetHTTPRetryDelay(OSPTPROVHANDLE, unsigned); int OSPPProviderSetHTTPRetryLimit(OSPTPROVHANDLE, unsigned); + int OSPPProviderSetConnectTimeout(OSPTPROVHANDLE, unsigned); int OSPPProviderSetHTTPTimeout(OSPTPROVHANDLE, unsigned); int OSPPProviderSetLocalKeys(OSPTPROVHANDLE, const OSPTPRIVATEKEY *, const void *); int OSPPProviderSetLocalValidation(OSPTPROVHANDLE, unsigned); diff --git a/src/ospcomm.c b/src/ospcomm.c index 607734e..fb442d7 100644 --- a/src/ospcomm.c +++ b/src/ospcomm.c @@ -279,6 +279,21 @@ int OSPPCommGetRetryLimit( return errorcode; } +int OSPPCommGetConnectTimeout( + OSPTCOMM *ospvComm, + unsigned *ospvTimeout) +{ + int errorcode = OSPC_ERR_NO_ERROR; + + if (ospvComm == OSPC_OSNULL) { + errorcode = OSPC_ERR_COMM_INVALID_ARG; + OSPM_DBGERRORLOG(errorcode, "ospvComm is NULL"); + } else + *ospvTimeout = ospvComm->ConnectTimeout; + + return errorcode; +} + int OSPPCommGetTimeout( OSPTCOMM *ospvComm, unsigned *ospvTimeout) @@ -354,6 +369,21 @@ int OSPPCommSetConnSelectionTimeout( return errorcode; } +int OSPPCommSetConnectTimeout( + OSPTCOMM *ospvComm, + unsigned ospvTimeout) +{ + int errorcode = OSPC_ERR_NO_ERROR; + + if (ospvComm == OSPC_OSNULL) { + errorcode = OSPC_ERR_COMM_INVALID_ARG; + OSPM_DBGERRORLOG(errorcode, "ospvComm is NULL"); + } else + ospvComm->ConnectTimeout = ospvTimeout; + + return errorcode; +} + int OSPPCommSetTimeout( OSPTCOMM *ospvComm, unsigned ospvTimeout) diff --git a/src/ospproviderapi.c b/src/ospproviderapi.c index 76fc70d..4ff30a6 100644 --- a/src/ospproviderapi.c +++ b/src/ospproviderapi.c @@ -239,6 +239,34 @@ int OSPPProviderGetHTTPRetryLimit( return errorcode; } +/* + * OSPPProviderGetConnectTimeout() + * + * Get time, in milliseconds, to wait for connecting to a provider + * HTTP connection. + * + * The OSPPProviderGetConnectTimeout function returns the timeout value + * that specifies how long to wait for connecting to HTTP connections + * with ospvProvider. The value, returned in the location pointed to by + * ospvConnectTimeout, is measured in milliseconds. + * + * Returns: OSPC_ERR_NO_ERROR if successful, OSPC_ERR_xxx otherwise. + */ +int OSPPProviderGetConnectTimeout( + OSPTPROVHANDLE ospvProvider, /* In - Provider handle */ + unsigned *ospvConnectTimeout) /* Out - Ptr to result store */ +{ + OSPTPROVIDER *provider = OSPC_OSNULL; + int errorcode = OSPC_ERR_NO_ERROR; + + provider = OSPPProviderGetContext(ospvProvider, &errorcode); + if (errorcode == OSPC_ERR_NO_ERROR) { + errorcode = OSPPCommGetConnectTimeout(provider->Comm, ospvConnectTimeout); + } + + return errorcode; +} + /* * OSPPProviderGetHTTPTimeout() * @@ -974,6 +1002,39 @@ int OSPPProviderSetHTTPRetryLimit( return errorcode; } +/* + * OSPPProviderSetConnectTimeout() + * + * Configure maximum time in ms to wait for connecting to provider. + * + * The OSPPProviderSetConnectTimeout function configures the maximum + * amount of time to wait for connecting to ospvProvider. That timeout, + * expressed in milliseconds, is indicated by the ospvConnectTimeout + * parameter. If no connection is established within this time, the current + * connection is aborted and the library attempts to connect with another + * service point. + * + * Returns: OSPC_ERR_NO_ERROR if successful, OSPC_ERR_xxx otherwise. + */ +int OSPPProviderSetConnectTimeout( + OSPTPROVHANDLE ospvProvider, /* In - Provider handle */ + unsigned ospvConnectTimeout) /* In - New connect timeout */ +{ + OSPTPROVIDER *provider = OSPC_OSNULL; + int errorcode = OSPC_ERR_NO_ERROR; + + provider = OSPPProviderGetContext(ospvProvider, &errorcode); + if (errorcode == OSPC_ERR_NO_ERROR) { + errorcode = OSPPCommSetConnectTimeout(provider->Comm, ospvConnectTimeout); + } + + if (errorcode == OSPC_ERR_NO_ERROR) { + errorcode = OSPPCommSetConnectTimeout(provider->CommForCapabilities, ospvConnectTimeout); + } + + return errorcode; +} + /* * OSPPProviderSetHTTPTimeout() * diff --git a/src/ospsocket.c b/src/ospsocket.c index 59a9a9d..75eeb56 100644 --- a/src/ospsocket.c +++ b/src/ospsocket.c @@ -193,9 +193,12 @@ int OSPPSockConnectAuditURL( (ospvHttp->Flags & OSPC_SOCK_CONNECTED_MASK); /* - * set the response timeout for the socket + * set the connect timeout for the socket */ - OSPPCommGetTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); + OSPPCommGetConnectTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); + if (socktimeout == 0) { + OSPPCommGetTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); + } timeout.tv_sec = socktimeout / 1000; timeout.tv_usec = socktimeout % 1000 * 1000; @@ -323,9 +326,12 @@ int OSPPSockConnectServicePoint( ospvHttp->Flags = (unsigned char)(ospvHttp->Flags & OSPC_SOCK_CONNECTED_MASK); /* - * set the response timeout for the socket + * set the connect timeout for the socket */ - OSPPCommGetTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); + OSPPCommGetConnectTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); + if (socktimeout == 0) { + OSPPCommGetTimeout((OSPTCOMM *)ospvHttp->Comm, &socktimeout); + } timeout.tv_sec = socktimeout / 1000; timeout.tv_usec = socktimeout % 1000 * 1000; diff --git a/test/Makefile b/test/Makefile index d644400..06a2d8a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -63,3 +63,4 @@ linux: $(TESTOBJS) clean : rm -f $(TESTAPP) $(TESTOBJS) + rm -f connect-test diff --git a/test/test_app.c b/test/test_app.c index c94817c..a403800 100644 --- a/test/test_app.c +++ b/test/test_app.c @@ -59,6 +59,7 @@ #define DEF_HTTP_PERSIST 60000 #define DEF_HTTP_RETRYDELAY 0 #define DEF_HTTP_RETRYLIMIT 1 +#define DEF_CONNECT_TIMEOUT (10 * 1000) #define DEF_HTTP_TIMEOUT (60 * 1000) #define DEF_TIME_LIMIT 4 #define DEF_CUST_ID 1000L @@ -446,6 +447,26 @@ int testOSPPProviderSetHTTPRetryDelay() return errcode; } +int testOSPPProviderGetConnectTimeout() +{ + int errcode = 0; + unsigned timeout; + + errcode = OSPPProviderGetConnectTimeout(OSPVProviderHandle, &timeout); + + printf("connect timeout = %u\n", timeout); + return errcode; +} + +int testOSPPProviderSetConnectTimeout() +{ + int errcode = 0; + + errcode = OSPPProviderSetConnectTimeout(OSPVProviderHandle, DEF_CONNECT_TIMEOUT); + + return errcode; +} + int testOSPPProviderGetHTTPTimeout() { int errcode = 0; @@ -2805,6 +2826,12 @@ int testAPI(int apinumber) case 48: errcode = testSetDestinationCount(); break; + case 81: + errcode = testOSPPProviderGetConnectTimeout(); + break; + case 82: + errcode = testOSPPProviderSetConnectTimeout(); + break; case 50: errcode = testSetCallingNumber(); break; @@ -3294,7 +3321,7 @@ int testMenu() if (!quietmode) { printf("\nSocket API functions\n"); printf("---------------------------------------------------------------------\n"); - printf(" 0) SetBlockFlag\n"); + printf(" 0) SetBlockingFlag\n"); printf("---------------------------------------------------------------------\n"); printf("Provider API functions\n"); printf("---------------------------------------------------------------------\n"); @@ -3328,6 +3355,7 @@ int testMenu() printf("43) BuildUsageFromScratch(OGW) 44) BuildUsageFromScratch(TGW)\n"); printf("45) GetLookAheadInfoIfPresent 46) ModifyDeviceIdentifiers\n"); printf("47) ModifyDeviceIdentifiersAgain 48) SetDestinationCount\n"); + printf("81) GetConnectTimeout 82) SetConnectTimeout\n"); printf("99) Sleep for 2 seconds\n"); printf("---------------------------------------------------------------------\n"); printf("Configuration Parameters\n");