Mstdlib-1.24.0
Common Blocking (synchronous) IO functions

Functions

M_io_error_t M_io_block_connect (M_io_t *io)
 
M_io_error_t M_io_block_accept (M_io_t **io_out, M_io_t *server_io, M_uint64 timeout_ms)
 
M_io_error_t M_io_block_read (M_io_t *io, unsigned char *buf, size_t buf_len, size_t *len_read, M_uint64 timeout_ms)
 
M_io_error_t M_io_block_read_into_buf (M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms)
 
M_io_error_t M_io_block_read_into_parser (M_io_t *io, M_parser_t *parser, M_uint64 timeout_ms)
 
M_io_error_t M_io_block_write (M_io_t *io, const unsigned char *buf, size_t buf_len, size_t *len_written, M_uint64 timeout_ms)
 
M_io_error_t M_io_block_write_from_buf (M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms)
 
M_io_error_t M_io_block_disconnect (M_io_t *io)
 

Detailed Description

The io system can be used with blocking operations. An explicit event loop is not required. This allows the stacked layers to be utilized with a more traditional blocking design.

Here is an example of the system using the loopback io back end to simulate a network connection to a remote server.

#include <mstdlib/mstdlib.h>
#include <mstdlib/mstdlib_io.h>
int main(int argc, char *argv)
{
M_io_t *io = NULL;
M_buf_t *buf;
M_parser_t *parser;
char *out;
buf = M_buf_create();
M_buf_add_str(buf, "TEST 123");
M_io_block_write_from_buf(io, buf, M_TIMEOUT_INF);
M_io_block_read_into_parser(io, parser, M_TIMEOUT_INF);
out = M_parser_read_strdup(parser, M_parser_len(parser));
M_printf("%s\n", out);
M_free(out);
M_buf_add_str(buf, "abc 456");
M_io_block_write_from_buf(io, buf, M_TIMEOUT_INF);
M_io_block_read_into_parser(io, parser, M_TIMEOUT_INF);
out = M_parser_read_strdup(parser, M_parser_len(parser));
M_printf("%s\n", out);
M_free(out);
return 0;
}
struct M_buf M_buf_t
Definition: m_buf.h:77
M_buf_t * M_buf_create(void) M_WARN_UNUSED_RESULT M_MALLOC
void M_buf_add_str(M_buf_t *buf, const char *str)
void M_buf_cancel(M_buf_t *buf) M_FREE(1)
ssize_t M_printf(const char *fmt,...)
M_io_error_t M_io_block_write_from_buf(M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms)
M_io_error_t M_io_block_disconnect(M_io_t *io)
M_io_error_t M_io_block_read_into_parser(M_io_t *io, M_parser_t *parser, M_uint64 timeout_ms)
M_io_error_t M_io_block_connect(M_io_t *io)
M_io_error_t M_io_loopback_create(M_io_t **io_out)
struct M_io M_io_t
Definition: m_io.h:59
void M_free(void *ptr) M_FREE(1)
size_t M_parser_len(const M_parser_t *parser)
void M_parser_destroy(M_parser_t *parser)
char * M_parser_read_strdup(M_parser_t *parser, size_t len)
struct M_parser M_parser_t
Definition: m_parser.h:52
M_parser_t * M_parser_create(M_uint32 flags)
@ M_PARSER_FLAG_NONE
Definition: m_parser.h:60

Function Documentation

◆ M_io_block_connect()

M_io_error_t M_io_block_connect ( M_io_t io)

Connect the io object to the remote end point.

Parameters
[in]ioio object.
Returns
Result.

◆ M_io_block_accept()

M_io_error_t M_io_block_accept ( M_io_t **  io_out,
M_io_t server_io,
M_uint64  timeout_ms 
)

Accept an io connection.

Parameters
[out]io_outio object created from the accept.
[in]server_ioio object which was listening.
[in]timeout_msAmount of time in milliseconds to wait for data.
Returns
Result.

◆ M_io_block_read()

M_io_error_t M_io_block_read ( M_io_t io,
unsigned char *  buf,
size_t  buf_len,
size_t *  len_read,
M_uint64  timeout_ms 
)

Read from an io object.

Parameters
[in]ioio object.
[out]bufBuffer to store data read from io object.
[in]buf_lenLenght of provided buffer.
[out]len_readNumber of bytes fread from the io object.
[in]timeout_msAmount of time in milliseconds to wait for data.
Returns
Result.

◆ M_io_block_read_into_buf()

M_io_error_t M_io_block_read_into_buf ( M_io_t io,
M_buf_t buf,
M_uint64  timeout_ms 
)

Read from an io object into an M_buf_t.

This will read all available data into the buffer.

Parameters
[in]ioio object.
[out]bufBuffer to store data read from io object.
[in]timeout_msAmount of time in milliseconds to wait for data.
Returns
Result.

◆ M_io_block_read_into_parser()

M_io_error_t M_io_block_read_into_parser ( M_io_t io,
M_parser_t parser,
M_uint64  timeout_ms 
)

Read from an io object into an M_parser_t.

This will read all available data into the buffer.

Parameters
[in]ioio object.
[out]parserParser to store data read from io object.
[in]timeout_msAmount of time in milliseconds to wait for data.
Returns
Result.

◆ M_io_block_write()

M_io_error_t M_io_block_write ( M_io_t io,
const unsigned char *  buf,
size_t  buf_len,
size_t *  len_written,
M_uint64  timeout_ms 
)

Write data to an io object.

This function will attempt to write as much data as possible. If not all data is written the application should try again.

Parameters
[in]ioio object.
[in]bufBuffer to write from.
[in]buf_lenNumber of bytes in buffer to write.
[out]len_writtenNumber of bytes from the buffer written.
[in]timeout_msAmount of time in milliseconds to wait for data.
Returns
Result.

◆ M_io_block_write_from_buf()

M_io_error_t M_io_block_write_from_buf ( M_io_t io,
M_buf_t buf,
M_uint64  timeout_ms 
)

Write data to an io object from an M_buf_t.

This function will attempt to write as much data as possible. If not all data is written the application should try again.

Parameters
[in]ioio object.
[in]bufBuffer to write from.
[in]timeout_msAmount of time in milliseconds to wait for data.
Returns
Result.

◆ M_io_block_disconnect()

M_io_error_t M_io_block_disconnect ( M_io_t io)

Gracefully issue a disconnect to the communications object.

Parameters
[in]ioio object.
Returns
Result.