blob: 82825ab90f9b62680135856521f55172ec258883 [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.
25 virtual void OnWriteBlocked(TlsConnection* connection) = 0;
26 virtual void OnWriteUnblocked(TlsConnection* connection) = 0;
27
28 // Called when |connection| experiences an error, such as a read error.
Jordan Baylesf46c0a62019-09-20 11:37:42 -070029 virtual void OnError(TlsConnection* connection, Error error) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070030
Jordan Baylesf46c0a62019-09-20 11:37:42 -070031 // Called when a |block| arrives on |connection|.
32 virtual void OnRead(TlsConnection* connection,
33 std::vector<uint8_t> block) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070034
35 protected:
36 virtual ~Client() = default;
37 };
38
Yuri Wiitalafb75b092019-11-14 14:13:18 -080039 virtual ~TlsConnection();
40
41 // Sets the Client associated with this instance. This should be called as
42 // soon as the factory provides a new TlsConnection instance via
43 // TlsConnectionFactory::OnAccepted() or OnConnected(). Pass nullptr to unset
44 // the Client.
45 virtual void SetClient(Client* client) = 0;
46
Jordan Bayles1c785bd2019-08-15 10:32:33 -070047 // Sends a message.
48 virtual void Write(const void* data, size_t len) = 0;
49
50 // Get the local address.
Yuri Wiitalafb75b092019-11-14 14:13:18 -080051 virtual IPEndpoint GetLocalEndpoint() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070052
53 // Get the connected remote address.
Yuri Wiitalafb75b092019-11-14 14:13:18 -080054 virtual IPEndpoint GetRemoteEndpoint() const = 0;
Ryan Keanefdebe6c2019-09-06 14:12:51 -070055
Jordan Bayles1c785bd2019-08-15 10:32:33 -070056 protected:
Yuri Wiitalafb75b092019-11-14 14:13:18 -080057 TlsConnection();
Jordan Bayles1c785bd2019-08-15 10:32:33 -070058};
59
Jordan Bayles1c785bd2019-08-15 10:32:33 -070060} // namespace openscreen
61
62#endif // PLATFORM_API_TLS_CONNECTION_H_