Skip to content

Commit

Permalink
shutdown socket prior to close
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddieAkeroyd committed Dec 22, 2023
1 parent b2cf380 commit 7f89753
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions asyn/drvAsynSerial/drvAsynIPServerPort.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ static void connectionListener(void *drvPvt)
int i;
portList_t *pl, *p;
int connected;
fd_set read_set;
struct timeval select_timeout;

/*
* Sanity check
Expand All @@ -308,13 +306,6 @@ static void connectionListener(void *drvPvt)
"drvAsynIPServerPort: %s started listening for connections on %s\n",
tty->portName, tty->serverInfo);
while (tty->fd != INVALID_SOCKET) {
FD_ZERO(&read_set);
FD_SET(tty->fd, &read_set);
select_timeout.tv_sec = 0;
select_timeout.tv_usec = 50000; /* 0.05 seconds, needs to be less than delay in ttyCleanup */
if (select(tty->fd + 1, &read_set, NULL, NULL, &select_timeout) < 1 || !FD_ISSET(tty->fd, &read_set)) {
continue;
}
if (tty->socketType == SOCK_DGRAM) {
if ((tty->UDPbufferPos == 0) && (tty->UDPbufferSize == 0)) {
tty->UDPbufferSize = recvfrom(tty->fd, tty->UDPbuffer, THEORETICAL_UDP_MAX_SIZE , 0, NULL, NULL);
Expand Down Expand Up @@ -506,9 +497,13 @@ static void ttyCleanup(void *pPvt)
if (!tty) return;
if (tty->fd >= 0) {
asynPrint(tty->pasynUser, ASYN_TRACE_FLOW, "drvAsynIPServerPort:ttyCleanup %s: shutdown socket %d\n", tty->portName, tty->fd);
epicsSocketDestroy(tty->fd);
SOCKET fd_save = tty->fd;
tty->fd = INVALID_SOCKET;
epicsThreadSleep(0.1); /* wait for accept thread to terminate */
/* shutdown then close the socket so accept thread will terminate */
shutdown(fd_save, SHUT_RDWR);
epicsThreadSleep(0.01); /* give time for shutdown to be noticed prior to close */
epicsSocketDestroy(fd_save);
epicsThreadSleep(0.1);
}
free(tty->portName);
free(tty->serverInfo);
Expand Down

0 comments on commit 7f89753

Please sign in to comment.