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 "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 24 matching lines...) Expand all Loading... | |
35 size(content.size()) { | 35 size(content.size()) { |
36 memcpy(data->data(), &content[0], size); | 36 memcpy(data->data(), &content[0], size); |
37 } | 37 } |
38 | 38 |
39 P2PSocketHostUdp::PendingPacket::~PendingPacket() { | 39 P2PSocketHostUdp::PendingPacket::~PendingPacket() { |
40 } | 40 } |
41 | 41 |
42 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, int id) | 42 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, int id) |
43 : P2PSocketHost(message_sender, id), | 43 : P2PSocketHost(message_sender, id), |
44 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), | 44 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), |
45 send_queue_bytes_(0), | |
46 send_pending_(false) { | 45 send_pending_(false) { |
47 } | 46 } |
48 | 47 |
49 P2PSocketHostUdp::~P2PSocketHostUdp() { | 48 P2PSocketHostUdp::~P2PSocketHostUdp() { |
50 if (state_ == STATE_OPEN) { | 49 if (state_ == STATE_OPEN) { |
51 DCHECK(socket_.get()); | 50 DCHECK(socket_.get()); |
52 socket_.reset(); | 51 socket_.reset(); |
53 } | 52 } |
54 } | 53 } |
55 | 54 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 } | 131 } |
133 } | 132 } |
134 | 133 |
135 message_sender_->Send(new P2PMsg_OnDataReceived(id_, recv_address_, data)); | 134 message_sender_->Send(new P2PMsg_OnDataReceived(id_, recv_address_, data)); |
136 } else if (result < 0 && !IsTransientError(result)) { | 135 } else if (result < 0 && !IsTransientError(result)) { |
137 LOG(ERROR) << "Error when reading from UDP socket: " << result; | 136 LOG(ERROR) << "Error when reading from UDP socket: " << result; |
138 OnError(); | 137 OnError(); |
139 } | 138 } |
140 } | 139 } |
141 | 140 |
141 | |
Ronghua Wu (Left Chromium)
2013/04/04 05:23:59
remove extra line
Sergey Ulanov
2013/04/04 18:40:34
Done.
| |
142 void P2PSocketHostUdp::Send(const net::IPEndPoint& to, | 142 void P2PSocketHostUdp::Send(const net::IPEndPoint& to, |
143 const std::vector<char>& data) { | 143 const std::vector<char>& data) { |
144 if (!socket_.get()) { | 144 if (!socket_.get()) { |
145 // The Send message may be sent after the an OnError message was | 145 // The Send message may be sent after the an OnError message was |
146 // sent by hasn't been processed the renderer. | 146 // sent by hasn't been processed the renderer. |
147 return; | 147 return; |
148 } | 148 } |
149 | 149 |
150 if (connected_peers_.find(to) == connected_peers_.end()) { | 150 if (connected_peers_.find(to) == connected_peers_.end()) { |
151 P2PSocketHost::StunMessageType type; | 151 P2PSocketHost::StunMessageType type; |
152 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); | 152 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); |
153 if (!stun || type == STUN_DATA_INDICATION) { | 153 if (!stun || type == STUN_DATA_INDICATION) { |
154 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() | 154 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() |
155 << " before STUN binding is finished."; | 155 << " before STUN binding is finished."; |
156 OnError(); | 156 OnError(); |
157 return; | 157 return; |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 if (send_pending_) { | 161 if (send_pending_) { |
162 if (send_queue_bytes_ + static_cast<int>(data.size()) > | |
163 kMaxSendBufferSize) { | |
164 LOG(WARNING) << "Send buffer is full. Dropping a packet."; | |
165 return; | |
166 } | |
167 send_queue_.push_back(PendingPacket(to, data)); | 162 send_queue_.push_back(PendingPacket(to, data)); |
168 send_queue_bytes_ += data.size(); | |
169 } else { | 163 } else { |
170 PendingPacket packet(to, data); | 164 PendingPacket packet(to, data); |
171 DoSend(packet); | 165 DoSend(packet); |
172 } | 166 } |
173 } | 167 } |
174 | 168 |
175 void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { | 169 void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { |
176 int result = socket_->SendTo(packet.data, packet.size, packet.to, | 170 int result = socket_->SendTo(packet.data, packet.size, packet.to, |
177 base::Bind(&P2PSocketHostUdp::OnSend, | 171 base::Bind(&P2PSocketHostUdp::OnSend, |
178 base::Unretained(this))); | 172 base::Unretained(this))); |
179 | 173 |
180 // sendto() may return an error, e.g. if we've received an ICMP Destination | 174 // sendto() may return an error, e.g. if we've received an ICMP Destination |
181 // Unreachable message. When this happens try sending the same packet again, | 175 // Unreachable message. When this happens try sending the same packet again, |
182 // and just drop it if it fails again. | 176 // and just drop it if it fails again. |
183 if (IsTransientError(result)) { | 177 if (IsTransientError(result)) { |
184 result = socket_->SendTo(packet.data, packet.size, packet.to, | 178 result = socket_->SendTo(packet.data, packet.size, packet.to, |
185 base::Bind(&P2PSocketHostUdp::OnSend, | 179 base::Bind(&P2PSocketHostUdp::OnSend, |
186 base::Unretained(this))); | 180 base::Unretained(this))); |
187 } | 181 } |
188 | 182 |
189 if (result == net::ERR_IO_PENDING) { | 183 if (result == net::ERR_IO_PENDING) { |
190 send_pending_ = true; | 184 send_pending_ = true; |
191 } else if (IsTransientError(result)) { | 185 } else { |
192 LOG(INFO) << "sendto() has failed twice returning a " | 186 DidCompleteSend(result); |
193 " transient error. Dropping the packet."; | |
194 } else if (result < 0) { | |
195 LOG(ERROR) << "Error when sending data in UDP socket: " << result; | |
196 OnError(); | |
197 } | 187 } |
198 } | 188 } |
199 | 189 |
200 void P2PSocketHostUdp::OnSend(int result) { | 190 void P2PSocketHostUdp::OnSend(int result) { |
201 DCHECK(send_pending_); | 191 DCHECK(send_pending_); |
202 DCHECK_NE(result, net::ERR_IO_PENDING); | 192 DCHECK_NE(result, net::ERR_IO_PENDING); |
203 | 193 |
204 send_pending_ = false; | 194 send_pending_ = false; |
205 | 195 |
206 if (result < 0 && !IsTransientError(result)) { | 196 DidCompleteSend(result); |
207 OnError(); | |
208 return; | |
209 } | |
210 | 197 |
211 // Send next packets if we have them waiting in the buffer. | 198 // Send next packets if we have them waiting in the buffer. |
212 while (!send_queue_.empty() && !send_pending_) { | 199 while (state_ == STATE_OPEN && !send_queue_.empty() && !send_pending_) { |
213 DoSend(send_queue_.front()); | 200 DoSend(send_queue_.front()); |
214 send_queue_bytes_ -= send_queue_.front().size; | |
215 send_queue_.pop_front(); | 201 send_queue_.pop_front(); |
216 } | 202 } |
217 } | 203 } |
218 | 204 |
205 void P2PSocketHostUdp::DidCompleteSend(int result) { | |
Ronghua Wu (Left Chromium)
2013/04/04 05:23:59
HandleSendResult?
Sergey Ulanov
2013/04/04 18:40:34
Done. Also renamed DidReadComplete().
| |
206 if (result > 0) { | |
207 message_sender_->Send(new P2PMsg_OnSendComplete(id_)); | |
208 } else if (IsTransientError(result)) { | |
209 LOG(INFO) << "sendto() has failed twice returning a " | |
210 " transient error. Dropping the packet."; | |
Ronghua Wu (Left Chromium)
2013/04/04 05:23:59
indent
Sergey Ulanov
2013/04/04 18:40:34
I believe it's indented correctly (there is no <<
| |
211 } else if (result < 0) { | |
212 LOG(ERROR) << "Error when sending data in UDP socket: " << result; | |
213 OnError(); | |
214 } | |
215 } | |
216 | |
219 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection( | 217 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection( |
220 const net::IPEndPoint& remote_address, int id) { | 218 const net::IPEndPoint& remote_address, int id) { |
221 NOTREACHED(); | 219 NOTREACHED(); |
222 OnError(); | 220 OnError(); |
223 return NULL; | 221 return NULL; |
224 } | 222 } |
225 | 223 |
226 } // namespace content | 224 } // namespace content |
OLD | NEW |