mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
Implement GetConnections().
This commit is contained in:
@@ -128,6 +128,23 @@ void DispatcherBase::Flush() {
|
||||
m_flush_cv.notify_one();
|
||||
}
|
||||
|
||||
std::vector<ConnectionInfo> DispatcherBase::GetConnections() const {
|
||||
std::vector<ConnectionInfo> conns;
|
||||
if (!m_active) return conns;
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_user_mutex);
|
||||
for (auto& conn : m_connections) {
|
||||
if (conn.net->state() != NetworkConnection::kActive) continue;
|
||||
auto& stream = conn.net->stream();
|
||||
conns.emplace_back(
|
||||
ConnectionInfo{conn.net->remote_id(), stream.getPeerIP(),
|
||||
static_cast<unsigned int>(stream.getPeerPort()),
|
||||
conn.net->last_update(), conn.net->proto_rev()});
|
||||
}
|
||||
|
||||
return conns;
|
||||
}
|
||||
|
||||
void DispatcherBase::DispatchThreadMain() {
|
||||
// local copy of active m_connections
|
||||
struct ConnectionRef {
|
||||
|
||||
@@ -39,6 +39,7 @@ class DispatcherBase {
|
||||
void SetUpdateRate(double interval);
|
||||
void SetIdentity(llvm::StringRef name);
|
||||
void Flush();
|
||||
std::vector<ConnectionInfo> GetConnections() const;
|
||||
|
||||
bool active() const { return m_active; }
|
||||
|
||||
@@ -77,7 +78,7 @@ class DispatcherBase {
|
||||
std::unique_ptr<NetworkAcceptor> m_server_acceptor;
|
||||
|
||||
// Mutex for user-accessible items
|
||||
std::mutex m_user_mutex;
|
||||
mutable std::mutex m_user_mutex;
|
||||
struct Connection {
|
||||
Connection() = default;
|
||||
explicit Connection(std::unique_ptr<NetworkConnection> net_)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "NetworkConnection.h"
|
||||
|
||||
#include "support/timestamp.h"
|
||||
#include "tcpsockets/NetworkStream.h"
|
||||
#include "Log.h"
|
||||
#include "raw_socket_istream.h"
|
||||
@@ -26,6 +27,7 @@ NetworkConnection::NetworkConnection(std::unique_ptr<NetworkStream> stream,
|
||||
m_active = false;
|
||||
m_proto_rev = 0x0300;
|
||||
m_state = static_cast<int>(kCreated);
|
||||
m_last_update = 0;
|
||||
}
|
||||
|
||||
NetworkConnection::~NetworkConnection() { Stop(); }
|
||||
@@ -98,6 +100,7 @@ void NetworkConnection::ReadThreadMain() {
|
||||
if (m_stream) m_stream->close();
|
||||
break;
|
||||
}
|
||||
m_last_update = Now();
|
||||
m_process_incoming(std::move(msg), this);
|
||||
}
|
||||
DEBUG3("read thread died");
|
||||
|
||||
@@ -55,6 +55,8 @@ class NetworkConnection {
|
||||
std::string remote_id() const;
|
||||
void set_remote_id(StringRef remote_id);
|
||||
|
||||
unsigned long long last_update() const { return m_last_update; }
|
||||
|
||||
NetworkConnection(const NetworkConnection&) = delete;
|
||||
NetworkConnection& operator=(const NetworkConnection&) = delete;
|
||||
|
||||
@@ -74,6 +76,7 @@ class NetworkConnection {
|
||||
std::atomic_int m_state;
|
||||
mutable std::mutex m_remote_id_mutex;
|
||||
std::string m_remote_id;
|
||||
std::atomic_ullong m_last_update;
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
@@ -126,7 +126,7 @@ void SetUpdateRate(double interval) {
|
||||
}
|
||||
|
||||
std::vector<ConnectionInfo> GetConnections() {
|
||||
return std::vector<ConnectionInfo>();
|
||||
return Dispatcher::GetInstance().GetConnections();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user