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_udp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_udp.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
8 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
9 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
10 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 // UDP packets cannot be bigger than 64k. | 15 // UDP packets cannot be bigger than 64k. |
15 const int kReadBufferSize = 65536; | 16 const int kReadBufferSize = 65536; |
16 | 17 |
(...skipping 10 matching lines...) Expand all Loading... |
27 } | 28 } |
28 | 29 |
29 P2PSocketHostUdp::PendingPacket::~PendingPacket() { | 30 P2PSocketHostUdp::PendingPacket::~PendingPacket() { |
30 } | 31 } |
31 | 32 |
32 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Message::Sender* message_sender, | 33 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Message::Sender* message_sender, |
33 int routing_id, int id) | 34 int routing_id, int id) |
34 : P2PSocketHost(message_sender, routing_id, id), | 35 : P2PSocketHost(message_sender, routing_id, id), |
35 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), | 36 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), |
36 send_queue_bytes_(0), | 37 send_queue_bytes_(0), |
37 send_pending_(false), | 38 send_pending_(false) { |
38 ALLOW_THIS_IN_INITIALIZER_LIST( | |
39 recv_callback_(this, &P2PSocketHostUdp::OnRecv)), | |
40 ALLOW_THIS_IN_INITIALIZER_LIST( | |
41 send_callback_(this, &P2PSocketHostUdp::OnSend)) { | |
42 } | 39 } |
43 | 40 |
44 P2PSocketHostUdp::~P2PSocketHostUdp() { | 41 P2PSocketHostUdp::~P2PSocketHostUdp() { |
45 if (state_ == STATE_OPEN) { | 42 if (state_ == STATE_OPEN) { |
46 DCHECK(socket_.get()); | 43 DCHECK(socket_.get()); |
47 socket_.reset(); | 44 socket_.reset(); |
48 } | 45 } |
49 } | 46 } |
50 | 47 |
51 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, | 48 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) | 83 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) |
87 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); | 84 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); |
88 | 85 |
89 state_ = STATE_ERROR; | 86 state_ = STATE_ERROR; |
90 } | 87 } |
91 | 88 |
92 void P2PSocketHostUdp::DoRead() { | 89 void P2PSocketHostUdp::DoRead() { |
93 int result; | 90 int result; |
94 do { | 91 do { |
95 result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_, | 92 result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_, |
96 &recv_callback_); | 93 base::Bind(&P2PSocketHostUdp::OnRecv, |
| 94 base::Unretained(this))); |
97 DidCompleteRead(result); | 95 DidCompleteRead(result); |
98 } while (result > 0); | 96 } while (result > 0); |
99 } | 97 } |
100 | 98 |
101 void P2PSocketHostUdp::OnRecv(int result) { | 99 void P2PSocketHostUdp::OnRecv(int result) { |
102 DidCompleteRead(result); | 100 DidCompleteRead(result); |
103 if (state_ == STATE_OPEN) { | 101 if (state_ == STATE_OPEN) { |
104 DoRead(); | 102 DoRead(); |
105 } | 103 } |
106 } | 104 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 send_queue_.push_back(PendingPacket(to, data)); | 158 send_queue_.push_back(PendingPacket(to, data)); |
161 send_queue_bytes_ += data.size(); | 159 send_queue_bytes_ += data.size(); |
162 } else { | 160 } else { |
163 PendingPacket packet(to, data); | 161 PendingPacket packet(to, data); |
164 DoSend(packet); | 162 DoSend(packet); |
165 } | 163 } |
166 } | 164 } |
167 | 165 |
168 void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { | 166 void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { |
169 int result = socket_->SendTo(packet.data, packet.size, packet.to, | 167 int result = socket_->SendTo(packet.data, packet.size, packet.to, |
170 &send_callback_); | 168 base::Bind(&P2PSocketHostUdp::OnSend, |
| 169 base::Unretained(this))); |
171 if (result == net::ERR_IO_PENDING) { | 170 if (result == net::ERR_IO_PENDING) { |
172 send_pending_ = true; | 171 send_pending_ = true; |
173 } else if (result < 0) { | 172 } else if (result < 0) { |
174 LOG(ERROR) << "Error when sending data in UDP socket: " << result; | 173 LOG(ERROR) << "Error when sending data in UDP socket: " << result; |
175 OnError(); | 174 OnError(); |
176 } | 175 } |
177 } | 176 } |
178 | 177 |
179 void P2PSocketHostUdp::OnSend(int result) { | 178 void P2PSocketHostUdp::OnSend(int result) { |
180 DCHECK(send_pending_); | 179 DCHECK(send_pending_); |
(...skipping 13 matching lines...) Expand all Loading... |
194 } | 193 } |
195 | 194 |
196 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection( | 195 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection( |
197 const net::IPEndPoint& remote_address, int id) { | 196 const net::IPEndPoint& remote_address, int id) { |
198 NOTREACHED(); | 197 NOTREACHED(); |
199 OnError(); | 198 OnError(); |
200 return NULL; | 199 return NULL; |
201 } | 200 } |
202 | 201 |
203 } // namespace content | 202 } // namespace content |
OLD | NEW |