blob: 77729c64612eff0e711e2552712365375e3ba0cc [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 {
Jordan Bayles1c785bd2019-08-15 10:32:33 -070015
16class TlsConnection {
17 public:
Yuri Wiitalafb75b092019-11-14 14:13:18 -080018 // Client callbacks are run via the TaskRunner used by TlsConnectionFactory.
Jordan Bayles1c785bd2019-08-15 10:32:33 -070019 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 Wiitala910cb7f2020-01-14 13:37:32 -080025 //
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 Bayles1c785bd2019-08-15 10:32:33 -070029
30 // Called when |connection| experiences an error, such as a read error.
Jordan Baylesf46c0a62019-09-20 11:37:42 -070031 virtual void OnError(TlsConnection* connection, Error error) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070032
Jordan Baylesf46c0a62019-09-20 11:37:42 -070033 // Called when a |block| arrives on |connection|.
34 virtual void OnRead(TlsConnection* connection,
35 std::vector<uint8_t> block) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070036
37 protected:
38 virtual ~Client() = default;
39 };
40
Yuri Wiitalafb75b092019-11-14 14:13:18 -080041 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 Wiitala910cb7f2020-01-14 13:37:32 -080049 // 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 Bayles1c785bd2019-08-15 10:32:33 -070057
58 // Get the local address.
Yuri Wiitalafb75b092019-11-14 14:13:18 -080059 virtual IPEndpoint GetLocalEndpoint() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070060
61 // Get the connected remote address.
Yuri Wiitalafb75b092019-11-14 14:13:18 -080062 virtual IPEndpoint GetRemoteEndpoint() const = 0;
Ryan Keanefdebe6c2019-09-06 14:12:51 -070063
Jordan Bayles1c785bd2019-08-15 10:32:33 -070064 protected:
Yuri Wiitalafb75b092019-11-14 14:13:18 -080065 TlsConnection();
Jordan Bayles1c785bd2019-08-15 10:32:33 -070066};
67
Jordan Bayles1c785bd2019-08-15 10:32:33 -070068} // namespace openscreen
69
70#endif // PLATFORM_API_TLS_CONNECTION_H_