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> |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 11 | #include "platform/base/error.h" |
| 12 | #include "platform/base/ip_address.h" |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 13 | |
| 14 | namespace openscreen { |
| 15 | namespace platform { |
| 16 | |
| 17 | class TlsConnection { |
| 18 | public: |
Yuri Wiitala | fb75b09 | 2019-11-14 14:13:18 -0800 | [diff] [blame^] | 19 | // Client callbacks are run via the TaskRunner used by TlsConnectionFactory. |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 20 | class Client { |
| 21 | public: |
| 22 | // Called when |connection| writing is blocked and unblocked, respectively. |
| 23 | // Note that implementations should do best effort to buffer packets even in |
| 24 | // blocked state, and should call OnError if we actually overflow the |
| 25 | // buffer. |
| 26 | virtual void OnWriteBlocked(TlsConnection* connection) = 0; |
| 27 | virtual void OnWriteUnblocked(TlsConnection* connection) = 0; |
| 28 | |
| 29 | // Called when |connection| experiences an error, such as a read error. |
Jordan Bayles | f46c0a6 | 2019-09-20 11:37:42 -0700 | [diff] [blame] | 30 | virtual void OnError(TlsConnection* connection, Error error) = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 31 | |
Jordan Bayles | f46c0a6 | 2019-09-20 11:37:42 -0700 | [diff] [blame] | 32 | // Called when a |block| arrives on |connection|. |
| 33 | virtual void OnRead(TlsConnection* connection, |
| 34 | std::vector<uint8_t> block) = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 35 | |
| 36 | protected: |
| 37 | virtual ~Client() = default; |
| 38 | }; |
| 39 | |
Yuri Wiitala | fb75b09 | 2019-11-14 14:13:18 -0800 | [diff] [blame^] | 40 | virtual ~TlsConnection(); |
| 41 | |
| 42 | // Sets the Client associated with this instance. This should be called as |
| 43 | // soon as the factory provides a new TlsConnection instance via |
| 44 | // TlsConnectionFactory::OnAccepted() or OnConnected(). Pass nullptr to unset |
| 45 | // the Client. |
| 46 | virtual void SetClient(Client* client) = 0; |
| 47 | |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 48 | // Sends a message. |
| 49 | virtual void Write(const void* data, size_t len) = 0; |
| 50 | |
| 51 | // Get the local address. |
Yuri Wiitala | fb75b09 | 2019-11-14 14:13:18 -0800 | [diff] [blame^] | 52 | virtual IPEndpoint GetLocalEndpoint() const = 0; |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 53 | |
| 54 | // Get the connected remote address. |
Yuri Wiitala | fb75b09 | 2019-11-14 14:13:18 -0800 | [diff] [blame^] | 55 | virtual IPEndpoint GetRemoteEndpoint() const = 0; |
Ryan Keane | fdebe6c | 2019-09-06 14:12:51 -0700 | [diff] [blame] | 56 | |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 57 | protected: |
Yuri Wiitala | fb75b09 | 2019-11-14 14:13:18 -0800 | [diff] [blame^] | 58 | TlsConnection(); |
Jordan Bayles | 1c785bd | 2019-08-15 10:32:33 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace platform |
| 62 | } // namespace openscreen |
| 63 | |
| 64 | #endif // PLATFORM_API_TLS_CONNECTION_H_ |