mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
SCRIPT namespace replacements
This commit is contained in:
committed by
Peter Johnson
parent
ae6c043632
commit
9aca8e0fd6
@@ -16,17 +16,17 @@
|
||||
|
||||
static constexpr int kTcpConnectAttemptTimeout = 1000;
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
namespace uv = wpi::net::uv;
|
||||
|
||||
using namespace wpilibws;
|
||||
|
||||
HALSimWS::HALSimWS(wpi::uv::Loop& loop, ProviderContainer& providers,
|
||||
HALSimWS::HALSimWS(wpi::net::uv::Loop& loop, ProviderContainer& providers,
|
||||
HALSimWSProviderSimDevices& simDevicesProvider)
|
||||
: m_loop(loop),
|
||||
m_providers(providers),
|
||||
m_simDevicesProvider(simDevicesProvider) {
|
||||
m_loop.error.connect([](uv::Error err) {
|
||||
wpi::print(stderr, "HALSim WS Client libuv Error: {}\n", err.str());
|
||||
wpi::util::print(stderr, "HALSim WS Client libuv Error: {}\n", err.str());
|
||||
});
|
||||
|
||||
m_tcp_client = uv::Tcp::Create(m_loop);
|
||||
@@ -54,7 +54,7 @@ bool HALSimWS::Initialize() {
|
||||
try {
|
||||
m_port = std::stoi(port);
|
||||
} catch (const std::invalid_argument& err) {
|
||||
wpi::print(stderr, "Error decoding HALSIMWS_PORT ({})\n", err.what());
|
||||
wpi::util::print(stderr, "Error decoding HALSIMWS_PORT ({})\n", err.what());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@@ -71,8 +71,8 @@ bool HALSimWS::Initialize() {
|
||||
const char* msgFilters = std::getenv("HALSIMWS_FILTERS");
|
||||
if (msgFilters != nullptr) {
|
||||
m_useMsgFiltering = true;
|
||||
wpi::split(wpi::trim(msgFilters), ',', -1, false,
|
||||
[&](auto val) { m_msgFilters[wpi::trim(val)] = true; });
|
||||
wpi::util::split(wpi::util::trim(msgFilters), ',', -1, false,
|
||||
[&](auto val) { m_msgFilters[wpi::util::trim(val)] = true; });
|
||||
} else {
|
||||
m_useMsgFiltering = false;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ void HALSimWS::Start() {
|
||||
|
||||
// Hook up TCP client events
|
||||
m_tcp_client->error.connect(
|
||||
[this, socket = m_tcp_client.get()](wpi::uv::Error err) {
|
||||
[this, socket = m_tcp_client.get()](wpi::net::uv::Error err) {
|
||||
if (m_tcp_connected) {
|
||||
m_tcp_connected = false;
|
||||
m_connect_attempts = 0;
|
||||
@@ -102,16 +102,16 @@ void HALSimWS::Start() {
|
||||
|
||||
// Print any filters we are using
|
||||
if (m_useMsgFiltering) {
|
||||
wpi::print("WS Message Filters:");
|
||||
wpi::util::print("WS Message Filters:");
|
||||
for (auto&& filter : m_msgFilters) {
|
||||
wpi::print("* \"{}\"\n", filter.first);
|
||||
wpi::util::print("* \"{}\"\n", filter.first);
|
||||
}
|
||||
} else {
|
||||
wpi::print("No WS Message Filters specified");
|
||||
wpi::util::print("No WS Message Filters specified");
|
||||
}
|
||||
|
||||
// Set up the connection timer
|
||||
wpi::print("Will attempt to connect to ws://{}:{}{}\n", m_host, m_port,
|
||||
wpi::util::print("Will attempt to connect to ws://{}:{}{}\n", m_host, m_port,
|
||||
m_uri);
|
||||
|
||||
// Set up the timer to attempt connection
|
||||
@@ -125,7 +125,7 @@ void HALSimWS::Start() {
|
||||
void HALSimWS::AttemptConnect() {
|
||||
m_connect_attempts++;
|
||||
|
||||
wpi::print("Connection Attempt {}\n", m_connect_attempts);
|
||||
wpi::util::print("Connection Attempt {}\n", m_connect_attempts);
|
||||
|
||||
struct sockaddr_in dest;
|
||||
uv::NameToAddr(m_host, m_port, &dest);
|
||||
@@ -170,7 +170,7 @@ void HALSimWS::CloseWebsocket(
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWS::OnNetValueChanged(const wpi::json& msg) {
|
||||
void HALSimWS::OnNetValueChanged(const wpi::util::json& msg) {
|
||||
// Look for "type" and "device" fields so that we can
|
||||
// generate the key
|
||||
|
||||
@@ -178,7 +178,7 @@ void HALSimWS::OnNetValueChanged(const wpi::json& msg) {
|
||||
auto& type = msg.at("type").get_ref<const std::string&>();
|
||||
auto& device = msg.at("device").get_ref<const std::string&>();
|
||||
|
||||
wpi::SmallString<64> key;
|
||||
wpi::util::SmallString<64> key;
|
||||
key.append(type);
|
||||
if (!device.empty()) {
|
||||
key.append("/");
|
||||
@@ -189,8 +189,8 @@ void HALSimWS::OnNetValueChanged(const wpi::json& msg) {
|
||||
if (provider) {
|
||||
provider->OnNetValueChanged(msg.at("data"));
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::print(stderr, "Error with incoming message: {}\n", e.what());
|
||||
} catch (wpi::util::json::exception& e) {
|
||||
wpi::util::print(stderr, "Error with incoming message: {}\n", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ using namespace wpilibws;
|
||||
|
||||
bool HALSimWSClient::Initialize() {
|
||||
bool result = true;
|
||||
runner.ExecSync([&](wpi::uv::Loop& loop) {
|
||||
runner.ExecSync([&](wpi::net::uv::Loop& loop) {
|
||||
simws = std::make_shared<HALSimWS>(loop, providers, simDevices);
|
||||
|
||||
if (!simws->Initialize()) {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "wpi/net/raw_uv_ostream.hpp"
|
||||
#include "wpi/util/print.hpp"
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
namespace uv = wpi::net::uv;
|
||||
|
||||
using namespace wpilibws;
|
||||
|
||||
@@ -21,7 +21,7 @@ void HALSimWSClientConnection::Initialize() {
|
||||
// Get a shared pointer to ourselves
|
||||
auto self = this->shared_from_this();
|
||||
|
||||
auto ws = wpi::WebSocket::CreateClient(
|
||||
auto ws = wpi::net::WebSocket::CreateClient(
|
||||
*m_stream, m_client->GetTargetUri(),
|
||||
fmt::format("{}:{}", m_client->GetTargetHost(),
|
||||
m_client->GetTargetPort()));
|
||||
@@ -48,13 +48,13 @@ void HALSimWSClientConnection::Initialize() {
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::json j;
|
||||
wpi::util::json j;
|
||||
try {
|
||||
j = wpi::json::parse(msg);
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
j = wpi::util::json::parse(msg);
|
||||
} catch (const wpi::util::json::parse_error& e) {
|
||||
std::string err("JSON parse failed: ");
|
||||
err += e.what();
|
||||
wpi::print(stderr, "{}\n", err);
|
||||
wpi::util::print(stderr, "{}\n", err);
|
||||
m_websocket->Fail(1003, err);
|
||||
return;
|
||||
}
|
||||
@@ -72,7 +72,7 @@ void HALSimWSClientConnection::Initialize() {
|
||||
});
|
||||
}
|
||||
|
||||
void HALSimWSClientConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
void HALSimWSClientConnection::OnSimValueChanged(const wpi::util::json& msg) {
|
||||
if (msg.empty()) {
|
||||
return;
|
||||
}
|
||||
@@ -83,12 +83,12 @@ void HALSimWSClientConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
if (!m_client->CanSendMessage(type)) {
|
||||
return;
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::print(stderr, "Error with message: {}\n", e.what());
|
||||
} catch (wpi::util::json::exception& e) {
|
||||
wpi::util::print(stderr, "Error with message: {}\n", e.what());
|
||||
}
|
||||
|
||||
wpi::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
wpi::util::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::net::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers.Allocate();
|
||||
}};
|
||||
@@ -98,14 +98,14 @@ void HALSimWSClientConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
// Call the websocket send function on the uv loop
|
||||
m_client->GetExec().Send([self = shared_from_this(), sendBufs] {
|
||||
self->m_websocket->SendText(sendBufs,
|
||||
[self](auto bufs, wpi::uv::Error err) {
|
||||
[self](auto bufs, wpi::net::uv::Error err) {
|
||||
{
|
||||
std::lock_guard lock(self->m_buffers_mutex);
|
||||
self->m_buffers.Release(bufs);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
wpi::print(stderr, "{}\n", err.str());
|
||||
wpi::util::print(stderr, "{}\n", err.str());
|
||||
std::fflush(stderr);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -25,9 +25,9 @@ class HALSimWSClientConnection;
|
||||
class HALSimWS : public std::enable_shared_from_this<HALSimWS> {
|
||||
public:
|
||||
using LoopFunc = std::function<void()>;
|
||||
using UvExecFunc = wpi::uv::Async<LoopFunc>;
|
||||
using UvExecFunc = wpi::net::uv::Async<LoopFunc>;
|
||||
|
||||
HALSimWS(wpi::uv::Loop& loop, ProviderContainer& providers,
|
||||
HALSimWS(wpi::net::uv::Loop& loop, ProviderContainer& providers,
|
||||
HALSimWSProviderSimDevices& simDevicesProvider);
|
||||
HALSimWS(const HALSimWS&) = delete;
|
||||
HALSimWS& operator=(const HALSimWS&) = delete;
|
||||
@@ -38,14 +38,14 @@ class HALSimWS : public std::enable_shared_from_this<HALSimWS> {
|
||||
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
|
||||
void OnNetValueChanged(const wpi::json& msg);
|
||||
void OnNetValueChanged(const wpi::util::json& msg);
|
||||
|
||||
bool CanSendMessage(std::string_view type);
|
||||
|
||||
const std::string& GetTargetHost() const { return m_host; }
|
||||
const std::string& GetTargetUri() const { return m_uri; }
|
||||
int GetTargetPort() const { return m_port; }
|
||||
wpi::uv::Loop& GetLoop() { return m_loop; }
|
||||
wpi::net::uv::Loop& GetLoop() { return m_loop; }
|
||||
|
||||
UvExecFunc& GetExec() { return *m_exec; }
|
||||
|
||||
@@ -53,13 +53,13 @@ class HALSimWS : public std::enable_shared_from_this<HALSimWS> {
|
||||
void AttemptConnect();
|
||||
|
||||
bool m_tcp_connected = false;
|
||||
std::shared_ptr<wpi::uv::Timer> m_connect_timer;
|
||||
std::shared_ptr<wpi::net::uv::Timer> m_connect_timer;
|
||||
int m_connect_attempts = 0;
|
||||
|
||||
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
|
||||
|
||||
wpi::uv::Loop& m_loop;
|
||||
std::shared_ptr<wpi::uv::Tcp> m_tcp_client;
|
||||
wpi::net::uv::Loop& m_loop;
|
||||
std::shared_ptr<wpi::net::uv::Tcp> m_tcp_client;
|
||||
std::shared_ptr<UvExecFunc> m_exec;
|
||||
|
||||
ProviderContainer& m_providers;
|
||||
@@ -70,7 +70,7 @@ class HALSimWS : public std::enable_shared_from_this<HALSimWS> {
|
||||
int m_port;
|
||||
|
||||
bool m_useMsgFiltering;
|
||||
wpi::StringMap<bool> m_msgFilters;
|
||||
wpi::util::StringMap<bool> m_msgFilters;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
|
||||
@@ -23,7 +23,7 @@ class HALSimWSClient {
|
||||
|
||||
ProviderContainer providers;
|
||||
HALSimWSProviderSimDevices simDevices{providers};
|
||||
wpi::EventLoopRunner runner;
|
||||
wpi::net::EventLoopRunner runner;
|
||||
std::shared_ptr<HALSimWS> simws;
|
||||
};
|
||||
|
||||
|
||||
@@ -24,23 +24,23 @@ class HALSimWSClientConnection
|
||||
public std::enable_shared_from_this<HALSimWSClientConnection> {
|
||||
public:
|
||||
explicit HALSimWSClientConnection(std::shared_ptr<HALSimWS> client,
|
||||
std::shared_ptr<wpi::uv::Stream> stream)
|
||||
std::shared_ptr<wpi::net::uv::Stream> stream)
|
||||
: m_client(std::move(client)),
|
||||
m_stream(std::move(stream)),
|
||||
m_buffers(128) {}
|
||||
|
||||
public:
|
||||
void OnSimValueChanged(const wpi::json& msg) override;
|
||||
void OnSimValueChanged(const wpi::util::json& msg) override;
|
||||
void Initialize();
|
||||
|
||||
private:
|
||||
std::shared_ptr<HALSimWS> m_client;
|
||||
std::shared_ptr<wpi::uv::Stream> m_stream;
|
||||
std::shared_ptr<wpi::net::uv::Stream> m_stream;
|
||||
|
||||
bool m_ws_connected = false;
|
||||
wpi::WebSocket* m_websocket = nullptr;
|
||||
wpi::net::WebSocket* m_websocket = nullptr;
|
||||
|
||||
wpi::uv::SimpleBufferPool<4> m_buffers;
|
||||
wpi::net::uv::SimpleBufferPool<4> m_buffers;
|
||||
std::mutex m_buffers_mutex;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user