OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
6 | 6 |
7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
8 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 VLOG(1) << "Local address: " << address.ToString(); | 102 VLOG(1) << "Local address: " << address.ToString(); |
103 state_ = STATE_OPEN; | 103 state_ = STATE_OPEN; |
104 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); | 104 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); |
105 DoRead(); | 105 DoRead(); |
106 } | 106 } |
107 | 107 |
108 void P2PSocketHostTcp::DoRead() { | 108 void P2PSocketHostTcp::DoRead() { |
109 int result; | 109 int result; |
110 do { | 110 do { |
111 if (!read_buffer_) { | 111 if (!read_buffer_.get()) { |
112 read_buffer_ = new net::GrowableIOBuffer(); | 112 read_buffer_ = new net::GrowableIOBuffer(); |
113 read_buffer_->SetCapacity(kReadBufferSize); | 113 read_buffer_->SetCapacity(kReadBufferSize); |
114 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { | 114 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { |
115 // Make sure that we always have at least kReadBufferSize of | 115 // Make sure that we always have at least kReadBufferSize of |
116 // remaining capacity in the read buffer. Normally all packets | 116 // remaining capacity in the read buffer. Normally all packets |
117 // are smaller than kReadBufferSize, so this is not really | 117 // are smaller than kReadBufferSize, so this is not really |
118 // required. | 118 // required. |
119 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize - | 119 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize - |
120 read_buffer_->RemainingCapacity()); | 120 read_buffer_->RemainingCapacity()); |
121 } | 121 } |
122 result = socket_->Read(read_buffer_, read_buffer_->RemainingCapacity(), | 122 result = socket_-> |
123 base::Bind(&P2PSocketHostTcp::OnRead, | 123 Read(read_buffer_.get(), read_buffer_->RemainingCapacity(), |
124 base::Unretained(this))); | 124 base::Bind(&P2PSocketHostTcp::OnRead, |
| 125 base::Unretained(this))); |
125 DidCompleteRead(result); | 126 DidCompleteRead(result); |
126 } while (result > 0); | 127 } while (result > 0); |
127 } | 128 } |
128 | 129 |
129 void P2PSocketHostTcp::OnRead(int result) { | 130 void P2PSocketHostTcp::OnRead(int result) { |
130 DidCompleteRead(result); | 131 DidCompleteRead(result); |
131 if (state_ == STATE_OPEN) { | 132 if (state_ == STATE_OPEN) { |
132 DoRead(); | 133 DoRead(); |
133 } | 134 } |
134 } | 135 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 } | 184 } |
184 | 185 |
185 void P2PSocketHostTcp::Send(const net::IPEndPoint& to, | 186 void P2PSocketHostTcp::Send(const net::IPEndPoint& to, |
186 const std::vector<char>& data) { | 187 const std::vector<char>& data) { |
187 if (!socket_.get()) { | 188 if (!socket_.get()) { |
188 // The Send message may be sent after the an OnError message was | 189 // The Send message may be sent after the an OnError message was |
189 // sent by hasn't been processed the renderer. | 190 // sent by hasn't been processed the renderer. |
190 return; | 191 return; |
191 } | 192 } |
192 | 193 |
193 if (write_buffer_) { | 194 if (write_buffer_.get()) { |
194 // Silently drop packet if we haven't finished sending previous | 195 // Silently drop packet if we haven't finished sending previous |
195 // packet. | 196 // packet. |
196 VLOG(1) << "Dropping TCP packet."; | 197 VLOG(1) << "Dropping TCP packet."; |
197 return; | 198 return; |
198 } | 199 } |
199 | 200 |
200 if (!(to == remote_address_)) { | 201 if (!(to == remote_address_)) { |
201 // Renderer should use this socket only to send data to | 202 // Renderer should use this socket only to send data to |
202 // |remote_address_|. | 203 // |remote_address_|. |
203 NOTREACHED(); | 204 NOTREACHED(); |
(...skipping 16 matching lines...) Expand all Loading... |
220 write_buffer_ = new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 221 write_buffer_ = new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
221 *reinterpret_cast<uint16*>(write_buffer_->data()) = | 222 *reinterpret_cast<uint16*>(write_buffer_->data()) = |
222 base::HostToNet16(data.size()); | 223 base::HostToNet16(data.size()); |
223 memcpy(write_buffer_->data() + kPacketHeaderSize, &data[0], data.size()); | 224 memcpy(write_buffer_->data() + kPacketHeaderSize, &data[0], data.size()); |
224 | 225 |
225 DoWrite(); | 226 DoWrite(); |
226 } | 227 } |
227 | 228 |
228 void P2PSocketHostTcp::DoWrite() { | 229 void P2PSocketHostTcp::DoWrite() { |
229 while (true) { | 230 while (true) { |
230 int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), | 231 int result = socket_-> |
231 base::Bind(&P2PSocketHostTcp::OnWritten, | 232 Write(write_buffer_.get(), write_buffer_->BytesRemaining(), |
232 base::Unretained(this))); | 233 base::Bind(&P2PSocketHostTcp::OnWritten, |
| 234 base::Unretained(this))); |
233 if (result >= 0) { | 235 if (result >= 0) { |
234 write_buffer_->DidConsume(result); | 236 write_buffer_->DidConsume(result); |
235 if (write_buffer_->BytesRemaining() == 0) { | 237 if (write_buffer_->BytesRemaining() == 0) { |
236 write_buffer_ = NULL; | 238 write_buffer_ = NULL; |
237 break; | 239 break; |
238 } | 240 } |
239 } else { | 241 } else { |
240 if (result != net::ERR_IO_PENDING) { | 242 if (result != net::ERR_IO_PENDING) { |
241 LOG(ERROR) << "Error when sending data in TCP socket: " << result; | 243 LOG(ERROR) << "Error when sending data in TCP socket: " << result; |
242 OnError(); | 244 OnError(); |
(...skipping 23 matching lines...) Expand all Loading... |
266 } | 268 } |
267 | 269 |
268 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( | 270 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( |
269 const net::IPEndPoint& remote_address, int id) { | 271 const net::IPEndPoint& remote_address, int id) { |
270 NOTREACHED(); | 272 NOTREACHED(); |
271 OnError(); | 273 OnError(); |
272 return NULL; | 274 return NULL; |
273 } | 275 } |
274 | 276 |
275 } // namespace content | 277 } // namespace content |
OLD | NEW |