硬汉嵌入式论坛

 找回密码
 立即注册
查看: 150|回复: 6
收起左侧

[ThreadX全家桶] TheadX NetXDUO的Keep alive设置

[复制链接]

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
118335
QQ
发表于 7 天前 | 显示全部楼层 |阅读模式
在 ThreadX NetX Duo 中设置 TCP Keepalive 功能,可以帮助检测 TCP 连接是否仍然有效,并在连接失效时自动断开。

NX_TCP_KEEPALIVE_INITIAL        从建立连接或最后一次数据交换后,到发送第一个 Keepalive 探测包前的等待时间。
NX_TCP_KEEPALIVE_RETRY        发送一个 Keepalive 探测包后,等待对方回应的超时时间。如果超时未收到应答,则会重发探测包。       
NX_TCP_KEEPALIVE_RETRIES        在认定连接失效并断开之前,最多发送的 Keepalive 探测包次数。       

[C] 纯文本查看 复制代码
/**************************************************************************/
/*                                                                        */
/*       Copyright (c) Microsoft Corporation. All rights reserved.        */
/*                                                                        */
/*       This software is licensed under the Microsoft Software License   */
/*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
/*       found in the LICENSE file at [url]https://aka.ms/AzureRTOS_EULA[/url]       */
/*       and in the root directory of this software.                      */
/*                                                                        */
/**************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   System Management (System)                                          */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Locate NetX system data in this file.  */

#define NX_SYSTEM_INIT


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_system.h"
#include "nx_packet.h"
#include "nx_arp.h"
#include "nx_rarp.h"
#include "nx_ip.h"
#include "nx_udp.h"
#include "nx_icmp.h"
#include "nx_igmp.h"
#include "nx_tcp.h"

#ifdef FEATURE_NX_IPV6
#include "nx_nd_cache.h"
#endif /* FEATURE_NX_IPV6 */

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_system_initialize                               PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function initializes the various components and system data    */
/*    structures.                                                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    pool_start                            Packet pool starting address  */
/*    pool_size                             Packet pool size in bytes     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_packet_pool_initialize            Initialize Packet Pool        */
/*                                            component                   */
/*    _nx_ip_initialize                     Initialize IP component       */
/*    _nx_tcp_initialize                    Initialize TCP component      */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
/*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
/*                                            resulting in version 6.1    */
/*                                                                        */
/**************************************************************************/
VOID  _nx_system_initialize(VOID)
{

    /* Check whether or not system has been initialized? */
    if (_nx_system_build_options_1 | _nx_system_build_options_2 |
        _nx_system_build_options_3 | _nx_system_build_options_4 | _nx_system_build_options_5)
    {

        /* Yes it is. Just return. */
        return;
    }

    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_SYSTEM_INITIALIZE, 0, 0, 0, 0, NX_TRACE_INTERNAL_EVENTS, 0, 0);

    /* Call the packet pool initialization component for NetX.  */
    _nx_packet_pool_initialize();

    /* Call the IP component initialization.  */
    _nx_ip_initialize();

    /* Call the TCP component initialization.  */
    /*lint -e{522} suppress lack of side-effects.  */
    _nx_tcp_initialize();

    /* Setup the build options variables.  */
    _nx_system_build_options_1 = 0
#ifdef NX_LITTLE_ENDIAN
        | (((ULONG)1) << 31)
#endif
#ifdef NX_DISABLE_ARP_AUTO_ENTRY
        | (((ULONG)1) << 30)
#endif
#ifdef NX_ENABLE_TCP_KEEPALIVE
        | (((ULONG)1) << 29)
#endif
#ifdef NX_TCP_IMMEDIATE_ACK
        | (((ULONG)1) << 28)
#endif
#ifdef NX_DRIVER_DEFERRED_PROCESSING
        | (((ULONG)1) << 27)
#endif
#ifdef NX_DISABLE_FRAGMENTATION
        | (((ULONG)1) << 26)
#endif
#ifdef NX_DISABLE_IP_RX_CHECKSUM
        | (((ULONG)1) << 25)
#endif
#ifdef NX_DISABLE_IP_TX_CHECKSUM
        | (((ULONG)1) << 24)
#endif
#ifdef NX_DISABLE_TCP_RX_CHECKSUM
        | (((ULONG)1) << 23)
#endif
#ifdef NX_DISABLE_TCP_TX_CHECKSUM
        | (((ULONG)1) << 22)
#endif
#ifdef NX_DISABLE_RESET_DISCONNECT
        | (((ULONG)1) << 21)
