2020-08-20 01:14:03 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* 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 <cinttypes>
|
|
|
|
|
#include <memory>
|
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;
|
|
|
|
|
bool IsValidWsUpgrade(wpi::StringRef protocol) override;
|
|
|
|
|
void ProcessWsUpgrade() override;
|
|
|
|
|
void SendFileResponse(int code, const wpi::Twine& codeText,
|
|
|
|
|
const wpi::Twine& contentType,
|
|
|
|
|
const wpi::Twine& filename,
|
|
|
|
|
const wpi::Twine& extraHeader = wpi::Twine{});
|
|
|
|
|
|
|
|
|
|
void MySendError(int code, const wpi::Twine& message);
|
|
|
|
|
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
|