forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_basic_stream_adapters.h
149 lines (117 loc) · 4.97 KB
/
websocket_basic_stream_adapters.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2018 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 NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_ADAPTERS_H_
#define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_ADAPTERS_H_
#include <memory>
#include "base/memory/weak_ptr.h"
#include "net/base/completion_callback.h"
#include "net/base/net_export.h"
#include "net/spdy/chromium/spdy_read_queue.h"
#include "net/spdy/chromium/spdy_stream.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/websockets/websocket_basic_stream.h"
namespace net {
class ClientSocketHandle;
class IOBuffer;
class SpdyBuffer;
// Trivial adapter to make WebSocketBasicStream use a TCP/IP or TLS socket.
class NET_EXPORT_PRIVATE WebSocketClientSocketHandleAdapter
: public WebSocketBasicStream::Adapter {
public:
WebSocketClientSocketHandleAdapter() = delete;
explicit WebSocketClientSocketHandleAdapter(
std::unique_ptr<ClientSocketHandle> connection);
~WebSocketClientSocketHandleAdapter() override;
int Read(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) override;
int Write(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback,
const NetworkTrafficAnnotationTag& traffic_annotation) override;
void Disconnect() override;
bool is_initialized() const override;
private:
std::unique_ptr<ClientSocketHandle> connection_;
};
// Adapter to make WebSocketBasicStream use an HTTP/2 stream.
// Sets itself as a delegate of the SpdyStream, and forwards headers-related
// methods to WebSocketHttp2HandshakeStream, which implements
// WebSocketSpdyStreamAdapter::Delegate. After the handshake, ownership of this
// object can be passed to WebSocketBasicStream, which can read and write using
// a ClientSocketHandle-like interface.
class NET_EXPORT_PRIVATE WebSocketSpdyStreamAdapter
: public WebSocketBasicStream::Adapter,
public SpdyStream::Delegate {
public:
// Interface for forwarding SpdyStream::Delegate methods necessary for the
// handshake.
class Delegate {
public:
virtual ~Delegate() = default;
virtual void OnHeadersSent() = 0;
virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0;
// Might destroy |this|.
virtual void OnClose(int status) = 0;
};
// |delegate| must be valid until DetachDelegate() is called.
WebSocketSpdyStreamAdapter(base::WeakPtr<SpdyStream> stream,
Delegate* delegate,
NetLogWithSource net_log);
~WebSocketSpdyStreamAdapter() override;
// Called by WebSocketSpdyStreamAdapter::Delegate before it is destroyed.
void DetachDelegate();
// WebSocketBasicStream::Adapter methods.
int Read(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) override;
// Write() must not be called before Delegate::OnHeadersSent() is called.
// Write() always returns asynchronously.
int Write(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback,
const NetworkTrafficAnnotationTag& traffic_annotation) override;
void Disconnect() override;
bool is_initialized() const override;
// SpdyStream::Delegate methods.
void OnHeadersSent() override;
void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override;
void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
void OnDataSent() override;
void OnTrailers(const SpdyHeaderBlock& trailers) override;
void OnClose(int status) override;
NetLogSource source_dependency() const override;
private:
// Copy data from read_data_ to read_buffer_.
int CopySavedReadDataIntoBuffer();
// Call WebSocketSpdyStreamAdapter::Delegate::OnClose().
void CallDelegateOnClose();
// True if SpdyStream::Delegate::OnHeadersSent() has been called.
// SpdyStream::SendData() must not be called before that.
bool headers_sent_;
// The underlying SpdyStream.
base::WeakPtr<SpdyStream> stream_;
// The error code with which SpdyStream was closed.
int stream_error_;
Delegate* delegate_;
// Buffer data pushed by SpdyStream until read through Read().
SpdyReadQueue read_data_;
// Read buffer and length used for both synchronous and asynchronous
// read operations.
IOBuffer* read_buffer_;
size_t read_length_;
// Read callback saved for asynchronous reads.
// Whenever |read_data_| is not empty, |read_callback_| must be null.
CompletionCallback read_callback_;
// Write length saved to be passed to |write_callback_|. This is necessary
// because SpdyStream::Delegate::OnDataSent() does not pass number of bytes
// written.
int write_length_;
// Write callback saved for asynchronous writes (all writes are asynchronous).
CompletionCallback write_callback_;
NetLogWithSource net_log_;
base::WeakPtrFactory<WebSocketSpdyStreamAdapter> weak_factory_;
};
} // namespace net
#endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_ADAPTERS_H_