本文以C Link SDK中的Demo文件./demos/tunnel_basic_demo.c
为例,介绍业务进程与远程登录进程隔离中远程登录运维进程的处理过程以及相关日志。
流程图
操作步骤
初始化。
/* 创建1个实例并内部初始化默认参数 */ void *tunnel_handle = aiot_tunnel_init(); if (tunnel_handle == NULL) { printf( "aiot_tunnel_init failed\n"); return -1; }
配置功能。
/* 配置网络连接的安全凭据, 上面已经创建好了 */ aiot_tunnel_setopt(tunnel_handle, AIOT_TUNNELOPT_NETWORK_CRED, (void *)&cred); /* 配置RA内部事件回调函数, 可选*/ aiot_tunnel_setopt(tunnel_handle, AIOT_TUNNELOPT_EVENT_HANDLER, (void *)_demo_tunnel_event_cb); /* 配置本地可支持的服务 */ for(int i = 0; i < sizeof(services) / sizeof(aiot_tunnel_service_t); i++) { aiot_tunnel_setopt(tunnel_handle, AIOT_TUNNELOPT_ADD_SERVICE, (void *)&services[i]); }
启动隧道后台服务。
/*开启线程,运行RA服务*/ pthread_attr_t attr; pthread_attr_init(&attr); if (0 != pthread_create(&g_tunnel_process_thread, &attr, aiot_tunnel_start, (void*) tunnel_handle)) { printf( "create remote_proxy_thread error!"); return -1; }
进程间通信,接收建连信息。
g_tunnel_switch_thread_running = 1; if (0 != pthread_create(&g_tunnel_switch_thread, &attr, demo_tunnel_listen_task, (void*) tunnel_handle)) { printf( "create aiot_tunnel_start error!"); return -1; }
隧道建连。
void demo_tunnel_do_operation(void *tunnel_handle, char *data, int32_t len) { demo_tunnel_info_t *info = (demo_tunnel_info_t *)data; aiot_tunnel_connect_param_t params; if(len != sizeof(demo_tunnel_info_t)) { return; } memset(¶ms, 0, sizeof(params)); params.host = info->host; params.port = info->port; params.path = info->path; params.token = info->token; if (1 == info->operation ) { /* 新增隧道建连 */ aiot_tunnel_add(tunnel_handle, info->tunnel_id, ¶ms); } else if(0 == info->operation) { /* 关闭隧道 */ aiot_tunnel_delete(tunnel_handle, info->tunnel_id); } else if(2 == info->operation) { /* 更新隧道建连信息 */ aiot_tunnel_update(tunnel_handle, info->tunnel_id, ¶ms); } }
运行日志
启动进程间通信监听。
demo_tunnel_listen_task Listen
监听到消息,并添加进隧道服务,完成隧道建连。
msg recv data len 644, [1644819889.299][LK-1C80] remote proxy thread start! [1644819889.344][LK-1C80] start to create cloud channel [1644819889.344][LK-1C80] connect remote service host iot-secure-tun******.aliyuncs.com, port 443success to establish tcp, fd=3 local port: 44444 [1644819889.400][LK-1000] establish mbedtls connection with server(host='iot-secure-*******.aliyuncs.com', port=[443]) [1644819889.455][LK-1000] success to establish mbedtls connection, (cost 55961 bytes in total, max used 58649 bytes) [1644819889.522][LK-0F10] open_cloud_proxy_channel success [1644819889.522][LK-1C80] websocket 4a2f************ee-b41c-4c601630e51a opened ra_event_cb AIOT_TUNNEL_EVT_CONNECT 4a2f************ee-b41c-4c601630e51a msg recv data len 644, ra_event_cb AIOT_TUNNEL_EVT_DISCONNECT 4a************ee-b41c-4c601630e51a [1644819893.633][LK-1C80] end the cloud channel. [1644819893.633][LK-1C80] close cloud channel! [1644819893.633][LK-1C80] websocket 4a***************e-b41c-4c601630e51a closed [1644819893.633][LK-1000] adapter_network_deinit [1644819893.688][LK-1C80] start to create cloud channel [1644819893.688][LK-1C80] connect remote service host iot-******************e.aliyuncs.com, port 443success to establish tcp, fd=3 local port: 44468 [1644819893.699][LK-1000] establish mbedtls connection with server(host='iot-s**************pre.aliyuncs.com', port=[443]) [1644819893.777][LK-1000] success to establish mbedtls connection, (cost 55961 bytes in total, max used 58649 bytes) [1644819893.833][LK-0F10] open_cloud_proxy_channel success [1644819893.833][LK-1C80] websocket 4a************ee-b41c-4c601630e51a opened ra_event_cb AIOT_TUNNEL_EVT_CONNECT 4a************ee-b41c-4c601630e51a
文档内容是否对您有帮助?