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((min_port == 0 && max_port == 0) || min_port > 0); |
| 146 DCHECK_LE(min_port, max_port); |
136 | 147 |
137 int result = socket_->Listen(local_address); | 148 int result = -1; |
| 149 if (min_port == 0) { |
| 150 result = socket_->Listen(local_address); |
| 151 } else if (local_address.port() == 0) { |
| 152 for (unsigned port = min_port; port <= max_port && result < 0; ++port) { |
| 153 result = socket_->Listen(net::IPEndPoint(local_address.address(), port)); |
| 154 if (result < 0 && port != max_port) |
| 155 socket_ = socket_factory_.Run(); |
| 156 } |
| 157 } else if (local_address.port() >= min_port && |
| 158 local_address.port() <= max_port) { |
| 159 result = socket_->Listen(local_address); |
| 160 } |
138 if (result < 0) { | 161 if (result < 0) { |
139 LOG(ERROR) << "bind() to " << local_address.ToString() | 162 LOG(ERROR) << "bind() to " << local_address.address().ToString() |
| 163 << (min_port == 0 |
| 164 ? base::StringPrintf(":%d", local_address.port()) |
| 165 : base::StringPrintf(", port range [%d-%d]", min_port, |
| 166 max_port)) |
140 << " failed: " << result; | 167 << " failed: " << result; |
141 OnError(); | 168 OnError(); |
142 return false; | 169 return false; |
143 } | 170 } |
144 | 171 |
145 // Setting recv socket buffer size. | 172 // Setting recv socket buffer size. |
146 if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) { | 173 if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) { |
147 LOG(WARNING) << "Failed to set socket receive buffer size to " | 174 LOG(WARNING) << "Failed to set socket receive buffer size to " |
148 << kRecvSocketBufferSize; | 175 << kRecvSocketBufferSize; |
149 } | 176 } |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 return socket_->SetSendBufferSize(value) == net::OK; | 422 return socket_->SetSendBufferSize(value) == net::OK; |
396 case P2P_SOCKET_OPT_DSCP: | 423 case P2P_SOCKET_OPT_DSCP: |
397 return (net::OK == socket_->SetDiffServCodePoint( | 424 return (net::OK == socket_->SetDiffServCodePoint( |
398 static_cast<net::DiffServCodePoint>(value))) ? true : false; | 425 static_cast<net::DiffServCodePoint>(value))) ? true : false; |
399 default: | 426 default: |
400 NOTREACHED(); | 427 NOTREACHED(); |
401 return false; | 428 return false; |
402 } | 429 } |
403 } | 430 } |
404 | 431 |
| 432 // static |
| 433 std::unique_ptr<net::DatagramServerSocket> |
| 434 P2PSocketHostUdp::DefaultSocketFactory() { |
| 435 net::UDPServerSocket* socket = new net::UDPServerSocket( |
| 436 GetContentClient()->browser()->GetNetLog(), net::NetLog::Source()); |
| 437 #if defined(OS_WIN) |
| 438 socket->UseNonBlockingIO(); |
| 439 #endif |
| 440 |
| 441 return base::WrapUnique(socket); |
| 442 } |
| 443 |
405 } // namespace content | 444 } // namespace content |
OLD | NEW |