blob: a1b2965caa0e47377262bf5e38c58b9569627220 [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>
9#include <memory>
10#include <string>
11#include <vector>
12
13#include "absl/types/optional.h"
14#include "platform/api/network_interface.h"
15#include "platform/api/task_runner.h"
16#include "platform/base/error.h"
17#include "platform/base/ip_address.h"
18#include "platform/base/macros.h"
19
20namespace openscreen {
21namespace platform {
22
23class TlsConnection {
24 public:
25 // Client callbacks are ran on the provided TaskRunner.
26 class Client {
27 public:
28 // Called when |connection| writing is blocked and unblocked, respectively.
29 // Note that implementations should do best effort to buffer packets even in
30 // blocked state, and should call OnError if we actually overflow the
31 // buffer.
32 virtual void OnWriteBlocked(TlsConnection* connection) = 0;
33 virtual void OnWriteUnblocked(TlsConnection* connection) = 0;
34
35 // Called when |connection| experiences an error, such as a read error.
Jordan Baylesf46c0a62019-09-20 11:37:42 -070036 virtual void OnError(TlsConnection* connection, Error error) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070037
Jordan Baylesf46c0a62019-09-20 11:37:42 -070038 // Called when a |block| arrives on |connection|.
39 virtual void OnRead(TlsConnection* connection,
40 std::vector<uint8_t> block) = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070041
42 protected:
43 virtual ~Client() = default;
44 };
45
46 // Sends a message.
47 virtual void Write(const void* data, size_t len) = 0;
48
49 // Get the local address.
mark a. foltz64b3c162019-10-07 11:14:26 -070050 virtual IPEndpoint local_address() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070051
52 // Get the connected remote address.
mark a. foltz64b3c162019-10-07 11:14:26 -070053 virtual IPEndpoint remote_address() const = 0;
Jordan Bayles1c785bd2019-08-15 10:32:33 -070054
55 // Sets the client for this instance.
56 void set_client(Client* client) { client_ = client; }
57
Ryan Keanefdebe6c2019-09-06 14:12:51 -070058 virtual ~TlsConnection() = default;
59
Jordan Bayles1c785bd2019-08-15 10:32:33 -070060 protected:
61 explicit TlsConnection(TaskRunner* task_runner) : task_runner_(task_runner) {}
62
Ryan Keanefdebe6c2019-09-06 14:12:51 -070063 // Called when |connection| writing is blocked and unblocked, respectively.
64 // This call will be proxied to the Client set for this TlsConnection.
65 void OnWriteBlocked();
66 void OnWriteUnblocked();
67
68 // Called when |connection| experiences an error, such as a read error. This
69 // call will be proxied to the Client set for this TlsConnection.
70 void OnError(Error error);
71
72 // Called when a |packet| arrives on |socket|. This call will be proxied to
73 // the Client set for this TlsConnection.
74 void OnRead(std::vector<uint8_t> message);
Jordan Bayles1c785bd2019-08-15 10:32:33 -070075
76 private:
77 Client* client_;
78 TaskRunner* const task_runner_;
79
80 OSP_DISALLOW_COPY_AND_ASSIGN(TlsConnection);
81};
82
83} // namespace platform
84} // namespace openscreen
85
86#endif // PLATFORM_API_TLS_CONNECTION_H_