|
cwist
Modern C web framework with libttak runtime
|
cwist_nats.c interface. More...
#include <cwist/net/nats/cwist_nats.h>#include <cwist/core/mem/alloc.h>#include <nats.h>#include <stdlib.h>#include <string.h>
Go to the source code of this file.
Macros | |
| #define | _GNU_SOURCE |
Functions | |
| cwist_error_t | cwist_nats_connect (cwist_nats_t **nats, const char *url) |
| Connect to a NATS server and create a handle. | |
| cwist_error_t | cwist_nats_subscribe (cwist_nats_t *nats, const char *subject, cwist_nats_msg_cb cb, void *ctx) |
| Subscribe to a NATS subject and register a message callback. | |
| cwist_error_t | cwist_nats_publish_string (cwist_nats_t *nats, const char *subject, const char *data) |
| Publish a NUL-terminated string message to a subject. | |
| void | cwist_nats_dispatch (cwist_nats_t *nats) |
| Pump pending I/O on the connection. | |
| void | cwist_nats_destroy (cwist_nats_t *nats) |
| Destroy a handle and release its resources. | |
| natsConnection * | cwist_nats_native (cwist_nats_t *nats) |
| Access the underlying NATS connection. | |
cwist_nats.c interface.
Definition in file cwist_nats.c.
| #define _GNU_SOURCE |
Definition at line 5 of file cwist_nats.c.
| cwist_error_t cwist_nats_connect | ( | cwist_nats_t ** | nats, |
| const char * | url | ||
| ) |
Connect to a NATS server and create a handle.
Connect to a NATS server.
Allocates a cwist_nats_t and connects to the given URL, or to the NATS default URL when url is NULL.
| [out] | nats | Receives the new handle on success; must not be NULL. |
| url | Server URL, or NULL for the default server. |
nats is NULL, CWIST_ERROR_NOMEM on allocation failure, or the natsStatus code on connection failure. Definition at line 56 of file cwist_nats.c.
References cap, cwist_alloc(), CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, cwist_free(), and make_error().
| void cwist_nats_destroy | ( | cwist_nats_t * | nats | ) |
Destroy a handle and release its resources.
Disconnect and destroy the NATS handle.
Unsubscribes and disconnects if still active, then frees the handle. Safe to call with NULL; the handle must not be used afterwards.
| nats | Handle to destroy, or NULL. |
Definition at line 161 of file cwist_nats.c.
References cap, and cwist_free().
| void cwist_nats_dispatch | ( | cwist_nats_t * | nats | ) |
Pump pending I/O on the connection.
Dispatch incoming messages.
Flushes the connection, pushing buffered outgoing messages and processing incoming traffic so delivered callbacks can run. Safe to call with a NULL or disconnected handle; such calls are no-ops.
| nats | Handle to flush, or NULL. |
Definition at line 148 of file cwist_nats.c.
References cap.
| natsConnection * cwist_nats_native | ( | cwist_nats_t * | nats | ) |
Access the underlying NATS connection.
Borrow the underlying cnats connection.
The returned pointer is owned by the handle and remains valid until cwist_nats_destroy is called.
| nats | Handle to query, or NULL. |
nats is NULL. Definition at line 180 of file cwist_nats.c.
References cap.
| cwist_error_t cwist_nats_publish_string | ( | cwist_nats_t * | nats, |
| const char * | subject, | ||
| const char * | data | ||
| ) |
Publish a NUL-terminated string message to a subject.
Publish a string message.
A NULL data is published as an empty string. Flushes the connection after publishing so the message is written out before returning.
| nats | Handle with an active connection; must not be NULL. |
| subject | Subject to publish to; must not be NULL. |
| data | Payload string, or NULL to send an empty payload. |
Definition at line 126 of file cwist_nats.c.
References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, and make_error().
| cwist_error_t cwist_nats_subscribe | ( | cwist_nats_t * | nats, |
| const char * | subject, | ||
| cwist_nats_msg_cb | cb, | ||
| void * | ctx | ||
| ) |
Subscribe to a NATS subject and register a message callback.
Subscribe to a subject.
Replaces any existing subscription on the handle: the previous subscription is destroyed before the new one is created. The callback is invoked from the NATS delivery thread via cwist_nats_adapter.
| nats | Handle with an active connection; must not be NULL. |
| subject | Subject to subscribe to; must not be NULL. |
| cb | Callback invoked for each incoming message; may be NULL. |
| ctx | Opaque pointer passed back to cb. |
Definition at line 92 of file cwist_nats.c.
References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, and make_error().