/*----------------------------------------------------------------------------*/ /* 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 #include #include #include #include #include #include #include #include namespace wpilibws { class HALSimWSClientConnection; class HALSimWS { public: static std::shared_ptr GetInstance() { return g_instance; } static void SetInstance(std::shared_ptr 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; bool Initialize(); static void Main(void*); static void Exit(void*); bool RegisterWebsocket(std::shared_ptr hws); void CloseWebsocket(std::shared_ptr 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 GetLoop() { return m_loop; } private: static std::shared_ptr g_instance; void MainLoop(); void AttemptConnect(); bool m_tcp_connected = false; std::shared_ptr m_connect_timer; int m_connect_attempts = 0; std::weak_ptr m_hws; ProviderContainer& m_providers; HALSimWSProviderSimDevices& m_simDevicesProvider; std::shared_ptr m_loop; std::shared_ptr m_tcp_client; std::string m_host; std::string m_uri; int m_port; }; } // namespace wpilibws