RegConnectionCB
Function Usage
Registers the notification callback function after a new connection is set up.
Secondary developers can implement a communication library based on any communication protocol. After the communication library establishes a reliable transmission channel, the callback function notifies the engine that a connection has been established. Then, the engine performs data communication via this channel.
Restrictions
The instruction stream engine requires secondary developers to implement the communication module for sending and receiving data. This function is an external symbol required by the instruction stream engine for secondary developers to implement in the communication module. The engine client calls this function to register the notification callback function after a new connection is set up.
Prototype
using OnNewConnectionCallback = void (*)(int connection);
int RegConnectionCB(OnNewConnectionCallback cb);
Parameters
Parameter |
Input/Output |
Type |
Description |
|---|---|---|---|
cb |
Input |
OnNewConnectionCallback |
Notification callback function after a new connection is set up |
Returns
Data Type: int
The value can be:
- 0: The callback function is successfully registered.
- Other values except 0: error code.
Example Call
// The function prototype declaration is implemented by users.
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";
// The callback function notifies the server that a new connection has been established, which is implemented by the cloud phone engine.
// The input parameter connection indicates the valid handle of the new connection.
static void OnNewConnection(int conn);
void Test()
{
// Function symbol for dynamically loading the dynamic communication library
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);
// Connect to the server.
int connRet = regConnection(OnNewConnection);
// Send data.
ssize_t ret = sendFully(conn, data, bufSize);
// Receive data.
ret = recvData(conn, data, bufSize);
// Release resources.
free(data);
closeConnection(conn);
dlclose(handle);
}