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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright 2021 ScyllaDB
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>
32 namespace seastar::experimental::websocket {
40 * \brief an error in handling a WebSocket connection
42 class exception : std::exception {
45 exception(std::string_view msg) : _msg(msg) {}
46 const char* what() const noexcept {
52 * \brief a WebSocket connection
54 class connection : public boost::intrusive::list_base_hook<> {
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};
65 * \param server owning \ref server
66 * \param fd established socket used for communication
68 connection(server& server, connected_socket&& fd)
71 , _read_buf(_fd.input())
72 , _write_buf(_fd.output())
79 * \brief serve WebSocket protocol on a connection
83 * \brief close the socket
90 future<> read_http_upgrade_request();
91 future<> response_loop();
92 void on_new_connection();
96 * \brief a WebSocket server
98 * A server capable of establishing and serving connections
99 * over WebSocket protocol.
102 std::vector<server_socket> _listeners;
104 boost::intrusive::list<connection> _connections;
107 * \brief listen for a WebSocket connection on given address
108 * \param addr address to listen on
110 void listen(socket_address addr);
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)
116 void listen(socket_address addr, listen_options lo);
119 * Stops the server and shuts down all active connections
123 friend class connection;
125 void do_accepts(int which);
126 future<> do_accept_one(int which);