#endif
#ifdef NX_DISABLE_RX_SIZE_CHECKING
        | (((ULONG)1) << 20)
#endif
#ifdef NX_DISABLE_ARP_INFO
        | (((ULONG)1) << 19)
#endif
#ifdef NX_DISABLE_IP_INFO
        | (((ULONG)1) << 18)
#endif
#ifdef NX_DISABLE_ICMP_INFO
        | (((ULONG)1) << 17)
#endif
#ifdef NX_DISABLE_IGMP_INFO
        | (((ULONG)1) << 16)
#endif
#ifdef NX_DISABLE_PACKET_INFO
        | (((ULONG)1) << 15)
#endif
#ifdef NX_DISABLE_RARP_INFO
        | (((ULONG)1) << 14)
#endif
#ifdef NX_DISABLE_TCP_INFO
        | (((ULONG)1) << 13)
#endif
#ifdef NX_DISABLE_UDP_INFO
        | (((ULONG)1) << 12)
#endif
    ;


    /* Add the retry shift value to the options.  */
#if (NX_TCP_RETRY_SHIFT > 0xF)
    _nx_system_build_options_1 |=  0xF;
#else
    _nx_system_build_options_1 |=  NX_TCP_RETRY_SHIFT;
#endif

#if (NX_IP_PERIODIC_RATE > 0xFFFFUL)
    _nx_system_build_options_2 =  ((ULONG)0xFFFF0000);
#else
    _nx_system_build_options_2 =  ((ULONG)NX_IP_PERIODIC_RATE) << 16;
#endif

#if (NX_ARP_EXPIRATION_RATE > 0xFF)
    _nx_system_build_options_2 |=  ((ULONG)0xFF) << 8;
#else
    _nx_system_build_options_2 |=  ((ULONG)NX_ARP_EXPIRATION_RATE) << 8;
#endif
#if (NX_ARP_UPDATE_RATE > 0xFF)
    _nx_system_build_options_2 |=  ((ULONG)0xFF);
#else
    _nx_system_build_options_2 |=  ((ULONG)NX_ARP_UPDATE_RATE);
#endif

    /* Setup third option word.  */
#if (NX_TCP_ACK_TIMER_RATE > 0xFF)
    _nx_system_build_options_3 =  ((ULONG)0xFF000000);
#else
    _nx_system_build_options_3 =  ((ULONG)NX_TCP_ACK_TIMER_RATE) << 24;
#endif
#if (NX_TCP_FAST_TIMER_RATE > 0xFF)
    _nx_system_build_options_3 |=  ((ULONG)0xFF) << 16;
#else
    _nx_system_build_options_3 |=  ((ULONG)NX_TCP_FAST_TIMER_RATE) << 16;
#endif
#if (NX_TCP_TRANSMIT_TIMER_RATE > 0xFF)
    _nx_system_build_options_3 |=  ((ULONG)0xFF) << 8;
#else
    _nx_system_build_options_3 |=  ((ULONG)NX_TCP_TRANSMIT_TIMER_RATE) << 8;
#endif
#if (NX_TCP_KEEPALIVE_RETRY > 0xFF)
    _nx_system_build_options_3 |=  ((ULONG)0xFF);
#else
    _nx_system_build_options_3 |=  ((ULONG)NX_TCP_KEEPALIVE_RETRY);
#endif

    /* Setup the fourth option word.  */
#if (NX_TCP_KEEPALIVE_INITIAL > 0xFFFFUL)
    _nx_system_build_options_4 =  ((ULONG)0xFFFF0000);
#else
    _nx_system_build_options_4 =  ((ULONG)NX_TCP_KEEPALIVE_INITIAL) << 16;
#endif
#if (NX_ARP_MAXIMUM_RETRIES > 0xFF)
    _nx_system_build_options_4 |=  ((ULONG)0xFF) << 8;
#else
    _nx_system_build_options_4 |=  ((ULONG)NX_ARP_MAXIMUM_RETRIES) << 8;
#endif
#if (NX_ARP_MAX_QUEUE_DEPTH > 0xF)
    _nx_system_build_options_4 |=  ((ULONG)0xF) << 4;
#else
    _nx_system_build_options_4 |=  ((ULONG)NX_ARP_MAX_QUEUE_DEPTH) << 4;
#endif
#if (NX_TCP_KEEPALIVE_RETRIES > 0xF)
    _nx_system_build_options_4 |=  ((ULONG)0xF);
#else
    _nx_system_build_options_4 |=  ((ULONG)NX_TCP_KEEPALIVE_RETRIES);
