fix tcp master read timeout

This commit is contained in:
aleks
2022-06-23 13:33:58 +02:00
parent 2733ceae72
commit 31ea52cf17
3 changed files with 12 additions and 10 deletions

View File

@@ -90,7 +90,6 @@
#define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS #define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS
#define MB_TCP_SEND_TIMEOUT (pdMS_TO_TICKS(MB_TCP_SEND_TIMEOUT_MS)) #define MB_TCP_SEND_TIMEOUT (pdMS_TO_TICKS(MB_TCP_SEND_TIMEOUT_MS))
#define MB_TCP_PORT_MAX_CONN (CONFIG_FMB_TCP_PORT_MAX_CONN) #define MB_TCP_PORT_MAX_CONN (CONFIG_FMB_TCP_PORT_MAX_CONN)
#define MB_TCP_PORT_RESPOND_TIMEOUT (CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND * 1000)
#define MB_TCP_FRAME_LOG_BUFSIZE (256) #define MB_TCP_FRAME_LOG_BUFSIZE (256)

View File

@@ -281,7 +281,7 @@ static int vMBTCPPortMasterRxCheck(int xSd, fd_set* pxFdSet, int xTimeMs)
return xRes; return xRes;
} }
static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHORT usLength) static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHORT usLength, uint16_t xTimeMs)
{ {
int xLength = 0; int xLength = 0;
UCHAR* pucBuf = pucDstBuf; UCHAR* pucBuf = pucDstBuf;
@@ -292,7 +292,7 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR
// Set receive timeout for socket <= slave respond time // Set receive timeout for socket <= slave respond time
xTime.tv_sec = 0; xTime.tv_sec = 0;
xTime.tv_usec = MB_TCP_PORT_RESPOND_TIMEOUT; xTime.tv_usec = xTimeMs * 1000;
setsockopt(pxInfo->xSockId, SOL_SOCKET, SO_RCVTIMEO, &xTime, sizeof(xTime)); setsockopt(pxInfo->xSockId, SOL_SOCKET, SO_RCVTIMEO, &xTime, sizeof(xTime));
// Receive data from connected client // Receive data from connected client
@@ -301,7 +301,7 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR
xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, 0); xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, 0);
if (xLength < 0) { if (xLength < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
// Read timeout occurred, continue reading // Read timeout occurred, check the timeout and return
} else if (errno == ENOTCONN) { } else if (errno == ENOTCONN) {
// Socket connection closed // Socket connection closed
ESP_LOGE(TAG, "Socket(#%d)(%s) connection closed.", ESP_LOGE(TAG, "Socket(#%d)(%s) connection closed.",
@@ -334,7 +334,8 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo)
if (pxInfo) { if (pxInfo) {
MB_PORT_CHECK((pxInfo->xSockId > 0), -1, "Try to read incorrect socket = #%d.", pxInfo->xSockId); MB_PORT_CHECK((pxInfo->xSockId > 0), -1, "Try to read incorrect socket = #%d.", pxInfo->xSockId);
// Read packet header // Read packet header
xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[0], MB_TCP_UID); xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[0],
MB_TCP_UID, xMBTCPPortMasterGetRespTimeLeft(pxInfo));
if (xRet < 0) { if (xRet < 0) {
pxInfo->xRcvErr = xRet; pxInfo->xRcvErr = xRet;
return xRet; return xRet;
@@ -347,7 +348,8 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo)
// If we have received the MBAP header we can analyze it and calculate // If we have received the MBAP header we can analyze it and calculate
// the number of bytes left to complete the current request. // the number of bytes left to complete the current request.
xLength = (int)MB_TCP_GET_FIELD(pxInfo->pucRcvBuf, MB_TCP_LEN); xLength = (int)MB_TCP_GET_FIELD(pxInfo->pucRcvBuf, MB_TCP_LEN);
xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[MB_TCP_UID], xLength); xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[MB_TCP_UID],
xLength, xMBTCPPortMasterGetRespTimeLeft(pxInfo));
if (xRet < 0) { if (xRet < 0) {
pxInfo->xRcvErr = xRet; pxInfo->xRcvErr = xRet;
return xRet; return xRet;

View File

@@ -61,7 +61,7 @@
/* ----------------------- Defines -----------------------------------------*/ /* ----------------------- Defines -----------------------------------------*/
#define MB_TCP_DISCONNECT_TIMEOUT ( CONFIG_FMB_TCP_CONNECTION_TOUT_SEC * 1000000 ) // disconnect timeout in uS #define MB_TCP_DISCONNECT_TIMEOUT ( CONFIG_FMB_TCP_CONNECTION_TOUT_SEC * 1000000 ) // disconnect timeout in uS
#define MB_TCP_RESP_TIMEOUT_MS ( MB_MASTER_TIMEOUT_MS_RESPOND - 2 ) // slave response time limit #define MB_TCP_RESP_TIMEOUT_MS ( MB_MASTER_TIMEOUT_MS_RESPOND - 1 ) // slave response time limit
#define MB_TCP_NET_LISTEN_BACKLOG ( SOMAXCONN ) #define MB_TCP_NET_LISTEN_BACKLOG ( SOMAXCONN )
/* ----------------------- Prototypes ---------------------------------------*/ /* ----------------------- Prototypes ---------------------------------------*/
@@ -150,12 +150,13 @@ xMBTCPPortInit( USHORT usTCPPort )
xConfig.pcBindAddr = NULL; xConfig.pcBindAddr = NULL;
// Create task for packet processing // Create task for packet processing
BaseType_t xErr = xTaskCreate(vMBTCPPortServerTask, BaseType_t xErr = xTaskCreatePinnedToCore(vMBTCPPortServerTask,
"tcp_server_task", "tcp_slave_task",
MB_TCP_STACK_SIZE, MB_TCP_STACK_SIZE,
NULL, NULL,
MB_TCP_TASK_PRIO, MB_TCP_TASK_PRIO,
&xConfig.xMbTcpTaskHandle); &xConfig.xMbTcpTaskHandle,
MB_PORT_TASK_AFFINITY);
vTaskSuspend(xConfig.xMbTcpTaskHandle); vTaskSuspend(xConfig.xMbTcpTaskHandle);
if (xErr != pdTRUE) if (xErr != pdTRUE)
{ {