WPILibC++ 2023.4.3
HttpWebSocketServerConnection.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_HTTPWEBSOCKETSERVERCONNECTION_H_
6#define WPINET_HTTPWEBSOCKETSERVERCONNECTION_H_
7
8#include <initializer_list>
9#include <memory>
10#include <span>
11#include <string>
12#include <string_view>
13
14#include <wpi/SmallVector.h>
15
17#include "wpinet/WebSocket.h"
19#include "wpinet/uv/Stream.h"
20
21namespace wpi {
22
23/**
24 * A server-side HTTP connection that also accepts WebSocket upgrades.
25 *
26 * @tparam Derived derived class for std::enable_shared_from_this.
27 */
28template <typename Derived>
30 : public HttpServerConnection,
31 public std::enable_shared_from_this<Derived> {
32 public:
33 /**
34 * Constructor.
35 *
36 * @param stream network stream
37 * @param protocols Acceptable subprotocols
38 */
39 HttpWebSocketServerConnection(std::shared_ptr<uv::Stream> stream,
40 std::span<const std::string_view> protocols);
41
42 /**
43 * Constructor.
44 *
45 * @param stream network stream
46 * @param protocols Acceptable subprotocols
47 */
49 std::shared_ptr<uv::Stream> stream,
50 std::initializer_list<std::string_view> protocols)
52 {protocols.begin(), protocols.end()}) {}
53
54 protected:
55 /**
56 * Check that an incoming WebSocket upgrade is okay. This is called prior
57 * to accepting the upgrade (so prior to ProcessWsUpgrade()).
58 *
59 * The implementation should check other headers and return true if the
60 * WebSocket connection should be accepted.
61 *
62 * @param protocol negotiated subprotocol
63 */
64 virtual bool IsValidWsUpgrade(std::string_view protocol) { return true; }
65
66 /**
67 * Process an incoming WebSocket upgrade. This is called after the header
68 * reader has been disconnected and the websocket has been accepted.
69 *
70 * The implementation should set up appropriate callbacks on the websocket
71 * object to continue communication.
72 *
73 * @note When a WebSocket upgrade occurs, the stream user data is replaced
74 * with the websocket, and the websocket user data points to "this".
75 * Replace the websocket user data with caution!
76 */
77 virtual void ProcessWsUpgrade() = 0;
78
79 /**
80 * WebSocket connection; not valid until ProcessWsUpgrade is called.
81 */
83
84 private:
85 WebSocketServerHelper m_helper;
87};
88
89} // namespace wpi
90
92
93#endif // WPINET_HTTPWEBSOCKETSERVERCONNECTION_H_
Definition: HttpServerConnection.h:19
A server-side HTTP connection that also accepts WebSocket upgrades.
Definition: HttpWebSocketServerConnection.h:31
virtual void ProcessWsUpgrade()=0
Process an incoming WebSocket upgrade.
virtual bool IsValidWsUpgrade(std::string_view protocol)
Check that an incoming WebSocket upgrade is okay.
Definition: HttpWebSocketServerConnection.h:64
HttpWebSocketServerConnection(std::shared_ptr< uv::Stream > stream, std::span< const std::string_view > protocols)
Constructor.
Definition: HttpWebSocketServerConnection.inc:15
WebSocket * m_websocket
WebSocket connection; not valid until ProcessWsUpgrade is called.
Definition: HttpWebSocketServerConnection.h:82
HttpWebSocketServerConnection(std::shared_ptr< uv::Stream > stream, std::initializer_list< std::string_view > protocols)
Constructor.
Definition: HttpWebSocketServerConnection.h:48
RFC 6455 compliant WebSocket client and server implementation.
Definition: WebSocket.h:34
WebSocket HTTP server helper.
Definition: WebSocketServer.h:33
basic_string_view< char > string_view
Definition: core.h:520
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18