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