]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/websocket/server.hh
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / include / seastar / websocket / server.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright 2021 ScyllaDB
20 */
21
22 #pragma once
23
24 #include <seastar/http/request_parser.hh>
25 #include <seastar/core/seastar.hh>
26 #include <seastar/core/sstring.hh>
27 #include <seastar/net/api.hh>
28 #include <seastar/core/gate.hh>
29 #include <seastar/core/queue.hh>
30 #include <seastar/core/when_all.hh>
31
32 namespace seastar::experimental::websocket {
33
34 class server;
35 struct reply {
36 //TODO: implement
37 };
38
39 /*!
40 * \brief an error in handling a WebSocket connection
41 */
42 class exception : std::exception {
43 std::string _msg;
44 public:
45 exception(std::string_view msg) : _msg(msg) {}
46 const char* what() const noexcept {
47 return _msg.c_str();
48 }
49 };
50
51 /*!
52 * \brief a WebSocket connection
53 */
54 class connection : public boost::intrusive::list_base_hook<> {
55 server& _server;
56 connected_socket _fd;
57 input_stream<char> _read_buf;
58 output_stream<char> _write_buf;
59 http_request_parser _http_parser;
60 std::unique_ptr<reply> _resp;
61 queue<std::unique_ptr<reply>> _replies{10};
62 bool _done = false;
63 public:
64 /*!
65 * \param server owning \ref server
66 * \param fd established socket used for communication
67 */
68 connection(server& server, connected_socket&& fd)
69 : _server(server)
70 , _fd(std::move(fd))
71 , _read_buf(_fd.input())
72 , _write_buf(_fd.output())
73 {
74 on_new_connection();
75 }
76 ~connection();
77
78 /*!
79 * \brief serve WebSocket protocol on a connection
80 */
81 future<> process();
82 /*!
83 * \brief close the socket
84 */
85 void shutdown();
86
87 protected:
88 future<> read_loop();
89 future<> read_one();
90 future<> read_http_upgrade_request();
91 future<> response_loop();
92 void on_new_connection();
93 };
94
95 /*!
96 * \brief a WebSocket server
97 *
98 * A server capable of establishing and serving connections
99 * over WebSocket protocol.
100 */
101 class server {
102 std::vector<server_socket> _listeners;
103 gate _task_gate;
104 boost::intrusive::list<connection> _connections;
105 public:
106 /*!
107 * \brief listen for a WebSocket connection on given address
108 * \param addr address to listen on
109 */
110 void listen(socket_address addr);
111 /*!
112 * \brief listen for a WebSocket connection on given address with custom options
113 * \param addr address to listen on
114 * \param lo custom listen options (\ref listen_options)
115 */
116 void listen(socket_address addr, listen_options lo);
117
118 /*!
119 * Stops the server and shuts down all active connections
120 */
121 future<> stop();
122
123 friend class connection;
124 protected:
125 void do_accepts(int which);
126 future<> do_accept_one(int which);
127 };
128
129 }