From: Guus Sliepen Date: Tue, 29 Jul 2014 14:51:11 +0000 (+0200) Subject: Remove errno and errstr from meshlink_handle_t, add thread-local meshlink_errno. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=0754f0dda52c1b6870732be98b7697be5f4ba9cd;p=meshlink Remove errno and errstr from meshlink_handle_t, add thread-local meshlink_errno. Errors can happen outside of the context of a meshlink handle, for example if meshlink_open() itself fails. Furthermore, errstr can always be derived from errno, so it is redundant. --- diff --git a/examples/chat.c b/examples/chat.c index 70d17d7d..1c07e422 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -42,7 +42,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { invitation = meshlink_invite(mesh, arg); if(!invitation) { - fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr); + fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } @@ -55,7 +55,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } if(!meshlink_join(mesh, arg)) - fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr); + fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); else fprintf(stderr, "Invitation accepted!\n"); } else if(!strcasecmp(buf, "kick")) { @@ -164,7 +164,7 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { } if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) { - fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr); + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); return; } @@ -193,7 +193,7 @@ int main(int argc, char *argv[]) { meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message); if(!meshlink_start(mesh)) { - fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr); + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; } diff --git a/examples/chatpp.cc b/examples/chatpp.cc index 864a3e80..73ced3c6 100644 --- a/examples/chatpp.cc +++ b/examples/chatpp.cc @@ -42,7 +42,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { invitation = mesh->invite(arg); if(!invitation) { - fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr); + fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror()); return; } @@ -55,7 +55,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { } if(!mesh->join(arg)) - fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr); + fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror()); else fprintf(stderr, "Invitation accepted!\n"); } else if(!strcasecmp(buf, "kick")) { @@ -81,7 +81,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { fprintf(stderr, "No nodes known!\n"); } else { printf("Known nodes:"); - for(int i = 0; i < n && i < 100; i++) + for(size_t i = 0; i < n && i < 100; i++) printf(" %s", nodes[i]->name); if(n > 100) printf(" (and %zu more)", n - 100); @@ -164,7 +164,7 @@ static void parse_input(meshlink::mesh *mesh, char *buf) { } if(!mesh->send(destination, msg, strlen(msg) + 1)) { - fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr); + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror()); return; } @@ -193,7 +193,7 @@ int main(int argc, char *argv[]) { mesh->set_log_cb(MESHLINK_INFO, log_message); if(!mesh->start()) { - fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr); + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror()); return 1; } diff --git a/src/meshlink++.h b/src/meshlink++.h index c8bdc2f9..a357cca6 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -387,6 +387,10 @@ namespace meshlink { static void close(mesh *mesh) { meshlink_close(mesh); } + + static const char *strerror(errno_t err = meshlink_errno) { + return meshlink_strerror(err); + } }; #endif // MESHLINKPP_H diff --git a/src/meshlink.c b/src/meshlink.c index 23fe1dc4..6d6b77f9 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -44,6 +44,8 @@ typedef struct { #define MSG_NOSIGNAL 0 #endif +__thread meshlink_errno_t meshlink_errno; + //TODO: this can go away completely const var_t variables[] = { /* Server configuration */ diff --git a/src/meshlink.h b/src/meshlink.h index b54e961e..018ba9a3 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -48,11 +48,12 @@ typedef enum { MESHLINK_ENOENT, ///< Node is not known } meshlink_errno_t; +/// A variable holding the last encountered error from MeshLink. +extern __thread meshlink_errno_t meshlink_errno; + #ifndef MESHLINK_INTERNAL_H struct meshlink_handle { - meshlink_errno_t meshlink_errno; ///< Code of the last encountered error. - const char *errstr; ///< Textual representation of most recent error encountered. }; struct meshlink_node {