Skip to content

Commit

Permalink
Changes to shorten connect() timeout when connecting to unavailable T…
Browse files Browse the repository at this point in the history
…CP ports
  • Loading branch information
MarkRivers committed Nov 5, 2024
1 parent 9519475 commit 56f1645
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions asyn/asynDriver/asynDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ typedef struct asynManager {
asynStatus (*isEnabled)(asynUser *pasynUser,int *yesNo);
asynStatus (*isAutoConnect)(asynUser *pasynUser,int *yesNo);
asynStatus (*setAutoConnectTimeout)(double timeout);
asynStatus (*getAutoConnectTimeout)(double *timeout);
asynStatus (*waitConnect)(asynUser *pasynUser, double timeout);
/*The following are methods for interrupts*/
asynStatus (*registerInterruptSource)(const char *portName,
Expand Down
11 changes: 11 additions & 0 deletions asyn/asynDriver/asynManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ static asynStatus isConnected(asynUser *pasynUser,int *yesNo);
static asynStatus isEnabled(asynUser *pasynUser,int *yesNo);
static asynStatus isAutoConnect(asynUser *pasynUser,int *yesNo);
static asynStatus setAutoConnectTimeout(double timeout);
static asynStatus getAutoConnectTimeout(double *timeout);
static asynStatus waitConnect(asynUser *pasynUser, double timeout);
static asynStatus registerInterruptSource(const char *portName,
asynInterface *pasynInterface, void **pasynPvt);
Expand Down Expand Up @@ -370,6 +371,7 @@ static asynManager manager = {
isEnabled,
isAutoConnect,
setAutoConnectTimeout,
getAutoConnectTimeout,
waitConnect,
registerInterruptSource,
getInterruptPvt,
Expand Down Expand Up @@ -2222,6 +2224,15 @@ static asynStatus setAutoConnectTimeout(double timeout)
return asynSuccess;
}

static asynStatus getAutoConnectTimeout(double *timeout)
{
if(!pasynBase) asynInit();
epicsMutexMustLock(pasynBase->lock);
*timeout = pasynBase->autoConnectTimeout;
epicsMutexUnlock(pasynBase->lock);
return asynSuccess;
}

static asynStatus setQueueLockPortTimeout(asynUser *pasynUser, double timeout)
{
userPvt *puserPvt = asynUserToUserPvt(pasynUser);
Expand Down
18 changes: 18 additions & 0 deletions asyn/drvAsynSerial/drvAsynIPPort.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,22 @@ connectIt(void *drvPvt, asynUser *pasynUser)
* problem is just that the device has DHCP'd itself an new number.
*/
if (tty->socketType != SOCK_DGRAM) {
double connectTimeout;
struct timeval saveTV, connectTV;
socklen_t svlen = sizeof saveTV;
pasynManager->getAutoConnectTimeout(&connectTimeout);
connectTV.tv_sec = (time_t)connectTimeout;
connectTV.tv_usec = (suseconds_t)((connectTimeout - connectTV.tv_sec)*1000000);
asynPrint(pasynUser, ASYN_TRACE_ERROR, "Calling setsockopt SO_SNDTIMEO tv_sec=%d tv_usec=%d\n", connectTV.tv_sec, connectTV.tv_usec);
if (getsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&saveTV, &svlen) < 0) {
asynPrint(pasynUser, ASYN_TRACE_ERROR, "connectIt, error calling getsockopt for SO_RECVTIMEO: %s\n", strerror(SOCKERRNO));
}
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&connectTV, sizeof connectTV) < 0) {
asynPrint(pasynUser, ASYN_TRACE_ERROR, "connectIt, error calling setsockopt for SO_RECVTIMEO: %s\n", strerror(SOCKERRNO));
}
asynPrint(pasynUser, ASYN_TRACE_ERROR, "connectIt, calling connect()\n");
if (connect(fd, &tty->farAddr.oa.sa, (int)tty->farAddrSize) < 0) {
asynPrint(pasynUser, ASYN_TRACE_ERROR, "connectIt, connect returned error: %s\n", strerror(SOCKERRNO));
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Can't connect to %s: %s",
tty->IPDeviceName, strerror(SOCKERRNO));
Expand All @@ -520,6 +535,9 @@ connectIt(void *drvPvt, asynUser *pasynUser)
tty->flags |= FLAG_NEED_LOOKUP;
return asynError;
}
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&saveTV, sizeof saveTV) < 0) {
asynPrint(pasynUser, ASYN_TRACE_ERROR, "connectIt, error calling setsockopt for SO_RECVTIMEO: %s\n", strerror(SOCKERRNO));
}
}
}
i = 1;
Expand Down

0 comments on commit 56f1645

Please sign in to comment.