2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
|
#include <memory>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2020-09-04 10:57:05 -07:00
|
|
|
#include <utility>
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
#include <HALSimBaseWebSocketConnection.h>
|
|
|
|
|
#include <wpi/HttpWebSocketServerConnection.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
|
|
|
|
#include <wpi/uv/AsyncFunction.h>
|
|
|
|
|
#include <wpi/uv/Buffer.h>
|
|
|
|
|
|
2020-09-04 10:57:05 -07:00
|
|
|
#include "HALSimWeb.h"
|
|
|
|
|
|
|
|
|
|
namespace wpi {
|
|
|
|
|
class json;
|
|
|
|
|
} // namespace wpi
|
2020-08-20 01:14:03 -04:00
|
|
|
|
2020-09-04 10:57:05 -07:00
|
|
|
namespace wpilibws {
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
class HALSimHttpConnection
|
|
|
|
|
: public wpi::HttpWebSocketServerConnection<HALSimHttpConnection>,
|
|
|
|
|
public HALSimBaseWebSocketConnection {
|
|
|
|
|
public:
|
2020-09-04 10:57:05 -07:00
|
|
|
HALSimHttpConnection(std::shared_ptr<HALSimWeb> server,
|
|
|
|
|
std::shared_ptr<wpi::uv::Stream> stream)
|
2020-08-20 01:14:03 -04:00
|
|
|
: wpi::HttpWebSocketServerConnection<HALSimHttpConnection>(stream, {}),
|
2020-09-04 10:57:05 -07:00
|
|
|
m_server(std::move(server)),
|
|
|
|
|
m_buffers(128) {}
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// callable from any thread
|
|
|
|
|
void OnSimValueChanged(const wpi::json& msg) override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void ProcessRequest() override;
|
2021-06-06 16:13:58 -07:00
|
|
|
bool IsValidWsUpgrade(std::string_view protocol) override;
|
2020-08-20 01:14:03 -04:00
|
|
|
void ProcessWsUpgrade() override;
|
2021-06-06 16:13:58 -07:00
|
|
|
void SendFileResponse(int code, std::string_view codeText,
|
|
|
|
|
std::string_view contentType, std::string_view filename,
|
|
|
|
|
std::string_view extraHeader = {});
|
2020-08-20 01:14:03 -04:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void MySendError(int code, std::string_view message);
|
2020-08-20 01:14:03 -04:00
|
|
|
void Log(int code);
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-04 10:57:05 -07:00
|
|
|
std::shared_ptr<HALSimWeb> m_server;
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
// is the websocket connected?
|
|
|
|
|
bool m_isWsConnected = false;
|
|
|
|
|
|
|
|
|
|
// these are only valid if the websocket is connected
|
2020-09-04 10:57:05 -07:00
|
|
|
wpi::uv::SimpleBufferPool<4> m_buffers;
|
2020-08-20 01:14:03 -04:00
|
|
|
std::mutex m_buffers_mutex;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace wpilibws
|