#endif

    /* Setup the fifth option word.  */
#if (NX_MAX_MULTICAST_GROUPS > 0xFF)
    _nx_system_build_options_5 =  ((ULONG)0xFF000000);
#else
    _nx_system_build_options_5 =  ((ULONG)NX_MAX_MULTICAST_GROUPS) << 24;
#endif
#if (NX_MAX_LISTEN_REQUESTS > 0xFF)
    _nx_system_build_options_5 |=  ((ULONG)0xFF) << 16;
#else
    _nx_system_build_options_5 |=  ((ULONG)NX_MAX_LISTEN_REQUESTS) << 16;
#endif
#if (NX_TCP_MAXIMUM_RETRIES > 0xFF)
    _nx_system_build_options_5 |=  ((ULONG)0xFF) << 8;
#else
    _nx_system_build_options_5 |=  ((ULONG)NX_TCP_MAXIMUM_RETRIES) << 8;
#endif
#if (NX_TCP_MAXIMUM_TX_QUEUE > 0xFF)
    _nx_system_build_options_5 |=  ((ULONG)0xFF);
#else
    _nx_system_build_options_5 |=  ((ULONG)NX_TCP_MAXIMUM_TX_QUEUE);
#endif
}

回复

使用道具 举报

0

主题

5

回帖

5

积分

新手上路

积分
5
发表于 7 天前 | 显示全部楼层
硬汉哥,应该如何配置这三个参数,让TCP一直保持在线,不自动断开呀?
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
118335
QQ
 楼主| 发表于 6 天前 | 显示全部楼层
Bruce 发表于 2025-9-19 16:49
硬汉哥,应该如何配置这三个参数,让TCP一直保持在线,不自动断开呀?

得设置个重连次数,因为有时候确实是断开了

NX_TCP_KEEPALIVE_RETRIES
回复

使用道具 举报

699

主题

3634

回帖

5756

积分

论坛元老

积分
5756
发表于 昨天 17:03 | 显示全部楼层
本帖最后由 hpdell 于 2025-9-25 17:05 编辑
eric2013 发表于 2025-9-20 11:15
得设置个重连次数,因为有时候确实是断开了

NX_TCP_KEEPALIVE_RETRIES

硬汉哥对这个研究的很深啦,

netxduo 系统默认配置参数


#ifndef NX_TCP_KEEPALIVE_INITIAL
#define NX_TCP_KEEPALIVE_INITIAL        7200        /* Number of seconds for initial */
#endif                                              /*   keepalive expiration, the   */
                                                    /*   default is 2 hours (120 min)*/
#ifndef NX_TCP_KEEPALIVE_RETRY
#define NX_TCP_KEEPALIVE_RETRY          75          /* After initial expiration,     */
#endif                                              /*   retry every 75 seconds      */

#ifndef NX_TCP_KEEPALIVE_RETRIES
#define NX_TCP_KEEPALIVE_RETRIES        10          /* Retry a maximum of 10 times   */
#endif
回复

使用道具 举报

0

主题

5

回帖

5

积分

新手上路

积分
5
发表于 昨天 17:19 | 显示全部楼层
hpdell 发表于 2025-9-25 17:03
硬汉哥对这个研究的很深啦,

netxduo 系统默认配置参数

我把NX_TCP_KEEPALIVE_INITIAL配置为1800秒,三个小时左右也会掉线,我现在有点搞不清楚是电脑休眠久了导致的掉线,还是说客户端这边自己Closed
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
118335
QQ
 楼主| 发表于 7 小时前 | 显示全部楼层
Bruce 发表于 2025-9-25 17:19
我把NX_TCP_KEEPALIVE_INITIAL配置为1800秒,三个小时左右也会掉线,我现在有点搞不清楚是电脑休眠久了导 ...

NetXDUO和LwIP都没有做网络协议栈的DeInit功能,做了的话,可以方便的采用此法方案解决断开问题

【实战经验分享】一劳永逸的解决网线随意热插拔问题
https://forum.anfulai.cn/forum.p ... 5386&fromuid=58
(出处: 硬汉嵌入式论坛)
回复

使用道具 举报

12

主题

84

回帖

140

积分

初级会员

积分
140
发表于 5 小时前 | 显示全部楼层
Bruce 发表于 2025-9-25 17:19
我把NX_TCP_KEEPALIVE_INITIAL配置为1800秒,三个小时左右也会掉线,我现在有点搞不清楚是电脑休眠久了导 ...

抓个包,看谁先挥手的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-9-26 19:29 , Processed in 0.042681 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表