|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2016 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#pragma once |
| 19 | +#ifndef NET_HTTP_WEBSOCKET_HPP |
| 20 | +#define NET_HTTP_WEBSOCKET_HPP |
| 21 | + |
| 22 | +#include <net/http/server.hpp> |
| 23 | +#include <net/http/client.hpp> |
| 24 | + |
| 25 | +namespace http { |
| 26 | + |
| 27 | +class WebSocket { |
| 28 | +public: |
| 29 | + typedef std::unique_ptr<WebSocket> WebSocket_ptr; |
| 30 | + // client connected |
| 31 | + typedef delegate<void(WebSocket_ptr)> connect_func; |
| 32 | + // server new client |
| 33 | + typedef delegate<bool(net::tcp::Socket, std::string)> accept_func; |
| 34 | + // data read (data, length) |
| 35 | + typedef delegate<void(const char*, size_t)> read_func; |
| 36 | + // closed (status code) |
| 37 | + typedef delegate<void(uint16_t)> close_func; |
| 38 | + // error (reason) |
| 39 | + typedef delegate<void(std::string)> error_func; |
| 40 | + |
| 41 | + /// Server-side connection |
| 42 | + WebSocket(http::Request_ptr request, |
| 43 | + http::Response_writer_ptr response, |
| 44 | + accept_func = nullptr); |
| 45 | + /// Client-side connection |
| 46 | + static void |
| 47 | + connect(http::Client& client, |
| 48 | + std::string origin, |
| 49 | + uri::URI dest, |
| 50 | + connect_func callback); |
| 51 | + |
| 52 | + enum mode_t { |
| 53 | + TEXT, |
| 54 | + BINARY |
| 55 | + }; |
| 56 | + void write(const char* buffer, size_t len, mode_t = TEXT); |
| 57 | + void write(net::tcp::buffer_t, size_t len, mode_t = TEXT); |
| 58 | + |
| 59 | + void write(const std::string& text) |
| 60 | + { |
| 61 | + write(text.c_str(), text.size(), TEXT); |
| 62 | + } |
| 63 | + |
| 64 | + // close the websocket |
| 65 | + void close(); |
| 66 | + |
| 67 | + // user callbacks |
| 68 | + close_func on_close = nullptr; |
| 69 | + error_func on_error = nullptr; |
| 70 | + read_func on_read = nullptr; |
| 71 | + |
| 72 | + bool is_alive() const noexcept { |
| 73 | + return this->conn != nullptr; |
| 74 | + } |
| 75 | + bool is_client() const noexcept { |
| 76 | + return this->clientside; |
| 77 | + } |
| 78 | + const auto& get_connection() const noexcept { |
| 79 | + return this->conn; |
| 80 | + } |
| 81 | + |
| 82 | + // string description for status codes |
| 83 | + static const char* status_code(uint16_t code); |
| 84 | + |
| 85 | + WebSocket(net::Stream_ptr, bool); |
| 86 | + WebSocket(WebSocket&&); |
| 87 | + ~WebSocket(); |
| 88 | + |
| 89 | +private: |
| 90 | + net::Stream_ptr conn; |
| 91 | + bool clientside; |
| 92 | + |
| 93 | + WebSocket(const WebSocket&) = delete; |
| 94 | + WebSocket& operator= (const WebSocket&) = delete; |
| 95 | + WebSocket& operator= (WebSocket&&) = delete; |
| 96 | + void read_data(net::tcp::buffer_t, size_t); |
| 97 | + bool write_opcode(uint8_t code, const char*, size_t); |
| 98 | + void failure(const std::string&); |
| 99 | + void tcp_closed(); |
| 100 | + void reset(); |
| 101 | +}; |
| 102 | +using WebSocket_ptr = WebSocket::WebSocket_ptr; |
| 103 | + |
| 104 | +} // http |
| 105 | + |
| 106 | +#endif |
0 commit comments