VRPN-main函数分析(2/2).md

VRPN-main函数分析(2/2)

上文分析了main函数的主要流程,这里详细分析几个重要的函数。

VRPN服务链接建立:vrpn_create_server_connection

    connection =
        vrpn_create_server_connection(con_name.str().c_str(), g_inLogName, g_outLogName);

函数原型如下:

vrpn_Connection *
vrpn_create_server_connection(const char *cname,
                              const char *local_in_logfile_name,
                              const char *local_out_logfile_name)
  • 参数:
    • cname:指定建立链接的地址,可以建立本地TCP/UDP类型,或loopback类型,或MPI类型,默认是”:3883”,将绑定本机的3883端口。
    • local_in_logfile_name:日志输入文件名,默认标准输入1。
    • local_out_logfile_name:日志输出文件名,默认标准输出2。
  • 返回值:如果成功,返回一个指向该其子类vrpn_Connection_IP的链接,如果失败,则返回NULL;

这个接口会根据传入的第一个参数来建立链接,启动vrpn服务时若没有带命令参数则使用本机3883端口来监听客户端的连接。

vrpn_Connection *
vrpn_create_server_connection(const char *cname,
                              const char *local_in_logfile_name,
                              const char *local_out_logfile_name)
{
    vrpn_Connection *c = NULL;
    ...(//======这里有一大段根据NIC指定的参数,即cname做字符串解析,省略======)
        else {
            // Find machine name and port number.  Port number returns default
            // if there is not one specified.  If the machine name is zero
            // length
            // (just got :port) then use NULL, which means the default NIC.
            char *machine = vrpn_copy_machine_name(location);
            if (strlen(machine) == 0) {
                delete[] machine;
                machine = NULL;
            }
            unsigned short port =
                static_cast<unsigned short>(vrpn_get_port_number(location));
            //建立VRPN链接
            c = new vrpn_Connection_IP(port, local_in_logfile_name,
                                       local_out_logfile_name, machine);
            printf("%s:machine=%s,port=%d\n",__func__,machine,port);
            if (machine) {
                delete[] machine;
            }
        }
    }
    delete[] location;

    if (!c) { // creation failed
        fprintf(stderr, "vrpn_create_server_connection(): Could not create new "
                        "connection.");
        return NULL;
    }
    //设置d_autoDeleteStatus为true,即当引用计数为0时,vrpn_connect
    //链接将会被delete掉
    c->setAutoDeleteStatus(true); 
    //添加引用计数
    c->addReference(); 

    return c;
}

上面代码中主要是调用vrpn_Connection_IP的构造函数来建立连接,其代码内容如下:

vrpn_Connection_IP::vrpn_Connection_IP(
    unsigned short listen_port_no, const char *local_in_logfile_name,
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值