[ntcore] Refactor meta-topic decoding from glass (#4809)

This commit is contained in:
Peter Johnson
2022-12-31 12:01:51 -08:00
committed by GitHub
parent b632b288a3
commit 1562eae74a
7 changed files with 666 additions and 150 deletions

View File

@@ -1702,6 +1702,170 @@ struct NT_String* NT_GetValueStringArray(const struct NT_Value* value,
/** @} */
/** @} */
/**
* @defgroup ntcore_c_meta_api ntcore C meta-topic API
*
* Meta-topic decoders for C.
*
* @{
*/
/**
* Subscriber options. Different from PubSubOptions in this reflects only
* options that are sent over the network.
*/
struct NT_Meta_SubscriberOptions {
double periodic;
NT_Bool topicsOnly;
NT_Bool sendAll;
NT_Bool prefixMatch;
};
/**
* Topic publisher (as published via `$pub$<topic>`).
*/
struct NT_Meta_TopicPublisher {
struct NT_String client;
uint64_t pubuid;
};
/**
* Topic subscriber (as published via `$sub$<topic>`).
*/
struct NT_Meta_TopicSubscriber {
struct NT_String client;
uint64_t subuid;
struct NT_Meta_SubscriberOptions options;
};
/**
* Client publisher (as published via `$clientpub$<client>` or `$serverpub`).
*/
struct NT_Meta_ClientPublisher {
int64_t uid;
struct NT_String topic;
};
/**
* Client subscriber (as published via `$clientsub$<client>` or `$serversub`).
*/
struct NT_Meta_ClientSubscriber {
int64_t uid;
size_t topicsCount;
struct NT_String* topics;
struct NT_Meta_SubscriberOptions options;
};
/**
* Client (as published via `$clients`).
*/
struct NT_Meta_Client {
struct NT_String id;
struct NT_String conn;
uint16_t version;
};
/**
* Decodes `$pub$<topic>` meta-topic data.
*
* @param data data contents
* @param size size of data contents
* @param count number of elements in returned array (output)
* @return Array of TopicPublishers, or NULL on decoding error.
*/
struct NT_Meta_TopicPublisher* NT_Meta_DecodeTopicPublishers(
const uint8_t* data, size_t size, size_t* count);
/**
* Decodes `$sub$<topic>` meta-topic data.
*
* @param data data contents
* @param size size of data contents
* @param count number of elements in returned array (output)
* @return Array of TopicSubscribers, or NULL on decoding error.
*/
struct NT_Meta_TopicSubscriber* NT_Meta_DecodeTopicSubscribers(
const uint8_t* data, size_t size, size_t* count);
/**
* Decodes `$clientpub$<topic>` meta-topic data.
*
* @param data data contents
* @param size size of data contents
* @param count number of elements in returned array (output)
* @return Array of ClientPublishers, or NULL on decoding error.
*/
struct NT_Meta_ClientPublisher* NT_Meta_DecodeClientPublishers(
const uint8_t* data, size_t size, size_t* count);
/**
* Decodes `$clientsub$<topic>` meta-topic data.
*
* @param data data contents
* @param size size of data contents
* @param count number of elements in returned array (output)
* @return Array of ClientSubscribers, or NULL on decoding error.
*/
struct NT_Meta_ClientSubscriber* NT_Meta_DecodeClientSubscribers(
const uint8_t* data, size_t size, size_t* count);
/**
* Decodes `$clients` meta-topic data.
*
* @param data data contents
* @param size size of data contents
* @param count number of elements in returned array (output)
* @return Array of Clients, or NULL on decoding error.
*/
struct NT_Meta_Client* NT_Meta_DecodeClients(const uint8_t* data, size_t size,
size_t* count);
/**
* Frees an array of NT_Meta_TopicPublisher.
*
* @param arr pointer to the array to free
* @param count size of the array to free
*/
void NT_Meta_FreeTopicPublishers(struct NT_Meta_TopicPublisher* arr,
size_t count);
/**
* Frees an array of NT_Meta_TopicSubscriber.
*
* @param arr pointer to the array to free
* @param count size of the array to free
*/
void NT_Meta_FreeTopicSubscribers(struct NT_Meta_TopicSubscriber* arr,
size_t count);
/**
* Frees an array of NT_Meta_ClientPublisher.
*
* @param arr pointer to the array to free
* @param count size of the array to free
*/
void NT_Meta_FreeClientPublishers(struct NT_Meta_ClientPublisher* arr,
size_t count);
/**
* Frees an array of NT_Meta_ClientSubscriber.
*
* @param arr pointer to the array to free
* @param count size of the array to free
*/
void NT_Meta_FreeClientSubscribers(struct NT_Meta_ClientSubscriber* arr,
size_t count);
/**
* Frees an array of NT_Meta_Client.
*
* @param arr pointer to the array to free
* @param count size of the array to free
*/
void NT_Meta_FreeClients(struct NT_Meta_Client* arr, size_t count);
/** @} */
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -1294,4 +1294,119 @@ NT_Listener AddPolledLogger(NT_ListenerPoller poller, unsigned int min_level,
/** @} */
/** @} */
/**
* NetworkTables meta-topic decoding functions.
*/
namespace meta {
/**
* @defgroup ntcore_cpp_meta_api ntcore C++ meta-topic API
*
* Meta-topic decoders for C++.
*
* @{
*/
/**
* Subscriber options. Different from PubSubOptions in this reflects only
* options that are sent over the network.
*/
struct SubscriberOptions {
double periodic = 0.1;
bool topicsOnly = false;
bool sendAll = false;
bool prefixMatch = false;
// std::string otherStr;
};
/**
* Topic publisher (as published via `$pub$<topic>`).
*/
struct TopicPublisher {
std::string client;
uint64_t pubuid = 0;
};
/**
* Topic subscriber (as published via `$sub$<topic>`).
*/
struct TopicSubscriber {
std::string client;
uint64_t subuid = 0;
SubscriberOptions options;
};
/**
* Client publisher (as published via `$clientpub$<client>` or `$serverpub`).
*/
struct ClientPublisher {
int64_t uid = -1;
std::string topic;
};
/**
* Client subscriber (as published via `$clientsub$<client>` or `$serversub`).
*/
struct ClientSubscriber {
int64_t uid = -1;
std::vector<std::string> topics;
SubscriberOptions options;
};
/**
* Client (as published via `$clients`).
*/
struct Client {
std::string id;
std::string conn;
uint16_t version = 0;
};
/**
* Decodes `$pub$<topic>` meta-topic data.
*
* @param data data contents
* @return Vector of TopicPublishers, or empty optional on decoding error.
*/
std::optional<std::vector<TopicPublisher>> DecodeTopicPublishers(
std::span<const uint8_t> data);
/**
* Decodes `$sub$<topic>` meta-topic data.
*
* @param data data contents
* @return Vector of TopicSubscribers, or empty optional on decoding error.
*/
std::optional<std::vector<TopicSubscriber>> DecodeTopicSubscribers(
std::span<const uint8_t> data);
/**
* Decodes `$clientpub$<topic>` meta-topic data.
*
* @param data data contents
* @return Vector of ClientPublishers, or empty optional on decoding error.
*/
std::optional<std::vector<ClientPublisher>> DecodeClientPublishers(
std::span<const uint8_t> data);
/**
* Decodes `$clientsub$<topic>` meta-topic data.
*
* @param data data contents
* @return Vector of ClientSubscribers, or empty optional on decoding error.
*/
std::optional<std::vector<ClientSubscriber>> DecodeClientSubscribers(
std::span<const uint8_t> data);
/**
* Decodes `$clients` meta-topic data.
*
* @param data data contents
* @return Vector of Clients, or empty optional on decoding error.
*/
std::optional<std::vector<Client>> DecodeClients(std::span<const uint8_t> data);
/** @} */
} // namespace meta
} // namespace nt