OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #include "remoting/protocol/quic_channel.h" | |
6 | |
7 #include "base/callback_helpers.h" | |
8 #include "net/base/io_buffer.h" | |
9 #include "net/base/net_errors.h" | |
10 | |
11 namespace remoting { | |
12 namespace protocol { | |
13 | |
14 static const size_t kNamePrefixLength = 1; | |
15 static const size_t kMaxNameLength = 255; | |
16 | |
17 QuicChannel::QuicChannel(net::QuicP2PStream* stream, | |
18 const base::Closure& on_destroyed_callback) | |
19 : stream_(stream), on_destroyed_callback_(on_destroyed_callback) { | |
20 DCHECK(stream_); | |
21 stream_->SetDelegate(this); | |
22 } | |
23 | |
24 QuicChannel::~QuicChannel() { | |
25 stream_->SetDelegate(nullptr); | |
26 on_destroyed_callback_.Run(); | |
27 } | |
28 | |
29 int QuicChannel::Read(const scoped_refptr<net::IOBuffer>& buffer, | |
30 int buffer_len, | |
31 const net::CompletionCallback& callback) { | |
32 DCHECK(read_callback_.is_null()); | |
33 | |
34 if (error_) | |
dcaiafa
2015/08/10 23:31:36
(error_ != net::OK) for consistency with QuicChann
Sergey Ulanov
2015/08/11 21:34:00
Done.
| |
35 return error_; | |
36 | |
37 if (data_received_.total_bytes() == 0) { | |
38 read_buffer_ = buffer; | |
39 read_buffer_size_ = buffer_len; | |
40 read_callback_ = callback; | |
41 return net::ERR_IO_PENDING; | |
42 } | |
43 | |
44 int result = std::min(buffer_len, data_received_.total_bytes()); | |
45 data_received_.CopyTo(buffer->data(), result); | |
46 data_received_.CropFront(result); | |
47 return result; | |
48 } | |
49 | |
50 int QuicChannel::Write(const scoped_refptr<net::IOBuffer>& buffer, | |
51 int buffer_len, | |
52 const net::CompletionCallback& callback) { | |
53 if (error_ != net::OK) | |
54 return error_; | |
55 | |
56 return stream_->Write(base::StringPiece(buffer->data(), buffer_len), | |
57 callback); | |
58 } | |
59 | |
60 void QuicChannel::SetName(const std::string& name) { | |
61 DCHECK(name_.empty()); | |
62 | |
63 name_ = name; | |
64 } | |
65 | |
66 void QuicChannel::OnDataReceived(const char* data, int length) { | |
67 if (read_callback_.is_null()) { | |
68 data_received_.AppendCopyOf(data, length); | |
69 return; | |
70 } | |
71 | |
72 DCHECK_EQ(data_received_.total_bytes(), 0); | |
73 int bytes_to_read = std::min(length, read_buffer_size_); | |
74 memcpy(read_buffer_->data(), data, bytes_to_read); | |
75 read_buffer_ = nullptr; | |
76 | |
77 // Copy leftover data to |data_received_|. | |
78 if (length > bytes_to_read) | |
79 data_received_.AppendCopyOf(data + bytes_to_read, length - bytes_to_read); | |
80 | |
81 base::ResetAndReturn(&read_callback_).Run(bytes_to_read); | |
82 } | |
83 | |
84 void QuicChannel::OnClose(net::QuicErrorCode error) { | |
85 error_ = (error == net::QUIC_NO_ERROR) ? net::ERR_CONNECTION_CLOSED | |
86 : net::ERR_QUIC_PROTOCOL_ERROR; | |
87 if (!read_callback_.is_null()) { | |
88 base::ResetAndReturn(&read_callback_).Run(error_); | |
89 } | |
90 } | |
91 | |
92 QuicClientChannel::QuicClientChannel(net::QuicP2PStream* stream, | |
93 const base::Closure& on_destroyed_callback, | |
94 const std::string& name) | |
95 : QuicChannel(stream, on_destroyed_callback) { | |
96 CHECK_LE(name.size(), kMaxNameLength); | |
97 | |
98 SetName(name); | |
99 | |
100 // Send the name to the host. | |
101 stream_->WriteHeader( | |
102 std::string(kNamePrefixLength, static_cast<char>(name.size())) + name); | |
103 } | |
104 | |
105 QuicClientChannel::~QuicClientChannel() {} | |
106 | |
107 QuicServerChannel::QuicServerChannel( | |
108 net::QuicP2PStream* stream, | |
109 const base::Closure& on_destroyed_callback) | |
110 : QuicChannel(stream, on_destroyed_callback) {} | |
111 | |
112 void QuicServerChannel::ReceiveName( | |
113 const base::Closure& name_received_callback) { | |
114 name_received_callback_ = name_received_callback; | |
115 | |
116 // First read 1 byte containing name length. | |
117 name_read_buffer_ = new net::DrainableIOBuffer( | |
118 new net::IOBuffer(kNamePrefixLength), kNamePrefixLength); | |
119 int result = Read(name_read_buffer_, kNamePrefixLength, | |
120 base::Bind(&QuicServerChannel::OnNameSizeReadResult, | |
121 base::Unretained(this))); | |
122 if (result != net::ERR_IO_PENDING) | |
123 OnNameSizeReadResult(result); | |
124 } | |
125 | |
126 QuicServerChannel::~QuicServerChannel() {} | |
127 | |
128 void QuicServerChannel::OnNameSizeReadResult(int result) { | |
129 if (result < 0) { | |
130 base::ResetAndReturn(&name_received_callback_).Run(); | |
131 return; | |
132 } | |
133 | |
134 DCHECK_EQ(result, static_cast<int>(kNamePrefixLength)); | |
135 name_length_ = *reinterpret_cast<uint8_t*>(name_read_buffer_->data()); | |
136 name_read_buffer_ = | |
137 new net::DrainableIOBuffer(new net::IOBuffer(name_length_), name_length_); | |
138 ReadNameLoop(0); | |
139 } | |
140 | |
141 void QuicServerChannel::ReadNameLoop(int result) { | |
142 while (result >= 0 && name_read_buffer_->BytesRemaining() > 0) { | |
143 result = Read(name_read_buffer_, name_read_buffer_->BytesRemaining(), | |
144 base::Bind(&QuicServerChannel::OnNameReadResult, | |
145 base::Unretained(this))); | |
146 if (result >= 0) { | |
147 name_read_buffer_->DidConsume(result); | |
148 } | |
149 } | |
150 | |
151 if (result < 0 && result != net::ERR_IO_PENDING) { | |
152 // Failed to read name for the stream. | |
153 base::ResetAndReturn(&name_received_callback_).Run(); | |
154 return; | |
155 } | |
156 | |
157 if (name_read_buffer_->BytesRemaining() == 0) { | |
158 name_read_buffer_->SetOffset(0); | |
159 SetName(std::string(name_read_buffer_->data(), | |
160 name_read_buffer_->data() + name_length_)); | |
161 base::ResetAndReturn(&name_received_callback_).Run(); | |
162 } | |
163 } | |
164 | |
165 void QuicServerChannel::OnNameReadResult(int result) { | |
166 if (result > 0) | |
167 name_read_buffer_->DidConsume(result); | |
168 | |
169 ReadNameLoop(result); | |
170 } | |
171 | |
172 } // namespace protocol | |
173 } // namespace remoting | |
OLD | NEW |