blob: 7c2777fb204f0dd177c1a4d68bf8489800253fe3 [file] [log] [blame]
Jordan Bayles1c785bd2019-08-15 10:32:33 -07001// 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 Bayles1c785bd2019-08-15 10:32:33 -07009#include <vector>
10
Jordan Bayles1c785bd2019-08-15 10:32:33 -070011#include "platform/base/error.h"
12#include "platform/base/ip_address.h"
Jordan Bayles1c785bd2019-08-15 10:32:33 -070013
14namespace openscreen {
15namespace platform {
16
17class TlsConnection {
18 public:
Yuri Wiitalafb75b092019-11-14 14:13:18 -080019 // Client callbacks are run via the TaskRunner used by TlsConnectionFactory.
Jordan Bayles1c785bd2019-08-15 10:32:33 -070020 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 Baylesf46c0a62019-09-20 11:37:42 -070030 virtual void OnError(TlsConnection* connection, Error error) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070031
Jordan Baylesf46c0a62019-09-20 11:37:42 -070032 // Called when a |block| arrives on |connection|.
33 virtual void OnRead(TlsConnection* connection,
34 std::vector<uint8_t> block) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070035
36 protected:
37 virtual ~Client() = default;
38 };
39
Yuri Wiitalafb75b092019-11-14 14:13:18 -080040 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 Bayles1c785bd2019-08-15 10:32:33 -070048 // Sends a message.
49 virtual void Write(const void* data, size_t len) = 0;
50
51 // Get the local address.
Yuri Wiitalafb75b092019-11-14 14:13:18 -080052 virtual IPEndpoint GetLocalEndpoint() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070053
54 // Get the connected remote address.
Yuri Wiitalafb75b092019-11-14 14:13:18 -080055 virtual IPEndpoint GetRemoteEndpoint() const = 0;
Ryan Keanefdebe6c2019-09-06 14:12:51 -070056
Jordan Bayles1c785bd2019-08-15 10:32:33 -070057 protected:
Yuri Wiitalafb75b092019-11-14 14:13:18 -080058 TlsConnection();
Jordan Bayles1c785bd2019-08-15 10:32:33 -070059};
60
61} // namespace platform
62} // namespace openscreen
63
64#endif // PLATFORM_API_TLS_CONNECTION_H_