[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

@@ -9,35 +9,31 @@
#include <cinttypes>
#include <memory>
#include <string>
#include <vector>
#include <utility>
#include <HALSimBaseWebSocketConnection.h>
#include <wpi/HttpWebSocketServerConnection.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
namespace wpilibws {
#include "HALSimWeb.h"
class HALSimWeb;
namespace wpi {
class json;
} // namespace wpi
namespace wpilibws {
class HALSimHttpConnection
: public wpi::HttpWebSocketServerConnection<HALSimHttpConnection>,
public HALSimBaseWebSocketConnection {
public:
using BufferPool = wpi::uv::SimpleBufferPool<4>;
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
explicit HALSimHttpConnection(std::shared_ptr<wpi::uv::Stream> stream,
wpi::StringRef webroot_sys,
wpi::StringRef webroot_user)
HALSimHttpConnection(std::shared_ptr<HALSimWeb> server,
std::shared_ptr<wpi::uv::Stream> stream)
: wpi::HttpWebSocketServerConnection<HALSimHttpConnection>(stream, {}),
m_webroot_sys(webroot_sys),
m_webroot_user(webroot_user) {}
m_server(std::move(server)),
m_buffers(128) {}
public:
// callable from any thread
@@ -56,18 +52,13 @@ class HALSimHttpConnection
void Log(int code);
private:
// Absolute paths of folders to retrieve data from
// -> /
std::string m_webroot_sys;
// -> /user
std::string m_webroot_user;
std::shared_ptr<HALSimWeb> m_server;
// is the websocket connected?
bool m_isWsConnected = false;
// these are only valid if the websocket is connected
std::shared_ptr<UvExecFunc> m_exec;
std::unique_ptr<BufferPool> m_buffers;
wpi::uv::SimpleBufferPool<4> m_buffers;
std::mutex m_buffers_mutex;
};

View File

@@ -8,74 +8,27 @@
#pragma once
#include <memory>
#include <string>
#include <WSBaseProvider.h>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpi/StringMap.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
#include <wpi/EventLoopRunner.h>
#include "HALSimWeb.h"
namespace wpilibws {
class HALSimHttpConnection;
class HALSimWeb {
class HALSimWSServer {
public:
static std::shared_ptr<HALSimWeb> GetInstance() { return g_instance; }
static void SetInstance(std::shared_ptr<HALSimWeb> inst) {
g_instance = inst;
}
explicit HALSimWeb(ProviderContainer& providers,
HALSimWSProviderSimDevices& simDevicesProvider)
: m_providers(providers), m_simDevicesProvider(simDevicesProvider) {}
HALSimWeb(const HALSimWeb&) = delete;
HALSimWeb& operator=(const HALSimWeb&) = delete;
HALSimWSServer() = default;
HALSimWSServer(const HALSimWSServer&) = delete;
HALSimWSServer& operator=(const HALSimWSServer&) = delete;
bool Initialize();
static void Main(void*);
static void Exit(void*);
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
// network -> sim
void OnNetValueChanged(const wpi::json& msg);
std::string GetServerUri() const { return m_uri; }
int GetServerPort() { return m_port; }
std::shared_ptr<wpi::uv::Loop> GetLoop() { return m_loop; }
private:
static std::shared_ptr<HALSimWeb> g_instance;
void MainLoop();
// connected http connection that contains active websocket
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
// list of providers
ProviderContainer& m_providers;
HALSimWSProviderSimDevices& m_simDevicesProvider;
std::shared_ptr<wpi::uv::Loop> m_loop;
std::shared_ptr<wpi::uv::Tcp> m_server;
// Absolute paths of folders to retrieve data from
// -> /
std::string m_webroot_sys;
// -> /user
std::string m_webroot_user;
std::string m_uri;
int m_port;
ProviderContainer providers;
HALSimWSProviderSimDevices simDevices{providers};
wpi::EventLoopRunner runner;
std::shared_ptr<HALSimWeb> simWeb;
};
} // namespace wpilibws

View File

@@ -0,0 +1,78 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <WSBaseProvider.h>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpi/StringRef.h>
#include <wpi/uv/Async.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
namespace wpi {
class json;
} // namespace wpi
namespace wpilibws {
class HALSimWeb : public std::enable_shared_from_this<HALSimWeb> {
public:
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::Async<LoopFunc>;
HALSimWeb(wpi::uv::Loop& loop, ProviderContainer& providers,
HALSimWSProviderSimDevices& simDevicesProvider);
HALSimWeb(const HALSimWeb&) = delete;
HALSimWeb& operator=(const HALSimWeb&) = delete;
bool Initialize();
void Start();
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
// network -> sim
void OnNetValueChanged(const wpi::json& msg);
wpi::StringRef GetWebrootSys() const { return m_webroot_sys; }
wpi::StringRef GetWebrootUser() const { return m_webroot_user; }
wpi::StringRef GetServerUri() const { return m_uri; }
int GetServerPort() const { return m_port; }
wpi::uv::Loop& GetLoop() { return m_loop; }
UvExecFunc& GetExec() { return *m_exec; }
private:
// connected http connection that contains active websocket
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
wpi::uv::Loop& m_loop;
std::shared_ptr<wpi::uv::Tcp> m_server;
std::shared_ptr<UvExecFunc> m_exec;
// list of providers
ProviderContainer& m_providers;
HALSimWSProviderSimDevices& m_simDevicesProvider;
// Absolute paths of folders to retrieve data from
// -> /
std::string m_webroot_sys;
// -> /user
std::string m_webroot_user;
std::string m_uri;
int m_port;
};
} // namespace wpilibws