aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2025-01-12 14:43:31 -0800
committerGarrett D'Amore <garrett@damore.org>2025-01-12 14:43:31 -0800
commit2ddaecdadb8a931188b3f3e6b8ad43b9cba45d0f (patch)
treeddab0fbc8bb631fc31c233e09cd83a561988158d
parentd88484cafbf973d55dc95b7edcae5064efa8bad0 (diff)
downloadnng-2ddaecdadb8a931188b3f3e6b8ad43b9cba45d0f.tar.gz
nng-2ddaecdadb8a931188b3f3e6b8ad43b9cba45d0f.tar.bz2
nng-2ddaecdadb8a931188b3f3e6b8ad43b9cba45d0f.zip
api: extend usage of nng_err
This replaces the int, and we will expand this further, as this makes it clear that the int is actually an error code and helps in debuggers that can provide symbolic values.
-rw-r--r--docs/ref/api/aio.md4
-rw-r--r--docs/ref/api/errors.md12
-rw-r--r--docs/ref/api/http.md25
-rw-r--r--include/nng/http.h50
-rw-r--r--include/nng/nng.h118
-rw-r--r--src/core/aio.c24
-rw-r--r--src/core/aio.h14
-rw-r--r--src/nng.c8
-rw-r--r--src/supplemental/http/http_public.c47
-rw-r--r--tests/cplusplus_pair.cc35
10 files changed, 168 insertions, 169 deletions
diff --git a/docs/ref/api/aio.md b/docs/ref/api/aio.md
index 1e6f76fa..dcf9aa85 100644
--- a/docs/ref/api/aio.md
+++ b/docs/ref/api/aio.md
@@ -81,7 +81,7 @@ is safe to call from _aio_'s own callback.
## Cancellation
```c
-void nng_aio_abort(nng_aio *aio, int err);
+void nng_aio_abort(nng_aio *aio, nng_err err);
void nng_aio_cancel(nng_aio *aio);
void nng_aio_stop(nng_aio *aio);
```
@@ -169,7 +169,7 @@ This is the same test used internally by [`nng_aio_wait`].
## Result of Operation
```c
-int nng_aio_result(nng_aio *aio);
+nng_err nng_aio_result(nng_aio *aio);
size_t nng_aio_count(nng_aio *aio);
```
diff --git a/docs/ref/api/errors.md b/docs/ref/api/errors.md
index 2a4ccc93..c7a50780 100644
--- a/docs/ref/api/errors.md
+++ b/docs/ref/api/errors.md
@@ -1,5 +1,9 @@
# Errors
+```c
+typedef enum ... nng_err;
+```
+
Many _NNG_ functions can fail for a variety of reasons.
These functions tend to return either zero on success,
or a non-zero error code to indicate failure.
@@ -7,7 +11,11 @@ or a non-zero error code to indicate failure.
which behave the same way, but _NNG_ does not use a separate
_errno_ variable.}}
-All these error codes are `int`.
+All these error codes are `nng_err`.
+
+> [!NOTE]
+> Many APIs are still using `int`, but the `nng_err` enumeration can be used
+> instead, and will help with debugging.
Not every possible error code is defined here, as sometimes
an underlying system or library error code is "wrapped".
@@ -15,7 +23,7 @@ an underlying system or library error code is "wrapped".
## Human Readable Error Message
```c
-const char *nng_strerror(int err);
+const char *nng_strerror(nng_err err);
```
The {{i:`nng_strerror`}} returns the human-readable description of the
diff --git a/docs/ref/api/http.md b/docs/ref/api/http.md
index 48b09300..da3d22d4 100644
--- a/docs/ref/api/http.md
+++ b/docs/ref/api/http.md
@@ -56,7 +56,7 @@ set when issuing the transaction.
### HTTP URI
```c
-int nng_http_set_uri(nng_http *conn, const char *uri, const char *query);
+nng_err nng_http_set_uri(nng_http *conn, const char *uri, const char *query);
const char *nng_http_get_uri(nng_http *conn);
```
@@ -76,7 +76,7 @@ will have any query concentated, for example "/api/get_user.cgi?name=garrett".
### HTTP Version
```c
-int nng_http_set_version(nng_http *conn, const char *version);
+nng_err nng_http_set_version(nng_http *conn, const char *version);
const char *nng_http_get_version(nng_http *conn);
```
@@ -210,8 +210,8 @@ The scan can be rest by setting _next_ to `NULL`.
### Modifying Headers
```c
-int nng_http_add_header(nng_http *conn, const char *key, const char *val);
-int nng_http_set_header(nng_http *conn, const char *key, const char *val);
+nng_err nng_http_add_header(nng_http *conn, const char *key, const char *val);
+nng_err nng_http_set_header(nng_http *conn, const char *key, const char *val);
void nng_http_del_header(nng_http *conn, const char *key);
```
@@ -323,7 +323,7 @@ They can be used to transfer request or response body data as well.
### Hijacking Connections
```c
-void nng_http_hijack(nng_http_conn *conn);
+nng_err nng_http_hijack(nng_http *conn);
```
TODO: This API will change to convert the conn into a stream object.
@@ -387,8 +387,8 @@ of its resources.
### Client TLS
```c
-int nng_http_client_get_tls(nng_http_client *client, nng_tls_config **tlsp);
-int nng_http_client_set_tls(nng_http_client *client, nng_tls_config *tls);
+nng_err nng_http_client_get_tls(nng_http_client *client, nng_tls_config **tlsp);
+nng_err nng_http_client_set_tls(nng_http_client *client, nng_tls_config *tls);
```
The {{i:`nng_http_client_get_tls`}} and {{i:`nng_http_client_set_tls`}} functions are used to
@@ -456,15 +456,6 @@ if ((rv = nng_aio_result(aio)) != 0) {
### Preparing a Transaction
-```c
-int nng_http_set_version(nng_http *conn, const char *version);
-int nng_http_set_uri(nng_http *conn, const char *uri);
-```
-
-The {{i:`nng_http_set_uri`}} function provides a URI for the transaction. This will be used to
-set the URI for the request. The URI typically appears like a path, starting with "/", although
-it may also contain a query string.
-
### Request Body
### Sending the Request
@@ -510,7 +501,7 @@ It does _not_ transfer any response body. To do that, use [`nng_http_read_all`]
### Submitting the Transaction
```c
-int nng_http_transact(nng_http *conn, nng_aio *aio);
+void nng_http_transact(nng_http *conn, nng_aio *aio);
```
The HTTP request is issued, and the response processed, asynchronously by the {{i:`nng_http_transact`}} function.
diff --git a/include/nng/http.h b/include/nng/http.h
index 683e1f38..0d271a23 100644
--- a/include/nng/http.h
+++ b/include/nng/http.h
@@ -147,7 +147,7 @@ NNG_DECL void nng_http_reset(nng_http *);
// include an optional query string, either inline in the URI to start,
// or as a separate argument. If the query string already exists
// and one is also supplied here, it will be appended (separated with &).
-NNG_DECL int nng_http_set_uri(nng_http *, const char *, const char *);
+NNG_DECL nng_err nng_http_set_uri(nng_http *, const char *, const char *);
// nng_http_get_uri returns the URI. It will be NULL if not set.
NNG_DECL const char *nng_http_get_uri(nng_http *);
@@ -167,7 +167,7 @@ NNG_DECL void nng_http_set_status(nng_http *, uint16_t, const char *);
// nng_http_set_version is used to change the version of a request.
// Normally the version is "HTTP/1.1". Note that the framework does
// not support HTTP/2 at all. Null sets the default ("HTTP/1.1").
-NNG_DECL int nng_http_set_version(nng_http *, const char *);
+NNG_DECL nng_err nng_http_set_version(nng_http *, const char *);
// nng_http_get_version is used to get the version of a request.
NNG_DECL const char *nng_http_get_version(nng_http *);
@@ -184,8 +184,8 @@ NNG_DECL const char *nng_http_get_method(nng_http *);
// a header to either the request or response. Clients modify the request
// headers, and servers (and callbacks on the server) modify response headers.
// These can return NNG_ENOMEM, NNG_MSGSIZE, etc.
-NNG_DECL int nng_http_set_header(nng_http *, const char *, const char *);
-NNG_DECL int nng_http_add_header(nng_http *, const char *, const char *);
+NNG_DECL nng_err nng_http_set_header(nng_http *, const char *, const char *);
+NNG_DECL nng_err nng_http_add_header(nng_http *, const char *, const char *);
// nng_http_del_header removes all of the headers for the given header.
// For clients this is done on the request headers, for servers its the
@@ -205,7 +205,7 @@ NNG_DECL void nng_http_set_body(nng_http *, void *, size_t);
// nng_http_copy_body sets the body to send out in the next exchange, but
// makes a local copy. It can fail due to NNG_ENOMEM.
-NNG_DECL int nng_http_copy_body(nng_http *, const void *, size_t);
+NNG_DECL nng_err nng_http_copy_body(nng_http *, const void *, size_t);
// nng_http_handler is a handler used on the server side to handle HTTP
// requests coming into a specific URL.
@@ -230,7 +230,7 @@ typedef struct nng_http_handler nng_http_handler;
// completion by nng_aio_finish. The second argument to this function is the
// handler data that was optionally set by nng_handler_set_data.
typedef void (*nng_http_handler_func)(nng_http *, void *, nng_aio *);
-NNG_DECL int nng_http_handler_alloc(
+NNG_DECL nng_err nng_http_handler_alloc(
nng_http_handler **, const char *, nng_http_handler_func);
// nng_http_handler_free frees the handler. This actually just drops a
@@ -241,19 +241,19 @@ NNG_DECL void nng_http_handler_free(nng_http_handler *);
// nng_http_handler_alloc_file creates a "file" based handler, that
// serves up static content from the given file path. The content-type
// supplied is determined from the file name using a simple built-in map.
-NNG_DECL int nng_http_handler_alloc_file(
+NNG_DECL nng_err nng_http_handler_alloc_file(
nng_http_handler **, const char *, const char *);
// nng_http_handler_alloc_static creates a static-content handler.
// The last argument is the content-type, which may be NULL (in which case
// "application/octet-stream" is assumed.)
-NNG_DECL int nng_http_handler_alloc_static(
+NNG_DECL nng_err nng_http_handler_alloc_static(
nng_http_handler **, const char *, const void *, size_t, const char *);
// nng_http_handler_alloc_redirect creates an HTTP redirect handler.
// The status is given, along with the new URL. If the status is 0,
// then 301 will be used instead.
-NNG_DECL int nng_http_handler_alloc_redirect(
+NNG_DECL nng_err nng_http_handler_alloc_redirect(
nng_http_handler **, const char *, uint16_t, const char *);
// nng_http_handler_alloc_file creates a "directory" based handler, that
@@ -262,7 +262,7 @@ NNG_DECL int nng_http_handler_alloc_redirect(
// directory content, otherwise a suitable error page is returned (the server
// does not generate index pages automatically.) The content-type for
// files is determined from the file name using a simple built-in map.
-NNG_DECL int nng_http_handler_alloc_directory(
+NNG_DECL nng_err nng_http_handler_alloc_directory(
nng_http_handler **, const char *, const char *);
// nng_http_handler_set_method sets the method that the handler will be
@@ -310,7 +310,7 @@ typedef struct nng_http_server nng_http_server;
// from the URL. If a server already exists, then a hold is placed on it, and
// that instance is returned. If no such server exists, then a new instance
// is created.
-NNG_DECL int nng_http_server_hold(nng_http_server **, const nng_url *);
+NNG_DECL nng_err nng_http_server_hold(nng_http_server **, const nng_url *);
// nng_http_server_release releases the hold on the server. If this is the
// last instance of the server, then it is shutdown and resources are freed.
@@ -319,7 +319,7 @@ NNG_DECL void nng_http_server_release(nng_http_server *);
// nng_http_server_start starts the server handling HTTP. Once this is
// called, it will not be possible to change certain parameters (such as
// any TLS configuration).
-NNG_DECL int nng_http_server_start(nng_http_server *);
+NNG_DECL nng_err nng_http_server_start(nng_http_server *);
// nng_http_server_stop stops the server. No new client connections are
// accepted after this returns. Once a server is stopped fully, the
@@ -331,14 +331,14 @@ NNG_DECL void nng_http_server_stop(nng_http_server *);
// This function will return NNG_EADDRINUSE if a conflicting handler
// is already registered (i.e. a handler with the same value for Host,
// Method, and URL.)
-NNG_DECL int nng_http_server_add_handler(
+NNG_DECL nng_err nng_http_server_add_handler(
nng_http_server *, nng_http_handler *);
// nni_http_del_handler removes the given handler. The caller is
// responsible for finalizing it afterwards. If the handler was not found
// (not registered), NNG_ENOENT is returned. In this case it is unsafe
// to make assumptions about the validity of the handler.
-NNG_DECL int nng_http_server_del_handler(
+NNG_DECL nng_err nng_http_server_del_handler(
nng_http_server *, nng_http_handler *);
// nng_http_server_set_tls adds a TLS configuration to the server,
@@ -347,36 +347,36 @@ NNG_DECL int nng_http_server_del_handler(
// server client, so the caller must have configured it reasonably.
// This API is not recommended unless the caller needs complete control
// over the TLS configuration.
-NNG_DECL int nng_http_server_set_tls(nng_http_server *, nng_tls_config *);
+NNG_DECL nng_err nng_http_server_set_tls(nng_http_server *, nng_tls_config *);
// nng_http_server_get_tls obtains the TLS configuration if one is present,
// or returns NNG_EINVAL. The TLS configuration is invalidated if the
// nng_http_server_set_tls function is called, so be careful.
-NNG_DECL int nng_http_server_get_tls(nng_http_server *, nng_tls_config **);
+NNG_DECL nng_err nng_http_server_get_tls(nng_http_server *, nng_tls_config **);
// nng_http_server_get_addr obtains the address with which the server was
// initialized or returns NNG_EINVAL. Useful for instance when the port has
// been automatically assigned.
-NNG_DECL int nng_http_server_get_addr(nng_http_server *, nng_sockaddr *);
+NNG_DECL nng_err nng_http_server_get_addr(nng_http_server *, nng_sockaddr *);
// nng_http_server_set_error_page sets a custom error page (HTML) content
// to be sent for the given error code. This is used when the error is
// generated internally by the framework.
-NNG_DECL int nng_http_server_set_error_page(
+NNG_DECL nng_err nng_http_server_set_error_page(
nng_http_server *, uint16_t, const char *);
// nng_http_server_set_error_file works like nng_http_server_error_page,
// except that the content is loaded from the named file path. The contents
// are loaded at the time this function is called, so this function should be
// called anytime the contents of the named file have changed.
-NNG_DECL int nng_http_server_set_error_file(
+NNG_DECL nng_err nng_http_server_set_error_file(
nng_http_server *, uint16_t, const char *);
-// nng_http_server_res_error takes replaces the body of the response with
+// nng_http_server_error takes replaces the body of the response with
// a custom error page previously set for the server, using the status
// of the response. The response must have the status set first using
// nng_http_res_set_status.
-NNG_DECL int nng_http_server_error(nng_http_server *, nng_http *);
+NNG_DECL nng_err nng_http_server_error(nng_http_server *, nng_http *);
// nng_http_hijack is intended to be called by a handler that wishes to
// take over the processing of the HTTP session -- usually to change protocols
@@ -389,7 +389,7 @@ NNG_DECL int nng_http_server_error(nng_http_server *, nng_http *);
// of the request structure. (Some hijackers may keep the request for
// further processing.)
-NNG_DECL int nng_http_hijack(nng_http *);
+NNG_DECL nng_err nng_http_hijack(nng_http *);
// nng_http_client represents a "client" object. Clients can be used
// to create HTTP connections. At present, connections are not cached
@@ -398,7 +398,7 @@ typedef struct nng_http_client nng_http_client;
// nng_http_client_alloc allocates a client object, associated with
// the given URL.
-NNG_DECL int nng_http_client_alloc(nng_http_client **, const nng_url *);
+NNG_DECL nng_err nng_http_client_alloc(nng_http_client **, const nng_url *);
// nng_http_client_free frees the client. Connections created by the
// the client are not necessarily closed.
@@ -408,12 +408,12 @@ NNG_DECL void nng_http_client_free(nng_http_client *);
// the entire TLS configuration on the client, so the caller must have
// configured it reasonably. This API is not recommended unless the
// caller needs complete control over the TLS configuration.
-NNG_DECL int nng_http_client_set_tls(nng_http_client *, nng_tls_config *);
+NNG_DECL nng_err nng_http_client_set_tls(nng_http_client *, nng_tls_config *);
// nng_http_client_get_tls obtains the TLS configuration if one is present,
// or returns NNG_EINVAL. The supplied TLS configuration object may
// be invalidated by any future calls to nni_http_client_set_tls.
-NNG_DECL int nng_http_client_get_tls(nng_http_client *, nng_tls_config **);
+NNG_DECL nng_err nng_http_client_get_tls(nng_http_client *, nng_tls_config **);
// nng_http_client_connect establishes a new connection with the server
// named in the URL used when the client was created. Once the connection
diff --git a/include/nng/nng.h b/include/nng/nng.h
index 516cdeb9..feb5a824 100644
--- a/include/nng/nng.h
+++ b/include/nng/nng.h
@@ -109,6 +109,62 @@ typedef int32_t nng_duration; // in milliseconds
// past, measured in milliseconds. The values are always positive.
typedef uint64_t nng_time;
+// Error codes. These generally have different values from UNIX errnos,
+// so take care about converting them. The one exception is that 0 is
+// unambiguously "success".
+//
+// NNG_SYSERR is a special code, which allows us to wrap errors from the
+// underlying operating system. We generally prefer to map errors to one
+// of the above, but if we cannot, then we just encode an error this way.
+// The bit is large enough to accommodate all known UNIX and Win32 error
+// codes. We try hard to match things semantically to one of our standard
+// errors. For example, a connection reset or aborted we treat as a
+// closed connection, because that's basically what it means. (The remote
+// peer closed the connection.) For certain kinds of resource exhaustion
+// we treat it the same as memory. But for files, etc. that's OS-specific,
+// and we use the generic below. Some of the above error codes we use
+// internally, and the application should never see (e.g. NNG_EINTR).
+//
+// NNG_ETRANERR is like ESYSERR, but is used to wrap transport specific
+// errors, from different transports. It should only be used when none
+// of the other options are available.
+typedef enum {
+ NNG_OK = 0, // not an error!
+ NNG_EINTR = 1,
+ NNG_ENOMEM = 2,
+ NNG_EINVAL = 3,
+ NNG_EBUSY = 4,
+ NNG_ETIMEDOUT = 5,
+ NNG_ECONNREFUSED = 6,
+ NNG_ECLOSED = 7,
+ NNG_EAGAIN = 8,
+ NNG_ENOTSUP = 9,
+ NNG_EADDRINUSE = 10,
+ NNG_ESTATE = 11,
+ NNG_ENOENT = 12,
+ NNG_EPROTO = 13,
+ NNG_EUNREACHABLE = 14,
+ NNG_EADDRINVAL = 15,
+ NNG_EPERM = 16,
+ NNG_EMSGSIZE = 17,
+ NNG_ECONNABORTED = 18,
+ NNG_ECONNRESET = 19,
+ NNG_ECANCELED = 20,
+ NNG_ENOFILES = 21,
+ NNG_ENOSPC = 22,
+ NNG_EEXIST = 23,
+ NNG_EREADONLY = 24,
+ NNG_EWRITEONLY = 25,
+ NNG_ECRYPTO = 26,
+ NNG_EPEERAUTH = 27,
+ NNG_EBADTYPE = 30,
+ NNG_ECONNSHUT = 31,
+ NNG_ESTOPPED = 999,
+ NNG_EINTERNAL = 1000,
+ NNG_ESYSERR = 0x10000000,
+ NNG_ETRANERR = 0x20000000
+} nng_err;
+
typedef struct nng_msg nng_msg;
typedef struct nng_stat nng_stat;
typedef struct nng_aio nng_aio;
@@ -379,7 +435,7 @@ NNG_DECL int nng_listener_get_tls(nng_listener, nng_tls_config **);
// nng_strerror returns a human-readable string associated with the error
// code supplied.
-NNG_DECL const char *nng_strerror(int);
+NNG_DECL const char *nng_strerror(nng_err);
// nng_send sends (or arranges to send) the data on the socket. Note that
// this function may (will!) return before any receiver has actually
@@ -531,7 +587,7 @@ NNG_DECL void nng_aio_stop(nng_aio *);
// nng_aio_result returns the status/result of the operation. This
// will be zero on successful completion, or an nng error code on
// failure.
-NNG_DECL int nng_aio_result(nng_aio *);
+NNG_DECL nng_err nng_aio_result(nng_aio *);
// nng_aio_count returns the number of bytes transferred for certain
// I/O operations. This is meaningless for other operations (e.g.
@@ -546,7 +602,7 @@ NNG_DECL void nng_aio_cancel(nng_aio *);
// nng_aio_abort is like nng_aio_cancel, but allows for a different
// error result to be returned.
-NNG_DECL void nng_aio_abort(nng_aio *, int);
+NNG_DECL void nng_aio_abort(nng_aio *, nng_err);
// nng_aio_wait waits synchronously for any pending operation to complete.
// It also waits for the callback to have completed execution. Therefore,
@@ -1000,62 +1056,6 @@ NNG_DECL void nng_device_aio(nng_aio *, nng_socket, nng_socket);
// automate the promotion of them to other APIs. This is an area open
// for discussion.
-// Error codes. These generally have different values from UNIX errnos,
-// so take care about converting them. The one exception is that 0 is
-// unambiguously "success".
-//
-// NNG_SYSERR is a special code, which allows us to wrap errors from the
-// underlying operating system. We generally prefer to map errors to one
-// of the above, but if we cannot, then we just encode an error this way.
-// The bit is large enough to accommodate all known UNIX and Win32 error
-// codes. We try hard to match things semantically to one of our standard
-// errors. For example, a connection reset or aborted we treat as a
-// closed connection, because that's basically what it means. (The remote
-// peer closed the connection.) For certain kinds of resource exhaustion
-// we treat it the same as memory. But for files, etc. that's OS-specific,
-// and we use the generic below. Some of the above error codes we use
-// internally, and the application should never see (e.g. NNG_EINTR).
-//
-// NNG_ETRANERR is like ESYSERR, but is used to wrap transport specific
-// errors, from different transports. It should only be used when none
-// of the other options are available.
-
-typedef enum nng_errno_enum {
- NNG_EINTR = 1,
- NNG_ENOMEM = 2,
- NNG_EINVAL = 3,
- NNG_EBUSY = 4,
- NNG_ETIMEDOUT = 5,
- NNG_ECONNREFUSED = 6,
- NNG_ECLOSED = 7,
- NNG_EAGAIN = 8,
- NNG_ENOTSUP = 9,
- NNG_EADDRINUSE = 10,
- NNG_ESTATE = 11,
- NNG_ENOENT = 12,
- NNG_EPROTO = 13,
- NNG_EUNREACHABLE = 14,
- NNG_EADDRINVAL = 15,
- NNG_EPERM = 16,
- NNG_EMSGSIZE = 17,
- NNG_ECONNABORTED = 18,
- NNG_ECONNRESET = 19,
- NNG_ECANCELED = 20,
- NNG_ENOFILES = 21,
- NNG_ENOSPC = 22,
- NNG_EEXIST = 23,
- NNG_EREADONLY = 24,
- NNG_EWRITEONLY = 25,
- NNG_ECRYPTO = 26,
- NNG_EPEERAUTH = 27,
- NNG_EBADTYPE = 30,
- NNG_ECONNSHUT = 31,
- NNG_ESTOPPED = 999,
- NNG_EINTERNAL = 1000,
- NNG_ESYSERR = 0x10000000,
- NNG_ETRANERR = 0x20000000
-} nng_err;
-
// nng_url_parse parses a URL string into a structured form.
// Note that the u_port member will be filled out with a numeric
// port if one isn't specified and a default port is appropriate for
diff --git a/src/core/aio.c b/src/core/aio.c
index bbeb5cef..81020bbb 100644
--- a/src/core/aio.c
+++ b/src/core/aio.c
@@ -296,7 +296,7 @@ nni_aio_get_output(nni_aio *aio, unsigned index)
return (NULL);
}
-int
+nng_err
nni_aio_result(nni_aio *aio)
{
return (aio->a_result);
@@ -325,7 +325,7 @@ nni_aio_busy(nni_aio *aio)
void
nni_aio_reset(nni_aio *aio)
{
- aio->a_result = 0;
+ aio->a_result = NNG_OK;
aio->a_count = 0;
aio->a_abort = false;
aio->a_expire_ok = false;
@@ -362,7 +362,7 @@ nni_aio_start(nni_aio *aio, nni_aio_cancel_fn cancel, void *data)
if (!aio->a_sleep) {
aio->a_expire_ok = false;
}
- aio->a_result = 0;
+ aio->a_result = NNG_OK;
// Do this outside the lock. Note that we don't strictly need to have
// done this for the failure cases below (the task framework does the
@@ -388,14 +388,14 @@ nni_aio_start(nni_aio *aio, nni_aio_cancel_fn cancel, void *data)
aio->a_abort = false;
aio->a_expire_ok = false;
aio->a_count = 0;
- NNI_ASSERT(aio->a_result != 0);
+ NNI_ASSERT(aio->a_result != NNG_OK);
nni_mtx_unlock(&eq->eq_mtx);
nni_task_dispatch(&aio->a_task);
return (false);
}
if (timeout) {
aio->a_sleep = false;
- aio->a_result = aio->a_expire_ok ? 0 : NNG_ETIMEDOUT;
+ aio->a_result = aio->a_expire_ok ? NNG_OK : NNG_ETIMEDOUT;
aio->a_expire_ok = false;
aio->a_count = 0;
nni_mtx_unlock(&eq->eq_mtx);
@@ -419,7 +419,7 @@ nni_aio_start(nni_aio *aio, nni_aio_cancel_fn cancel, void *data)
// nni_aio_abort is called by a consumer which guarantees that the aio
// is still valid.
void
-nni_aio_abort(nni_aio *aio, int rv)
+nni_aio_abort(nni_aio *aio, nng_err rv)
{
if (aio != NULL && aio->a_init) {
nni_aio_cancel_fn fn;
@@ -451,7 +451,7 @@ nni_aio_abort(nni_aio *aio, int rv)
static void
nni_aio_finish_impl(
- nni_aio *aio, int rv, size_t count, nni_msg *msg, bool sync)
+ nni_aio *aio, nng_err rv, size_t count, nni_msg *msg, bool sync)
{
nni_aio_expire_q *eq = aio->a_expire_q;
@@ -479,21 +479,21 @@ nni_aio_finish_impl(
}
void
-nni_aio_finish(nni_aio *aio, int result, size_t count)
+nni_aio_finish(nni_aio *aio, nng_err result, size_t count)
{
nni_aio_finish_impl(aio, result, count, NULL, false);
}
void
-nni_aio_finish_sync(nni_aio *aio, int result, size_t count)
+nni_aio_finish_sync(nni_aio *aio, nng_err result, size_t count)
{
nni_aio_finish_impl(aio, result, count, NULL, true);
}
void
-nni_aio_finish_error(nni_aio *aio, int result)
+nni_aio_finish_error(nni_aio *aio, nng_err result)
{
- nni_aio_finish_impl(aio, result, 0, NULL, false);
+ nni_aio_finish_impl(aio, result, NNG_OK, NULL, false);
}
void
@@ -539,7 +539,7 @@ nni_aio_completions_init(nni_aio_completions *clp)
void
nni_aio_completions_add(
- nni_aio_completions *clp, nni_aio *aio, int result, size_t count)
+ nni_aio_completions *clp, nni_aio *aio, nng_err result, size_t count)
{
NNI_ASSERT(!nni_aio_list_active(aio));
aio->a_reap_node.rn_next = *clp;
diff --git a/src/core/aio.h b/src/core/aio.h
index cbf2c919..25475323 100644
--- a/src/core/aio.h
+++ b/src/core/aio.h
@@ -85,7 +85,7 @@ extern nni_msg *nni_aio_get_msg(nni_aio *);
// for the operation. It is only valid to call this when the operation is
// complete (such as when the callback is executed or after nni_aio_wait
// is performed).
-extern int nni_aio_result(nni_aio *);
+extern nng_err nni_aio_result(nni_aio *);
// nni_aio_count returns the number of bytes of data transferred, if any.
// As with nni_aio_result, it is only defined if the I/O operation has
@@ -114,19 +114,19 @@ extern void nni_aio_list_remove(nni_aio *);
extern int nni_aio_list_active(nni_aio *);
// nni_aio_finish is called by the provider when an operation is complete.
-extern void nni_aio_finish(nni_aio *, int, size_t);
+extern void nni_aio_finish(nni_aio *, nng_err, size_t);
// nni_aio_finish_sync is to be called when a synchronous completion is
// desired. It is very important that the caller not hold any locks when
// calling this, but it is useful for chaining completions to minimize
// context switch overhead during completions.
-extern void nni_aio_finish_sync(nni_aio *, int, size_t);
-extern void nni_aio_finish_error(nni_aio *, int);
+extern void nni_aio_finish_sync(nni_aio *, nng_err, size_t);
+extern void nni_aio_finish_error(nni_aio *, nng_err);
extern void nni_aio_finish_msg(nni_aio *, nni_msg *);
// nni_aio_abort is used to abort an operation. Any pending I/O or
// timeouts are canceled if possible, and the callback will be returned
// with the indicated result (NNG_ECLOSED or NNG_ECANCELED is recommended.)
-extern void nni_aio_abort(nni_aio *, int rv);
+extern void nni_aio_abort(nni_aio *, nng_err);
extern void *nni_aio_get_prov_data(nni_aio *);
extern void nni_aio_set_prov_data(nni_aio *, void *);
@@ -186,7 +186,7 @@ extern void nni_aio_completions_run(nni_aio_completions *);
// appropriate) to the completion list. This should be done while the
// appropriate lock is held. The aio must not be scheduled.
extern void nni_aio_completions_add(
- nni_aio_completions *, nni_aio *, int, size_t);
+ nni_aio_completions *, nni_aio *, nng_err, size_t);
extern int nni_aio_sys_init(nng_init_params *);
extern bool nni_aio_sys_drain(void);
@@ -206,7 +206,7 @@ struct nng_aio {
size_t a_count; // Bytes transferred (I/O only)
nni_time a_expire; // Absolute timeout
nni_duration a_timeout; // Relative timeout
- int a_result; // Result code (nng_errno)
+ nng_err a_result; // Result code (nng_errno)
bool a_stop; // Shutting down (no new operations)
bool a_sleep; // Sleeping with no action
bool a_expire_ok; // Expire from sleep is ok
diff --git a/src/nng.c b/src/nng.c
index 002f29f8..d9b111a8 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -1234,7 +1234,7 @@ nng_device(nng_socket s1, nng_socket s2)
}
static const struct {
- int code;
+ nng_err code;
const char *msg;
} nni_errors[] = {
// clang-format off
@@ -1276,7 +1276,7 @@ static const struct {
// Misc.
const char *
-nng_strerror(int num)
+nng_strerror(nng_err num)
{
static char unknownerrbuf[32];
for (int i = 0; nni_errors[i].msg != NULL; i++) {
@@ -1859,7 +1859,7 @@ nng_sleep_aio(nng_duration ms, nng_aio *aio)
nni_sleep_aio(ms, aio);
}
-int
+nng_err
nng_aio_result(nng_aio *aio)
{
return (nni_aio_result(aio));
@@ -1890,7 +1890,7 @@ nng_aio_busy(nng_aio *aio)
}
void
-nng_aio_abort(nng_aio *aio, int err_code)
+nng_aio_abort(nng_aio *aio, nng_err err_code)
{
nni_aio_abort(aio, err_code);
}
diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c
index 9c4145fd..1152729d 100644
--- a/src/supplemental/http/http_public.c
+++ b/src/supplemental/http/http_public.c
@@ -27,7 +27,7 @@ nng_http_get_header(nng_http *conn, const char *key)
#endif
}
-int
+nng_err
nng_http_set_header(nng_http *conn, const char *key, const char *val)
{
#ifdef NNG_SUPP_HTTP
@@ -40,7 +40,7 @@ nng_http_set_header(nng_http *conn, const char *key, const char *val)
#endif
}
-int
+nng_err
nng_http_add_header(nng_http *conn, const char *key, const char *val)
{
#ifdef NNG_SUPP_HTTP
@@ -61,7 +61,6 @@ nng_http_del_header(nng_http *conn, const char *key)
#else
NNI_ARG_UNUSED(conn);
NNI_ARG_UNUSED(key);
- return (NNG_ENOTSUP);
#endif
}
@@ -78,7 +77,7 @@ nng_http_set_body(nng_http *conn, void *data, size_t sz)
#endif
}
-int
+nng_err
nng_http_copy_body(nng_http *conn, const void *data, size_t len)
{
#ifdef NNG_SUPP_HTTP
@@ -114,7 +113,7 @@ nng_http_get_uri(nng_http *conn)
#endif
}
-int
+nng_err
nng_http_set_uri(nng_http *conn, const char *uri, const char *query)
{
#ifdef NNG_SUPP_HTTP
@@ -174,7 +173,7 @@ nng_http_get_reason(nng_http *conn)
#endif
}
-int
+nng_err
nng_http_set_version(nng_http *conn, const char *version)
{
#ifdef NNG_SUPP_HTTP
@@ -295,7 +294,7 @@ nng_http_read_response(nng_http *conn, nng_aio *aio)
#endif
}
-int
+nng_err
nng_http_handler_alloc(
nng_http_handler **hp, const char *uri, nng_http_handler_func cb)
{
@@ -319,7 +318,7 @@ nng_http_handler_free(nng_http_handler *h)
#endif
}
-int
+nng_err
nng_http_handler_alloc_file(
nng_http_handler **hp, const char *uri, const char *path)
{
@@ -333,7 +332,7 @@ nng_http_handler_alloc_file(
#endif
}
-int
+nng_err
nng_http_handler_alloc_directory(
nng_http_handler **hp, const char *uri, const char *path)
{
@@ -347,7 +346,7 @@ nng_http_handler_alloc_directory(
#endif
}
-int
+nng_err
nng_http_handler_alloc_redirect(
nng_http_handler **hp, const char *uri, uint16_t status, const char *where)
{
@@ -362,7 +361,7 @@ nng_http_handler_alloc_redirect(
#endif
}
-int
+nng_err
nng_http_handler_alloc_static(nng_http_handler **hp, const char *uri,
const void *data, size_t size, const char *ctype)
{
@@ -434,7 +433,7 @@ nng_http_handler_set_data(nng_http_handler *h, void *dat, void (*dtor)(void *))
#endif
}
-int
+nng_err
nng_http_server_hold(nng_http_server **srvp, const nng_url *url)
{
#ifdef NNG_SUPP_HTTP
@@ -456,7 +455,7 @@ nng_http_server_release(nng_http_server *srv)
#endif
}
-int
+nng_err
nng_http_server_start(nng_http_server *srv)
{
#ifdef NNG_SUPP_HTTP
@@ -477,7 +476,7 @@ nng_http_server_stop(nng_http_server *srv)
#endif
}
-int
+nng_err
nng_http_server_add_handler(nng_http_server *srv, nng_http_handler *h)
{
#ifdef NNG_SUPP_HTTP
@@ -489,7 +488,7 @@ nng_http_server_add_handler(nng_http_server *srv, nng_http_handler *h)
#endif
}
-int
+nng_err
nng_http_server_del_handler(nng_http_server *srv, nng_http_handler *h)
{
#ifdef NNG_SUPP_HTTP
@@ -501,7 +500,7 @@ nng_http_server_del_handler(nng_http_server *srv, nng_http_handler *h)
#endif
}
-int
+nng_err
nng_http_server_set_error_page(
nng_http_server *srv, uint16_t code, const char *body)
{
@@ -515,7 +514,7 @@ nng_http_server_set_error_page(
#endif
}
-int
+nng_err
nng_http_server_set_tls(nng_http_server *srv, nng_tls_config *cfg)
{
#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS)
@@ -527,7 +526,7 @@ nng_http_server_set_tls(nng_http_server *srv, nng_tls_config *cfg)
#endif
}
-int
+nng_err
nng_http_server_get_tls(nng_http_server *srv, nng_tls_config **cfg)
{
#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS)
@@ -539,7 +538,7 @@ nng_http_server_get_tls(nng_http_server *srv, nng_tls_config **cfg)
#endif
}
-int
+nng_err
nng_http_server_get_addr(nng_http_server *srv, nng_sockaddr *addr)
{
#ifdef NNG_SUPP_HTTP
@@ -555,7 +554,7 @@ nng_http_server_get_addr(nng_http_server *srv, nng_sockaddr *addr)
#endif
}
-int
+nng_err
nng_http_server_error(nng_http_server *srv, nng_http *conn)
{
#ifdef NNG_SUPP_HTTP
@@ -567,7 +566,7 @@ nng_http_server_error(nng_http_server *srv, nng_http *conn)
#endif
}
-int
+nng_err
nng_http_hijack(nng_http *conn)
{
#ifdef NNG_SUPP_HTTP
@@ -578,7 +577,7 @@ nng_http_hijack(nng_http *conn)
#endif
}
-int
+nng_err
nng_http_client_alloc(nng_http_client **clip, const nng_url *url)
{
#ifdef NNG_SUPP_HTTP
@@ -600,7 +599,7 @@ nng_http_client_free(nng_http_client *cli)
#endif
}
-int
+nng_err
nng_http_client_set_tls(nng_http_client *cli, nng_tls_config *cfg)
{
#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS)
@@ -612,7 +611,7 @@ nng_http_client_set_tls(nng_http_client *cli, nng_tls_config *cfg)
#endif
}
-int
+nng_err
nng_http_client_get_tls(nng_http_client *cli, nng_tls_config **cfgp)
{
#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS)
diff --git a/tests/cplusplus_pair.cc b/tests/cplusplus_pair.cc
index 1148aaae..4481a4d7 100644
--- a/tests/cplusplus_pair.cc
+++ b/tests/cplusplus_pair.cc
@@ -7,10 +7,11 @@
// found online at https://opensource.org/licenses/MIT.
//
-#include "nng/nng.h"
-
#include <cstdio>
#include <cstring>
+#include <iostream>
+
+#include <nng/nng.h>
#define SOCKET_ADDRESS "inproc://c++"
@@ -22,58 +23,58 @@ main(int argc, char **argv)
nng_socket s1;
nng_socket s2;
- int rv;
+ nng_err rv;
size_t sz;
char buf[8];
(void) argc;
(void) argv;
nng_init(NULL);
- if ((rv = nng_pair1_open(&s1)) != 0) {
+ if ((rv = (nng_err) nng_pair1_open(&s1)) != 0) {
throw nng_strerror(rv);
}
- if ((rv = nng_pair1_open(&s2)) != 0) {
+ if ((rv = (nng_err) nng_pair1_open(&s2)) != 0) {
throw nng_strerror(rv);
}
- if ((rv = nng_listen(s1, SOCKET_ADDRESS, NULL, 0)) != 0) {
+ if ((rv = (nng_err) nng_listen(s1, SOCKET_ADDRESS, NULL, 0)) != 0) {
throw nng_strerror(rv);
}
- if ((rv = nng_dial(s2, SOCKET_ADDRESS, NULL, 0)) != 0) {
+ if ((rv = (nng_err) nng_dial(s2, SOCKET_ADDRESS, NULL, 0)) != 0) {
throw nng_strerror(rv);
}
- if ((rv = nng_send(s2, (void *) "ABC", 4, 0)) != 0) {
+ if ((rv = (nng_err) nng_send(s2, (void *) "ABC", 4, 0)) != 0) {
throw nng_strerror(rv);
}
sz = sizeof(buf);
- if ((rv = nng_recv(s1, buf, &sz, 0)) != 0) {
+ if ((rv = (nng_err) nng_recv(s1, buf, &sz, 0)) != 0) {
throw nng_strerror(rv);
}
- if ((sz != 4) || (memcmp(buf, "ABC", 4) != 0)) {
+ if ((sz != 4) || (std::strcmp(buf, "ABC") != 0)) {
throw "Contents did not match";
}
- if ((rv = nng_send(s1, (void *) "DEF", 4, 0)) != 0) {
+ if ((rv = (nng_err) nng_send(s1, (void *) "DEF", 4, 0)) != 0) {
throw nng_strerror(rv);
}
sz = sizeof(buf);
- if ((rv = nng_recv(s2, buf, &sz, 0)) != 0) {
+ if ((rv = (nng_err) nng_recv(s2, buf, &sz, 0)) != 0) {
throw nng_strerror(rv);
}
- if ((sz != 4) || (memcmp(buf, "DEF", 4) != 0)) {
+ if ((sz != 4) || (std::strcmp(buf, "DEF") != 0)) {
throw "Contents did not match";
}
- if ((rv = nng_socket_close(s1)) != 0) {
+ if ((rv = (nng_err) nng_socket_close(s1)) != 0) {
throw nng_strerror(rv);
}
- if ((rv = nng_socket_close(s2)) != 0) {
+ if ((rv = (nng_err) nng_socket_close(s2)) != 0) {
throw nng_strerror(rv);
}
- printf("Pass.\n");
+ std::cout << "Pass." << std::endl;
nng_fini();
#else
(void) argc;
(void) argv;
- printf("Skipped (protocol unconfigured).\n");
+ std::cout << "Skipped (protocol unconfigured)." << std::endl;
#endif
return (0);