Function anjay_get_sockets

Function Documentation

AVS_LIST<avs_net_socket_t*const> anjay_get_sockets(anjay_t *anjay)

Retrieves a list of sockets used for communication with LwM2M servers. Returned list must not be freed nor modified.

Example usage: poll()-based application loop

struct pollfd poll_fd = { .events = POLLIN, .fd = -1 };

while (true) {
    AVS_LIST(avs_net_socket_t*) sockets = anjay_get_sockets(anjay);
    if (sockets) {
        // assuming there is only one socket
        poll_fd.fd = *(const int*)avs_net_socket_get_system(*sockets);
    } else {
        // sockets not initialized yet
        poll_fd.fd = -1;
    }
    if (poll(&poll_fd, 1, 1000) > 0) {
         if (poll_fd.revents & POLLIN) {
             if (anjay_serve(anjay, *sockets)) {
                 log("anjay_serve failed");
             }
         }
    }
}

NOTE: The returned list will be invalidated by any subsequent call to anjay_get_sockets() or anjay_get_socket_entries . If you need to call these functions from multiple threads, you need to implement additional synchronization to achieve thread safety.

The socket object pointers themselves may additionally be invalidated by a call to anjay_sched_run, anjay_serve, anjay_serve_any or during the execution of anjay_event_loop_run or anjay_event_loop_run_with_error_handling . For this reason, it is recommended to only call this function from callback functions called from within Anjay, in scheduler jobs, or as part of a custom event loop.

Parameters:

anjay – Anjay object to operate on.

Returns:

A list of valid server sockets on success, NULL when the device is not connected to any server.