Files
allwpilib/simulation/halsim_ws_server/src/main/native/include/HALSimHttpConnection.h
Peter Johnson b2c3b2dd8e Use std::string_view and fmtlib across all libraries (#3402)
- Twine, StringRef, Format, and NativeFormatting have been removed
- Logging now uses fmtlib style formatting
- Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or
std::puts()/std::fputs() (for unformatted strings).
- A wpi/fmt/raw_ostream.h header has been added to enable
fmt::print() with wpi::raw_ostream
2021-06-06 16:13:58 -07:00

63 lines
1.7 KiB
C++

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <cinttypes>
#include <memory>
#include <string_view>
#include <utility>
#include <HALSimBaseWebSocketConnection.h>
#include <wpi/HttpWebSocketServerConnection.h>
#include <wpi/mutex.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
#include "HALSimWeb.h"
namespace wpi {
class json;
} // namespace wpi
namespace wpilibws {
class HALSimHttpConnection
: public wpi::HttpWebSocketServerConnection<HALSimHttpConnection>,
public HALSimBaseWebSocketConnection {
public:
HALSimHttpConnection(std::shared_ptr<HALSimWeb> server,
std::shared_ptr<wpi::uv::Stream> stream)
: wpi::HttpWebSocketServerConnection<HALSimHttpConnection>(stream, {}),
m_server(std::move(server)),
m_buffers(128) {}
public:
// callable from any thread
void OnSimValueChanged(const wpi::json& msg) override;
protected:
void ProcessRequest() override;
bool IsValidWsUpgrade(std::string_view protocol) override;
void ProcessWsUpgrade() override;
void SendFileResponse(int code, std::string_view codeText,
std::string_view contentType, std::string_view filename,
std::string_view extraHeader = {});
void MySendError(int code, std::string_view message);
void Log(int code);
private:
std::shared_ptr<HALSimWeb> m_server;
// is the websocket connected?
bool m_isWsConnected = false;
// these are only valid if the websocket is connected
wpi::uv::SimpleBufferPool<4> m_buffers;
std::mutex m_buffers_mutex;
};
} // namespace wpilibws