---
title: RegConnectionCB
description: "注册新连接建立后的通知回调函数。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_17_0012.html
sourcePath: /source/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_17_0012.html
indexId: 3496952056fde39b7cda83b5f40c9383c391d594c45f5ec7a3909438151121bc85
---
# RegConnectionCB

#### 函数功能

注册新连接建立后的通知回调函数。

二次开发者可基于任意通信协议实现通信库，并在通信库建立好可靠传输通信通道后，通过回调函数通知引擎已经建立好连接，后续引擎将基于该连接进行数据通信。


#### 约束说明

指令流引擎要求二次开发者实现通信模块，用于数据的发送和接收。该函数是指令流引擎要求二次开发者在通信模块实现的外部符号，指令流客户端引擎会调用该函数注册新连接建立后的通知回调函数。


#### 函数原型

using OnNewConnectionCallback = void (*)(int connection);

int RegConnectionCB(OnNewConnectionCallback cb);


#### 参数说明

| 参数名称 | 输入/输出 | 参数类型 | 参数描述 |
| --- | --- | --- | --- |
| cb | 输入 | OnNewConnectionCallback | 新连接建立后的通知回调函数。 |


#### 返回值说明

数据类型：int

取值如下：

- 0：注册回调函数成功。
- 非0：错误码。


#### 调用示例

```
// 函数原型声明，实现由客户提供 
using OnNewConnectionCallback = void (*)(int connection);
using RegConnectionCBFunc = int (*)(OnNewConnectionCallback newConnCb);
using CloseConnectionFunc = int (*)(int connection);
using SendFullyFunc = ssize_t (*)(int connection, uint8_t *buf, size_t len); 
using RecvFunc = ssize_t (*)(int connection, uint8_t *buf, size_t len);
const char *soPath = "./libCommunication.so";

//服务端收到新连接回调函数，由云手机引擎提供
//输入参数connection代表新连接的有效句柄
static void OnNewConnection(int conn);

void Test()  
{
    // 动态加载通信动态库的函数符号
    void *handle = dlopen(soPath, RTLD_GLOBAL | RTLD_LAZY | RTLD_NODELETE);
    RegConnectionCBFunc regConnection = (RegConnectionCBFunc)dlsym(handle, "RegConnectionCB");
    CloseConnectionFunc closeConnection = (CloseConnectionFunc)dlsym(handle, "CloseConnection");
    SendFullyFunc sendFully = (SendFullyFunc)dlsym(handle, "SendFully");
    RecvFunc recvData = (RecvFunc)dlsym(handle, "Recv");
    size_t bufSize = 1024; 
    uint8_t *data = malloc(bufSize); 
    // 创建连接，连接服务端 
    int connRet = regConnection(OnNewConnection);  
    // 发送数据
    ssize_t ret = sendFully(conn, data, bufSize); 
    // 接收数据
    ret = recvData(conn, data, bufSize);
    // 释放资源 
    free(data); 
    closeConnection(conn);
    dlclose(handle);
}
```
