[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

@@ -0,0 +1,75 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpi/uv/Async.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
#include <wpi/uv/Timer.h>
namespace wpi {
class json;
} // namespace wpi
namespace wpilibws {
class HALSimWSClientConnection;
class HALSimWS : public std::enable_shared_from_this<HALSimWS> {
public:
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::Async<LoopFunc>;
HALSimWS(wpi::uv::Loop& loop, ProviderContainer& providers,
HALSimWSProviderSimDevices& simDevicesProvider);
HALSimWS(const HALSimWS&) = delete;
HALSimWS& operator=(const HALSimWS&) = delete;
bool Initialize();
void Start();
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
void OnNetValueChanged(const wpi::json& msg);
wpi::StringRef GetTargetHost() const { return m_host; }
wpi::StringRef GetTargetUri() const { return m_uri; }
int GetTargetPort() const { return m_port; }
wpi::uv::Loop& GetLoop() { return m_loop; }
UvExecFunc& GetExec() { return *m_exec; }
private:
void AttemptConnect();
bool m_tcp_connected = false;
std::shared_ptr<wpi::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;
std::shared_ptr<UvExecFunc> m_exec;
ProviderContainer& m_providers;
HALSimWSProviderSimDevices& m_simDevicesProvider;
std::string m_host;
std::string m_uri;
int m_port;
};
} // namespace wpilibws

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* 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. */
@@ -8,67 +8,27 @@
#pragma once
#include <memory>
#include <string>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpi/json.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
#include <wpi/uv/Timer.h>
#include <wpi/EventLoopRunner.h>
#include "HALSimWS.h"
namespace wpilibws {
class HALSimWSClientConnection;
class HALSimWS {
class HALSimWSClient {
public:
static std::shared_ptr<HALSimWS> GetInstance() { return g_instance; }
static void SetInstance(std::shared_ptr<HALSimWS> inst) { g_instance = inst; }
explicit HALSimWS(ProviderContainer& providers,
HALSimWSProviderSimDevices& simDevicesProvider)
: m_providers(providers), m_simDevicesProvider(simDevicesProvider) {}
HALSimWS(const HALSimWS&) = delete;
HALSimWS& operator=(const HALSimWS&) = delete;
HALSimWSClient() = default;
HALSimWSClient(const HALSimWSClient&) = delete;
HALSimWSClient& operator=(const HALSimWSClient&) = 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);
void OnNetValueChanged(const wpi::json& msg);
std::string GetTargetHost() const { return m_host; }
std::string GetTargetUri() const { return m_uri; }
int GetTargetPort() { return m_port; }
std::shared_ptr<wpi::uv::Loop> GetLoop() { return m_loop; }
private:
static std::shared_ptr<HALSimWS> g_instance;
void MainLoop();
void AttemptConnect();
bool m_tcp_connected = false;
std::shared_ptr<wpi::uv::Timer> m_connect_timer;
int m_connect_attempts = 0;
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
ProviderContainer& m_providers;
HALSimWSProviderSimDevices& m_simDevicesProvider;
std::shared_ptr<wpi::uv::Loop> m_loop;
std::shared_ptr<wpi::uv::Tcp> m_tcp_client;
std::string m_host;
std::string m_uri;
int m_port;
ProviderContainer providers;
HALSimWSProviderSimDevices simDevices{providers};
wpi::EventLoopRunner runner;
std::shared_ptr<HALSimWS> simws;
};
} // namespace wpilibws

View File

@@ -8,15 +8,20 @@
#pragma once
#include <memory>
#include <utility>
#include <HALSimBaseWebSocketConnection.h>
#include <wpi/WebSocket.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
#include <wpi/uv/Stream.h>
#include "HALSimWS.h"
namespace wpi {
class json;
} // namespace wpi
namespace wpilibws {
class HALSimWS;
@@ -25,25 +30,24 @@ class HALSimWSClientConnection
: public HALSimBaseWebSocketConnection,
public std::enable_shared_from_this<HALSimWSClientConnection> {
public:
using BufferPool = wpi::uv::SimpleBufferPool<4>;
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
explicit HALSimWSClientConnection(std::shared_ptr<wpi::uv::Stream> stream)
: m_stream(stream) {}
explicit HALSimWSClientConnection(std::shared_ptr<HALSimWS> client,
std::shared_ptr<wpi::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 Initialize();
private:
std::shared_ptr<HALSimWS> m_client;
std::shared_ptr<wpi::uv::Stream> m_stream;
bool m_ws_connected = false;
wpi::WebSocket* m_websocket = nullptr;
std::shared_ptr<UvExecFunc> m_exec;
std::unique_ptr<BufferPool> m_buffers;
wpi::uv::SimpleBufferPool<4> m_buffers;
std::mutex m_buffers_mutex;
};