OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/p2p_messages.h" | 7 #include "content/common/p2p_messages.h" |
8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
11 #include "net/base/sys_byteorder.h" | 11 #include "net/base/sys_byteorder.h" |
12 #include "net/socket/tcp_client_socket.h" | 12 #include "net/socket/tcp_client_socket.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 const int kReadBufferSize = 4096; | 15 const int kReadBufferSize = 4096; |
16 const int kPacketHeaderSize = sizeof(uint16); | 16 const int kPacketHeaderSize = sizeof(uint16); |
17 } // namespace | 17 } // namespace |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Message::Sender* message_sender, | 21 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Message::Sender* message_sender, |
22 int routing_id, int id) | 22 int routing_id, int id) |
23 : P2PSocketHost(message_sender, routing_id, id), | 23 : P2PSocketHost(message_sender, routing_id, id), |
24 connected_(false), | 24 connected_(false) { |
25 ALLOW_THIS_IN_INITIALIZER_LIST( | |
26 connect_callback_(this, &P2PSocketHostTcp::OnConnected)), | |
27 ALLOW_THIS_IN_INITIALIZER_LIST( | |
28 read_callback_(this, &P2PSocketHostTcp::OnRead)), | |
29 ALLOW_THIS_IN_INITIALIZER_LIST( | |
30 write_callback_(this, &P2PSocketHostTcp::OnWritten)) { | |
31 } | 25 } |
32 | 26 |
33 P2PSocketHostTcp::~P2PSocketHostTcp() { | 27 P2PSocketHostTcp::~P2PSocketHostTcp() { |
34 if (state_ == STATE_OPEN) { | 28 if (state_ == STATE_OPEN) { |
35 DCHECK(socket_.get()); | 29 DCHECK(socket_.get()); |
36 socket_.reset(); | 30 socket_.reset(); |
37 } | 31 } |
38 } | 32 } |
39 | 33 |
40 bool P2PSocketHostTcp::InitAccepted(const net::IPEndPoint& remote_address, | 34 bool P2PSocketHostTcp::InitAccepted(const net::IPEndPoint& remote_address, |
(...skipping 20 matching lines...) Expand all Loading... |
61 NULL, net::NetLog::Source())); | 55 NULL, net::NetLog::Source())); |
62 if (tcp_socket->Bind(local_address) != net::OK) { | 56 if (tcp_socket->Bind(local_address) != net::OK) { |
63 OnError(); | 57 OnError(); |
64 return false; | 58 return false; |
65 } | 59 } |
66 socket_.reset(tcp_socket.release()); | 60 socket_.reset(tcp_socket.release()); |
67 | 61 |
68 if (socket_->SetSendBufferSize(kMaxSendBufferSize)) | 62 if (socket_->SetSendBufferSize(kMaxSendBufferSize)) |
69 LOG(WARNING) << "Failed to set send buffer size for TCP socket."; | 63 LOG(WARNING) << "Failed to set send buffer size for TCP socket."; |
70 | 64 |
71 int result = socket_->Connect(&connect_callback_); | 65 int result = socket_->Connect( |
| 66 base::Bind(&P2PSocketHostTcp::OnConnected, base::Unretained(this))); |
72 if (result != net::ERR_IO_PENDING) { | 67 if (result != net::ERR_IO_PENDING) { |
73 OnConnected(result); | 68 OnConnected(result); |
74 } | 69 } |
75 | 70 |
76 return state_ != STATE_ERROR; | 71 return state_ != STATE_ERROR; |
77 } | 72 } |
78 | 73 |
79 void P2PSocketHostTcp::OnError() { | 74 void P2PSocketHostTcp::OnError() { |
80 socket_.reset(); | 75 socket_.reset(); |
81 | 76 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 read_buffer_->SetCapacity(kReadBufferSize); | 113 read_buffer_->SetCapacity(kReadBufferSize); |
119 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { | 114 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { |
120 // Make sure that we always have at least kReadBufferSize of | 115 // Make sure that we always have at least kReadBufferSize of |
121 // remaining capacity in the read buffer. Normally all packets | 116 // remaining capacity in the read buffer. Normally all packets |
122 // are smaller than kReadBufferSize, so this is not really | 117 // are smaller than kReadBufferSize, so this is not really |
123 // required. | 118 // required. |
124 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize - | 119 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize - |
125 read_buffer_->RemainingCapacity()); | 120 read_buffer_->RemainingCapacity()); |
126 } | 121 } |
127 result = socket_->Read(read_buffer_, read_buffer_->RemainingCapacity(), | 122 result = socket_->Read(read_buffer_, read_buffer_->RemainingCapacity(), |
128 &read_callback_); | 123 base::Bind(&P2PSocketHostTcp::OnRead, |
| 124 base::Unretained(this))); |
129 DidCompleteRead(result); | 125 DidCompleteRead(result); |
130 } while (result > 0); | 126 } while (result > 0); |
131 } | 127 } |
132 | 128 |
133 void P2PSocketHostTcp::OnRead(int result) { | 129 void P2PSocketHostTcp::OnRead(int result) { |
134 DidCompleteRead(result); | 130 DidCompleteRead(result); |
135 if (state_ == STATE_OPEN) { | 131 if (state_ == STATE_OPEN) { |
136 DoRead(); | 132 DoRead(); |
137 } | 133 } |
138 } | 134 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 write_buffer_ = new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 221 write_buffer_ = new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
226 *reinterpret_cast<uint16*>(write_buffer_->data()) = htons(data.size()); | 222 *reinterpret_cast<uint16*>(write_buffer_->data()) = htons(data.size()); |
227 memcpy(write_buffer_->data() + kPacketHeaderSize, &data[0], data.size()); | 223 memcpy(write_buffer_->data() + kPacketHeaderSize, &data[0], data.size()); |
228 | 224 |
229 DoWrite(); | 225 DoWrite(); |
230 } | 226 } |
231 | 227 |
232 void P2PSocketHostTcp::DoWrite() { | 228 void P2PSocketHostTcp::DoWrite() { |
233 while (true) { | 229 while (true) { |
234 int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), | 230 int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), |
235 &write_callback_); | 231 base::Bind(&P2PSocketHostTcp::OnWritten, |
| 232 base::Unretained(this))); |
236 if (result >= 0) { | 233 if (result >= 0) { |
237 write_buffer_->DidConsume(result); | 234 write_buffer_->DidConsume(result); |
238 if (write_buffer_->BytesRemaining() == 0) { | 235 if (write_buffer_->BytesRemaining() == 0) { |
239 write_buffer_ = NULL; | 236 write_buffer_ = NULL; |
240 break; | 237 break; |
241 } | 238 } |
242 } else { | 239 } else { |
243 if (result != net::ERR_IO_PENDING) { | 240 if (result != net::ERR_IO_PENDING) { |
244 LOG(ERROR) << "Error when sending data in TCP socket: " << result; | 241 LOG(ERROR) << "Error when sending data in TCP socket: " << result; |
245 OnError(); | 242 OnError(); |
(...skipping 23 matching lines...) Expand all Loading... |
269 } | 266 } |
270 | 267 |
271 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( | 268 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( |
272 const net::IPEndPoint& remote_address, int id) { | 269 const net::IPEndPoint& remote_address, int id) { |
273 NOTREACHED(); | 270 NOTREACHED(); |
274 OnError(); | 271 OnError(); |
275 return NULL; | 272 return NULL; |
276 } | 273 } |
277 | 274 |
278 } // namespace content | 275 } // namespace content |
OLD | NEW |