blob: 3f484d595bcdc59f4e56893719a3e1fae5fb5da9 [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.
36 virtual void OnError(TlsConnection* socket, Error error) = 0;
37
38 // Called when a |packet| arrives on |socket|.
39 virtual void OnRead(TlsConnection* socket,
40 std::vector<uint8_t> message) = 0;
41
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.
50 virtual const IPEndpoint& local_address() const = 0;
51
52 // Get the connected remote address.
53 virtual const IPEndpoint& remote_address() const = 0;
54
55 // Sets the client for this instance.
56 void set_client(Client* client) { client_ = client; }
57
58 protected:
59 explicit TlsConnection(TaskRunner* task_runner) : task_runner_(task_runner) {}
60
61 // TODO(jophba, rwkeane): Hide access to these behind local methods.
62 Client* client() const { return client_; }
63 TaskRunner* task_runner() const { return task_runner_; }
64
65 private:
66 Client* client_;
67 TaskRunner* const task_runner_;
68
69 OSP_DISALLOW_COPY_AND_ASSIGN(TlsConnection);
70};
71
72} // namespace platform
73} // namespace openscreen
74
75#endif // PLATFORM_API_TLS_CONNECTION_H_