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,