Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PLATFORM_API_TLS_CONNECTION_H_ |
| 6 | #define PLATFORM_API_TLS_CONNECTION_H_ |
| 7 | |
| 8 | #include <cstdint> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "absl/types/optional.h" |
| 14 | #include "platform/api/network_interface.h" |
| 15 | #include "platform/api/task_runner.h" |
| 16 | #include "platform/base/error.h" |
| 17 | #include "platform/base/ip_address.h" |
| 18 | #include "platform/base/macros.h" |
| 19 | |
| 20 | namespace openscreen { |
| 21 | namespace platform { |
| 22 | |
| 23 | class TlsConnection { |
| 24 | public: |
| 25 | // Client callbacks are ran on the provided TaskRunner. |
| 26 | class Client { |
| 27 | public: |
| 28 | // Called when |connection| writing is blocked and unblocked, respectively. |
| 29 | // Note that implementations should do best effort to buffer packets even in |
| 30 | // blocked state, and should call OnError if we actually overflow the |
| 31 | // buffer. |
| 32 | virtual void OnWriteBlocked(TlsConnection* connection) = 0; |
| 33 | virtual void OnWriteUnblocked(TlsConnection* connection) = 0; |
| 34 | |
| 35 | // Called when |connection| experiences an error, such as a read error. |
Jordan Bayles | f46c0a6 | 2019-09-20 11:37:42 -0700 | [diff] [blame] | 36 | virtual void OnError(TlsConnection* connection, Error error) = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 37 | |
Jordan Bayles | f46c0a6 | 2019-09-20 11:37:42 -0700 | [diff] [blame] | 38 | // Called when a |block| arrives on |connection|. |
| 39 | virtual void OnRead(TlsConnection* connection, |
| 40 | std::vector<uint8_t> block) = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | virtual ~Client() = default; |
| 44 | }; |
| 45 | |
| 46 | // Sends a message. |
| 47 | virtual void Write(const void* data, size_t len) = 0; |
| 48 | |
| 49 | // Get the local address. |
mark a. foltz | 64b3c16 | 2019-10-07 11:14:26 -0700 | [diff] [blame] | 50 | virtual IPEndpoint local_address() const = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 51 | |
| 52 | // Get the connected remote address. |
mark a. foltz | 64b3c16 | 2019-10-07 11:14:26 -0700 | [diff] [blame] | 53 | virtual IPEndpoint remote_address() const = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 54 | |
| 55 | // Sets the client for this instance. |
| 56 | void set_client(Client* client) { client_ = client; } |
| 57 | |
Ryan Keane | fdebe6c | 2019-09-06 14:12:51 -0700 | [diff] [blame] | 58 | virtual ~TlsConnection() = default; |
| 59 | |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 60 | protected: |
| 61 | explicit TlsConnection(TaskRunner* task_runner) : task_runner_(task_runner) {} |
| 62 | |
Ryan Keane | fdebe6c | 2019-09-06 14:12:51 -0700 | [diff] [blame] | 63 | // Called when |connection| writing is blocked and unblocked, respectively. |
| 64 | // This call will be proxied to the Client set for this TlsConnection. |
| 65 | void OnWriteBlocked(); |
| 66 | void OnWriteUnblocked(); |
| 67 | |
| 68 | // Called when |connection| experiences an error, such as a read error. This |
| 69 | // call will be proxied to the Client set for this TlsConnection. |
| 70 | void OnError(Error error); |
| 71 | |
| 72 | // Called when a |packet| arrives on |socket|. This call will be proxied to |
| 73 | // the Client set for this TlsConnection. |
| 74 | void OnRead(std::vector<uint8_t> message); |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | Client* client_; |
| 78 | TaskRunner* const task_runner_; |
| 79 | |
| 80 | OSP_DISALLOW_COPY_AND_ASSIGN(TlsConnection); |
| 81 | }; |
| 82 | |
| 83 | } // namespace platform |
| 84 | } // namespace openscreen |
| 85 | |
| 86 | #endif // PLATFORM_API_TLS_CONNECTION_H_ |