|
| 1 | +/* |
| 2 | +Copyright (c) 2020 Cedric Jimenez |
| 3 | +This file is part of OpenOCPP. |
| 4 | +
|
| 5 | +OpenOCPP is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU Lesser General Public License as published by |
| 7 | +the Free Software Foundation, either version 3 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +OpenOCPP is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU Lesser General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU Lesser General Public License |
| 16 | +along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +#ifndef IWEBSOCKETSERVER_H |
| 20 | +#define IWEBSOCKETSERVER_H |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +namespace ocpp |
| 27 | +{ |
| 28 | +namespace websockets |
| 29 | +{ |
| 30 | + |
| 31 | +/** @brief Interface for websocket server implementations */ |
| 32 | +class IWebsocketServer |
| 33 | +{ |
| 34 | + public: |
| 35 | + // Forward declarations |
| 36 | + class IListener; |
| 37 | + class IClient; |
| 38 | + struct Credentials; |
| 39 | + |
| 40 | + /** @brief Destructor */ |
| 41 | + virtual ~IWebsocketServer() { } |
| 42 | + |
| 43 | + /** |
| 44 | + * @brief Start the server |
| 45 | + * @param url URL to listen to |
| 46 | + * @param protocol Name of the protocol to use |
| 47 | + * @param credentials Credentials to use |
| 48 | + * @param ping_interval Interval between 2 websocket PING messages when the socket is idle |
| 49 | + * @return true if the server has been started, false otherwise |
| 50 | + */ |
| 51 | + virtual bool start(const std::string& url, |
| 52 | + const std::string& protocol, |
| 53 | + const Credentials& credentials, |
| 54 | + std::chrono::milliseconds ping_interval = std::chrono::seconds(5)) = 0; |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Stop the server |
| 58 | + * @return true if the server has been stopped, false otherwise |
| 59 | + */ |
| 60 | + virtual bool stop() = 0; |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief Register a listener to the websocket events |
| 64 | + * @param listener Listener object |
| 65 | + */ |
| 66 | + virtual void registerListener(IListener& listener) = 0; |
| 67 | + |
| 68 | + /** @brief Interface for the websocket server listeners */ |
| 69 | + class IListener |
| 70 | + { |
| 71 | + public: |
| 72 | + /** @brief Destructor */ |
| 73 | + virtual ~IListener() { } |
| 74 | + |
| 75 | + /** |
| 76 | + * @brief Called to check the user credentials for HTTP basic authentication |
| 77 | + * @param uri Requested URI |
| 78 | + * @param user User name |
| 79 | + * @param password Password |
| 80 | + * @return true if the credentials are valid, false otherwise |
| 81 | + */ |
| 82 | + virtual bool wsCheckCredentials(const char* uri, const std::string& user, const std::string& password) = 0; |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Called when connection is successfull |
| 86 | + * @param uri Requested URI |
| 87 | + * @param client Client connection |
| 88 | + */ |
| 89 | + virtual void wsClientConnected(const char* uri, std::shared_ptr<IClient> client) = 0; |
| 90 | + |
| 91 | + /** @brief Called on critical error */ |
| 92 | + virtual void wsServerError() = 0; |
| 93 | + }; |
| 94 | + |
| 95 | + /** @brief Interface for websocket client connection */ |
| 96 | + class IClient |
| 97 | + { |
| 98 | + public: |
| 99 | + // Forward declarations |
| 100 | + class IListener; |
| 101 | + |
| 102 | + /** @brief Destructor */ |
| 103 | + virtual ~IClient() { } |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Disconnect the client |
| 107 | + * @return true if the disconnection is successfull, false otherwise |
| 108 | + */ |
| 109 | + virtual bool disconnect() = 0; |
| 110 | + |
| 111 | + /** |
| 112 | + * @brief Indicate if the client is connected |
| 113 | + * @return true if the client is connected, false otherwise |
| 114 | + */ |
| 115 | + virtual bool isConnected() = 0; |
| 116 | + |
| 117 | + /** |
| 118 | + * @brief Send data through the websocket connection |
| 119 | + * @param buffer Buffer containing the data to send |
| 120 | + * @param size Size of the buffer in bytes |
| 121 | + * @return true is the data has been sent, false otherwise |
| 122 | + */ |
| 123 | + virtual bool send(const void* data, size_t size) = 0; |
| 124 | + |
| 125 | + /** |
| 126 | + * @brief Register a listener to the websocket events |
| 127 | + * @param listener Listener object |
| 128 | + */ |
| 129 | + virtual void registerListener(IListener& listener) = 0; |
| 130 | + |
| 131 | + /** @brief Interface for the websocket clients listeners */ |
| 132 | + class IListener |
| 133 | + { |
| 134 | + public: |
| 135 | + /** @brief Destructor */ |
| 136 | + virtual ~IListener() { } |
| 137 | + |
| 138 | + /** @brief Called when connection is lost */ |
| 139 | + virtual void wsClientDisconnected() = 0; |
| 140 | + |
| 141 | + /** @brief Called when a critical error occured */ |
| 142 | + virtual void wsClientError() = 0; |
| 143 | + |
| 144 | + /** |
| 145 | + * @brief Call when data has been received |
| 146 | + * @param buffer Buffer containing received data |
| 147 | + * @param size Size of the buffer in bytes |
| 148 | + */ |
| 149 | + virtual void wsClientDataReceived(const void* data, size_t size) = 0; |
| 150 | + }; |
| 151 | + }; |
| 152 | + |
| 153 | + /** @brief Connection credentials */ |
| 154 | + struct Credentials |
| 155 | + { |
| 156 | + // Basic authentication |
| 157 | + |
| 158 | + /** @bool Enable HTTP basic authentication */ |
| 159 | + bool http_basic_authent; |
| 160 | + |
| 161 | + // TLS connections (wss URLs only) |
| 162 | + |
| 163 | + /** @brief Cipher list for TLSv1.2 connections, leave empty for default |
| 164 | + * (OpenSSL format, default = system dependent) */ |
| 165 | + std::string tls12_cipher_list; |
| 166 | + /** @brief Cipher list for TLSv1.3 connections, leave empty for default |
| 167 | + * (OpenSSL format, default = system dependent) */ |
| 168 | + std::string tls13_cipher_list; |
| 169 | + /** @brief ECDH curve, leave empty for default |
| 170 | + * (OpenSSL format, default = system dependent) */ |
| 171 | + std::string ecdh_curve; |
| 172 | + /** @brief Server certificate */ |
| 173 | + std::string server_certificate; |
| 174 | + /** @brief Server certificate's private key */ |
| 175 | + std::string server_certificate_private_key; |
| 176 | + /** @brief Server certificate's private key passphrase */ |
| 177 | + std::string server_certificate_private_key_passphrase; |
| 178 | + /** @brief Certification Authority signing chain for the server certificate */ |
| 179 | + std::string server_certificate_ca; |
| 180 | + /** @brief Certification Authority signing chain for the clients certificates */ |
| 181 | + std::string client_certificate_ca; |
| 182 | + /** @bool Enable client authentication using certificate */ |
| 183 | + bool client_certificate_authent; |
| 184 | + }; |
| 185 | +}; |
| 186 | + |
| 187 | +} // namespace websockets |
| 188 | +} // namespace ocpp |
| 189 | + |
| 190 | +#endif // IWEBSOCKETSERVER_H |
0 commit comments