int nopoll_conn_read ( noPollConn conn,
char *  buffer,
int  bytes,
nopoll_bool  block,
long int  timeout 
)

Allows to read the provided amount of bytes from the provided connection, leaving the content read on the buffer provided.

Optionally, the function allows blocking the caller until the amount of bytes requested are satisfied. Also, the function allows to timeout the operation after provided amount of time.

Parameters
connThe connection where the read operation will take place.
bufferThe buffer where the result is returned. Memory buffer must be enough to hold bytes requested and must be acquired by the caller.
bytesNumber of bytes to be read from the connection.
blockIf nopoll_true, the caller will be blocked until the amount of bytes requested are satisfied or until the timeout is reached (if enabled). If nopoll_false is provided, the function won't block and will return all bytes available at this moment.
timeout(milliseconds 1sec = 1000ms) If provided a value higher than 0, a timeout will be enabled to complete the operation. If the timeout is reached, the function will return the bytes read so far. Please note that the function has a precision of 10ms.
Returns
Number of bytes read or -1 if it fails. The function returns -1 when no content is available to be read and you pass block == nopoll_false

Note that the function doesn't clear the buffer received. Only memory (bytes) notified by the value returned by this function should be accessed by the caller. In the same direction you can't use the buffer as a nul-terminated string because the function doesn't add the final \0 to the content read.

More calls to nopoll_conn_read might be needed

Because nopoll_conn_read calls to nopoll_conn_get_msg to get content to satisfy buffer read requested, it might happen that the actual amount of bytes requested by this function (nopoll_conn_read) is smaller than the number of bytes that can be satisfied (because nopoll_conn_get_msg reported a noPollMsg with bigger content).

In such case, these bytes will be reported on next call to nopoll_conn_read.

However, it might also happen that the socket is being watched (by select(), poll(), epoll(), similar mechanism) and because all bytes have been read and are retained in a noPollMsg inside the provided noPollConn) this might cause a block until a forced nopoll_conn_read happens again (because the socket does not report any content to be read and nopoll_conn_read is not called).

In such scenarios, if you want to avoid such lockings, please use nopoll_conn_read_pending to do more calls to nopoll_conn_read

References nopoll_conn_get_msg(), nopoll_conn_is_ok(), NOPOLL_EWOULDBLOCK, NOPOLL_LEVEL_CRITICAL, NOPOLL_LEVEL_DEBUG, nopoll_msg_get_payload(), nopoll_msg_get_payload_size(), nopoll_msg_ref(), nopoll_msg_unref(), nopoll_sleep(), nopoll_timeval_substract(), and nopoll_true.