Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
Cellular socket improvement: read/write exit on remote closure. (#1070)
Browse files Browse the repository at this point in the history
If a cellular socket write or read function (i.e. uCellSockWrite()/uCellSockRead()) were in progress when the remote-end of the connection closed the socket, the read/write processes would not notice, they would just continue what they were doing until time out. The functions will now exit if that occurs.

Thanks to arnoutdekimo for pointing this out.
  • Loading branch information
RobMeades authored Jan 23, 2024
1 parent f95208f commit c315b4f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cell/src/u_cell_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ typedef struct {
void (*pClosedCallback) (uDeviceHandle_t, int32_t); /**< Set to NULL
if socket is
not in use. */
bool closedByRemote; /**< Will be set to true if +UUSOCL lands. */
} uCellSockSocket_t;

/** Definition of a URC handler.
Expand Down Expand Up @@ -226,6 +227,7 @@ static uCellSockSocket_t *pSockCreate(int32_t sockHandle,
pSock->pAsyncClosedCallback = NULL;
pSock->pDataCallback = NULL;
pSock->pClosedCallback = NULL;
pSock->closedByRemote = false;
}

return pSock;
Expand All @@ -249,6 +251,7 @@ static void sockFree(int32_t sockHandle)
pSock->pAsyncClosedCallback = NULL;
pSock->pDataCallback = NULL;
pSock->pClosedCallback = NULL;
pSock->closedByRemote = false;
}
}
}
Expand Down Expand Up @@ -367,6 +370,7 @@ static void UUSOCL_urc(const uAtClientHandle_t atHandle,
closedCallback,
U_INT32_TO_PTR(pSocket->sockHandle));
}
pSocket->closedByRemote = true;
}
}
}
Expand Down Expand Up @@ -1567,7 +1571,7 @@ int32_t uCellSockWrite(uDeviceHandle_t cellHandle,
while ((leftToSendSize > 0) &&
(negErrnoLocalOrSize == U_SOCK_ENONE) &&
(x < U_CELL_SOCK_TCP_RETRY_LIMIT) &&
written) {
written && !pSocket->closedByRemote) {
if (leftToSendSize < thisSendSize) {
thisSendSize = leftToSendSize;
}
Expand Down Expand Up @@ -1738,7 +1742,8 @@ int32_t uCellSockRead(uDeviceHandle_t cellHandle,
// pending data or room in the buffer
while ((dataSizeBytes > 0) &&
(pSocket->pendingBytes > 0) &&
(negErrnoLocalOrSize == U_SOCK_ENONE)) {
(negErrnoLocalOrSize == U_SOCK_ENONE) &&
!pSocket->closedByRemote) {
thisWantedReceiveSize = dataLengthMax;
if (thisWantedReceiveSize > (int32_t) dataSizeBytes) {
thisWantedReceiveSize = (int32_t) dataSizeBytes;
Expand Down

0 comments on commit c315b4f

Please sign in to comment.