blob: a8fa3c4867e000d83c0d996cfa3a54b42e3455c7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CAST_COMMON_CHANNEL_CAST_SOCKET_H_
#define CAST_COMMON_CHANNEL_CAST_SOCKET_H_
#include <vector>
#include "platform/api/tls_connection.h"
namespace cast {
namespace channel {
using openscreen::Error;
using TlsConnection = openscreen::platform::TlsConnection;
class CastMessage;
// Represents a simple message-oriented socket for communicating with the Cast
// V2 protocol. It isn't thread-safe, so it should only be used on the same
// TaskRunner thread as its TlsConnection.
class CastSocket : public TlsConnection::Client {
public:
class Client {
public:
virtual ~Client() = default;
// Called when a terminal error on |socket| has occurred.
virtual void OnError(CastSocket* socket, Error error) = 0;
virtual void OnMessage(CastSocket* socket, CastMessage message) = 0;
};
CastSocket(std::unique_ptr<TlsConnection> connection,
Client* client,
uint32_t socket_id);
~CastSocket();
// Sends |message| immediately unless the underlying TLS connection is
// write-blocked, in which case |message| will be queued. No error is
// returned for both queueing and successful sending. An error will be
// returned if |message| cannot be serialized for any reason.
Error SendMessage(const CastMessage& message);
void set_client(Client* client) {
OSP_DCHECK(client);
client_ = client;
}
uint32_t socket_id() const { return socket_id_; }
// TlsConnection::Client overrides.
void OnWriteBlocked(TlsConnection* connection) override;
void OnWriteUnblocked(TlsConnection* connection) override;
void OnError(TlsConnection* connection, Error error) override;
void OnRead(TlsConnection* connection, std::vector<uint8_t> block) override;
private:
enum class State {
kOpen,
kBlocked,
kError,
};
Client* client_;
const std::unique_ptr<TlsConnection> connection_;
std::vector<uint8_t> read_buffer_;
const uint32_t socket_id_;
State state_ = State::kOpen;
std::vector<std::vector<uint8_t>> message_queue_;
};
} // namespace channel
} // namespace cast
#endif // CAST_COMMON_CHANNEL_CAST_SOCKET_H_
|