[sim] WebSockets: don't override HAL_Main

Also clean up some other implementation aspects for cleaner shutdown and
reduce peak memory allocation.
This commit is contained in:
Peter Johnson
2020-09-04 10:57:05 -07:00
parent f1b1bdb121
commit f86a5f9b09
19 changed files with 780 additions and 851 deletions

View File

@@ -19,15 +19,12 @@
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/Request.h>
#include "HALSimWSServer.h"
namespace uv = wpi::uv;
namespace wpilibws {
using namespace wpilibws;
bool HALSimHttpConnection::IsValidWsUpgrade(wpi::StringRef protocol) {
auto hws = HALSimWeb::GetInstance();
if (m_request.GetUrl() != hws->GetServerUri()) {
if (m_request.GetUrl() != m_server->GetServerUri()) {
MySendError(404, "invalid websocket address");
return false;
}
@@ -39,21 +36,7 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
conn.disconnect(); // one-shot
m_buffers = std::make_unique<BufferPool>();
m_exec =
UvExecFunc::Create(m_stream.GetLoop(), [](auto out, LoopFunc func) {
func();
out.set_value();
});
auto hws = HALSimWeb::GetInstance();
if (!hws) {
Log(503);
m_websocket->Fail(503, "HALSimWeb unavailable");
return;
}
if (!hws->RegisterWebsocket(shared_from_this())) {
if (!m_server->RegisterWebsocket(shared_from_this())) {
Log(409);
m_websocket->Fail(409, "Only a single simulation websocket is allowed");
return;
@@ -66,8 +49,7 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
// parse incoming JSON, dispatch to parent
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
auto hws = HALSimWeb::GetInstance();
if (!m_isWsConnected || !hws) {
if (!m_isWsConnected) {
return;
}
@@ -80,7 +62,7 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
m_websocket->Fail(400, err);
return;
}
hws->OnNetValueChanged(j);
m_server->OnNetValueChanged(j);
});
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
@@ -89,10 +71,7 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
wpi::errs() << "HALWebSim: websocket disconnected\n";
m_isWsConnected = false;
auto hws = HALSimWeb::GetInstance();
if (hws) {
hws->CloseWebsocket(shared_from_this());
}
m_server->CloseWebsocket(shared_from_this());
}
});
}
@@ -102,84 +81,27 @@ void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
wpi::SmallVector<uv::Buffer, 4> sendBufs;
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
std::lock_guard lock(m_buffers_mutex);
return m_buffers->Allocate();
return m_buffers.Allocate();
}};
os << msg;
// call the websocket send function on the uv loop
m_exec->Call([this, sendBufs]() mutable {
m_websocket->SendText(sendBufs, [this](auto bufs, wpi::uv::Error err) {
{
std::lock_guard lock(m_buffers_mutex);
m_buffers->Release(bufs);
}
m_server->GetExec().Send([self = shared_from_this(), sendBufs] {
self->m_websocket->SendText(sendBufs,
[self](auto bufs, wpi::uv::Error err) {
{
std::lock_guard lock(self->m_buffers_mutex);
self->m_buffers.Release(bufs);
}
if (err) {
wpi::errs() << err.str() << "\n";
wpi::errs().flush();
}
});
if (err) {
wpi::errs() << err.str() << "\n";
wpi::errs().flush();
}
});
});
}
class SendfileReq : public uv::RequestImpl<SendfileReq, uv_fs_t> {
public:
SendfileReq(uv_file out, uv_file in, int64_t inOffset, size_t len)
: m_out(out), m_in(in), m_inOffset(inOffset), m_len(len) {
error = [this](uv::Error err) { GetLoop().error(err); };
}
uv::Loop& GetLoop() const {
return *static_cast<uv::Loop*>(GetRaw()->loop->data);
}
int Send(uv::Loop& loop) {
int err = uv_fs_sendfile(loop.GetRaw(), GetRaw(), m_out, m_in, m_inOffset,
m_len, [](uv_fs_t* req) {
auto& h = *static_cast<SendfileReq*>(req->data);
if (req->result < 0) {
h.ReportError(req->result);
h.complete();
h.Release();
return;
}
h.m_inOffset += req->result;
h.m_len -= req->result;
if (h.m_len == 0) {
// done
h.complete();
h.Release(); // this is always a one-shot
return;
}
// need to send more
h.Send(h.GetLoop());
});
if (err < 0) {
ReportError(err);
complete();
}
return err;
}
wpi::sig::Signal<> complete;
private:
uv_file m_out;
uv_file m_in;
int64_t m_inOffset;
size_t m_len;
};
void Sendfile(uv::Loop& loop, uv_file out, uv_file in, int64_t inOffset,
size_t len, std::function<void()> complete) {
auto req = std::make_shared<SendfileReq>(out, in, inOffset, len);
if (complete) req->complete.connect(complete);
int err = req->Send(loop);
if (err >= 0) req->Keep();
}
void HALSimHttpConnection::SendFileResponse(int code,
const wpi::Twine& codeText,
const wpi::Twine& contentType,
@@ -262,10 +184,12 @@ void HALSimHttpConnection::ProcessRequest() {
std::string prefix = (wpi::sys::path::get_separator() + "user" +
wpi::sys::path::get_separator())
.str();
wpi::sys::path::replace_path_prefix(nativePath, prefix, m_webroot_user);
wpi::sys::path::replace_path_prefix(nativePath, prefix,
m_server->GetWebrootUser());
} else {
wpi::sys::path::replace_path_prefix(
nativePath, wpi::sys::path::get_separator(), m_webroot_sys);
wpi::sys::path::replace_path_prefix(nativePath,
wpi::sys::path::get_separator(),
m_server->GetWebrootSys());
}
if (wpi::sys::fs::is_directory(nativePath)) {
@@ -295,5 +219,3 @@ void HALSimHttpConnection::Log(int code) {
<< m_request.GetMajor() << "." << m_request.GetMinor() << " "
<< code << "\n";
}
} // namespace wpilibws

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -7,180 +7,49 @@
#include "HALSimWSServer.h"
#include <wpi/FileSystem.h>
#include <wpi/Path.h>
#include <wpi/SmallString.h>
#include <wpi/Twine.h>
#include <wpi/UrlParser.h>
#include <wpi/WebSocketServer.h>
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
#include <WSProviderContainer.h>
#include <WSProvider_Analog.h>
#include <WSProvider_DIO.h>
#include <WSProvider_DriverStation.h>
#include <WSProvider_Encoder.h>
#include <WSProvider_Joystick.h>
#include <WSProvider_PWM.h>
#include <WSProvider_Relay.h>
#include <WSProvider_RoboRIO.h>
#include <WSProvider_SimDevice.h>
#include <WSProvider_dPWM.h>
#include "HALSimHttpConnection.h"
using namespace wpilibws;
namespace uv = wpi::uv;
bool HALSimWSServer::Initialize() {
bool result = true;
runner.ExecSync([&](wpi::uv::Loop& loop) {
simWeb = std::make_shared<HALSimWeb>(loop, providers, simDevices);
namespace wpilibws {
std::shared_ptr<HALSimWeb> HALSimWeb::g_instance;
bool HALSimWeb::Initialize() {
// determine where to get static content from
// wpi::SmallVector<char, 64> tmp;
wpi::SmallString<64> tmp;
const char* webroot_sys = std::getenv("HALSIMWS_SYSROOT");
if (webroot_sys != NULL) {
wpi::StringRef tstr(webroot_sys);
tmp.append(tstr);
} else {
wpi::sys::fs::current_path(tmp);
wpi::sys::path::append(tmp, "sim");
}
wpi::sys::fs::make_absolute(tmp);
m_webroot_sys = wpi::Twine(tmp).str();
tmp.clear();
const char* webroot_user = std::getenv("HALSIMWS_USERROOT");
if (webroot_user != NULL) {
wpi::StringRef tstr(webroot_user);
tmp.append(tstr);
} else {
wpi::sys::fs::current_path(tmp);
wpi::sys::path::append(tmp, "sim", "user");
}
wpi::sys::fs::make_absolute(tmp);
m_webroot_user = wpi::Twine(tmp).str();
const char* uri = std::getenv("HALSIMWS_URI");
if (uri != NULL) {
m_uri = uri;
} else {
m_uri = "/wpilibws";
}
const char* port = std::getenv("HALSIMWS_PORT");
if (port != NULL) {
try {
m_port = std::stoi(port);
} catch (const std::invalid_argument& err) {
wpi::errs() << "Error decoding HALSIMWS_PORT (" << err.what() << ")\n";
return false;
}
} else {
m_port = 8080;
}
// create libuv things
m_loop = uv::Loop::Create();
if (!m_loop) {
return false;
}
m_loop->error.connect([](uv::Error err) {
wpi::errs() << "HALSim WS Server libuv ERROR: " << err.str() << '\n';
});
m_server = uv::Tcp::Create(m_loop);
if (!m_server) {
return false;
}
m_server->Bind("", m_port);
return true;
}
void HALSimWeb::Main(void* param) {
GetInstance()->MainLoop();
SetInstance(nullptr);
}
void HALSimWeb::MainLoop() {
// when we get a connection, accept it and start reading
m_server->connection.connect([this, srv = m_server.get()] {
auto tcp = srv->Accept();
if (!tcp) return;
tcp->SetNoDelay(true);
auto conn = std::make_shared<HALSimHttpConnection>(tcp, m_webroot_sys,
m_webroot_user);
tcp->SetData(conn);
});
// start listening for incoming connections
m_server->Listen();
wpi::outs() << "Listening at http://localhost:" << m_port << "\n";
wpi::outs() << "WebSocket URI: " << m_uri << "\n";
m_loop->Run();
}
void HALSimWeb::Exit(void* param) {
auto inst = GetInstance();
if (!inst) return;
auto loop = inst->m_loop;
loop->Walk([](uv::Handle& h) {
h.SetLoopClosing(true);
h.Close();
});
}
bool HALSimWeb::RegisterWebsocket(
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
if (m_hws.lock()) {
return false;
}
m_hws = hws;
m_simDevicesProvider.OnNetworkConnected(hws);
// notify all providers that they should use this new websocket instead
m_providers.ForEach([hws](std::shared_ptr<HALSimWSBaseProvider> provider) {
provider->OnNetworkConnected(hws);
});
return true;
}
void HALSimWeb::CloseWebsocket(
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
// Inform the providers that they need to cancel callbacks
m_simDevicesProvider.OnNetworkDisconnected();
m_providers.ForEach([](std::shared_ptr<HALSimWSBaseProvider> provider) {
provider->OnNetworkDisconnected();
});
if (hws == m_hws.lock()) {
m_hws.reset();
}
}
void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
// Look for "type" and "device" fields so that we can
// generate the key
try {
auto& type = msg.at("type").get_ref<const std::string&>();
auto& device = msg.at("device").get_ref<const std::string&>();
wpi::SmallString<64> key;
key.append(type);
if (!device.empty()) {
key.append("/");
key.append(device);
if (!simWeb->Initialize()) {
result = false;
return;
}
auto provider = m_providers.Get(key.str());
if (provider) {
provider->OnNetValueChanged(msg.at("data"));
}
} catch (wpi::json::exception& e) {
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
}
}
WSRegisterFunc registerFunc = [&](auto key, auto provider) {
providers.Add(key, provider);
};
} // namespace wpilibws
HALSimWSProviderAnalogIn::Initialize(registerFunc);
HALSimWSProviderAnalogOut::Initialize(registerFunc);
HALSimWSProviderDIO::Initialize(registerFunc);
HALSimWSProviderDigitalPWM::Initialize(registerFunc);
HALSimWSProviderDriverStation::Initialize(registerFunc);
HALSimWSProviderEncoder::Initialize(registerFunc);
HALSimWSProviderJoystick::Initialize(registerFunc);
HALSimWSProviderPWM::Initialize(registerFunc);
HALSimWSProviderRelay::Initialize(registerFunc);
HALSimWSProviderRoboRIO::Initialize(registerFunc);
simDevices.Initialize(loop);
simWeb->Start();
});
return result;
}

View File

@@ -0,0 +1,170 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "HALSimWeb.h"
#include <wpi/FileSystem.h>
#include <wpi/Path.h>
#include <wpi/SmallString.h>
#include <wpi/Twine.h>
#include <wpi/UrlParser.h>
#include <wpi/WebSocketServer.h>
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
#include "HALSimHttpConnection.h"
namespace uv = wpi::uv;
using namespace wpilibws;
HALSimWeb::HALSimWeb(wpi::uv::Loop& loop, ProviderContainer& providers,
HALSimWSProviderSimDevices& simDevicesProvider)
: m_loop(loop),
m_providers(providers),
m_simDevicesProvider(simDevicesProvider) {
m_loop.error.connect([](uv::Error err) {
wpi::errs() << "HALSim WS Server libuv ERROR: " << err.str() << '\n';
});
m_server = uv::Tcp::Create(m_loop);
m_exec = UvExecFunc::Create(m_loop);
if (m_exec) {
m_exec->wakeup.connect([](auto func) { func(); });
}
}
bool HALSimWeb::Initialize() {
if (!m_server || !m_exec) {
return false;
}
// determine where to get static content from
// wpi::SmallVector<char, 64> tmp;
wpi::SmallString<64> tmp;
const char* webroot_sys = std::getenv("HALSIMWS_SYSROOT");
if (webroot_sys != NULL) {
wpi::StringRef tstr(webroot_sys);
tmp.append(tstr);
} else {
wpi::sys::fs::current_path(tmp);
wpi::sys::path::append(tmp, "sim");
}
wpi::sys::fs::make_absolute(tmp);
m_webroot_sys = wpi::Twine(tmp).str();
tmp.clear();
const char* webroot_user = std::getenv("HALSIMWS_USERROOT");
if (webroot_user != NULL) {
wpi::StringRef tstr(webroot_user);
tmp.append(tstr);
} else {
wpi::sys::fs::current_path(tmp);
wpi::sys::path::append(tmp, "sim", "user");
}
wpi::sys::fs::make_absolute(tmp);
m_webroot_user = wpi::Twine(tmp).str();
const char* uri = std::getenv("HALSIMWS_URI");
if (uri != NULL) {
m_uri = uri;
} else {
m_uri = "/wpilibws";
}
const char* port = std::getenv("HALSIMWS_PORT");
if (port != NULL) {
try {
m_port = std::stoi(port);
} catch (const std::invalid_argument& err) {
wpi::errs() << "Error decoding HALSIMWS_PORT (" << err.what() << ")\n";
return false;
}
} else {
m_port = 8080;
}
return true;
}
void HALSimWeb::Start() {
m_server->Bind("", m_port);
// when we get a connection, accept it and start reading
m_server->connection.connect([this, srv = m_server.get()] {
auto tcp = srv->Accept();
if (!tcp) return;
tcp->SetNoDelay(true);
auto conn = std::make_shared<HALSimHttpConnection>(shared_from_this(), tcp);
tcp->SetData(conn);
});
// start listening for incoming connections
m_server->Listen();
wpi::outs() << "Listening at http://localhost:" << m_port << "\n";
wpi::outs() << "WebSocket URI: " << m_uri << "\n";
}
bool HALSimWeb::RegisterWebsocket(
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
if (m_hws.lock()) {
return false;
}
m_hws = hws;
m_simDevicesProvider.OnNetworkConnected(hws);
// notify all providers that they should use this new websocket instead
m_providers.ForEach([hws](std::shared_ptr<HALSimWSBaseProvider> provider) {
provider->OnNetworkConnected(hws);
});
return true;
}
void HALSimWeb::CloseWebsocket(
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
// Inform the providers that they need to cancel callbacks
m_simDevicesProvider.OnNetworkDisconnected();
m_providers.ForEach([](std::shared_ptr<HALSimWSBaseProvider> provider) {
provider->OnNetworkDisconnected();
});
if (hws == m_hws.lock()) {
m_hws.reset();
}
}
void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
// Look for "type" and "device" fields so that we can
// generate the key
try {
auto& type = msg.at("type").get_ref<const std::string&>();
auto& device = msg.at("device").get_ref<const std::string&>();
wpi::SmallString<64> key;
key.append(type);
if (!device.empty()) {
key.append("/");
key.append(device);
}
auto provider = m_providers.Get(key.str());
if (provider) {
provider->OnNetValueChanged(msg.at("data"));
}
} catch (wpi::json::exception& e) {
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
}
}

View File

@@ -5,18 +5,9 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include <WSProviderContainer.h>
#include <WSProvider_Analog.h>
#include <WSProvider_DIO.h>
#include <WSProvider_DriverStation.h>
#include <WSProvider_Encoder.h>
#include <WSProvider_Joystick.h>
#include <WSProvider_PWM.h>
#include <WSProvider_Relay.h>
#include <WSProvider_RoboRIO.h>
#include <WSProvider_SimDevice.h>
#include <WSProvider_dPWM.h>
#include <hal/Main.h>
#include <memory>
#include <hal/Extensions.h>
#include <wpi/raw_ostream.h>
#include "HALSimWSServer.h"
@@ -24,44 +15,23 @@
using namespace std::placeholders;
using namespace wpilibws;
// Currently, robots never terminate, so we keep static objects that are
// never properly released or cleaned up.
static ProviderContainer providers;
static HALSimWSProviderSimDevices simDevices(providers);
static std::unique_ptr<HALSimWSServer> gServer;
extern "C" {
#if defined(WIN32) || defined(_WIN32)
__declspec(dllexport)
#endif
int HALSIM_InitExtension(void) {
wpi::outs() << "Websocket Simulator Initializing.\n";
auto hsw = std::make_shared<HALSimWeb>(providers, simDevices);
HALSimWeb::SetInstance(hsw);
wpi::outs() << "Websocket WS Server Initializing.\n";
if (!hsw->Initialize()) {
HAL_OnShutdown(nullptr, [](void*) { gServer.reset(); });
gServer = std::make_unique<HALSimWSServer>();
if (!gServer->Initialize()) {
return -1;
}
WSRegisterFunc registerFunc = [&](auto key, auto provider) {
providers.Add(key, provider);
};
HALSimWSProviderAnalogIn::Initialize(registerFunc);
HALSimWSProviderAnalogOut::Initialize(registerFunc);
HALSimWSProviderDIO::Initialize(registerFunc);
HALSimWSProviderDigitalPWM::Initialize(registerFunc);
HALSimWSProviderDriverStation::Initialize(registerFunc);
HALSimWSProviderEncoder::Initialize(registerFunc);
HALSimWSProviderJoystick::Initialize(registerFunc);
HALSimWSProviderPWM::Initialize(registerFunc);
HALSimWSProviderRelay::Initialize(registerFunc);
HALSimWSProviderRoboRIO::Initialize(registerFunc);
simDevices.Initialize(hsw->GetLoop());
HAL_SetMain(nullptr, HALSimWeb::Main, HALSimWeb::Exit);
wpi::outs() << "Websocket Simulator Initialized!\n";
wpi::outs() << "Websocket WS Server Initialized!\n";
return 0;
}
} // extern "C"