Type: amongoc_loop
#
- <amongoc/loop.h> (header file)#
- <amongoc/default_loop.h> (header file)#
-
struct amongoc_const_buffer#
-
struct amongoc_mutable_buffer#
Provides sized-buffer types that can be used for scatter/gather I/O. The
buf
member ofamongoc_const_buffer
is a pointer-to-constvoid
.
-
struct amongoc_loop#
An event loop for the amongoc library.
- Header:
-
amongoc_box userdata#
The arbitrary user data associated with the event loop.
-
amongoc_loop_vtable const *vtable#
The virtual method table for the event loop
-
mlib_allocator amongoc_loop_get_allocator(const amongoc_loop *loop)#
Get the
mlib_allocator
associate with an event loop.- Parameters:
loop – An initialized event loop
- Header:
Calls
amongoc_loop_vtable::get_allocator()
to look for the allocator. If the loop does not provide an allocator the this function will returnamongoc_default_allocator
.
-
struct amongoc_loop_vtable#
- Header:
This struct should be filled out with function pointers that implement the associated asynchronous operation functions. The “methods” documented below correspond to the function pointers that need to be provided.
Function Pointers
- void call_soon(
- amongoc_loop *self,
- amongoc_status st,
- amongoc_box arg,
- amongoc_handler hnd,
Register the given
amongoc_handler
to be completed by the event loop without any blocking or associated delay.- Parameters:
st – The
amongoc_status
to be given toamongoc_handler_complete()
arg – The value that will be passed as the
res
argument foramongoc_handler_complete()
hnd – The
amongoc_handler
that will be run.
The event loop must invoke the handler as-if by: amongoc_handler_complete(hnd, st, arg).
- void call_later(
- amongoc_loop *self,
- timespec duration,
- amongoc_box arg,
- amongoc_handler hnd,
Register an
amongoc_handler
to be completed after a set delay.- Parameters:
duration – The delay after which the operation should be completed.
arg – The result value that should be passed to the handler when it is completed.
hnd – The handler that should be completed.
The event loop should perform amongoc_handler_complete(hnd, amongoc_okay, arg) no sooner than after
duration
amount of time has elapsed since the call tocall_later()
.If the event loop needs to invoke the handler earlier due to errors or cancellation, then a non-zero
amongoc_status
should be given toamongoc_handler_complete()
to notify the handler that its duration may not have elapsed.
- void getaddrinfo(
- amongoc_loop *self,
- const char *name,
- const char *svc,
- amongoc_handler on_resolve,
Initiate a name-resolution operation.
- Parameters:
name – The name that should be resolve (e.g. a domain name or IP address)
svc – Hint for the service to be resolved (e.g. a port number or protocol name)
on_resolve – The handler to be invoked when resolution completes.
Upon success, the result value given to
amongoc_handler_complete()
will be treated as an opaque object containing the resolved results, to be used withtcp_connect()
.
- void tcp_connect(
- amongoc_loop *self,
- amongoc_view addrinfo,
- amongoc_handler on_connect,
Initiate a TCP connect operation.
- Parameters:
addrinfo – The result object that was given to the
on_resolve
handler from a successful completion of agetaddrinfo()
operation.on_connect – The handler to be invoked when the operation completes.
Upon success, the result value to
amongoc_handler_complete()
will be treated as an opaque object representing the live TCP connection. The connection object may be destroyed at any time viaamongoc_box_destroy()
, which should release any associated resources and close the connection.
- void tcp_write_some(
- amongoc_loop *self,
- amongoc_view conn,
- const amongoc_const_buffer *bufs,
- size_t nbufs,
- amongoc_handler on_write,
Write some data to a TCP connection.
- Parameters:
conn – The connection object that resulted from
tcp_connect()
.bufs – Pointer to an array of buffers to be written.
nbufs – The number of buffers pointer-to by
bufs
.on_write – The handler for the operation.
This function should write some data from the given buffesr into the TCP connection referenced by
conn
. It is not required that all data be written in a single call. The result value given toamongoc_handler_complete()
must be asize_t
value equal to the number of bytes that were successfully written to the stream.
- void tcp_read_some(
- amongoc_loop *self,
- amongoc_view conn,
- const amongoc_mutable_buffer *bufs,
- size_t nbufs,
- amongoc_handler on_read,
Read some data from a TCP connection.
- Parameters:
conn – The connection object that came from
tcp_connect()
.bufs – Pointer to an array of buffers that will receive data.
maxlen – The number of buffers pointed-to by
bufs
.on_read – A handler for the operation.
This function should read data from the TCP connection
conn
into the buffers ofbufs
. The result given toamongoc_handler_complete()
must be asize_t
value equal to the number of bytes that were read from the stream.
-
mlib_allocator get_allocator(const amongoc_loop *self) [[optional]]#
Obtain the
mlib_allocator
associated with the event loop. Various library components will call this function to perform dynamic memory management for objects associated with the event loop.Note
Do not call this method directly. Use
amongoc_loop_get_allocator()
.
The Default Event Loop#
amongoc
provides a default event loop in
amongoc/default_loop.h
. This is a simple single-threaded event
loop that provides all the base operations.
-
void amongoc_default_loop_init(amongoc_loop *[[storage]] loop)#
Initialize a new default event loop.
- Parameters:
loop – Pointer to
[[storage]]
for a newamongoc_loop
- Header:
Each call to this function must be followed by a later call to
amongoc_default_loop_destroy()
.
-
void amongoc_default_loop_destroy(amongoc_loop *loop)#
Destroy a default event loop.
- Parameters:
loop – Pointer to a loop that was previously initiatlized using
amongoc_default_loop_init()
.- Header:
-
void amongoc_default_loop_run(amongoc_loop *loop)#
Execute the default event loop.
- Parameters:
loop – A loop constructed with
amongoc_default_loop_init()
.- Header:
This function will run all pending asynchronous operations until there is no more work to be executed in the event loop.