Chromium Code Reviews| 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_udp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_udp.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | |
| 8 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" | |
| 12 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 14 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" | 16 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" |
| 15 #include "content/common/p2p_messages.h" | 17 #include "content/common/p2p_messages.h" |
| 16 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
| 17 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
| 18 #include "ipc/ipc_sender.h" | 20 #include "ipc/ipc_sender.h" |
| 19 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 20 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 21 #include "third_party/webrtc/media/base/rtputils.h" | 23 #include "third_party/webrtc/media/base/rtputils.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 id(id) { | 85 id(id) { |
| 84 memcpy(data->data(), &content[0], size); | 86 memcpy(data->data(), &content[0], size); |
| 85 } | 87 } |
| 86 | 88 |
| 87 P2PSocketHostUdp::PendingPacket::PendingPacket(const PendingPacket& other) = | 89 P2PSocketHostUdp::PendingPacket::PendingPacket(const PendingPacket& other) = |
| 88 default; | 90 default; |
| 89 | 91 |
| 90 P2PSocketHostUdp::PendingPacket::~PendingPacket() { | 92 P2PSocketHostUdp::PendingPacket::~PendingPacket() { |
| 91 } | 93 } |
| 92 | 94 |
| 95 P2PSocketHostUdp::P2PSocketHostUdp( | |
| 96 IPC::Sender* message_sender, | |
| 97 int socket_id, | |
| 98 P2PMessageThrottler* throttler, | |
| 99 const DatagramServerSocketFactory& socket_factory) | |
| 100 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::UDP), | |
| 101 socket_(socket_factory.Run()), | |
| 102 send_pending_(false), | |
| 103 last_dscp_(net::DSCP_CS0), | |
| 104 throttler_(throttler), | |
| 105 send_buffer_size_(0), | |
| 106 socket_factory_(socket_factory) {} | |
| 107 | |
| 93 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, | 108 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, |
| 94 int socket_id, | 109 int socket_id, |
| 95 P2PMessageThrottler* throttler) | 110 P2PMessageThrottler* throttler) |
| 96 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::UDP), | 111 : P2PSocketHostUdp(message_sender, |
| 97 send_pending_(false), | 112 socket_id, |
| 98 last_dscp_(net::DSCP_CS0), | 113 throttler, |
| 99 throttler_(throttler), | 114 base::Bind(&P2PSocketHostUdp::DefaultSocketFactory)) {} |
| 100 send_buffer_size_(0) { | |
| 101 net::UDPServerSocket* socket = new net::UDPServerSocket( | |
| 102 GetContentClient()->browser()->GetNetLog(), net::NetLog::Source()); | |
| 103 #if defined(OS_WIN) | |
| 104 socket->UseNonBlockingIO(); | |
| 105 #endif | |
| 106 socket_.reset(socket); | |
| 107 } | |
| 108 | 115 |
| 109 P2PSocketHostUdp::~P2PSocketHostUdp() { | 116 P2PSocketHostUdp::~P2PSocketHostUdp() { |
| 110 if (state_ == STATE_OPEN) { | 117 if (state_ == STATE_OPEN) { |
| 111 DCHECK(socket_.get()); | 118 DCHECK(socket_.get()); |
| 112 socket_.reset(); | 119 socket_.reset(); |
| 113 } | 120 } |
| 114 } | 121 } |
| 115 | 122 |
| 116 void P2PSocketHostUdp::SetSendBufferSize() { | 123 void P2PSocketHostUdp::SetSendBufferSize() { |
| 117 unsigned int send_buffer_size = 0; | 124 unsigned int send_buffer_size = 0; |
| 118 | 125 |
| 119 base::StringToUint( | 126 base::StringToUint( |
| 120 base::FieldTrialList::FindFullName("WebRTC-SystemUDPSendSocketSize"), | 127 base::FieldTrialList::FindFullName("WebRTC-SystemUDPSendSocketSize"), |
| 121 &send_buffer_size); | 128 &send_buffer_size); |
| 122 | 129 |
| 123 if (send_buffer_size > 0) { | 130 if (send_buffer_size > 0) { |
| 124 if (!SetOption(P2P_SOCKET_OPT_SNDBUF, send_buffer_size)) { | 131 if (!SetOption(P2P_SOCKET_OPT_SNDBUF, send_buffer_size)) { |
| 125 LOG(WARNING) << "Failed to set socket send buffer size to " | 132 LOG(WARNING) << "Failed to set socket send buffer size to " |
| 126 << send_buffer_size; | 133 << send_buffer_size; |
| 127 } else { | 134 } else { |
| 128 send_buffer_size_ = send_buffer_size; | 135 send_buffer_size_ = send_buffer_size; |
| 129 } | 136 } |
| 130 } | 137 } |
| 131 } | 138 } |
| 132 | 139 |
| 133 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, | 140 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, |
| 141 uint16_t min_port, | |
| 142 uint16_t max_port, | |
| 134 const P2PHostAndIPEndPoint& remote_address) { | 143 const P2PHostAndIPEndPoint& remote_address) { |
| 135 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 144 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 145 DCHECK(local_address.port() == 0 || (min_port == 0 && max_port == 0)); | |
|
tommi (sloooow) - chröme
2016/07/11 17:54:59
what if e.g. local_address.port() != 0 but the pol
Guido Urdaneta
2016/07/12 10:15:00
The original PS was not treating the range as poli
| |
| 136 | 146 |
| 137 int result = socket_->Listen(local_address); | 147 int result = -1; |
| 148 if (min_port == 0) { | |
|
tommi (sloooow) - chröme
2016/07/11 17:54:59
maybe this should be:
if (min_port == 0 || local_
Guido Urdaneta
2016/07/12 10:15:00
Done.
| |
| 149 result = socket_->Listen(local_address); | |
| 150 } else { | |
| 151 for (unsigned port = min_port; port <= max_port && result < 0; ++port) { | |
| 152 result = socket_->Listen(net::IPEndPoint(local_address.address(), port)); | |
|
tommi (sloooow) - chröme
2016/07/11 17:54:59
otherwise, here we ignore local_address.port() if
Guido Urdaneta
2016/07/12 10:15:00
Done.
| |
| 153 if (result < 0 && port != max_port) | |
| 154 socket_ = socket_factory_.Run(); | |
| 155 } | |
| 156 } | |
| 138 if (result < 0) { | 157 if (result < 0) { |
| 139 LOG(ERROR) << "bind() to " << local_address.ToString() | 158 LOG(ERROR) << "bind() to " << local_address.address().ToString() |
| 159 << (min_port == 0 | |
| 160 ? base::StringPrintf(":%d", local_address.port()) | |
| 161 : base::StringPrintf(", port range [%d-%d]", min_port, | |
| 162 max_port)) | |
| 140 << " failed: " << result; | 163 << " failed: " << result; |
| 141 OnError(); | 164 OnError(); |
| 142 return false; | 165 return false; |
| 143 } | 166 } |
| 144 | 167 |
| 145 // Setting recv socket buffer size. | 168 // Setting recv socket buffer size. |
| 146 if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) { | 169 if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) { |
| 147 LOG(WARNING) << "Failed to set socket receive buffer size to " | 170 LOG(WARNING) << "Failed to set socket receive buffer size to " |
| 148 << kRecvSocketBufferSize; | 171 << kRecvSocketBufferSize; |
| 149 } | 172 } |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 return socket_->SetSendBufferSize(value) == net::OK; | 416 return socket_->SetSendBufferSize(value) == net::OK; |
| 394 case P2P_SOCKET_OPT_DSCP: | 417 case P2P_SOCKET_OPT_DSCP: |
| 395 return (net::OK == socket_->SetDiffServCodePoint( | 418 return (net::OK == socket_->SetDiffServCodePoint( |
| 396 static_cast<net::DiffServCodePoint>(value))) ? true : false; | 419 static_cast<net::DiffServCodePoint>(value))) ? true : false; |
| 397 default: | 420 default: |
| 398 NOTREACHED(); | 421 NOTREACHED(); |
| 399 return false; | 422 return false; |
| 400 } | 423 } |
| 401 } | 424 } |
| 402 | 425 |
| 426 // static | |
| 427 std::unique_ptr<net::DatagramServerSocket> | |
| 428 P2PSocketHostUdp::DefaultSocketFactory() { | |
| 429 net::UDPServerSocket* socket = new net::UDPServerSocket( | |
| 430 GetContentClient()->browser()->GetNetLog(), net::NetLog::Source()); | |
| 431 #if defined(OS_WIN) | |
| 432 socket->UseNonBlockingIO(); | |
| 433 #endif | |
| 434 | |
| 435 return base::WrapUnique(socket); | |
| 436 } | |
| 437 | |
| 403 } // namespace content | 438 } // namespace content |
| OLD | NEW |