mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[ntcore] Unify listeners (#4536)
This combines all 4 NT listener APIs (topic, value, connection, and logging) into a single unified listener API.
This commit is contained in:
@@ -1,128 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
class NetworkTableInstance;
|
||||
|
||||
/**
|
||||
* Connection listener. This calls back to a callback function when a connection
|
||||
* change occurs. The callback function is called asynchronously on a separate
|
||||
* thread, so it's important to use synchronization or atomics when accessing
|
||||
* any shared state from the callback function.
|
||||
*/
|
||||
class ConnectionListener final {
|
||||
public:
|
||||
ConnectionListener() = default;
|
||||
|
||||
/**
|
||||
* Create a listener for connection changes.
|
||||
*
|
||||
* @param inst Instance
|
||||
* @param immediateNotify if notification should be immediately created for
|
||||
* existing connections
|
||||
* @param listener Listener function
|
||||
*/
|
||||
ConnectionListener(
|
||||
NetworkTableInstance inst, bool immediateNotify,
|
||||
std::function<void(const ConnectionNotification&)> listener);
|
||||
|
||||
ConnectionListener(const ConnectionListener&) = delete;
|
||||
ConnectionListener& operator=(const ConnectionListener&) = delete;
|
||||
ConnectionListener(ConnectionListener&& rhs);
|
||||
ConnectionListener& operator=(ConnectionListener&& rhs);
|
||||
~ConnectionListener();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_ConnectionListener GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Wait for the connection listener queue to be empty. This is primarily
|
||||
* useful for deterministic testing. This blocks until either the connection
|
||||
* listener queue is empty (e.g. there are no more events that need to be
|
||||
* passed along to callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForQueue(double timeout);
|
||||
|
||||
private:
|
||||
NT_ConnectionListener m_handle{0};
|
||||
};
|
||||
|
||||
/**
|
||||
* A connection listener. This queues connection notifications. Code using
|
||||
* the listener must periodically call readQueue() to read the notifications.
|
||||
*/
|
||||
class ConnectionListenerPoller final {
|
||||
public:
|
||||
ConnectionListenerPoller() = default;
|
||||
|
||||
/**
|
||||
* Construct a connection listener poller.
|
||||
*
|
||||
* @param inst Instance
|
||||
*/
|
||||
explicit ConnectionListenerPoller(NetworkTableInstance inst);
|
||||
|
||||
ConnectionListenerPoller(const ConnectionListenerPoller&) = delete;
|
||||
ConnectionListenerPoller& operator=(const ConnectionListenerPoller&) = delete;
|
||||
ConnectionListenerPoller(ConnectionListenerPoller&& rhs);
|
||||
ConnectionListenerPoller& operator=(ConnectionListenerPoller&& rhs);
|
||||
~ConnectionListenerPoller();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_ConnectionListenerPoller GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Create a connection listener.
|
||||
*
|
||||
* @param immediateNotify if notification should be immediately created for
|
||||
* existing connections
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ConnectionListener Add(bool immediateNotify);
|
||||
|
||||
/**
|
||||
* Remove a connection listener.
|
||||
*
|
||||
* @param listener Listener handle
|
||||
*/
|
||||
void Remove(NT_ConnectionListener listener);
|
||||
|
||||
/**
|
||||
* Read connection notifications.
|
||||
*
|
||||
* @return Connection notifications since the previous call to readQueue()
|
||||
*/
|
||||
std::vector<ConnectionNotification> ReadQueue();
|
||||
|
||||
private:
|
||||
NT_ConnectionListenerPoller m_handle{0};
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
#include "ConnectionListener.inc"
|
||||
@@ -1,83 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "networktables/ConnectionListener.h"
|
||||
#include "networktables/NetworkTableInstance.h"
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
inline ConnectionListener::ConnectionListener(
|
||||
NetworkTableInstance inst, bool immediateNotify,
|
||||
std::function<void(const ConnectionNotification&)> listener)
|
||||
: m_handle{
|
||||
AddConnectionListener(inst.GetHandle(), immediateNotify, listener)} {}
|
||||
|
||||
inline ConnectionListener::ConnectionListener(ConnectionListener&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline ConnectionListener& ConnectionListener::operator=(
|
||||
ConnectionListener&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ConnectionListener::~ConnectionListener() {
|
||||
if (m_handle != 0) {
|
||||
nt::RemoveConnectionListener(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool ConnectionListener::WaitForQueue(double timeout) {
|
||||
if (m_handle != 0) {
|
||||
return nt::WaitForConnectionListenerQueue(m_handle, timeout);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline ConnectionListenerPoller::ConnectionListenerPoller(
|
||||
NetworkTableInstance inst)
|
||||
: m_handle(nt::CreateConnectionListenerPoller(inst.GetHandle())) {}
|
||||
|
||||
inline ConnectionListenerPoller::ConnectionListenerPoller(
|
||||
ConnectionListenerPoller&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline ConnectionListenerPoller& ConnectionListenerPoller::operator=(
|
||||
ConnectionListenerPoller&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ConnectionListenerPoller::~ConnectionListenerPoller() {
|
||||
if (m_handle != 0) {
|
||||
nt::DestroyConnectionListenerPoller(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline NT_ConnectionListener ConnectionListenerPoller::Add(
|
||||
bool immediateNotify) {
|
||||
return nt::AddPolledConnectionListener(m_handle, immediateNotify);
|
||||
}
|
||||
|
||||
inline void ConnectionListenerPoller::Remove(NT_ConnectionListener listener) {
|
||||
nt::RemoveConnectionListener(listener);
|
||||
}
|
||||
|
||||
inline std::vector<ConnectionNotification>
|
||||
ConnectionListenerPoller::ReadQueue() {
|
||||
return nt::ReadConnectionListenerQueue(m_handle);
|
||||
}
|
||||
|
||||
} // namespace nt
|
||||
@@ -357,9 +357,28 @@ class NetworkTableInstance final {
|
||||
|
||||
/**
|
||||
* @{
|
||||
* @name Connection Listener Functions
|
||||
* @name Listener Functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Remove a listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
static void RemoveListener(NT_Listener listener);
|
||||
|
||||
/**
|
||||
* Wait for the listener queue to be empty. This is primarily
|
||||
* useful for deterministic testing. This blocks until either the
|
||||
* listener queue is empty (e.g. there are no more events that need to be
|
||||
* passed along to callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForListenerQueue(double timeout);
|
||||
|
||||
/**
|
||||
* Add a connection listener. The callback function is called asynchronously
|
||||
* on a separate thread, so it's important to use synchronization or atomics
|
||||
@@ -369,200 +388,84 @@ class NetworkTableInstance final {
|
||||
* @param callback listener to add
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ConnectionListener AddConnectionListener(
|
||||
bool immediate_notify,
|
||||
std::function<void(const ConnectionNotification& event)> callback) const;
|
||||
NT_Listener AddConnectionListener(bool immediate_notify,
|
||||
ListenerCallback callback) const;
|
||||
|
||||
/**
|
||||
* Remove a connection listener.
|
||||
*
|
||||
* @param conn_listener Listener handle to remove
|
||||
*/
|
||||
static void RemoveConnectionListener(NT_ConnectionListener conn_listener);
|
||||
|
||||
/**
|
||||
* Wait for the connection listener queue to be empty. This is primarily
|
||||
* useful for deterministic testing. This blocks until either the connection
|
||||
* listener queue is empty (e.g. there are no more events that need to be
|
||||
* passed along to callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForConnectionListenerQueue(double timeout);
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @{
|
||||
* @name Topic Listener Functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add a topic listener for changes on a particular topic. The callback
|
||||
* Add a listener for changes on a particular topic. The callback
|
||||
* function is called asynchronously on a separate thread, so it's important
|
||||
* to use synchronization or atomics when accessing any shared state from the
|
||||
* callback function.
|
||||
*
|
||||
* This creates a corresponding internal subscriber with the lifetime of the
|
||||
* listener.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventMask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
Topic topic, unsigned int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
NT_Listener AddListener(Topic topic, unsigned int eventMask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Add a topic listener for topic changes on a subscriber. The callback
|
||||
* Add a listener for changes on a subscriber. The callback
|
||||
* function is called asynchronously on a separate thread, so it's important
|
||||
* to use synchronization or atomics when accessing any shared state from the
|
||||
* callback function. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventMask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
Subscriber& subscriber, unsigned int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
NT_Listener AddListener(Subscriber& subscriber, unsigned int eventMask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Add a topic listener for topic changes on a subscriber. The callback
|
||||
* Add a listener for changes on a subscriber. The callback
|
||||
* function is called asynchronously on a separate thread, so it's important
|
||||
* to use synchronization or atomics when accessing any shared state from the
|
||||
* callback function. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventMask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
MultiSubscriber& subscriber, int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
NT_Listener AddListener(MultiSubscriber& subscriber, int eventMask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Add a topic listener for topic changes on an entry. The callback function
|
||||
* Add a listener for changes on an entry. The callback function
|
||||
* is called asynchronously on a separate thread, so it's important to use
|
||||
* synchronization or atomics when accessing any shared state from the
|
||||
* callback function.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventMask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
NetworkTableEntry& entry, int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
NT_Listener AddListener(NetworkTableEntry& entry, int eventMask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Add a topic listener for changes to topics with names that start with any
|
||||
* Add a listener for changes to topics with names that start with any
|
||||
* of the given prefixes. The callback function is called asynchronously on a
|
||||
* separate thread, so it's important to use synchronization or atomics when
|
||||
* accessing any shared state from the callback function.
|
||||
*
|
||||
* This creates a corresponding internal subscriber with the lifetime of the
|
||||
* listener.
|
||||
*
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventMask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
std::span<const std::string_view> prefixes, int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Remove a topic listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
static void RemoveTopicListener(NT_TopicListener listener);
|
||||
|
||||
/**
|
||||
* Wait for the topic listener queue to be empty. This is primarily useful for
|
||||
* deterministic testing. This blocks until either the topic listener queue is
|
||||
* empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForTopicListenerQueue(double timeout);
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @{
|
||||
* @name Value Listener Functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add a value listener for value changes on a subscriber. The callback
|
||||
* function is called asynchronously on a separate thread, so it's important
|
||||
* to use synchronization or atomics when accessing any shared state from the
|
||||
* callback function. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of ValueListenerFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener AddValueListener(
|
||||
Subscriber& subscriber, unsigned int eventMask,
|
||||
std::function<void(const ValueNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Add a value listener for value changes on a subscriber. The callback
|
||||
* function is called asynchronously on a separate thread, so it's important
|
||||
* to use synchronization or atomics when accessing any shared state from the
|
||||
* callback function. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of ValueListenerFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener AddValueListener(
|
||||
MultiSubscriber& subscriber, unsigned int eventMask,
|
||||
std::function<void(const ValueNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Add a value listener for value changes on an entry. The callback function
|
||||
* is called asynchronously on a separate thread, so it's important to use
|
||||
* synchronization or atomics when accessing any shared state from the
|
||||
* callback function.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param eventMask Bitmask of ValueListenerFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener AddValueListener(
|
||||
NetworkTableEntry& entry, int eventMask,
|
||||
std::function<void(const ValueNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Remove a value listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
static void RemoveValueListener(NT_ValueListener listener);
|
||||
|
||||
/**
|
||||
* Wait for the value listener queue to be empty. This is primarily useful for
|
||||
* deterministic testing. This blocks until either the value listener queue is
|
||||
* empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForValueListenerQueue(double timeout);
|
||||
NT_Listener AddListener(std::span<const std::string_view> prefixes,
|
||||
int eventMask, ListenerCallback listener);
|
||||
|
||||
/** @} */
|
||||
|
||||
@@ -771,20 +674,13 @@ class NetworkTableInstance final {
|
||||
* log messages with level greater than or equal to minLevel and less than or
|
||||
* equal to maxLevel; messages outside this range will be silently ignored.
|
||||
*
|
||||
* @param func log callback function
|
||||
* @param minLevel minimum log level
|
||||
* @param maxLevel maximum log level
|
||||
* @return Logger handle
|
||||
* @param func callback function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Logger AddLogger(std::function<void(const LogMessage& msg)> func,
|
||||
unsigned int minLevel, unsigned int maxLevel);
|
||||
|
||||
/**
|
||||
* Remove a logger.
|
||||
*
|
||||
* @param logger Logger handle to remove
|
||||
*/
|
||||
static void RemoveLogger(NT_Logger logger);
|
||||
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel,
|
||||
ListenerCallback func);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "networktables/NetworkTableInstance.h"
|
||||
#include "networktables/Topic.h"
|
||||
#include "ntcore_c.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
@@ -82,81 +83,42 @@ inline NetworkTableEntry NetworkTableInstance::GetEntry(std::string_view name) {
|
||||
return NetworkTableEntry{::nt::GetEntry(m_handle, name)};
|
||||
}
|
||||
|
||||
inline NT_ConnectionListener NetworkTableInstance::AddConnectionListener(
|
||||
bool immediate_notify,
|
||||
std::function<void(const ConnectionNotification& event)> callback) const {
|
||||
return ::nt::AddConnectionListener(m_handle, immediate_notify,
|
||||
std::move(callback));
|
||||
inline bool NetworkTableInstance::WaitForListenerQueue(double timeout) {
|
||||
return ::nt::WaitForListenerQueue(m_handle, timeout);
|
||||
}
|
||||
|
||||
inline bool NetworkTableInstance::WaitForConnectionListenerQueue(
|
||||
double timeout) {
|
||||
return ::nt::WaitForConnectionListenerQueue(m_handle, timeout);
|
||||
inline void NetworkTableInstance::RemoveListener(NT_Listener listener) {
|
||||
::nt::RemoveListener(listener);
|
||||
}
|
||||
|
||||
inline void NetworkTableInstance::RemoveConnectionListener(
|
||||
NT_ConnectionListener conn_listener) {
|
||||
::nt::RemoveConnectionListener(conn_listener);
|
||||
inline NT_Listener NetworkTableInstance::AddConnectionListener(
|
||||
bool immediate_notify, ListenerCallback callback) const {
|
||||
return ::nt::AddListener(
|
||||
m_handle,
|
||||
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
inline NT_TopicListener NetworkTableInstance::AddTopicListener(
|
||||
Topic topic, unsigned int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener) {
|
||||
return ::nt::AddTopicListener(topic.GetHandle(), eventMask,
|
||||
std::move(listener));
|
||||
inline NT_Listener NetworkTableInstance::AddListener(
|
||||
Topic topic, unsigned int eventMask, ListenerCallback listener) {
|
||||
return ::nt::AddListener(topic.GetHandle(), eventMask, std::move(listener));
|
||||
}
|
||||
|
||||
inline NT_TopicListener NetworkTableInstance::AddTopicListener(
|
||||
Subscriber& subscriber, unsigned int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener) {
|
||||
return ::nt::AddTopicListener(subscriber.GetHandle(), eventMask,
|
||||
std::move(listener));
|
||||
inline NT_Listener NetworkTableInstance::AddListener(
|
||||
Subscriber& subscriber, unsigned int eventMask, ListenerCallback listener) {
|
||||
return ::nt::AddListener(subscriber.GetHandle(), eventMask,
|
||||
std::move(listener));
|
||||
}
|
||||
|
||||
inline NT_TopicListener NetworkTableInstance::AddTopicListener(
|
||||
NetworkTableEntry& entry, int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener) {
|
||||
return ::nt::AddTopicListener(entry.GetHandle(), eventMask,
|
||||
std::move(listener));
|
||||
inline NT_Listener NetworkTableInstance::AddListener(
|
||||
NetworkTableEntry& entry, int eventMask, ListenerCallback listener) {
|
||||
return ::nt::AddListener(entry.GetHandle(), eventMask, std::move(listener));
|
||||
}
|
||||
|
||||
inline NT_TopicListener NetworkTableInstance::AddTopicListener(
|
||||
inline NT_Listener NetworkTableInstance::AddListener(
|
||||
std::span<const std::string_view> prefixes, int eventMask,
|
||||
std::function<void(const TopicNotification&)> listener) {
|
||||
return ::nt::AddTopicListener(m_handle, prefixes, eventMask,
|
||||
std::move(listener));
|
||||
}
|
||||
|
||||
inline void NetworkTableInstance::RemoveTopicListener(
|
||||
NT_TopicListener listener) {
|
||||
return ::nt::RemoveTopicListener(listener);
|
||||
}
|
||||
|
||||
inline bool NetworkTableInstance::WaitForTopicListenerQueue(double timeout) {
|
||||
return ::nt::WaitForTopicListenerQueue(m_handle, timeout);
|
||||
}
|
||||
|
||||
inline NT_ValueListener NetworkTableInstance::AddValueListener(
|
||||
Subscriber& subscriber, unsigned int eventMask,
|
||||
std::function<void(const ValueNotification&)> listener) {
|
||||
return ::nt::AddValueListener(subscriber.GetHandle(), eventMask,
|
||||
std::move(listener));
|
||||
}
|
||||
|
||||
inline NT_ValueListener NetworkTableInstance::AddValueListener(
|
||||
NetworkTableEntry& entry, int eventMask,
|
||||
std::function<void(const ValueNotification&)> listener) {
|
||||
return ::nt::AddValueListener(entry.GetHandle(), eventMask,
|
||||
std::move(listener));
|
||||
}
|
||||
|
||||
inline void NetworkTableInstance::RemoveValueListener(
|
||||
NT_ValueListener listener) {
|
||||
::nt::RemoveValueListener(listener);
|
||||
}
|
||||
|
||||
inline bool NetworkTableInstance::WaitForValueListenerQueue(double timeout) {
|
||||
return ::nt::WaitForValueListenerQueue(m_handle, timeout);
|
||||
ListenerCallback listener) {
|
||||
return ::nt::AddListener(m_handle, prefixes, eventMask, std::move(listener));
|
||||
}
|
||||
|
||||
inline unsigned int NetworkTableInstance::GetNetworkMode() const {
|
||||
@@ -254,14 +216,10 @@ inline void NetworkTableInstance::StopConnectionDataLog(
|
||||
::nt::StopConnectionDataLog(logger);
|
||||
}
|
||||
|
||||
inline NT_Logger NetworkTableInstance::AddLogger(
|
||||
std::function<void(const LogMessage& msg)> func, unsigned int min_level,
|
||||
unsigned int max_level) {
|
||||
return ::nt::AddLogger(m_handle, func, min_level, max_level);
|
||||
}
|
||||
|
||||
inline void NetworkTableInstance::RemoveLogger(NT_Logger logger) {
|
||||
::nt::RemoveLogger(logger);
|
||||
inline NT_Listener NetworkTableInstance::AddLogger(unsigned int min_level,
|
||||
unsigned int max_level,
|
||||
ListenerCallback func) {
|
||||
return ::nt::AddLogger(m_handle, min_level, max_level, std::move(func));
|
||||
}
|
||||
|
||||
} // namespace nt
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
class MultiSubscriber;
|
||||
class NetworkTableEntry;
|
||||
class NetworkTableInstance;
|
||||
class Subscriber;
|
||||
class Topic;
|
||||
|
||||
/**
|
||||
* Event listener. This calls back to a callback function when an event
|
||||
* matching the specified mask occurs. The callback function is called
|
||||
* asynchronously on a separate thread, so it's important to use synchronization
|
||||
* or atomics when accessing any shared state from the callback function.
|
||||
*/
|
||||
class NetworkTableListener final {
|
||||
public:
|
||||
NetworkTableListener() = default;
|
||||
|
||||
/**
|
||||
* Create a listener for changes to topics with names that start with any of
|
||||
* the given prefixes. This creates a corresponding internal subscriber with
|
||||
* the lifetime of the listener.
|
||||
*
|
||||
* @param inst Instance
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateListener(
|
||||
NetworkTableInstance inst, std::span<const std::string_view> prefixes,
|
||||
unsigned int mask, ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Create a listener for changes on a particular topic. This creates a
|
||||
* corresponding internal subscriber with the lifetime of the listener.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateListener(Topic topic, unsigned int mask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateListener(Subscriber& subscriber,
|
||||
unsigned int mask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateListener(MultiSubscriber& subscriber,
|
||||
unsigned int mask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateListener(NetworkTableEntry& entry,
|
||||
unsigned int mask,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Create a connection listener.
|
||||
*
|
||||
* @param inst instance
|
||||
* @param immediate_notify notify listener of all existing connections
|
||||
* @param listener listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateConnectionListener(
|
||||
NetworkTableInstance inst, bool immediate_notify,
|
||||
ListenerCallback listener);
|
||||
|
||||
/**
|
||||
* Create a listener for log messages. By default, log messages are sent to
|
||||
* stderr; this function sends log messages with the specified levels to the
|
||||
* provided callback function instead. The callback function will only be
|
||||
* called for log messages with level greater than or equal to minLevel and
|
||||
* less than or equal to maxLevel; messages outside this range will be
|
||||
* silently ignored.
|
||||
*
|
||||
* @param inst instance
|
||||
* @param minLevel minimum log level
|
||||
* @param maxLevel maximum log level
|
||||
* @param listener listener function
|
||||
* @return Listener
|
||||
*/
|
||||
static NetworkTableListener CreateLogger(NetworkTableInstance inst,
|
||||
unsigned int minLevel,
|
||||
unsigned int maxLevel,
|
||||
ListenerCallback listener);
|
||||
|
||||
NetworkTableListener(const NetworkTableListener&) = delete;
|
||||
NetworkTableListener& operator=(const NetworkTableListener&) = delete;
|
||||
NetworkTableListener(NetworkTableListener&& rhs);
|
||||
NetworkTableListener& operator=(NetworkTableListener&& rhs);
|
||||
~NetworkTableListener();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_Listener GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Wait for the listener queue to be empty. This is primarily useful for
|
||||
* deterministic testing. This blocks until either the listener queue is
|
||||
* empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForQueue(double timeout);
|
||||
|
||||
private:
|
||||
explicit NetworkTableListener(NT_Listener handle) : m_handle{handle} {}
|
||||
|
||||
NT_Listener m_handle{0};
|
||||
};
|
||||
|
||||
/**
|
||||
* Event polled listener. This queues events matching the specified mask. Code
|
||||
* using the listener must periodically call ReadQueue() to read the
|
||||
* events.
|
||||
*/
|
||||
class NetworkTableListenerPoller final {
|
||||
public:
|
||||
NetworkTableListenerPoller() = default;
|
||||
|
||||
/**
|
||||
* Construct a listener poller.
|
||||
*
|
||||
* @param inst Instance
|
||||
*/
|
||||
explicit NetworkTableListenerPoller(NetworkTableInstance inst);
|
||||
|
||||
NetworkTableListenerPoller(const NetworkTableListenerPoller&) = delete;
|
||||
NetworkTableListenerPoller& operator=(const NetworkTableListenerPoller&) =
|
||||
delete;
|
||||
NetworkTableListenerPoller(NetworkTableListenerPoller&& rhs);
|
||||
NetworkTableListenerPoller& operator=(NetworkTableListenerPoller&& rhs);
|
||||
~NetworkTableListenerPoller();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_ListenerPoller GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Start listening to topic changes for topics with names that start with any
|
||||
* of the given prefixes. This creates a corresponding internal subscriber
|
||||
* with the lifetime of the listener.
|
||||
*
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddListener(std::span<const std::string_view> prefixes,
|
||||
unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to changes to a particular topic. This creates a
|
||||
* corresponding internal subscriber with the lifetime of the listener.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddListener(Topic topic, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddListener(Subscriber& subscriber, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddListener(MultiSubscriber& subscriber, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param mask Bitmask of EventFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddListener(NetworkTableEntry& entry, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Add a connection listener. The callback function is called asynchronously
|
||||
* on a separate thread, so it's important to use synchronization or atomics
|
||||
* when accessing any shared state from the callback function.
|
||||
*
|
||||
* @param immediate_notify notify listener of all existing connections
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddConnectionListener(bool immediate_notify);
|
||||
|
||||
/**
|
||||
* Add logger callback function. By default, log messages are sent to stderr;
|
||||
* this function sends log messages with the specified levels to the provided
|
||||
* callback function instead. The callback function will only be called for
|
||||
* log messages with level greater than or equal to minLevel and less than or
|
||||
* equal to maxLevel; messages outside this range will be silently ignored.
|
||||
*
|
||||
* @param minLevel minimum log level
|
||||
* @param maxLevel maximum log level
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel);
|
||||
|
||||
/**
|
||||
* Remove a listener.
|
||||
*
|
||||
* @param listener Listener handle
|
||||
*/
|
||||
void RemoveListener(NT_Listener listener);
|
||||
|
||||
/**
|
||||
* Read events.
|
||||
*
|
||||
* @return Events since the previous call to ReadQueue()
|
||||
*/
|
||||
std::vector<Event> ReadQueue();
|
||||
|
||||
private:
|
||||
NT_ListenerPoller m_handle{0};
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
#include "NetworkTableListener.inc"
|
||||
@@ -0,0 +1,160 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "networktables/MultiSubscriber.h"
|
||||
#include "networktables/NetworkTableEntry.h"
|
||||
#include "networktables/NetworkTableInstance.h"
|
||||
#include "networktables/NetworkTableListener.h"
|
||||
#include "networktables/Topic.h"
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateListener(
|
||||
NetworkTableInstance inst, std::span<const std::string_view> prefixes,
|
||||
unsigned int mask, ListenerCallback listener) {
|
||||
return NetworkTableListener{
|
||||
::nt::AddListener(inst.GetHandle(), prefixes, mask, std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateListener(
|
||||
Topic topic, unsigned int mask, ListenerCallback listener) {
|
||||
return NetworkTableListener{
|
||||
nt::AddListener(topic.GetHandle(), mask, std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateListener(
|
||||
Subscriber& subscriber, unsigned int mask, ListenerCallback listener) {
|
||||
return NetworkTableListener{
|
||||
::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateListener(
|
||||
MultiSubscriber& subscriber, unsigned int mask, ListenerCallback listener) {
|
||||
return NetworkTableListener{
|
||||
::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateListener(
|
||||
NetworkTableEntry& entry, unsigned int mask, ListenerCallback listener) {
|
||||
return NetworkTableListener{
|
||||
::nt::AddListener(entry.GetHandle(), mask, std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateConnectionListener(
|
||||
NetworkTableInstance inst, bool immediate_notify,
|
||||
ListenerCallback listener) {
|
||||
return NetworkTableListener{::nt::AddListener(
|
||||
inst.GetHandle(),
|
||||
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
|
||||
std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener NetworkTableListener::CreateLogger(
|
||||
NetworkTableInstance inst, unsigned int minLevel, unsigned int maxLevel,
|
||||
ListenerCallback listener) {
|
||||
return NetworkTableListener{::nt::AddLogger(inst.GetHandle(), minLevel,
|
||||
maxLevel, std::move(listener))};
|
||||
}
|
||||
|
||||
inline NetworkTableListener::NetworkTableListener(NetworkTableListener&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline NetworkTableListener& NetworkTableListener::operator=(
|
||||
NetworkTableListener&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline NetworkTableListener::~NetworkTableListener() {
|
||||
if (m_handle != 0) {
|
||||
nt::RemoveListener(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool NetworkTableListener::WaitForQueue(double timeout) {
|
||||
if (m_handle != 0) {
|
||||
return nt::WaitForListenerQueue(m_handle, timeout);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline NetworkTableListenerPoller::NetworkTableListenerPoller(
|
||||
NetworkTableInstance inst)
|
||||
: m_handle(nt::CreateListenerPoller(inst.GetHandle())) {}
|
||||
|
||||
inline NetworkTableListenerPoller::NetworkTableListenerPoller(
|
||||
NetworkTableListenerPoller&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline NetworkTableListenerPoller& NetworkTableListenerPoller::operator=(
|
||||
NetworkTableListenerPoller&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline NetworkTableListenerPoller::~NetworkTableListenerPoller() {
|
||||
if (m_handle != 0) {
|
||||
nt::DestroyListenerPoller(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddListener(
|
||||
std::span<const std::string_view> prefixes, unsigned int mask) {
|
||||
return nt::AddPolledListener(m_handle, prefixes, mask);
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddListener(Topic topic,
|
||||
unsigned int mask) {
|
||||
return ::nt::AddPolledListener(m_handle, topic.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddListener(
|
||||
Subscriber& subscriber, unsigned int mask) {
|
||||
return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddListener(
|
||||
MultiSubscriber& subscriber, unsigned int mask) {
|
||||
return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddListener(
|
||||
NetworkTableEntry& entry, unsigned int mask) {
|
||||
return ::nt::AddPolledListener(m_handle, entry.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddConnectionListener(
|
||||
bool immediate_notify) {
|
||||
return ::nt::AddPolledListener(
|
||||
m_handle, ::nt::GetInstanceFromHandle(m_handle),
|
||||
NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
|
||||
}
|
||||
|
||||
inline NT_Listener NetworkTableListenerPoller::AddLogger(
|
||||
unsigned int minLevel, unsigned int maxLevel) {
|
||||
return ::nt::AddPolledLogger(m_handle, minLevel, maxLevel);
|
||||
}
|
||||
|
||||
inline void NetworkTableListenerPoller::RemoveListener(NT_Listener listener) {
|
||||
::nt::RemoveListener(listener);
|
||||
}
|
||||
|
||||
inline std::vector<Event> NetworkTableListenerPoller::ReadQueue() {
|
||||
return ::nt::ReadListenerQueue(m_handle);
|
||||
}
|
||||
|
||||
} // namespace nt
|
||||
@@ -1,263 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
class MultiSubscriber;
|
||||
class NetworkTableEntry;
|
||||
class NetworkTableInstance;
|
||||
class Subscriber;
|
||||
class Topic;
|
||||
|
||||
/**
|
||||
* Flag values for use with topic listeners.
|
||||
*
|
||||
* The flags are a bitmask and must be OR'ed together to indicate the
|
||||
* combination of events desired to be received.
|
||||
*
|
||||
* The constants kPublish, kUnpublish, and kProperties represent different
|
||||
* events that can occur to entries.
|
||||
*
|
||||
* @ingroup ntcore_cpp_api
|
||||
*/
|
||||
struct TopicListenerFlags {
|
||||
TopicListenerFlags() = delete;
|
||||
|
||||
/**
|
||||
* Initial listener addition.
|
||||
* Set this flag to receive immediate notification of entries matching the
|
||||
* flag criteria (generally only useful when combined with kPublish).
|
||||
*/
|
||||
static constexpr unsigned int kImmediate = NT_TOPIC_NOTIFY_IMMEDIATE;
|
||||
|
||||
/**
|
||||
* Newly published topic.
|
||||
*
|
||||
* Set this flag to receive a notification when a topic is initially
|
||||
* published.
|
||||
*/
|
||||
static constexpr unsigned int kPublish = NT_TOPIC_NOTIFY_PUBLISH;
|
||||
|
||||
/**
|
||||
* Topic has no more publishers.
|
||||
*
|
||||
* Set this flag to receive a notification when a topic has no more
|
||||
* publishers.
|
||||
*/
|
||||
static constexpr unsigned int kUnpublish = NT_TOPIC_NOTIFY_UNPUBLISH;
|
||||
|
||||
/**
|
||||
* Topic's properties changed.
|
||||
*
|
||||
* Set this flag to receive a notification when an topic's properties change.
|
||||
*/
|
||||
static constexpr unsigned int kProperties = NT_TOPIC_NOTIFY_PROPERTIES;
|
||||
};
|
||||
|
||||
/**
|
||||
* Topic change listener. This calls back to a callback function when a topic
|
||||
* change matching the specified mask occurs. The callback function is called
|
||||
* asynchronously on a separate thread, so it's important to use synchronization
|
||||
* or atomics when accessing any shared state from the callback function.
|
||||
*/
|
||||
class TopicListener final {
|
||||
public:
|
||||
TopicListener() = default;
|
||||
|
||||
/**
|
||||
* Create a listener for changes to topics with names that start with any of
|
||||
* the given prefixes.
|
||||
*
|
||||
* @param inst Instance
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
TopicListener(NetworkTableInstance inst,
|
||||
std::span<const std::string_view> prefixes, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Create a listener for changes on a particular topic.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
TopicListener(Topic topic, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
TopicListener(Subscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
TopicListener(MultiSubscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
TopicListener(NetworkTableEntry& entry, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener);
|
||||
|
||||
TopicListener(const TopicListener&) = delete;
|
||||
TopicListener& operator=(const TopicListener&) = delete;
|
||||
TopicListener(TopicListener&& rhs);
|
||||
TopicListener& operator=(TopicListener&& rhs);
|
||||
~TopicListener();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_TopicListener GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Wait for the topic listener queue to be empty. This is primarily useful for
|
||||
* deterministic testing. This blocks until either the topic listener queue is
|
||||
* empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForQueue(double timeout);
|
||||
|
||||
private:
|
||||
NT_TopicListener m_handle{0};
|
||||
};
|
||||
|
||||
/**
|
||||
* Topic change listener. This queues topic change events matching the specified
|
||||
* mask. Code using the listener must periodically call readQueue() to read the
|
||||
* events.
|
||||
*/
|
||||
class TopicListenerPoller final {
|
||||
public:
|
||||
TopicListenerPoller() = default;
|
||||
|
||||
/**
|
||||
* Construct a topic listener poller.
|
||||
*
|
||||
* @param inst Instance
|
||||
*/
|
||||
explicit TopicListenerPoller(NetworkTableInstance inst);
|
||||
|
||||
TopicListenerPoller(const TopicListenerPoller&) = delete;
|
||||
TopicListenerPoller& operator=(const TopicListenerPoller&) = delete;
|
||||
TopicListenerPoller(TopicListenerPoller&& rhs);
|
||||
TopicListenerPoller& operator=(TopicListenerPoller&& rhs);
|
||||
~TopicListenerPoller();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_TopicListenerPoller GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Start listening to changes to a particular topic.
|
||||
*
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener Add(std::span<const std::string_view> prefixes,
|
||||
unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes for topics with names that start with any
|
||||
* of the given prefixes.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener Add(Topic topic, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener Add(Subscriber& subscriber, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener Add(MultiSubscriber& subscriber, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param mask Bitmask of TopicListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener Add(NetworkTableEntry& entry, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Remove a listener.
|
||||
*
|
||||
* @param listener Listener handle
|
||||
*/
|
||||
void Remove(NT_TopicListener listener);
|
||||
|
||||
/**
|
||||
* Read topic notifications.
|
||||
*
|
||||
* @return Topic notifications since the previous call to readQueue()
|
||||
*/
|
||||
std::vector<TopicNotification> ReadQueue();
|
||||
|
||||
private:
|
||||
NT_TopicListenerPoller m_handle{0};
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
#include "TopicListener.inc"
|
||||
@@ -1,123 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "networktables/MultiSubscriber.h"
|
||||
#include "networktables/NetworkTableEntry.h"
|
||||
#include "networktables/NetworkTableInstance.h"
|
||||
#include "networktables/Topic.h"
|
||||
#include "networktables/TopicListener.h"
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
inline TopicListener::TopicListener(
|
||||
NetworkTableInstance inst, std::span<const std::string_view> prefixes,
|
||||
unsigned int mask, std::function<void(const TopicNotification&)> listener)
|
||||
: m_handle{AddTopicListener(inst.GetHandle(), prefixes, mask, listener)} {}
|
||||
|
||||
inline TopicListener::TopicListener(
|
||||
Topic topic, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener)
|
||||
: m_handle{AddTopicListener(topic.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline TopicListener::TopicListener(
|
||||
Subscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener)
|
||||
: m_handle{AddTopicListener(subscriber.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline TopicListener::TopicListener(
|
||||
MultiSubscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener)
|
||||
: m_handle{AddTopicListener(subscriber.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline TopicListener::TopicListener(
|
||||
NetworkTableEntry& entry, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> listener)
|
||||
: m_handle{AddTopicListener(entry.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline TopicListener::TopicListener(TopicListener&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline TopicListener& TopicListener::operator=(TopicListener&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TopicListener::~TopicListener() {
|
||||
if (m_handle != 0) {
|
||||
nt::RemoveTopicListener(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool TopicListener::WaitForQueue(double timeout) {
|
||||
if (m_handle != 0) {
|
||||
return nt::WaitForTopicListenerQueue(m_handle, timeout);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline TopicListenerPoller::TopicListenerPoller(NetworkTableInstance inst)
|
||||
: m_handle(nt::CreateTopicListenerPoller(inst.GetHandle())) {}
|
||||
|
||||
inline TopicListenerPoller::TopicListenerPoller(TopicListenerPoller&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline TopicListenerPoller& TopicListenerPoller::operator=(
|
||||
TopicListenerPoller&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TopicListenerPoller::~TopicListenerPoller() {
|
||||
if (m_handle != 0) {
|
||||
nt::DestroyTopicListenerPoller(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline NT_TopicListener TopicListenerPoller::Add(
|
||||
std::span<const std::string_view> prefixes, unsigned int mask) {
|
||||
return nt::AddPolledTopicListener(m_handle, prefixes, mask);
|
||||
}
|
||||
|
||||
inline NT_TopicListener TopicListenerPoller::Add(Topic topic,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledTopicListener(m_handle, topic.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_TopicListener TopicListenerPoller::Add(Subscriber& subscriber,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledTopicListener(m_handle, subscriber.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_TopicListener TopicListenerPoller::Add(MultiSubscriber& subscriber,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledTopicListener(m_handle, subscriber.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_TopicListener TopicListenerPoller::Add(NetworkTableEntry& entry,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledTopicListener(m_handle, entry.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline void TopicListenerPoller::Remove(NT_TopicListener listener) {
|
||||
nt::RemoveTopicListener(listener);
|
||||
}
|
||||
|
||||
inline std::vector<TopicNotification> TopicListenerPoller::ReadQueue() {
|
||||
return nt::ReadTopicListenerQueue(m_handle);
|
||||
}
|
||||
|
||||
} // namespace nt
|
||||
@@ -1,203 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
class MultiSubscriber;
|
||||
class NetworkTableEntry;
|
||||
class NetworkTableInstance;
|
||||
class Subscriber;
|
||||
|
||||
/**
|
||||
* Flag values for use with value listeners.
|
||||
*
|
||||
* The flags are a bitmask and must be OR'ed together to indicate the
|
||||
* combination of events desired to be received.
|
||||
*
|
||||
* By default, notifications are only generated for remote changes occurring
|
||||
* after the listener is created. The constants kImmediate and kLocal are
|
||||
* modifiers that cause notifications to be generated at other times.
|
||||
*/
|
||||
struct ValueListenerFlags {
|
||||
ValueListenerFlags() = delete;
|
||||
|
||||
/**
|
||||
* Initial listener addition.
|
||||
*
|
||||
* Set this flag to receive immediate notification of the current value.
|
||||
*/
|
||||
static constexpr unsigned int kImmediate = NT_VALUE_NOTIFY_IMMEDIATE;
|
||||
|
||||
/**
|
||||
* Changed locally.
|
||||
*
|
||||
* Set this flag to receive notification of both local changes and changes
|
||||
* coming from remote nodes. By default, notifications are only generated for
|
||||
* remote changes.
|
||||
*/
|
||||
static constexpr unsigned int kLocal = NT_VALUE_NOTIFY_LOCAL;
|
||||
};
|
||||
|
||||
/**
|
||||
* Value change listener. This calls back to a callback function when a value
|
||||
* change matching the specified mask occurs. The callback function is called
|
||||
* asynchronously on a separate thread, so it's important to use synchronization
|
||||
* or atomics when accessing any shared state from the callback function.
|
||||
*/
|
||||
class ValueListener final {
|
||||
public:
|
||||
ValueListener() = default;
|
||||
|
||||
/**
|
||||
* Create a listener for value changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of ValueListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
ValueListener(Subscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Create a listener for value changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of ValueListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
ValueListener(MultiSubscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> listener);
|
||||
|
||||
/**
|
||||
* Create a listener for value changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param mask Bitmask of ValueListenerFlags values
|
||||
* @param listener Listener function
|
||||
*/
|
||||
ValueListener(NetworkTableEntry& entry, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> listener);
|
||||
|
||||
ValueListener(const ValueListener&) = delete;
|
||||
ValueListener& operator=(const ValueListener&) = delete;
|
||||
ValueListener(ValueListener&& rhs);
|
||||
ValueListener& operator=(ValueListener&& rhs);
|
||||
~ValueListener();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_ValueListener GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Wait for the value listener queue to be empty. This is primarily useful for
|
||||
* deterministic testing. This blocks until either the value listener queue is
|
||||
* empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
|
||||
* a negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForQueue(double timeout);
|
||||
|
||||
private:
|
||||
NT_ValueListener m_handle{0};
|
||||
};
|
||||
|
||||
/**
|
||||
* Value change listener. This queues value change events matching the specified
|
||||
* mask. Code using the listener must periodically call readQueue() to read the
|
||||
* events.
|
||||
*/
|
||||
class ValueListenerPoller final {
|
||||
public:
|
||||
ValueListenerPoller() = default;
|
||||
|
||||
/**
|
||||
* Construct a value listener poller.
|
||||
*
|
||||
* @param inst Instance
|
||||
*/
|
||||
explicit ValueListenerPoller(NetworkTableInstance inst);
|
||||
|
||||
ValueListenerPoller(const ValueListenerPoller&) = delete;
|
||||
ValueListenerPoller& operator=(const ValueListenerPoller&) = delete;
|
||||
ValueListenerPoller(ValueListenerPoller&& rhs);
|
||||
ValueListenerPoller& operator=(ValueListenerPoller&& rhs);
|
||||
~ValueListenerPoller();
|
||||
|
||||
explicit operator bool() const { return m_handle != 0; }
|
||||
|
||||
/**
|
||||
* Gets the native handle.
|
||||
*
|
||||
* @return Handle
|
||||
*/
|
||||
NT_ValueListenerPoller GetHandle() const { return m_handle; }
|
||||
|
||||
/**
|
||||
* Start listening to value changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of ValueListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener Add(Subscriber& subscriber, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to value changes on a subscriber. This does NOT keep the
|
||||
* subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param mask Bitmask of ValueListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener Add(MultiSubscriber& subscriber, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Start listening to value changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param mask Bitmask of ValueListenerFlags values
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener Add(NetworkTableEntry& entry, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Remove a listener.
|
||||
*
|
||||
* @param listener Listener handle
|
||||
*/
|
||||
void Remove(NT_ValueListener listener);
|
||||
|
||||
/**
|
||||
* Reads value listener queue (all value changes since last call).
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @return Array of value notifications.
|
||||
*/
|
||||
std::vector<ValueNotification> ReadQueue();
|
||||
|
||||
private:
|
||||
NT_ValueListenerPoller m_handle{0};
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
#include "ValueListener.inc"
|
||||
@@ -1,101 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "networktables/MultiSubscriber.h"
|
||||
#include "networktables/NetworkTableEntry.h"
|
||||
#include "networktables/NetworkTableInstance.h"
|
||||
#include "networktables/Topic.h"
|
||||
#include "networktables/ValueListener.h"
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
inline ValueListener::ValueListener(
|
||||
Subscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> listener)
|
||||
: m_handle{AddValueListener(subscriber.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline ValueListener::ValueListener(
|
||||
MultiSubscriber& subscriber, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> listener)
|
||||
: m_handle{AddValueListener(subscriber.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline ValueListener::ValueListener(
|
||||
NetworkTableEntry& entry, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> listener)
|
||||
: m_handle{AddValueListener(entry.GetHandle(), mask, listener)} {}
|
||||
|
||||
inline ValueListener::ValueListener(ValueListener&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline ValueListener& ValueListener::operator=(ValueListener&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ValueListener::~ValueListener() {
|
||||
if (m_handle != 0) {
|
||||
nt::RemoveValueListener(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool ValueListener::WaitForQueue(double timeout) {
|
||||
if (m_handle != 0) {
|
||||
return nt::WaitForValueListenerQueue(m_handle, timeout);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline ValueListenerPoller::ValueListenerPoller(NetworkTableInstance inst)
|
||||
: m_handle(nt::CreateValueListenerPoller(inst.GetHandle())) {}
|
||||
|
||||
inline ValueListenerPoller::ValueListenerPoller(ValueListenerPoller&& rhs)
|
||||
: m_handle(rhs.m_handle) {
|
||||
rhs.m_handle = 0;
|
||||
}
|
||||
|
||||
inline ValueListenerPoller& ValueListenerPoller::operator=(
|
||||
ValueListenerPoller&& rhs) {
|
||||
std::swap(m_handle, rhs.m_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ValueListenerPoller::~ValueListenerPoller() {
|
||||
if (m_handle != 0) {
|
||||
nt::DestroyValueListenerPoller(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
inline NT_ValueListener ValueListenerPoller::Add(Subscriber& subscriber,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledValueListener(m_handle, subscriber.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_ValueListener ValueListenerPoller::Add(MultiSubscriber& subscriber,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledValueListener(m_handle, subscriber.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline NT_ValueListener ValueListenerPoller::Add(NetworkTableEntry& entry,
|
||||
unsigned int mask) {
|
||||
return nt::AddPolledValueListener(m_handle, entry.GetHandle(), mask);
|
||||
}
|
||||
|
||||
inline void ValueListenerPoller::Remove(NT_ValueListener listener) {
|
||||
nt::RemoveValueListener(listener);
|
||||
}
|
||||
|
||||
inline std::vector<ValueNotification> ValueListenerPoller::ReadQueue() {
|
||||
return nt::ReadValueListenerQueue(m_handle);
|
||||
}
|
||||
|
||||
} // namespace nt
|
||||
@@ -29,21 +29,15 @@ typedef int NT_Bool;
|
||||
|
||||
typedef unsigned int NT_Handle;
|
||||
typedef NT_Handle NT_ConnectionDataLogger;
|
||||
typedef NT_Handle NT_ConnectionListener;
|
||||
typedef NT_Handle NT_ConnectionListenerPoller;
|
||||
typedef NT_Handle NT_DataLogger;
|
||||
typedef NT_Handle NT_Entry;
|
||||
typedef NT_Handle NT_Inst;
|
||||
typedef NT_Handle NT_Logger;
|
||||
typedef NT_Handle NT_LoggerPoller;
|
||||
typedef NT_Handle NT_Listener;
|
||||
typedef NT_Handle NT_ListenerPoller;
|
||||
typedef NT_Handle NT_MultiSubscriber;
|
||||
typedef NT_Handle NT_Topic;
|
||||
typedef NT_Handle NT_TopicListener;
|
||||
typedef NT_Handle NT_TopicListenerPoller;
|
||||
typedef NT_Handle NT_Subscriber;
|
||||
typedef NT_Handle NT_Publisher;
|
||||
typedef NT_Handle NT_ValueListener;
|
||||
typedef NT_Handle NT_ValueListenerPoller;
|
||||
|
||||
/** Default network tables port number (NT3) */
|
||||
#define NT_DEFAULT_PORT3 1735
|
||||
@@ -103,20 +97,33 @@ enum NT_PubSubOptionType {
|
||||
NT_PUBSUB_KEEPDUPLICATES, /* preserve duplicate values */
|
||||
};
|
||||
|
||||
/** Topic notification flags. */
|
||||
enum NT_TopicNotifyKind {
|
||||
NT_TOPIC_NOTIFY_NONE = 0,
|
||||
NT_TOPIC_NOTIFY_IMMEDIATE = 0x01, /* initial listener addition */
|
||||
NT_TOPIC_NOTIFY_PUBLISH = 0x02, /* initially published */
|
||||
NT_TOPIC_NOTIFY_UNPUBLISH = 0x04, /* no more publishers */
|
||||
NT_TOPIC_NOTIFY_PROPERTIES = 0x08, /* properties changed */
|
||||
};
|
||||
|
||||
/** Value notification flags. */
|
||||
enum NT_ValueNotifyKind {
|
||||
NT_VALUE_NOTIFY_NONE = 0,
|
||||
NT_VALUE_NOTIFY_IMMEDIATE = 0x01, /* initial listener addition */
|
||||
NT_VALUE_NOTIFY_LOCAL = 0x02, /* changed locally */
|
||||
/** Event notification flags. */
|
||||
enum NT_EventFlags {
|
||||
NT_EVENT_NONE = 0,
|
||||
/** Initial listener addition. */
|
||||
NT_EVENT_IMMEDIATE = 0x01,
|
||||
/** Client connected (on server, any client connected). */
|
||||
NT_EVENT_CONNECTED = 0x02,
|
||||
/** Client disconnected (on server, any client disconnected). */
|
||||
NT_EVENT_DISCONNECTED = 0x04,
|
||||
/** Any connection event (connect or disconnect). */
|
||||
NT_EVENT_CONNECTION = NT_EVENT_CONNECTED | NT_EVENT_DISCONNECTED,
|
||||
/** New topic published. */
|
||||
NT_EVENT_PUBLISH = 0x08,
|
||||
/** Topic unpublished. */
|
||||
NT_EVENT_UNPUBLISH = 0x10,
|
||||
/** Topic properties changed. */
|
||||
NT_EVENT_PROPERTIES = 0x20,
|
||||
/** Any topic event (publish, unpublish, or properties changed). */
|
||||
NT_EVENT_TOPIC = NT_EVENT_PUBLISH | NT_EVENT_UNPUBLISH | NT_EVENT_PROPERTIES,
|
||||
/** Topic value updated (via network). */
|
||||
NT_EVENT_VALUE_REMOTE = 0x40,
|
||||
/** Topic value updated (local). */
|
||||
NT_EVENT_VALUE_LOCAL = 0x80,
|
||||
/** Topic value updated (network or local). */
|
||||
NT_EVENT_VALUE_ALL = NT_EVENT_VALUE_REMOTE | NT_EVENT_VALUE_LOCAL,
|
||||
/** Log message. */
|
||||
NT_EVENT_LOGMESSAGE = 0x100,
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -178,6 +185,7 @@ struct NT_Value {
|
||||
} data;
|
||||
};
|
||||
|
||||
/** NetworkTables Topic Information */
|
||||
struct NT_TopicInfo {
|
||||
/** Topic handle */
|
||||
NT_Topic topic;
|
||||
@@ -221,23 +229,8 @@ struct NT_ConnectionInfo {
|
||||
unsigned int protocol_version;
|
||||
};
|
||||
|
||||
/** NetworkTables Topic Notification */
|
||||
struct NT_TopicNotification {
|
||||
/** Listener that was triggered. */
|
||||
NT_TopicListener listener;
|
||||
|
||||
/** Topic info. */
|
||||
struct NT_TopicInfo info;
|
||||
|
||||
/** Update flags. */
|
||||
unsigned int flags;
|
||||
};
|
||||
|
||||
/** NetworkTables Value Notification */
|
||||
struct NT_ValueNotification {
|
||||
/** Listener that was triggered. */
|
||||
NT_ValueListener listener;
|
||||
|
||||
/** NetworkTables value event data. */
|
||||
struct NT_ValueEventData {
|
||||
/** Topic handle. */
|
||||
NT_Topic topic;
|
||||
|
||||
@@ -246,31 +239,10 @@ struct NT_ValueNotification {
|
||||
|
||||
/** The new value. */
|
||||
struct NT_Value value;
|
||||
|
||||
/**
|
||||
* Update flags. For example, NT_NOTIFY_NEW if the key did not previously
|
||||
* exist.
|
||||
*/
|
||||
unsigned int flags;
|
||||
};
|
||||
|
||||
/** NetworkTables Connection Notification */
|
||||
struct NT_ConnectionNotification {
|
||||
/** Listener that was triggered. */
|
||||
NT_ConnectionListener listener;
|
||||
|
||||
/** True if event is due to connection being established. */
|
||||
NT_Bool connected;
|
||||
|
||||
/** Connection info. */
|
||||
struct NT_ConnectionInfo conn;
|
||||
};
|
||||
|
||||
/** NetworkTables log message. */
|
||||
struct NT_LogMessage {
|
||||
/** The logger that generated the message. */
|
||||
NT_Logger logger;
|
||||
|
||||
/** Log level of the message. See NT_LogLevel. */
|
||||
unsigned int level;
|
||||
|
||||
@@ -284,6 +256,30 @@ struct NT_LogMessage {
|
||||
char* message;
|
||||
};
|
||||
|
||||
/** NetworkTables event */
|
||||
struct NT_Event {
|
||||
/** Listener that triggered this event. */
|
||||
NT_Handle listener;
|
||||
|
||||
/**
|
||||
* Event flags (NT_EventFlags). Also indicates the data included with the
|
||||
* event:
|
||||
* - NT_EVENT_CONNECTED or NT_EVENT_DISCONNECTED: connInfo
|
||||
* - NT_EVENT_PUBLISH, NT_EVENT_UNPUBLISH, or NT_EVENT_PROPERTIES: topicInfo
|
||||
* - NT_EVENT_VALUE_REMOTE, NT_NOTIFY_VALUE_LOCAL: valueData
|
||||
* - NT_EVENT_LOGMESSAGE: logMessage
|
||||
*/
|
||||
unsigned int flags;
|
||||
|
||||
/** Event data; content depends on flags. */
|
||||
union {
|
||||
struct NT_ConnectionInfo connInfo;
|
||||
struct NT_TopicInfo topicInfo;
|
||||
struct NT_ValueEventData valueData;
|
||||
struct NT_LogMessage logMessage;
|
||||
} data;
|
||||
};
|
||||
|
||||
/** NetworkTables publish/subscribe option. */
|
||||
struct NT_PubSubOption {
|
||||
/** Option type. */
|
||||
@@ -804,346 +800,184 @@ void NT_UnsubscribeMultiple(NT_MultiSubscriber sub);
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup ntcore_topiclistener_cfunc Topic Listener Functions
|
||||
* @defgroup ntcore_listener_cfunc Listener Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Topic listener callback function.
|
||||
* Event listener callback function.
|
||||
*
|
||||
* @param data data pointer provided to callback creation function
|
||||
* @param event event info
|
||||
*/
|
||||
typedef void (*NT_TopicListenerCallback)(
|
||||
void* data, const struct NT_TopicNotification* event);
|
||||
typedef void (*NT_ListenerCallback)(void* data, const struct NT_Event* event);
|
||||
|
||||
/**
|
||||
* Creates a listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using NT_AddPolledXListener()) will be stored in the queue and
|
||||
* must be collected by calling NT_ReadListenerQueue().
|
||||
* The returned handle must be destroyed with NT_DestroyListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_ListenerPoller NT_CreateListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroys a listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void NT_DestroyListenerPoller(NT_ListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Read notifications.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param len length of returned array (output)
|
||||
* @return Array of events. Returns NULL and len=0 if no events since last
|
||||
* call.
|
||||
*/
|
||||
struct NT_Event* NT_ReadListenerQueue(NT_ListenerPoller poller, size_t* len);
|
||||
|
||||
/**
|
||||
* Removes a listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
void NT_RemoveListener(NT_Listener listener);
|
||||
|
||||
/**
|
||||
* Wait for the listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
NT_Bool NT_WaitForListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Create a listener for changes to topics with names that start with
|
||||
* the given prefix.
|
||||
* the given prefix. This creates a corresponding internal subscriber with the
|
||||
* lifetime of the listener.
|
||||
*
|
||||
* @param inst Instance handle
|
||||
* @param prefix Topic name string prefix
|
||||
* @param prefix_len Length of topic name string prefix
|
||||
* @param mask Bitmask of NT_TopicListenerFlags values
|
||||
* @param mask Bitmask of NT_EventFlags values (only topic and value events will
|
||||
* be generated)
|
||||
* @param data Data passed to callback function
|
||||
* @param callback Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener NT_AddTopicListener(NT_Inst inst, const char* prefix,
|
||||
size_t prefix_len, unsigned int mask,
|
||||
void* data,
|
||||
NT_TopicListenerCallback callback);
|
||||
NT_Listener NT_AddListenerSingle(NT_Inst inst, const char* prefix,
|
||||
size_t prefix_len, unsigned int mask,
|
||||
void* data, NT_ListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Create a listener for changes to topics with names that start with any of
|
||||
* the given prefixes.
|
||||
* the given prefixes. This creates a corresponding internal subscriber with the
|
||||
* lifetime of the listener.
|
||||
*
|
||||
* @param inst Instance handle
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param prefixes_len Number of elements in prefixes array
|
||||
* @param mask Bitmask of NT_TopicListenerFlags values
|
||||
* @param mask Bitmask of NT_EventFlags values (only topic and value events will
|
||||
* be generated)
|
||||
* @param data Data passed to callback function
|
||||
* @param callback Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener NT_AddTopicListenerMultiple(NT_Inst inst,
|
||||
const struct NT_String* prefixes,
|
||||
size_t prefixes_len,
|
||||
unsigned int mask, void* data,
|
||||
NT_TopicListenerCallback callback);
|
||||
NT_Listener NT_AddListenerMultiple(NT_Inst inst,
|
||||
const struct NT_String* prefixes,
|
||||
size_t prefixes_len, unsigned int mask,
|
||||
void* data, NT_ListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Create a listener for changes on a particular topic.
|
||||
* Create a listener.
|
||||
*
|
||||
* @param topic Topic handle
|
||||
* @param mask Bitmask of NT_TopicListenerFlags values
|
||||
* Some combinations of handle and mask have no effect:
|
||||
* - connection and log message events are only generated on instances
|
||||
* - topic and value events are only generated on non-instances
|
||||
*
|
||||
* Adding value and topic events on a topic will create a corresponding internal
|
||||
* subscriber with the lifetime of the listener.
|
||||
*
|
||||
* Adding a log message listener through this function will only result in
|
||||
* events at NT_LOG_INFO or higher; for more customized settings, use
|
||||
* NT_AddLogger().
|
||||
*
|
||||
* @param handle Handle
|
||||
* @param mask Bitmask of NT_EventFlags values
|
||||
* @param data Data passed to callback function
|
||||
* @param callback Listener function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener NT_AddTopicListenerSingle(NT_Topic topic, unsigned int mask,
|
||||
void* data,
|
||||
NT_TopicListenerCallback callback);
|
||||
NT_Listener NT_AddListener(NT_Handle handle, unsigned int mask, void* data,
|
||||
NT_ListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Wait for the topic listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the topic listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
NT_Bool NT_WaitForTopicListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Creates a topic listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using NT_AddPolledTopicListener()) will be stored in the queue and
|
||||
* must be collected by calling NT_ReadTopicListenerQueue().
|
||||
* The returned handle must be destroyed with NT_DestroyTopicListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_TopicListenerPoller NT_CreateTopicListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroys a topic listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void NT_DestroyTopicListenerPoller(NT_TopicListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Read topic notifications.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param len length of returned array (output)
|
||||
* @return Array of topic notifications. Returns NULL and len=0 if no
|
||||
* notifications since last call.
|
||||
*/
|
||||
struct NT_TopicNotification* NT_ReadTopicListenerQueue(
|
||||
NT_TopicListenerPoller poller, size_t* len);
|
||||
|
||||
/**
|
||||
* Creates a polled topic listener.
|
||||
* The caller is responsible for calling NT_ReadTopicListenerQueue() to poll.
|
||||
* Creates a polled topic listener. This creates a corresponding internal
|
||||
* subscriber with the lifetime of the listener.
|
||||
* The caller is responsible for calling NT_ReadListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param prefix UTF-8 string prefix
|
||||
* @param prefix_len Length of UTF-8 string prefix
|
||||
* @param mask NT_NotifyKind bitmask
|
||||
* @param mask NT_EventFlags bitmask (only topic and value events
|
||||
* will be generated)
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener NT_AddPolledTopicListener(NT_TopicListenerPoller poller,
|
||||
const char* prefix,
|
||||
size_t prefix_len,
|
||||
unsigned int mask);
|
||||
NT_Listener NT_AddPolledListenerSingle(NT_ListenerPoller poller,
|
||||
const char* prefix, size_t prefix_len,
|
||||
unsigned int mask);
|
||||
|
||||
/**
|
||||
* Creates a polled topic listener.
|
||||
* The caller is responsible for calling NT_ReadTopicListenerQueue() to poll.
|
||||
* Creates a polled topic listener. This creates a corresponding internal
|
||||
* subscriber with the lifetime of the listener.
|
||||
* The caller is responsible for calling NT_ReadListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param prefixes array of UTF-8 string prefixes
|
||||
* @param prefixes_len Length of prefixes array
|
||||
* @param mask NT_EventFlags bitmask (only topic and value events
|
||||
* will be generated)
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Listener NT_AddPolledListenerMultiple(NT_ListenerPoller poller,
|
||||
const struct NT_String* prefixes,
|
||||
size_t prefixes_len,
|
||||
unsigned int mask);
|
||||
|
||||
/**
|
||||
* Creates a polled listener.
|
||||
* The caller is responsible for calling NT_ReadListenerQueue() to poll.
|
||||
*
|
||||
* Some combinations of handle and mask have no effect:
|
||||
* - connection and log message events are only generated on instances
|
||||
* - topic and value events are only generated on non-instances
|
||||
*
|
||||
* Adding value and topic events on a topic will create a corresponding internal
|
||||
* subscriber with the lifetime of the listener.
|
||||
*
|
||||
* Adding a log message listener through this function will only result in
|
||||
* events at NT_LOG_INFO or higher; for more customized settings, use
|
||||
* NT_AddPolledLogger().
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param handle handle
|
||||
* @param mask NT_NotifyKind bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener NT_AddPolledTopicListenerMultiple(
|
||||
NT_TopicListenerPoller poller, const struct NT_String* prefixes,
|
||||
size_t prefixes_len, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Creates a polled topic listener.
|
||||
* The caller is responsible for calling NT_ReadTopicListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param topic topic
|
||||
* @param mask NT_NotifyKind bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener NT_AddPolledTopicListenerSingle(NT_TopicListenerPoller poller,
|
||||
NT_Topic topic,
|
||||
unsigned int mask);
|
||||
|
||||
/**
|
||||
* Removes a topic listener.
|
||||
*
|
||||
* @param topic_listener Listener handle to remove
|
||||
*/
|
||||
void NT_RemoveTopicListener(NT_TopicListener topic_listener);
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup ntcore_valuelistener_cfunc Value Listener Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Value listener callback function.
|
||||
*
|
||||
* @param data data pointer provided to callback creation function
|
||||
* @param event event info
|
||||
*/
|
||||
typedef void (*NT_ValueListenerCallback)(
|
||||
void* data, const struct NT_ValueNotification* event);
|
||||
|
||||
/**
|
||||
* Create a listener for value changes on a subscriber.
|
||||
*
|
||||
* @param subentry Subscriber/entry
|
||||
* @param mask Bitmask of NT_ValueListenerFlags values
|
||||
* @param data Data passed to listener function
|
||||
* @param callback Listener function
|
||||
*/
|
||||
NT_ValueListener NT_AddValueListener(NT_Handle subentry, unsigned int mask,
|
||||
void* data,
|
||||
NT_ValueListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Wait for the value listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the value listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
NT_Bool NT_WaitForValueListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Create a value listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using NT_AddPolledValueListener()) will be stored in the queue and
|
||||
* must be collected by calling NT_ReadValueListenerQueue().
|
||||
* The returned handle must be destroyed with NT_DestroyValueListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_ValueListenerPoller NT_CreateValueListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroy a value listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void NT_DestroyValueListenerPoller(NT_ValueListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Reads value listener queue (all value changes since last call).
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param len length of returned array (output)
|
||||
* @return Array of value notifications. Returns NULL and len=0 if no
|
||||
* notifications since last call.
|
||||
*/
|
||||
struct NT_ValueNotification* NT_ReadValueListenerQueue(
|
||||
NT_ValueListenerPoller poller, size_t* len);
|
||||
|
||||
/**
|
||||
* Create a polled value listener.
|
||||
* The caller is responsible for calling NT_ReadValueListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param subentry subscriber or entry handle
|
||||
* @param flags NT_NotifyKind bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener NT_AddPolledValueListener(NT_ValueListenerPoller poller,
|
||||
NT_Handle subentry,
|
||||
unsigned int flags);
|
||||
|
||||
/**
|
||||
* Remove a value listener.
|
||||
*
|
||||
* @param value_listener Listener handle to remove
|
||||
*/
|
||||
void NT_RemoveValueListener(NT_ValueListener value_listener);
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup ntcore_connectionlistener_cfunc Connection Listener Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Connection listener callback function.
|
||||
* Called when a network connection is made or lost.
|
||||
*
|
||||
* @param data data pointer provided to callback creation function
|
||||
* @param event event info
|
||||
*/
|
||||
typedef void (*NT_ConnectionListenerCallback)(
|
||||
void* data, const struct NT_ConnectionNotification* event);
|
||||
|
||||
/**
|
||||
* Add a connection listener.
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @param data data pointer to pass to callback
|
||||
* @param callback listener to add
|
||||
* @param immediate_notify notify listener of all existing connections
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ConnectionListener NT_AddConnectionListener(
|
||||
NT_Inst inst, NT_Bool immediate_notify, void* data,
|
||||
NT_ConnectionListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Wait for the connection listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the connection listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
NT_Bool NT_WaitForConnectionListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Create a connection listener poller.
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using NT_AddPolledConnectionListener()) will be stored in the queue
|
||||
* and must be collected by calling NT_PollConnectionListener().
|
||||
* The returned handle must be destroyed with
|
||||
* NT_DestroyConnectionListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_ConnectionListenerPoller NT_CreateConnectionListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroy a connection listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void NT_DestroyConnectionListenerPoller(NT_ConnectionListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Create a polled connection listener.
|
||||
* The caller is responsible for calling NT_PollConnectionListener() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param immediate_notify notify listener of all existing connections
|
||||
*/
|
||||
NT_ConnectionListener NT_AddPolledConnectionListener(
|
||||
NT_ConnectionListenerPoller poller, NT_Bool immediate_notify);
|
||||
|
||||
/**
|
||||
* Get the next connection event. This blocks until the next connect or
|
||||
* disconnect occurs. This is intended to be used with
|
||||
* NT_AddPolledConnectionListener(); connection listeners created using
|
||||
* NT_AddConnectionListener() will not be serviced through this function.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param len length of returned array (output)
|
||||
* @return Array of information on the connection events. Only returns NULL
|
||||
* if an error occurred (e.g. the instance was invalid or is shutting
|
||||
* down).
|
||||
*/
|
||||
struct NT_ConnectionNotification* NT_ReadConnectionListenerQueue(
|
||||
NT_ConnectionListenerPoller poller, size_t* len);
|
||||
|
||||
/**
|
||||
* Remove a connection listener.
|
||||
*
|
||||
* @param conn_listener Listener handle to remove
|
||||
*/
|
||||
void NT_RemoveConnectionListener(NT_ConnectionListener conn_listener);
|
||||
NT_Listener NT_AddPolledListener(NT_ListenerPoller poller, NT_Handle handle,
|
||||
unsigned int mask);
|
||||
|
||||
/** @} */
|
||||
|
||||
@@ -1390,67 +1224,19 @@ void NT_DisposeTopicInfoArray(struct NT_TopicInfo* arr, size_t count);
|
||||
void NT_DisposeTopicInfo(struct NT_TopicInfo* info);
|
||||
|
||||
/**
|
||||
* Disposes an topic notification array.
|
||||
* Disposes an event array.
|
||||
*
|
||||
* @param arr pointer to the array to dispose
|
||||
* @param count number of elements in the array
|
||||
*/
|
||||
void NT_DisposeTopicNotificationArray(struct NT_TopicNotification* arr,
|
||||
size_t count);
|
||||
void NT_DisposeEventArray(struct NT_Event* arr, size_t count);
|
||||
|
||||
/**
|
||||
* Disposes a single topic notification.
|
||||
* Disposes a single event.
|
||||
*
|
||||
* @param info pointer to the info to dispose
|
||||
* @param event pointer to the event to dispose
|
||||
*/
|
||||
void NT_DisposeTopicNotification(struct NT_TopicNotification* info);
|
||||
|
||||
/**
|
||||
* Disposes an value notification array.
|
||||
*
|
||||
* @param arr pointer to the array to dispose
|
||||
* @param count number of elements in the array
|
||||
*/
|
||||
void NT_DisposeValueNotificationArray(struct NT_ValueNotification* arr,
|
||||
size_t count);
|
||||
|
||||
/**
|
||||
* Disposes a single value notification.
|
||||
*
|
||||
* @param info pointer to the info to dispose
|
||||
*/
|
||||
void NT_DisposeValueNotification(struct NT_ValueNotification* info);
|
||||
|
||||
/**
|
||||
* Disposes a connection notification array.
|
||||
*
|
||||
* @param arr pointer to the array to dispose
|
||||
* @param count number of elements in the array
|
||||
*/
|
||||
void NT_DisposeConnectionNotificationArray(
|
||||
struct NT_ConnectionNotification* arr, size_t count);
|
||||
|
||||
/**
|
||||
* Disposes a single connection notification.
|
||||
*
|
||||
* @param info pointer to the info to dispose
|
||||
*/
|
||||
void NT_DisposeConnectionNotification(struct NT_ConnectionNotification* info);
|
||||
|
||||
/**
|
||||
* Disposes a log message array.
|
||||
*
|
||||
* @param arr pointer to the array to dispose
|
||||
* @param count number of elements in the array
|
||||
*/
|
||||
void NT_DisposeLogMessageArray(struct NT_LogMessage* arr, size_t count);
|
||||
|
||||
/**
|
||||
* Disposes a single log message.
|
||||
*
|
||||
* @param info pointer to the info to dispose
|
||||
*/
|
||||
void NT_DisposeLogMessage(struct NT_LogMessage* info);
|
||||
void NT_DisposeEvent(struct NT_Event* event);
|
||||
|
||||
/**
|
||||
* Returns monotonic current time in 1 us increments.
|
||||
@@ -1481,14 +1267,6 @@ void NT_SetNow(uint64_t timestamp);
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Log function.
|
||||
*
|
||||
* @param data data pointer passed to NT_AddLogger()
|
||||
* @param msg message information
|
||||
*/
|
||||
typedef void (*NT_LogFunc)(void* data, const struct NT_LogMessage* msg);
|
||||
|
||||
/**
|
||||
* Add logger callback function. By default, log messages are sent to stderr;
|
||||
* this function sends log messages to the provided callback function instead.
|
||||
@@ -1497,61 +1275,28 @@ typedef void (*NT_LogFunc)(void* data, const struct NT_LogMessage* msg);
|
||||
* messages outside this range will be silently ignored.
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @param data data pointer to pass to func
|
||||
* @param func log callback function
|
||||
* @param min_level minimum log level
|
||||
* @param max_level maximum log level
|
||||
* @return Logger handle
|
||||
* @param data data pointer to pass to func
|
||||
* @param func listener callback function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Logger NT_AddLogger(NT_Inst inst, void* data, NT_LogFunc func,
|
||||
unsigned int min_level, unsigned int max_level);
|
||||
NT_Listener NT_AddLogger(NT_Inst inst, unsigned int min_level,
|
||||
unsigned int max_level, void* data,
|
||||
NT_ListenerCallback func);
|
||||
|
||||
/**
|
||||
* Create a log poller. A poller provides a single queue of poll events.
|
||||
* The returned handle must be destroyed with NT_DestroyLoggerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_LoggerPoller NT_CreateLoggerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroy a log poller. This will abort any blocked polling call and prevent
|
||||
* additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void NT_DestroyLoggerPoller(NT_LoggerPoller poller);
|
||||
|
||||
/**
|
||||
* Set the log level for a log poller. Events will only be generated for
|
||||
* Set the log level for a listener poller. Events will only be generated for
|
||||
* log messages with level greater than or equal to min_level and less than or
|
||||
* equal to max_level; messages outside this range will be silently ignored.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param min_level minimum log level
|
||||
* @param max_level maximum log level
|
||||
* @return Logger handle
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Logger NT_AddPolledLogger(NT_LoggerPoller poller, unsigned int min_level,
|
||||
unsigned int max_level);
|
||||
|
||||
/**
|
||||
* Get the next log event. This blocks until the next log occurs.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param len length of returned array (output)
|
||||
* @return Array of information on the log events. Only returns NULL if an
|
||||
* error occurred (e.g. the instance was invalid or is shutting down).
|
||||
*/
|
||||
struct NT_LogMessage* NT_ReadLoggerQueue(NT_LoggerPoller poller, size_t* len);
|
||||
|
||||
/**
|
||||
* Remove a logger.
|
||||
*
|
||||
* @param logger Logger handle to remove
|
||||
*/
|
||||
void NT_RemoveLogger(NT_Logger logger);
|
||||
NT_Listener NT_AddPolledLogger(NT_ListenerPoller poller, unsigned int min_level,
|
||||
unsigned int max_level);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "networktables/NetworkTableValue.h"
|
||||
@@ -40,6 +41,47 @@ namespace nt {
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Event notification flags.
|
||||
*
|
||||
* The flags are a bitmask and must be OR'ed together to indicate the
|
||||
* combination of events desired to be received.
|
||||
*
|
||||
*/
|
||||
struct EventFlags {
|
||||
EventFlags() = delete;
|
||||
|
||||
static constexpr unsigned int kNone = NT_EVENT_NONE;
|
||||
/**
|
||||
* Initial listener addition.
|
||||
* Set this flag to receive immediate notification of matches to the
|
||||
* flag criteria.
|
||||
*/
|
||||
static constexpr unsigned int kImmediate = NT_EVENT_IMMEDIATE;
|
||||
/** Client connected (on server, any client connected). */
|
||||
static constexpr unsigned int kConnected = NT_EVENT_CONNECTED;
|
||||
/** Client disconnected (on server, any client disconnected). */
|
||||
static constexpr unsigned int kDisconnected = NT_EVENT_DISCONNECTED;
|
||||
/** Any connection event (connect or disconnect). */
|
||||
static constexpr unsigned int kConnection = kConnected | kDisconnected;
|
||||
/** New topic published. */
|
||||
static constexpr unsigned int kPublish = NT_EVENT_PUBLISH;
|
||||
/** Topic unpublished. */
|
||||
static constexpr unsigned int kUnpublish = NT_EVENT_UNPUBLISH;
|
||||
/** Topic properties changed. */
|
||||
static constexpr unsigned int kProperties = NT_EVENT_PROPERTIES;
|
||||
/** Any topic event (publish, unpublish, or properties changed). */
|
||||
static constexpr unsigned int kTopic = kPublish | kUnpublish | kProperties;
|
||||
/** Topic value updated (via network). */
|
||||
static constexpr unsigned int kValueRemote = NT_EVENT_VALUE_REMOTE;
|
||||
/** Topic value updated (local). */
|
||||
static constexpr unsigned int kValueLocal = NT_EVENT_VALUE_LOCAL;
|
||||
/** Topic value updated (network or local). */
|
||||
static constexpr unsigned int kValueAll = kValueRemote | kValueLocal;
|
||||
/** Log message. */
|
||||
static constexpr unsigned int kLogMessage = NT_EVENT_LOGMESSAGE;
|
||||
};
|
||||
|
||||
/** NetworkTables Topic Information */
|
||||
struct TopicInfo {
|
||||
/** Topic handle */
|
||||
@@ -106,47 +148,12 @@ struct ConnectionInfo {
|
||||
}
|
||||
};
|
||||
|
||||
/** NetworkTables Topic Notification */
|
||||
class TopicNotification {
|
||||
/** NetworkTables Value Event Data */
|
||||
class ValueEventData {
|
||||
public:
|
||||
TopicNotification() = default;
|
||||
TopicNotification(NT_TopicListener listener_, TopicInfo info_,
|
||||
unsigned int flags_)
|
||||
: listener(listener_), info(std::move(info_)), flags(flags_) {}
|
||||
|
||||
/** Listener that was triggered. */
|
||||
NT_TopicListener listener{0};
|
||||
|
||||
/** Topic info. */
|
||||
TopicInfo info;
|
||||
|
||||
/**
|
||||
* Notification flags.
|
||||
*/
|
||||
unsigned int flags{0};
|
||||
|
||||
friend void swap(TopicNotification& first, TopicNotification& second) {
|
||||
using std::swap;
|
||||
swap(first.listener, second.listener);
|
||||
swap(first.info, second.info);
|
||||
swap(first.flags, second.flags);
|
||||
}
|
||||
};
|
||||
|
||||
/** NetworkTables Value Notification */
|
||||
class ValueNotification {
|
||||
public:
|
||||
ValueNotification() = default;
|
||||
ValueNotification(NT_ValueListener listener_, NT_Topic topic_,
|
||||
NT_Handle subentry_, Value value_, unsigned int flags_)
|
||||
: listener(listener_),
|
||||
topic(topic_),
|
||||
subentry(subentry_),
|
||||
value(std::move(value_)),
|
||||
flags(flags_) {}
|
||||
|
||||
/** Listener that was triggered. */
|
||||
NT_ValueListener listener{0};
|
||||
ValueEventData() = default;
|
||||
ValueEventData(NT_Topic topic, NT_Handle subentry, Value value)
|
||||
: topic{topic}, subentry{subentry}, value{std::move(value)} {}
|
||||
|
||||
/** Topic handle. */
|
||||
NT_Topic topic{0};
|
||||
@@ -156,63 +163,15 @@ class ValueNotification {
|
||||
|
||||
/** The new value. */
|
||||
Value value;
|
||||
|
||||
/**
|
||||
* Update flags. For example, NT_NOTIFY_NEW if the key did not previously
|
||||
* exist.
|
||||
*/
|
||||
unsigned int flags{0};
|
||||
|
||||
friend void swap(ValueNotification& first, ValueNotification& second) {
|
||||
using std::swap;
|
||||
swap(first.listener, second.listener);
|
||||
swap(first.topic, second.topic);
|
||||
swap(first.subentry, second.subentry);
|
||||
swap(first.value, second.value);
|
||||
swap(first.flags, second.flags);
|
||||
}
|
||||
};
|
||||
|
||||
/** NetworkTables Connection Notification */
|
||||
class ConnectionNotification {
|
||||
public:
|
||||
ConnectionNotification() = default;
|
||||
ConnectionNotification(NT_ConnectionListener listener_, bool connected_,
|
||||
ConnectionInfo conn_)
|
||||
: listener(listener_), connected(connected_), conn(std::move(conn_)) {}
|
||||
|
||||
/** Listener that was triggered. */
|
||||
NT_ConnectionListener listener{0};
|
||||
|
||||
/** True if event is due to connection being established. */
|
||||
bool connected = false;
|
||||
|
||||
/** Connection info. */
|
||||
ConnectionInfo conn;
|
||||
|
||||
friend void swap(ConnectionNotification& first,
|
||||
ConnectionNotification& second) {
|
||||
using std::swap;
|
||||
swap(first.listener, second.listener);
|
||||
swap(first.connected, second.connected);
|
||||
swap(first.conn, second.conn);
|
||||
}
|
||||
};
|
||||
|
||||
/** NetworkTables log message. */
|
||||
class LogMessage {
|
||||
public:
|
||||
LogMessage() = default;
|
||||
LogMessage(NT_Logger logger_, unsigned int level_, std::string_view filename_,
|
||||
unsigned int line_, std::string_view message_)
|
||||
: logger(logger_),
|
||||
level(level_),
|
||||
filename(filename_),
|
||||
line(line_),
|
||||
message(message_) {}
|
||||
|
||||
/** The logger that generated the message. */
|
||||
NT_Logger logger{0};
|
||||
LogMessage(unsigned int level, std::string_view filename, unsigned int line,
|
||||
std::string_view message)
|
||||
: level{level}, filename{filename}, line{line}, message{message} {}
|
||||
|
||||
/** Log level of the message. See NT_LogLevel. */
|
||||
unsigned int level{0};
|
||||
@@ -225,15 +184,71 @@ class LogMessage {
|
||||
|
||||
/** The message. */
|
||||
std::string message;
|
||||
};
|
||||
|
||||
friend void swap(LogMessage& first, LogMessage& second) {
|
||||
using std::swap;
|
||||
swap(first.logger, second.logger);
|
||||
swap(first.level, second.level);
|
||||
swap(first.filename, second.filename);
|
||||
swap(first.line, second.line);
|
||||
swap(first.message, second.message);
|
||||
/** NetworkTables event */
|
||||
class Event {
|
||||
public:
|
||||
Event() = default;
|
||||
Event(NT_Listener listener, unsigned int flags, ConnectionInfo info)
|
||||
: listener{listener}, flags{flags}, data{std::move(info)} {}
|
||||
Event(NT_Listener listener, unsigned int flags, TopicInfo info)
|
||||
: listener{listener}, flags{flags}, data{std::move(info)} {}
|
||||
Event(NT_Listener listener, unsigned int flags, ValueEventData data)
|
||||
: listener{listener}, flags{flags}, data{std::move(data)} {}
|
||||
Event(NT_Listener listener, unsigned int flags, LogMessage msg)
|
||||
: listener{listener}, flags{flags}, data{std::move(msg)} {}
|
||||
Event(NT_Listener listener, unsigned int flags, NT_Topic topic,
|
||||
NT_Handle subentry, Value value)
|
||||
: listener{listener},
|
||||
flags{flags},
|
||||
data{ValueEventData{topic, subentry, std::move(value)}} {}
|
||||
Event(NT_Listener listener, unsigned int flags, unsigned int level,
|
||||
std::string_view filename, unsigned int line, std::string_view message)
|
||||
: listener{listener},
|
||||
flags{flags},
|
||||
data{LogMessage{level, filename, line, message}} {}
|
||||
|
||||
/** Listener that triggered this event. */
|
||||
NT_Listener listener{0};
|
||||
|
||||
/**
|
||||
* Event flags (NT_EventFlags). Also indicates the data included with the
|
||||
* event:
|
||||
* - NT_NOTIFY_CONNECTED or NT_NOTIFY_DISCONNECTED: GetConnectionInfo()
|
||||
* - NT_NOTIFY_PUBLISH, NT_NOTIFY_UNPUBLISH, or NT_NOTIFY_PROPERTIES:
|
||||
* GetTopicInfo()
|
||||
* - NT_NOTIFY_VALUE, NT_NOTIFY_VALUE_LOCAL: GetValueData()
|
||||
* - NT_NOTIFY_LOGMESSAGE: GetLogMessage()
|
||||
*/
|
||||
unsigned int flags{0};
|
||||
|
||||
/** Event data; content depends on flags. */
|
||||
std::variant<ConnectionInfo, TopicInfo, ValueEventData, LogMessage> data;
|
||||
|
||||
const ConnectionInfo* GetConnectionInfo() const {
|
||||
return std::get_if<nt::ConnectionInfo>(&data);
|
||||
}
|
||||
ConnectionInfo* GetConnectionInfo() {
|
||||
return std::get_if<nt::ConnectionInfo>(&data);
|
||||
}
|
||||
|
||||
const TopicInfo* GetTopicInfo() const {
|
||||
return std::get_if<nt::TopicInfo>(&data);
|
||||
}
|
||||
TopicInfo* GetTopicInfo() { return std::get_if<nt::TopicInfo>(&data); }
|
||||
|
||||
const ValueEventData* GetValueEventData() const {
|
||||
return std::get_if<nt::ValueEventData>(&data);
|
||||
}
|
||||
ValueEventData* GetValueEventData() {
|
||||
return std::get_if<nt::ValueEventData>(&data);
|
||||
}
|
||||
|
||||
const LogMessage* GetLogMessage() const {
|
||||
return std::get_if<nt::LogMessage>(&data);
|
||||
}
|
||||
LogMessage* GetLogMessage() { return std::get_if<nt::LogMessage>(&data); }
|
||||
};
|
||||
|
||||
/** NetworkTables publish/subscribe option. */
|
||||
@@ -771,272 +786,134 @@ void UnsubscribeMultiple(NT_MultiSubscriber sub);
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup ntcore_topiclistener_func Topic Listener Functions
|
||||
* @defgroup ntcore_listener_func Listener Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
using ListenerCallback = std::function<void(const Event&)>;
|
||||
|
||||
/**
|
||||
* Creates a listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using AddPolledListener()) will be stored in the queue and
|
||||
* must be collected by calling ReadListenerQueue().
|
||||
* The returned handle must be destroyed with DestroyListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_ListenerPoller CreateListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroys a listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void DestroyListenerPoller(NT_ListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Read notifications.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @return Array of events. Returns empty array if no events since last call.
|
||||
*/
|
||||
std::vector<Event> ReadListenerQueue(NT_ListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Removes a listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
void RemoveListener(NT_Listener listener);
|
||||
|
||||
/**
|
||||
* Wait for the listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Create a listener for changes to topics with names that start with any of
|
||||
* the given prefixes.
|
||||
* the given prefixes. This creates a corresponding internal subscriber with the
|
||||
* lifetime of the listener.
|
||||
*
|
||||
* @param inst Instance handle
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param mask Bitmask of NT_TopicListenerFlags values
|
||||
* @param mask Bitmask of NT_EventFlags values (only topic and value events will
|
||||
* be generated)
|
||||
* @param callback Listener function
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
NT_Inst inst, std::span<const std::string_view> prefixes, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> callback);
|
||||
NT_Listener AddListener(NT_Inst inst,
|
||||
std::span<const std::string_view> prefixes,
|
||||
unsigned int mask, ListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Create a listener for changes on a particular topic.
|
||||
* Create a listener.
|
||||
*
|
||||
* @param handle Topic, subscriber, multi-subscriber, or entry handle
|
||||
* @param mask Bitmask of NT_TopicListenerFlags values
|
||||
* Some combinations of handle and mask have no effect:
|
||||
* - connection and log message events are only generated on instances
|
||||
* - topic and value events are only generated on non-instances
|
||||
*
|
||||
* Adding value and topic events on a topic will create a corresponding internal
|
||||
* subscriber with the lifetime of the listener.
|
||||
*
|
||||
* Adding a log message listener through this function will only result in
|
||||
* events at NT_LOG_INFO or higher; for more customized settings, use
|
||||
* AddLogger().
|
||||
*
|
||||
* @param handle Instance, topic, subscriber, multi-subscriber, or entry handle
|
||||
* @param mask Bitmask of NT_EventFlags values
|
||||
* @param callback Listener function
|
||||
*/
|
||||
NT_TopicListener AddTopicListener(
|
||||
NT_Handle handle, unsigned int mask,
|
||||
std::function<void(const TopicNotification&)> callback);
|
||||
NT_Listener AddListener(NT_Handle handle, unsigned int mask,
|
||||
ListenerCallback callback);
|
||||
|
||||
/**
|
||||
* Wait for the topic listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the topic listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
* Creates a polled listener. This creates a corresponding internal subscriber
|
||||
* with the lifetime of the listener.
|
||||
* The caller is responsible for calling ReadListenerQueue() to poll.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForTopicListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Creates a topic listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using AddPolledTopicListener()) will be stored in the queue and
|
||||
* must be collected by calling ReadTopicListenerQueue().
|
||||
* The returned handle must be destroyed with DestroyTopicListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_TopicListenerPoller CreateTopicListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroys a topic listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void DestroyTopicListenerPoller(NT_TopicListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Read topic notifications.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @return Array of topic notifications. Returns empty array if no
|
||||
* notifications since last call.
|
||||
*/
|
||||
std::vector<TopicNotification> ReadTopicListenerQueue(
|
||||
NT_TopicListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Creates a polled topic listener.
|
||||
* The caller is responsible for calling ReadTopicListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param prefixes array of UTF-8 string prefixes
|
||||
* @param mask NT_NotifyKind bitmask
|
||||
* @param poller poller handle
|
||||
* @param prefixes array of UTF-8 string prefixes
|
||||
* @param mask Bitmask of NT_EventFlags values (only topic and value events will
|
||||
* be generated)
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddPolledTopicListener(
|
||||
NT_TopicListenerPoller poller, std::span<const std::string_view> prefixes,
|
||||
unsigned int mask);
|
||||
NT_Listener AddPolledListener(NT_ListenerPoller poller,
|
||||
std::span<const std::string_view> prefixes,
|
||||
unsigned int mask);
|
||||
|
||||
/**
|
||||
* Creates a polled topic listener.
|
||||
* The caller is responsible for calling ReadTopicListenerQueue() to poll.
|
||||
* Creates a polled listener.
|
||||
* The caller is responsible for calling ReadListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param handle topic, subscriber, multi-subscriber, or entry handle
|
||||
* @param mask NT_NotifyKind bitmask
|
||||
* Some combinations of handle and mask have no effect:
|
||||
* - connection and log message events are only generated on instances
|
||||
* - topic and value events are only generated on non-instances
|
||||
*
|
||||
* Adding value and topic events on a topic will create a corresponding internal
|
||||
* subscriber with the lifetime of the listener.
|
||||
*
|
||||
* Adding a log message listener through this function will only result in
|
||||
* events at NT_LOG_INFO or higher; for more customized settings, use
|
||||
* AddPolledLogger().
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param handle instance, topic, subscriber, multi-subscriber, or entry handle
|
||||
* @param mask NT_EventFlags bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_TopicListener AddPolledTopicListener(NT_TopicListenerPoller poller,
|
||||
NT_Handle handle, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Removes a topic listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
void RemoveTopicListener(NT_TopicListener listener);
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup ntcore_valuelistener_func Value Listener Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a listener for value changes on a subscriber.
|
||||
*
|
||||
* @param subentry Subscriber/entry
|
||||
* @param mask Bitmask of NT_ValueListenerFlags values
|
||||
* @param callback Listener function
|
||||
*/
|
||||
NT_ValueListener AddValueListener(
|
||||
NT_Handle subentry, unsigned int mask,
|
||||
std::function<void(const ValueNotification&)> callback);
|
||||
|
||||
/**
|
||||
* Wait for the value listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the value listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForValueListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Create a value listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using AddPolledValueListener()) will be stored in the queue and
|
||||
* must be collected by calling ReadValueListenerQueue().
|
||||
* The returned handle must be destroyed with DestroyValueListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_ValueListenerPoller CreateValueListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroy a value listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void DestroyValueListenerPoller(NT_ValueListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Reads value listener queue (all value changes since last call).
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @return Array of value notifications.
|
||||
*/
|
||||
std::vector<ValueNotification> ReadValueListenerQueue(
|
||||
NT_ValueListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Create a polled value listener.
|
||||
* The caller is responsible for calling ReadValueListenerQueue() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param subentry subscriber or entry handle
|
||||
* @param mask NT_NotifyKind bitmask
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ValueListener AddPolledValueListener(NT_ValueListenerPoller poller,
|
||||
NT_Handle subentry, unsigned int mask);
|
||||
|
||||
/**
|
||||
* Remove a value listener.
|
||||
*
|
||||
* @param listener Listener handle to remove
|
||||
*/
|
||||
void RemoveValueListener(NT_ValueListener listener);
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup ntcore_connectionlistener_func Connection Listener Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add a connection listener.
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @param callback listener to add
|
||||
* @param immediate_notify notify listener of all existing connections
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_ConnectionListener AddConnectionListener(
|
||||
NT_Inst inst, bool immediate_notify,
|
||||
std::function<void(const ConnectionNotification& event)> callback);
|
||||
|
||||
/**
|
||||
* Wait for the connection listener queue to be empty. This is primarily useful
|
||||
* for deterministic testing. This blocks until either the connection listener
|
||||
* queue is empty (e.g. there are no more events that need to be passed along to
|
||||
* callbacks or poll queues) or the timeout expires.
|
||||
*
|
||||
* @param handle handle
|
||||
* @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
|
||||
* negative value to block indefinitely
|
||||
* @return False if timed out, otherwise true.
|
||||
*/
|
||||
bool WaitForConnectionListenerQueue(NT_Handle handle, double timeout);
|
||||
|
||||
/**
|
||||
* Create a connection listener poller.
|
||||
*
|
||||
* A poller provides a single queue of poll events. Events linked to this
|
||||
* poller (using AddPolledConnectionListener()) will be stored in the queue and
|
||||
* must be collected by calling PollConnectionListener().
|
||||
* The returned handle must be destroyed with DestroyConnectionListenerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_ConnectionListenerPoller CreateConnectionListenerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroy a connection listener poller. This will abort any blocked polling
|
||||
* call and prevent additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void DestroyConnectionListenerPoller(NT_ConnectionListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Create a polled connection listener.
|
||||
* The caller is responsible for calling PollConnectionListener() to poll.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @param immediate_notify notify listener of all existing connections
|
||||
*/
|
||||
NT_ConnectionListener AddPolledConnectionListener(
|
||||
NT_ConnectionListenerPoller poller, bool immediate_notify);
|
||||
|
||||
/**
|
||||
* Get the next connection event. This blocks until the next connect or
|
||||
* disconnect occurs. This is intended to be used with
|
||||
* AddPolledConnectionListener(); connection listeners created using
|
||||
* AddConnectionListener() will not be serviced through this function.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @return Information on the connection events. Only returns empty if an
|
||||
* error occurred (e.g. the instance was invalid or is shutting down).
|
||||
*/
|
||||
std::vector<ConnectionNotification> ReadConnectionListenerQueue(
|
||||
NT_ConnectionListenerPoller poller);
|
||||
|
||||
/**
|
||||
* Remove a connection listener.
|
||||
*
|
||||
* @param conn_listener Listener handle to remove
|
||||
*/
|
||||
void RemoveConnectionListener(NT_ConnectionListener conn_listener);
|
||||
NT_Listener AddPolledListener(NT_ListenerPoller poller, NT_Handle handle,
|
||||
unsigned int mask);
|
||||
|
||||
/** @} */
|
||||
|
||||
@@ -1313,31 +1190,13 @@ void StopConnectionDataLog(NT_ConnectionDataLogger logger);
|
||||
* messages outside this range will be silently ignored.
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @param func log callback function
|
||||
* @param min_level minimum log level
|
||||
* @param max_level maximum log level
|
||||
* @return Logger handle
|
||||
* @param func listener callback function
|
||||
* @return Listener handle
|
||||
*/
|
||||
NT_Logger AddLogger(NT_Inst inst,
|
||||
std::function<void(const LogMessage& msg)> func,
|
||||
unsigned int min_level, unsigned int max_level);
|
||||
|
||||
/**
|
||||
* Create a log poller. A poller provides a single queue of poll events.
|
||||
* The returned handle must be destroyed with DestroyLoggerPoller().
|
||||
*
|
||||
* @param inst instance handle
|
||||
* @return poller handle
|
||||
*/
|
||||
NT_LoggerPoller CreateLoggerPoller(NT_Inst inst);
|
||||
|
||||
/**
|
||||
* Destroy a log poller. This will abort any blocked polling call and prevent
|
||||
* additional events from being generated for this poller.
|
||||
*
|
||||
* @param poller poller handle
|
||||
*/
|
||||
void DestroyLoggerPoller(NT_LoggerPoller poller);
|
||||
NT_Listener AddLogger(NT_Inst inst, unsigned int min_level,
|
||||
unsigned int max_level, ListenerCallback func);
|
||||
|
||||
/**
|
||||
* Set the log level for a log poller. Events will only be generated for
|
||||
@@ -1349,24 +1208,8 @@ void DestroyLoggerPoller(NT_LoggerPoller poller);
|
||||
* @param max_level maximum log level
|
||||
* @return Logger handle
|
||||
*/
|
||||
NT_Logger AddPolledLogger(NT_LoggerPoller poller, unsigned int min_level,
|
||||
unsigned int max_level);
|
||||
|
||||
/**
|
||||
* Get the next log event. This blocks until the next log occurs.
|
||||
*
|
||||
* @param poller poller handle
|
||||
* @return Information on the log events. Only returns empty if an error
|
||||
* occurred (e.g. the instance was invalid or is shutting down).
|
||||
*/
|
||||
std::vector<LogMessage> ReadLoggerQueue(NT_LoggerPoller poller);
|
||||
|
||||
/**
|
||||
* Remove a logger.
|
||||
*
|
||||
* @param logger Logger handle to remove
|
||||
*/
|
||||
void RemoveLogger(NT_Logger logger);
|
||||
NT_Listener AddPolledLogger(NT_ListenerPoller poller, unsigned int min_level,
|
||||
unsigned int max_level);
|
||||
|
||||
/** @} */
|
||||
/** @} */
|
||||
|
||||
Reference in New Issue
Block a user