【p2p、分布式,区块链笔记 UPNP】: Libupnp test_init.c 02 初始化SDK --- UpnpInitPreamble

在这里插入图片描述

  • 该代码是用于初始化 UPnP(Universal Plug and Play)SDK 的初始步骤的实现。英文preamble /ˈpriːæmbl/ 意为n.序言;前言;导言;开场白;绪论 v. 作序言[绪论]。函数 UpnpInitPreamble() 负责执行一系列关键任务,以确保 UPnP SDK 在使用之前的各个组件都被正确配置和启动。

代码解析

/*! 
 * \brief 初始化过程如下 Performs the initial steps in initializing the UPnP SDK.
 *
 * \li **初始化 Winsock 库**(仅限 Windows)                Winsock library is initialized for the process (Windows specific). 
 * \li **初始化日志系统**,用于调试信息输出。                The logging (for debug messages) is initialized.
 * \li **初始化互斥锁、句柄表和线程池**,确保多线程环境中的安全和稳定。 Mutexes, Handle table and thread pools are allocated and initialized.
 * \li **设置 SOAP 和 GENA 的回调函数**,如果启用了这些功能。 Callback functions for SOAP and GENA are set, if they're enabled.
 * \li **初始化 SDK 的定时器线程**,用于定时任务。           The SDK timer thread is initialized.
 *
 * \return UPNP_E_SUCCESS on success.
 */

函数分析

static int UpnpInitPreamble(void) {
   
   
    int retVal = UPNP_E_SUCCESS;  // 初始化返回值为成功
    int i;                        // 循环变量
    
#ifdef UPNP_HAVE_OPTSSDP // FALSE
    uuid_upnp nls_uuid;           // 用于 SSDP 的 NLS UUID
#endif /* UPNP_HAVE_OPTSSDP */

    // WinsockInit根据平台初始化 Winsock 库(Windows 平台特有,Winsock 是 Windows 中用于网络编程的 API,定义在https://github1s.com/pupnp/pupnp/blob/branch-1.14.x/upnp/src/api/upnpapi.c#L281-L319)
    //  Linux 上不需要显式地初始化网络库,也不需要像 Windows 上那样调用 WSAStartup() 和 WSACleanup()
    retVal = WinsockInit();
    if (retVal != UPNP_E_SUCCESS) {
   
   
        return retVal;  // Winsock 初始化失败,返回错误代码
    }

    // 通过时间来初始化随机数种子,用于 SDK 内部的随机数生成
    srand((unsigned int)time(NULL));

    // 初始化日志系统,用于调试输出
    retVal = UpnpInitLog();
    if (retVal != UPNP_E_SUCCESS) {
   
   
        // 如果日志系统初始化失败,返回通用初始化失败错误
        return UPNP_E_INIT_FAILED;
    }

    UpnpPrintf(UPNP_INFO, API, __FILE__, __LINE__
If you are experiencing problems with the Universal Plug and Play service, your computer might not be able to automatically detect the presence of other networked devices, such as PCs, printers, Internet access points and so on. That is where the UPnP Test application comes in. This simple program is designed to help you identify the issues that prevent the UPnP protocol from functioning correctly. Before you get your hopes up, you should know that this tool does not solve the detected problems, but only performs a series of tests to identify the possible causes. One advantage is that the application does not require installation, so your system registry is not affected in any way. The interface is compact and simple, comprising only two panels: one that displays the test type and its short description and the other for viewing which of the tests passed and which failed. The program can verify whether the operating system provides support for the UPnP service and allows you to check if the Simple Service Discovery Protocol (SSDP) and the UPnPHost services are running. It also verifies the connection between your network adapter and your router and the system's capacity to receive UPnP messages, as well as the router's capability to report an external IP address. One of the tests is designed to check if the Windows firewall service is blocking the traffic between your router and the system, thus preventing UPnP from working. The results can be copied to your clipboard by simply pressing a button and the tests can be redone easily. If you want to fix the detected issues, the link in the main interface can prove useful. In conclusion, UPnP Test is a simple tool for detecting problems related to device-to-device networking. However, it can only suggest possible reasons why the UPnP is not working, fixing the detected issues is totally up to you.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值