From bd0081fec87ee32fa57adf5c882a6cad5394eaa0 Mon Sep 17 00:00:00 2001 From: David Steele Date: Wed, 28 Apr 2021 12:37:22 -0400 Subject: [PATCH] Update IoClient/IoSession to use StringIds. Using StringId for the client/session type removes String constants and some awkward referencing/dereferencing needed to use a String constant in the interface. Converting IoSessionRole to StringId removes a conditional in ioSessionToLog() and improves debug logging by outputting client/server instead of 0/1. --- doc/xml/release.xml | 1 + src/common/io/client.c | 4 ++-- src/common/io/client.intern.h | 5 ++--- src/common/io/session.c | 6 +++--- src/common/io/session.h | 6 ++++-- src/common/io/session.intern.h | 5 ++--- src/common/io/socket/client.c | 7 +------ src/common/io/socket/client.h | 3 +-- src/common/io/socket/session.c | 4 ++-- src/common/io/tls/client.c | 7 +------ src/common/io/tls/client.h | 3 +-- src/common/io/tls/session.c | 2 +- 12 files changed, 21 insertions(+), 32 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 940bd9c89..873b5911b 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -97,6 +97,7 @@ + diff --git a/src/common/io/client.c b/src/common/io/client.c index aae8f388b..75d7e5945 100644 --- a/src/common/io/client.c +++ b/src/common/io/client.c @@ -27,7 +27,7 @@ ioClientNew(void *driver, const IoClientInterface *interface) ASSERT(driver != NULL); ASSERT(interface != NULL); - ASSERT(interface->type != NULL); + ASSERT(interface->type != 0); ASSERT(interface->name != NULL); ASSERT(interface->open != NULL); ASSERT(interface->toLog != NULL); @@ -52,5 +52,5 @@ String * ioClientToLog(const IoClient *this) { return strNewFmt( - "{type: %s, driver: %s}", strZ(*this->pub.interface->type), strZ(this->pub.interface->toLog(this->pub.driver))); + "{type: %s, driver: %s}", strZ(strIdToStr(this->pub.interface->type)), strZ(this->pub.interface->toLog(this->pub.driver))); } diff --git a/src/common/io/client.intern.h b/src/common/io/client.intern.h index 6e951f9b0..d72930042 100644 --- a/src/common/io/client.intern.h +++ b/src/common/io/client.intern.h @@ -13,9 +13,8 @@ Interface ***********************************************************************************************************************************/ typedef struct IoClientInterface { - // Type used to identify the client. This is stored as a pointer to a String pointer so it can be used with an existing String - // constant (e.g. created with STRING_EXTERN()) without needing to be copied. - const String *const *type; + // Type used to identify the client + StringId type; // Client name, usually host:port or some other unique indentifier const String *(*name)(void *driver); diff --git a/src/common/io/session.c b/src/common/io/session.c index 4ab5dfdab..40f1cf254 100644 --- a/src/common/io/session.c +++ b/src/common/io/session.c @@ -27,7 +27,7 @@ ioSessionNew(void *driver, const IoSessionInterface *interface) ASSERT(driver != NULL); ASSERT(interface != NULL); - ASSERT(interface->type != NULL); + ASSERT(interface->type != 0); ASSERT(interface->close != NULL); ASSERT(interface->ioRead != NULL); ASSERT(interface->ioWrite != NULL); @@ -67,6 +67,6 @@ String * ioSessionToLog(const IoSession *this) { return strNewFmt( - "{type: %s, role: %s, driver: %s}", strZ(*this->pub.interface->type), - ioSessionRole(this) == ioSessionRoleClient ? "client" : "server", strZ(this->pub.interface->toLog(this->pub.driver))); + "{type: %s, role: %s, driver: %s}", strZ(strIdToStr(this->pub.interface->type)), strZ(strIdToStr(ioSessionRole(this))), + strZ(this->pub.interface->toLog(this->pub.driver))); } diff --git a/src/common/io/session.h b/src/common/io/session.h index 93ef40fe8..bbc68964e 100644 --- a/src/common/io/session.h +++ b/src/common/io/session.h @@ -7,6 +7,8 @@ be closed when work with them is done but they also contain destructors to do cl #ifndef COMMON_IO_SESSION_H #define COMMON_IO_SESSION_H +#include "common/type/stringId.h" + /*********************************************************************************************************************************** Object type ***********************************************************************************************************************************/ @@ -17,8 +19,8 @@ Session roles ***********************************************************************************************************************************/ typedef enum { - ioSessionRoleClient, // Client session - ioSessionRoleServer, // Server session + ioSessionRoleClient = STRID5("client", 0x28e2a5830), // Client session + ioSessionRoleServer = STRID5("server", 0x245b48b30), // Server session } IoSessionRole; #include "common/io/read.h" diff --git a/src/common/io/session.intern.h b/src/common/io/session.intern.h index 11c4d56eb..fb02ffce7 100644 --- a/src/common/io/session.intern.h +++ b/src/common/io/session.intern.h @@ -12,9 +12,8 @@ Interface ***********************************************************************************************************************************/ typedef struct IoSessionInterface { - // Type used to identify the session. This is stored as a pointer to a String pointer so it can be used with an existing String - // constant (e.g. created with STRING_EXTERN()) without needing to be copied. - const String *const *type; + // Type used to identify the session + StringId type; // Close the session void (*close)(void *driver); diff --git a/src/common/io/socket/client.c b/src/common/io/socket/client.c index 7f06b07c2..1d742acfe 100644 --- a/src/common/io/socket/client.c +++ b/src/common/io/socket/client.c @@ -19,11 +19,6 @@ Socket Client #include "common/type/object.h" #include "common/wait.h" -/*********************************************************************************************************************************** -Io client type -***********************************************************************************************************************************/ -STRING_EXTERN(IO_CLIENT_SOCKET_TYPE_STR, IO_CLIENT_SOCKET_TYPE); - /*********************************************************************************************************************************** Statistics constants ***********************************************************************************************************************************/ @@ -176,7 +171,7 @@ sckClientName(THIS_VOID) /**********************************************************************************************************************************/ static const IoClientInterface sckClientInterface = { - .type = &IO_CLIENT_SOCKET_TYPE_STR, + .type = IO_CLIENT_SOCKET_TYPE, .name = sckClientName, .open = sckClientOpen, .toLog = sckClientToLog, diff --git a/src/common/io/socket/client.h b/src/common/io/socket/client.h index f8d4631c0..277290c77 100644 --- a/src/common/io/socket/client.h +++ b/src/common/io/socket/client.h @@ -12,8 +12,7 @@ A simple socket client intended to allow access to services that are exposed via /*********************************************************************************************************************************** Io client type ***********************************************************************************************************************************/ -#define IO_CLIENT_SOCKET_TYPE "socket" - STRING_DECLARE(IO_CLIENT_SOCKET_TYPE_STR); +#define IO_CLIENT_SOCKET_TYPE STRID5("socket", 0x28558df30) /*********************************************************************************************************************************** Statistics constants diff --git a/src/common/io/socket/session.c b/src/common/io/socket/session.c index bc877aebd..f94ba4321 100644 --- a/src/common/io/socket/session.c +++ b/src/common/io/socket/session.c @@ -154,7 +154,7 @@ sckSessionRole(const THIS_VOID) /**********************************************************************************************************************************/ static const IoSessionInterface sckSessionInterface = { - .type = &IO_CLIENT_SOCKET_TYPE_STR, + .type = IO_CLIENT_SOCKET_TYPE, .close = sckSessionClose, .fd = sckSessionFd, .ioRead = sckSessionIoRead, @@ -167,7 +167,7 @@ IoSession * sckSessionNew(IoSessionRole role, int fd, const String *host, unsigned int port, TimeMSec timeout) { FUNCTION_LOG_BEGIN(logLevelDebug) - FUNCTION_LOG_PARAM(ENUM, role); + FUNCTION_LOG_PARAM(STRING_ID, role); FUNCTION_LOG_PARAM(INT, fd); FUNCTION_LOG_PARAM(STRING, host); FUNCTION_LOG_PARAM(UINT, port); diff --git a/src/common/io/tls/client.c b/src/common/io/tls/client.c index d34ad7ecb..c02bb99e7 100644 --- a/src/common/io/tls/client.c +++ b/src/common/io/tls/client.c @@ -20,11 +20,6 @@ TLS Client #include "common/type/object.h" #include "common/wait.h" -/*********************************************************************************************************************************** -Io client type -***********************************************************************************************************************************/ -STRING_EXTERN(IO_CLIENT_TLS_TYPE_STR, IO_CLIENT_TLS_TYPE); - /*********************************************************************************************************************************** Statistics constants ***********************************************************************************************************************************/ @@ -336,7 +331,7 @@ tlsClientName(THIS_VOID) /**********************************************************************************************************************************/ static const IoClientInterface tlsClientInterface = { - .type = &IO_CLIENT_TLS_TYPE_STR, + .type = IO_CLIENT_TLS_TYPE, .name = tlsClientName, .open = tlsClientOpen, .toLog = tlsClientToLog, diff --git a/src/common/io/tls/client.h b/src/common/io/tls/client.h index 92e610f6f..a6e864981 100644 --- a/src/common/io/tls/client.h +++ b/src/common/io/tls/client.h @@ -14,8 +14,7 @@ This object is intended to be used for multiple TLS sessions so ioClientOpen() c /*********************************************************************************************************************************** Io client type ***********************************************************************************************************************************/ -#define IO_CLIENT_TLS_TYPE "tls" - STRING_DECLARE(IO_CLIENT_TLS_TYPE_STR); +#define IO_CLIENT_TLS_TYPE STRID5("tls", 0x4d940) /*********************************************************************************************************************************** Statistics constants diff --git a/src/common/io/tls/session.c b/src/common/io/tls/session.c index b6f68b602..bb4990dac 100644 --- a/src/common/io/tls/session.c +++ b/src/common/io/tls/session.c @@ -341,7 +341,7 @@ tlsSessionRole(const THIS_VOID) /**********************************************************************************************************************************/ static const IoSessionInterface tlsSessionInterface = { - .type = &IO_CLIENT_TLS_TYPE_STR, + .type = IO_CLIENT_TLS_TYPE, .close = tlsSessionClose, .ioRead = tlsSessionIoRead, .ioWrite = tlsSessionIoWrite,