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 "ipc/ipc_sender.h" | 9 #include "ipc/ipc_sender.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 VLOG(1) << "Local address: " << address.ToString(); | 106 VLOG(1) << "Local address: " << address.ToString(); |
107 state_ = STATE_OPEN; | 107 state_ = STATE_OPEN; |
108 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); | 108 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); |
109 DoRead(); | 109 DoRead(); |
110 } | 110 } |
111 | 111 |
112 void P2PSocketHostTcpBase::DoRead() { | 112 void P2PSocketHostTcpBase::DoRead() { |
113 int result; | 113 int result; |
114 do { | 114 do { |
115 if (!read_buffer_) { | 115 if (!read_buffer_.get()) { |
116 read_buffer_ = new net::GrowableIOBuffer(); | 116 read_buffer_ = new net::GrowableIOBuffer(); |
117 read_buffer_->SetCapacity(kReadBufferSize); | 117 read_buffer_->SetCapacity(kReadBufferSize); |
118 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { | 118 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { |
119 // Make sure that we always have at least kReadBufferSize of | 119 // Make sure that we always have at least kReadBufferSize of |
120 // remaining capacity in the read buffer. Normally all packets | 120 // remaining capacity in the read buffer. Normally all packets |
121 // are smaller than kReadBufferSize, so this is not really | 121 // are smaller than kReadBufferSize, so this is not really |
122 // required. | 122 // required. |
123 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize - | 123 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize - |
124 read_buffer_->RemainingCapacity()); | 124 read_buffer_->RemainingCapacity()); |
125 } | 125 } |
126 result = socket_->Read(read_buffer_, read_buffer_->RemainingCapacity(), | 126 result = socket_->Read( |
127 base::Bind(&P2PSocketHostTcp::OnRead, | 127 read_buffer_.get(), |
128 base::Unretained(this))); | 128 read_buffer_->RemainingCapacity(), |
| 129 base::Bind(&P2PSocketHostTcp::OnRead, base::Unretained(this))); |
129 DidCompleteRead(result); | 130 DidCompleteRead(result); |
130 } while (result > 0); | 131 } while (result > 0); |
131 } | 132 } |
132 | 133 |
133 void P2PSocketHostTcpBase::OnRead(int result) { | 134 void P2PSocketHostTcpBase::OnRead(int result) { |
134 DidCompleteRead(result); | 135 DidCompleteRead(result); |
135 if (state_ == STATE_OPEN) { | 136 if (state_ == STATE_OPEN) { |
136 DoRead(); | 137 DoRead(); |
137 } | 138 } |
138 } | 139 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 OnError(); | 181 OnError(); |
181 return; | 182 return; |
182 } | 183 } |
183 } | 184 } |
184 | 185 |
185 DoSend(to, data); | 186 DoSend(to, data); |
186 } | 187 } |
187 | 188 |
188 void P2PSocketHostTcpBase::WriteOrQueue( | 189 void P2PSocketHostTcpBase::WriteOrQueue( |
189 scoped_refptr<net::DrainableIOBuffer>& buffer) { | 190 scoped_refptr<net::DrainableIOBuffer>& buffer) { |
190 if (write_buffer_) { | 191 if (write_buffer_.get()) { |
191 write_queue_.push(buffer); | 192 write_queue_.push(buffer); |
192 return; | 193 return; |
193 } | 194 } |
194 | 195 |
195 write_buffer_ = buffer; | 196 write_buffer_ = buffer; |
196 DoWrite(); | 197 DoWrite(); |
197 } | 198 } |
198 | 199 |
199 void P2PSocketHostTcpBase::DoWrite() { | 200 void P2PSocketHostTcpBase::DoWrite() { |
200 while (write_buffer_ && state_ == STATE_OPEN && !write_pending_) { | 201 while (write_buffer_.get() && state_ == STATE_OPEN && !write_pending_) { |
201 int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), | 202 int result = socket_->Write( |
202 base::Bind(&P2PSocketHostTcp::OnWritten, | 203 write_buffer_.get(), |
203 base::Unretained(this))); | 204 write_buffer_->BytesRemaining(), |
| 205 base::Bind(&P2PSocketHostTcp::OnWritten, base::Unretained(this))); |
204 HandleWriteResult(result); | 206 HandleWriteResult(result); |
205 } | 207 } |
206 } | 208 } |
207 | 209 |
208 void P2PSocketHostTcpBase::OnWritten(int result) { | 210 void P2PSocketHostTcpBase::OnWritten(int result) { |
209 DCHECK(write_pending_); | 211 DCHECK(write_pending_); |
210 DCHECK_NE(result, net::ERR_IO_PENDING); | 212 DCHECK_NE(result, net::ERR_IO_PENDING); |
211 | 213 |
212 write_pending_ = false; | 214 write_pending_ = false; |
213 HandleWriteResult(result); | 215 HandleWriteResult(result); |
214 DoWrite(); | 216 DoWrite(); |
215 } | 217 } |
216 | 218 |
217 void P2PSocketHostTcpBase::HandleWriteResult(int result) { | 219 void P2PSocketHostTcpBase::HandleWriteResult(int result) { |
218 DCHECK(write_buffer_); | 220 DCHECK(write_buffer_.get()); |
219 if (result >= 0) { | 221 if (result >= 0) { |
220 write_buffer_->DidConsume(result); | 222 write_buffer_->DidConsume(result); |
221 if (write_buffer_->BytesRemaining() == 0) { | 223 if (write_buffer_->BytesRemaining() == 0) { |
222 message_sender_->Send(new P2PMsg_OnSendComplete(id_)); | 224 message_sender_->Send(new P2PMsg_OnSendComplete(id_)); |
223 if (write_queue_.empty()) { | 225 if (write_queue_.empty()) { |
224 write_buffer_ = NULL; | 226 write_buffer_ = NULL; |
225 } else { | 227 } else { |
226 write_buffer_ = write_queue_.front(); | 228 write_buffer_ = write_queue_.front(); |
227 write_queue_.pop(); | 229 write_queue_.pop(); |
228 } | 230 } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 } else { | 388 } else { |
387 packet_size += kTurnChannelDataHeaderSize; | 389 packet_size += kTurnChannelDataHeaderSize; |
388 // Calculate any padding if present. | 390 // Calculate any padding if present. |
389 if (packet_size % 4) | 391 if (packet_size % 4) |
390 *pad_bytes = 4 - packet_size % 4; | 392 *pad_bytes = 4 - packet_size % 4; |
391 } | 393 } |
392 return packet_size; | 394 return packet_size; |
393 } | 395 } |
394 | 396 |
395 } // namespace content | 397 } // namespace content |
OLD | NEW |