Rate This Document
Findability
Accuracy
Completeness
Readability

SendFully

Function Usage

Sends data to the peer end.

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 calls this function to send data. The communication library must ensure that SendFully returns a result only after it has sent all the data transferred by the caller or when an error occurs. In addition, the communication library must support the calling of multi-thread mutexes.

Prototype

ssize_t SendFully(int connection, uint8_t *buf, size_t len)

Parameters

Parameter

Input/Output

Type

Description

connection

Input

int

Connection handle returned by OnNewConnectionCallback

buf

Input

uint8_t *

Memory pointer that points to the data to be sent.

len

Input

size_t

Length of the data to be sent. The size cannot exceed 64 KB.

Returns

Data type: ssize_t

The value can be:

  • > 0: Number of sent bytes
  • -3: Invalid parameter. For example, the connection handle is invalid, buf is null, or len is 0.
  • -4: Disconnected

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 to the new connection.
static void OnNewConnection(int conn);

void Test()  
{
    // Function symbol for dynamically loading the communication dynamic 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);
}