| 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/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" | 9 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" |
| 10 #include "content/common/p2p_messages.h" | 10 #include "content/common/p2p_messages.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 uid <<= 32; | 44 uid <<= 32; |
| 45 uid |= packet_id; | 45 uid |= packet_id; |
| 46 return uid; | 46 return uid; |
| 47 } | 47 } |
| 48 | 48 |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 namespace content { | 51 namespace content { |
| 52 | 52 |
| 53 P2PSocketHostUdp::PendingPacket::PendingPacket( | 53 P2PSocketHostUdp::PendingPacket::PendingPacket( |
| 54 const net::IPEndPoint& to, const std::vector<char>& content, uint64 id) | 54 const net::IPEndPoint& to, |
| 55 const std::vector<char>& content, |
| 56 uint64 id, |
| 57 net::DiffServCodePoint dscp_) |
| 55 : to(to), | 58 : to(to), |
| 56 data(new net::IOBuffer(content.size())), | 59 data(new net::IOBuffer(content.size())), |
| 57 size(content.size()), | 60 size(content.size()), |
| 58 id(id) { | 61 id(id), |
| 62 dscp(dscp_) { |
| 59 memcpy(data->data(), &content[0], size); | 63 memcpy(data->data(), &content[0], size); |
| 60 } | 64 } |
| 61 | 65 |
| 62 P2PSocketHostUdp::PendingPacket::~PendingPacket() { | 66 P2PSocketHostUdp::PendingPacket::~PendingPacket() { |
| 63 } | 67 } |
| 64 | 68 |
| 65 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, | 69 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, |
| 66 int id, | 70 int id, |
| 67 P2PMessageThrottler* throttler) | 71 P2PMessageThrottler* throttler) |
| 68 : P2PSocketHost(message_sender, id), | 72 : P2PSocketHost(message_sender, id), |
| 69 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), | 73 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), |
| 70 send_pending_(false), | 74 send_pending_(false), |
| 71 send_packet_count_(0), | 75 send_packet_count_(0), |
| 72 throttler_(throttler) { | 76 throttler_(throttler), |
| 77 last_dscp_(net::DSCP_CS0) { |
| 73 } | 78 } |
| 74 | 79 |
| 75 P2PSocketHostUdp::~P2PSocketHostUdp() { | 80 P2PSocketHostUdp::~P2PSocketHostUdp() { |
| 76 if (state_ == STATE_OPEN) { | 81 if (state_ == STATE_OPEN) { |
| 77 DCHECK(socket_.get()); | 82 DCHECK(socket_.get()); |
| 78 socket_.reset(); | 83 socket_.reset(); |
| 79 } | 84 } |
| 80 } | 85 } |
| 81 | 86 |
| 82 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, | 87 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 166 } |
| 162 | 167 |
| 163 message_sender_->Send(new P2PMsg_OnDataReceived(id_, recv_address_, data)); | 168 message_sender_->Send(new P2PMsg_OnDataReceived(id_, recv_address_, data)); |
| 164 } else if (result < 0 && !IsTransientError(result)) { | 169 } else if (result < 0 && !IsTransientError(result)) { |
| 165 LOG(ERROR) << "Error when reading from UDP socket: " << result; | 170 LOG(ERROR) << "Error when reading from UDP socket: " << result; |
| 166 OnError(); | 171 OnError(); |
| 167 } | 172 } |
| 168 } | 173 } |
| 169 | 174 |
| 170 void P2PSocketHostUdp::Send(const net::IPEndPoint& to, | 175 void P2PSocketHostUdp::Send(const net::IPEndPoint& to, |
| 171 const std::vector<char>& data) { | 176 const std::vector<char>& data, |
| 177 net::DiffServCodePoint dscp) { |
| 172 if (!socket_) { | 178 if (!socket_) { |
| 173 // The Send message may be sent after the an OnError message was | 179 // The Send message may be sent after the an OnError message was |
| 174 // sent by hasn't been processed the renderer. | 180 // sent by hasn't been processed the renderer. |
| 175 return; | 181 return; |
| 176 } | 182 } |
| 177 | 183 |
| 178 if (connected_peers_.find(to) == connected_peers_.end()) { | 184 if (connected_peers_.find(to) == connected_peers_.end()) { |
| 179 P2PSocketHost::StunMessageType type; | 185 P2PSocketHost::StunMessageType type; |
| 180 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); | 186 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); |
| 181 if (!stun || type == STUN_DATA_INDICATION) { | 187 if (!stun || type == STUN_DATA_INDICATION) { |
| 182 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() | 188 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() |
| 183 << " before STUN binding is finished."; | 189 << " before STUN binding is finished."; |
| 184 OnError(); | 190 OnError(); |
| 185 return; | 191 return; |
| 186 } | 192 } |
| 187 | 193 |
| 188 if (throttler_->DropNextPacket(data.size())) { | 194 if (throttler_->DropNextPacket(data.size())) { |
| 189 LOG(INFO) << "STUN message is dropped due to high volume."; | 195 LOG(INFO) << "STUN message is dropped due to high volume."; |
| 190 // Do not reset socket. | 196 // Do not reset socket. |
| 191 return; | 197 return; |
| 192 } | 198 } |
| 193 } | 199 } |
| 194 | 200 |
| 195 if (send_pending_) { | 201 if (send_pending_) { |
| 196 send_queue_.push_back(PendingPacket(to, data, send_packet_count_)); | 202 send_queue_.push_back(PendingPacket(to, data, send_packet_count_, dscp)); |
| 197 } else { | 203 } else { |
| 198 PendingPacket packet(to, data, send_packet_count_); | 204 PendingPacket packet(to, data, send_packet_count_, dscp); |
| 199 DoSend(packet); | 205 DoSend(packet); |
| 200 } | 206 } |
| 201 ++send_packet_count_; | 207 ++send_packet_count_; |
| 202 } | 208 } |
| 203 | 209 |
| 204 void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { | 210 void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { |
| 205 TRACE_EVENT_ASYNC_BEGIN1("p2p", "Udp::DoSend", | 211 TRACE_EVENT_ASYNC_BEGIN1("p2p", "Udp::DoSend", |
| 206 GetUniqueEventId(this, packet.id), | 212 GetUniqueEventId(this, packet.id), |
| 207 "size", packet.size); | 213 "size", packet.size); |
| 214 if (last_dscp_ != packet.dscp && last_dscp_ != net::DSCP_NO_CHANGE) { |
| 215 int result = socket_->SetDiffServCodePoint(packet.dscp); |
| 216 if (result == net::OK) { |
| 217 last_dscp_ = packet.dscp; |
| 218 } else if (!IsTransientError(result) && last_dscp_ != net::DSCP_CS0) { |
| 219 // We receieved a non-transient error, and it seems we have |
| 220 // not changed the DSCP in the past, disable DSCP as it unlikely |
| 221 // to work in the future. |
| 222 last_dscp_ = net::DSCP_NO_CHANGE; |
| 223 } |
| 224 } |
| 208 int result = socket_->SendTo( | 225 int result = socket_->SendTo( |
| 209 packet.data.get(), | 226 packet.data.get(), |
| 210 packet.size, | 227 packet.size, |
| 211 packet.to, | 228 packet.to, |
| 212 base::Bind(&P2PSocketHostUdp::OnSend, base::Unretained(this), packet.id)); | 229 base::Bind(&P2PSocketHostUdp::OnSend, base::Unretained(this), packet.id)); |
| 213 | 230 |
| 214 // sendto() may return an error, e.g. if we've received an ICMP Destination | 231 // sendto() may return an error, e.g. if we've received an ICMP Destination |
| 215 // Unreachable message. When this happens try sending the same packet again, | 232 // Unreachable message. When this happens try sending the same packet again, |
| 216 // and just drop it if it fails again. | 233 // and just drop it if it fails again. |
| 217 if (IsTransientError(result)) { | 234 if (IsTransientError(result)) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 278 } |
| 262 | 279 |
| 263 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection( | 280 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection( |
| 264 const net::IPEndPoint& remote_address, int id) { | 281 const net::IPEndPoint& remote_address, int id) { |
| 265 NOTREACHED(); | 282 NOTREACHED(); |
| 266 OnError(); | 283 OnError(); |
| 267 return NULL; | 284 return NULL; |
| 268 } | 285 } |
| 269 | 286 |
| 270 } // namespace content | 287 } // namespace content |
| OLD | NEW |