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 "remoting/client/plugin/pepper_packet_socket_factory.h" | 5 #include "remoting/client/plugin/pepper_packet_socket_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "ppapi/cpp/net_address.h" | 11 #include "ppapi/cpp/net_address.h" |
12 #include "ppapi/cpp/udp_socket.h" | 12 #include "ppapi/cpp/udp_socket.h" |
13 #include "ppapi/utility/completion_callback_factory.h" | 13 #include "ppapi/utility/completion_callback_factory.h" |
14 #include "remoting/client/plugin/pepper_util.h" | 14 #include "remoting/client/plugin/pepper_util.h" |
15 #include "remoting/protocol/socket_util.h" | 15 #include "remoting/protocol/socket_util.h" |
16 #include "third_party/webrtc/base/asyncpacketsocket.h" | 16 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h" |
17 | 17 |
18 namespace remoting { | 18 namespace remoting { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Size of the buffer to allocate for RecvFrom(). | 22 // Size of the buffer to allocate for RecvFrom(). |
23 const int kReceiveBufferSize = 65536; | 23 const int kReceiveBufferSize = 65536; |
24 | 24 |
25 // Maximum amount of data in the send buffers. This is necessary to | 25 // Maximum amount of data in the send buffers. This is necessary to |
26 // prevent out-of-memory crashes if the caller sends data faster than | 26 // prevent out-of-memory crashes if the caller sends data faster than |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 return net::ERR_NETWORK_ACCESS_DENIED; | 74 return net::ERR_NETWORK_ACCESS_DENIED; |
75 case PP_ERROR_MESSAGE_TOO_BIG: | 75 case PP_ERROR_MESSAGE_TOO_BIG: |
76 return net::ERR_MSG_TOO_BIG; | 76 return net::ERR_MSG_TOO_BIG; |
77 case PP_ERROR_ADDRESS_IN_USE: | 77 case PP_ERROR_ADDRESS_IN_USE: |
78 return net::ERR_ADDRESS_IN_USE; | 78 return net::ERR_ADDRESS_IN_USE; |
79 default: | 79 default: |
80 return net::ERR_FAILED; | 80 return net::ERR_FAILED; |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 class UdpPacketSocket : public rtc::AsyncPacketSocket { | 84 class UdpPacketSocket : public talk_base::AsyncPacketSocket { |
85 public: | 85 public: |
86 explicit UdpPacketSocket(const pp::InstanceHandle& instance); | 86 explicit UdpPacketSocket(const pp::InstanceHandle& instance); |
87 virtual ~UdpPacketSocket(); | 87 virtual ~UdpPacketSocket(); |
88 | 88 |
89 // |min_port| and |max_port| are set to zero if the port number | 89 // |min_port| and |max_port| are set to zero if the port number |
90 // should be assigned by the OS. | 90 // should be assigned by the OS. |
91 bool Init(const rtc::SocketAddress& local_address, | 91 bool Init(const talk_base::SocketAddress& local_address, |
92 int min_port, | 92 int min_port, |
93 int max_port); | 93 int max_port); |
94 | 94 |
95 // rtc::AsyncPacketSocket interface. | 95 // talk_base::AsyncPacketSocket interface. |
96 virtual rtc::SocketAddress GetLocalAddress() const OVERRIDE; | 96 virtual talk_base::SocketAddress GetLocalAddress() const OVERRIDE; |
97 virtual rtc::SocketAddress GetRemoteAddress() const OVERRIDE; | 97 virtual talk_base::SocketAddress GetRemoteAddress() const OVERRIDE; |
98 virtual int Send(const void* data, size_t data_size, | 98 virtual int Send(const void* data, size_t data_size, |
99 const rtc::PacketOptions& options) OVERRIDE; | 99 const talk_base::PacketOptions& options) OVERRIDE; |
100 virtual int SendTo(const void* data, | 100 virtual int SendTo(const void* data, |
101 size_t data_size, | 101 size_t data_size, |
102 const rtc::SocketAddress& address, | 102 const talk_base::SocketAddress& address, |
103 const rtc::PacketOptions& options) OVERRIDE; | 103 const talk_base::PacketOptions& options) OVERRIDE; |
104 virtual int Close() OVERRIDE; | 104 virtual int Close() OVERRIDE; |
105 virtual State GetState() const OVERRIDE; | 105 virtual State GetState() const OVERRIDE; |
106 virtual int GetOption(rtc::Socket::Option opt, int* value) OVERRIDE; | 106 virtual int GetOption(talk_base::Socket::Option opt, int* value) OVERRIDE; |
107 virtual int SetOption(rtc::Socket::Option opt, int value) OVERRIDE; | 107 virtual int SetOption(talk_base::Socket::Option opt, int value) OVERRIDE; |
108 virtual int GetError() const OVERRIDE; | 108 virtual int GetError() const OVERRIDE; |
109 virtual void SetError(int error) OVERRIDE; | 109 virtual void SetError(int error) OVERRIDE; |
110 | 110 |
111 private: | 111 private: |
112 struct PendingPacket { | 112 struct PendingPacket { |
113 PendingPacket(const void* buffer, | 113 PendingPacket(const void* buffer, |
114 int buffer_size, | 114 int buffer_size, |
115 const pp::NetAddress& address); | 115 const pp::NetAddress& address); |
116 | 116 |
117 scoped_refptr<net::IOBufferWithSize> data; | 117 scoped_refptr<net::IOBufferWithSize> data; |
(...skipping 10 matching lines...) Expand all Loading... |
128 void OnReadCompleted(int result, pp::NetAddress address); | 128 void OnReadCompleted(int result, pp::NetAddress address); |
129 void HandleReadResult(int result, pp::NetAddress address); | 129 void HandleReadResult(int result, pp::NetAddress address); |
130 | 130 |
131 pp::InstanceHandle instance_; | 131 pp::InstanceHandle instance_; |
132 | 132 |
133 pp::UDPSocket socket_; | 133 pp::UDPSocket socket_; |
134 | 134 |
135 State state_; | 135 State state_; |
136 int error_; | 136 int error_; |
137 | 137 |
138 rtc::SocketAddress local_address_; | 138 talk_base::SocketAddress local_address_; |
139 | 139 |
140 // Used to scan ports when necessary. Both values are set to 0 when | 140 // Used to scan ports when necessary. Both values are set to 0 when |
141 // the port number is assigned by OS. | 141 // the port number is assigned by OS. |
142 uint16_t min_port_; | 142 uint16_t min_port_; |
143 uint16_t max_port_; | 143 uint16_t max_port_; |
144 | 144 |
145 std::vector<char> receive_buffer_; | 145 std::vector<char> receive_buffer_; |
146 | 146 |
147 bool send_pending_; | 147 bool send_pending_; |
148 std::list<PendingPacket> send_queue_; | 148 std::list<PendingPacket> send_queue_; |
(...skipping 23 matching lines...) Expand all Loading... |
172 max_port_(0), | 172 max_port_(0), |
173 send_pending_(false), | 173 send_pending_(false), |
174 send_queue_size_(0), | 174 send_queue_size_(0), |
175 callback_factory_(this) { | 175 callback_factory_(this) { |
176 } | 176 } |
177 | 177 |
178 UdpPacketSocket::~UdpPacketSocket() { | 178 UdpPacketSocket::~UdpPacketSocket() { |
179 Close(); | 179 Close(); |
180 } | 180 } |
181 | 181 |
182 bool UdpPacketSocket::Init(const rtc::SocketAddress& local_address, | 182 bool UdpPacketSocket::Init(const talk_base::SocketAddress& local_address, |
183 int min_port, | 183 int min_port, |
184 int max_port) { | 184 int max_port) { |
185 if (socket_.is_null()) { | 185 if (socket_.is_null()) { |
186 return false; | 186 return false; |
187 } | 187 } |
188 | 188 |
189 local_address_ = local_address; | 189 local_address_ = local_address; |
190 max_port_ = max_port; | 190 max_port_ = max_port; |
191 min_port_ = min_port; | 191 min_port_ = min_port; |
192 | 192 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 callback_factory_.NewCallback(&UdpPacketSocket::OnBindCompleted); | 232 callback_factory_.NewCallback(&UdpPacketSocket::OnBindCompleted); |
233 int result = socket_.Bind(pp_local_address, callback); | 233 int result = socket_.Bind(pp_local_address, callback); |
234 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING); | 234 DCHECK_EQ(result, PP_OK_COMPLETIONPENDING); |
235 } | 235 } |
236 } else { | 236 } else { |
237 LOG(ERROR) << "Failed to bind UDP socket to " << local_address_.ToString() | 237 LOG(ERROR) << "Failed to bind UDP socket to " << local_address_.ToString() |
238 << ", error: " << result; | 238 << ", error: " << result; |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 rtc::SocketAddress UdpPacketSocket::GetLocalAddress() const { | 242 talk_base::SocketAddress UdpPacketSocket::GetLocalAddress() const { |
243 DCHECK_EQ(state_, STATE_BOUND); | 243 DCHECK_EQ(state_, STATE_BOUND); |
244 return local_address_; | 244 return local_address_; |
245 } | 245 } |
246 | 246 |
247 rtc::SocketAddress UdpPacketSocket::GetRemoteAddress() const { | 247 talk_base::SocketAddress UdpPacketSocket::GetRemoteAddress() const { |
248 // UDP sockets are not connected - this method should never be called. | 248 // UDP sockets are not connected - this method should never be called. |
249 NOTREACHED(); | 249 NOTREACHED(); |
250 return rtc::SocketAddress(); | 250 return talk_base::SocketAddress(); |
251 } | 251 } |
252 | 252 |
253 int UdpPacketSocket::Send(const void* data, size_t data_size, | 253 int UdpPacketSocket::Send(const void* data, size_t data_size, |
254 const rtc::PacketOptions& options) { | 254 const talk_base::PacketOptions& options) { |
255 // UDP sockets are not connected - this method should never be called. | 255 // UDP sockets are not connected - this method should never be called. |
256 NOTREACHED(); | 256 NOTREACHED(); |
257 return EWOULDBLOCK; | 257 return EWOULDBLOCK; |
258 } | 258 } |
259 | 259 |
260 int UdpPacketSocket::SendTo(const void* data, | 260 int UdpPacketSocket::SendTo(const void* data, |
261 size_t data_size, | 261 size_t data_size, |
262 const rtc::SocketAddress& address, | 262 const talk_base::SocketAddress& address, |
263 const rtc::PacketOptions& options) { | 263 const talk_base::PacketOptions& options) { |
264 if (state_ != STATE_BOUND) { | 264 if (state_ != STATE_BOUND) { |
265 // TODO(sergeyu): StunPort may try to send stun request before we | 265 // TODO(sergeyu): StunPort may try to send stun request before we |
266 // are bound. Fix that problem and change this to DCHECK. | 266 // are bound. Fix that problem and change this to DCHECK. |
267 return EINVAL; | 267 return EINVAL; |
268 } | 268 } |
269 | 269 |
270 if (error_ != 0) { | 270 if (error_ != 0) { |
271 return error_; | 271 return error_; |
272 } | 272 } |
273 | 273 |
(...skipping 11 matching lines...) Expand all Loading... |
285 DoSend(); | 285 DoSend(); |
286 return data_size; | 286 return data_size; |
287 } | 287 } |
288 | 288 |
289 int UdpPacketSocket::Close() { | 289 int UdpPacketSocket::Close() { |
290 state_ = STATE_CLOSED; | 290 state_ = STATE_CLOSED; |
291 socket_.Close(); | 291 socket_.Close(); |
292 return 0; | 292 return 0; |
293 } | 293 } |
294 | 294 |
295 rtc::AsyncPacketSocket::State UdpPacketSocket::GetState() const { | 295 talk_base::AsyncPacketSocket::State UdpPacketSocket::GetState() const { |
296 return state_; | 296 return state_; |
297 } | 297 } |
298 | 298 |
299 int UdpPacketSocket::GetOption(rtc::Socket::Option opt, int* value) { | 299 int UdpPacketSocket::GetOption(talk_base::Socket::Option opt, int* value) { |
300 // Options are not supported for Pepper UDP sockets. | 300 // Options are not supported for Pepper UDP sockets. |
301 return -1; | 301 return -1; |
302 } | 302 } |
303 | 303 |
304 int UdpPacketSocket::SetOption(rtc::Socket::Option opt, int value) { | 304 int UdpPacketSocket::SetOption(talk_base::Socket::Option opt, int value) { |
305 // Options are not supported for Pepper UDP sockets. | 305 // Options are not supported for Pepper UDP sockets. |
306 return -1; | 306 return -1; |
307 } | 307 } |
308 | 308 |
309 int UdpPacketSocket::GetError() const { | 309 int UdpPacketSocket::GetError() const { |
310 return error_; | 310 return error_; |
311 } | 311 } |
312 | 312 |
313 void UdpPacketSocket::SetError(int error) { | 313 void UdpPacketSocket::SetError(int error) { |
314 error_ = error; | 314 error_ = error; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 | 378 |
379 void UdpPacketSocket::OnReadCompleted(int result, pp::NetAddress address) { | 379 void UdpPacketSocket::OnReadCompleted(int result, pp::NetAddress address) { |
380 HandleReadResult(result, address); | 380 HandleReadResult(result, address); |
381 if (result > 0) { | 381 if (result > 0) { |
382 DoRead(); | 382 DoRead(); |
383 } | 383 } |
384 } | 384 } |
385 | 385 |
386 void UdpPacketSocket::HandleReadResult(int result, pp::NetAddress address) { | 386 void UdpPacketSocket::HandleReadResult(int result, pp::NetAddress address) { |
387 if (result > 0) { | 387 if (result > 0) { |
388 rtc::SocketAddress socket_address; | 388 talk_base::SocketAddress socket_address; |
389 PpNetAddressToSocketAddress(address, &socket_address); | 389 PpNetAddressToSocketAddress(address, &socket_address); |
390 SignalReadPacket(this, &receive_buffer_[0], result, socket_address, | 390 SignalReadPacket(this, &receive_buffer_[0], result, socket_address, |
391 rtc::CreatePacketTime(0)); | 391 talk_base::CreatePacketTime(0)); |
392 } else if (result != PP_ERROR_ABORTED) { | 392 } else if (result != PP_ERROR_ABORTED) { |
393 LOG(ERROR) << "Received error when reading from UDP socket: " << result; | 393 LOG(ERROR) << "Received error when reading from UDP socket: " << result; |
394 } | 394 } |
395 } | 395 } |
396 | 396 |
397 } // namespace | 397 } // namespace |
398 | 398 |
399 PepperPacketSocketFactory::PepperPacketSocketFactory( | 399 PepperPacketSocketFactory::PepperPacketSocketFactory( |
400 const pp::InstanceHandle& instance) | 400 const pp::InstanceHandle& instance) |
401 : pp_instance_(instance) { | 401 : pp_instance_(instance) { |
402 } | 402 } |
403 | 403 |
404 PepperPacketSocketFactory::~PepperPacketSocketFactory() { | 404 PepperPacketSocketFactory::~PepperPacketSocketFactory() { |
405 } | 405 } |
406 | 406 |
407 rtc::AsyncPacketSocket* PepperPacketSocketFactory::CreateUdpSocket( | 407 talk_base::AsyncPacketSocket* PepperPacketSocketFactory::CreateUdpSocket( |
408 const rtc::SocketAddress& local_address, | 408 const talk_base::SocketAddress& local_address, |
409 int min_port, | 409 int min_port, |
410 int max_port) { | 410 int max_port) { |
411 scoped_ptr<UdpPacketSocket> result(new UdpPacketSocket(pp_instance_)); | 411 scoped_ptr<UdpPacketSocket> result(new UdpPacketSocket(pp_instance_)); |
412 if (!result->Init(local_address, min_port, max_port)) | 412 if (!result->Init(local_address, min_port, max_port)) |
413 return NULL; | 413 return NULL; |
414 return result.release(); | 414 return result.release(); |
415 } | 415 } |
416 | 416 |
417 rtc::AsyncPacketSocket* PepperPacketSocketFactory::CreateServerTcpSocket( | 417 talk_base::AsyncPacketSocket* PepperPacketSocketFactory::CreateServerTcpSocket( |
418 const rtc::SocketAddress& local_address, | 418 const talk_base::SocketAddress& local_address, |
419 int min_port, | 419 int min_port, |
420 int max_port, | 420 int max_port, |
421 int opts) { | 421 int opts) { |
422 // We don't use TCP sockets for remoting connections. | 422 // We don't use TCP sockets for remoting connections. |
423 NOTREACHED(); | 423 NOTREACHED(); |
424 return NULL; | 424 return NULL; |
425 } | 425 } |
426 | 426 |
427 rtc::AsyncPacketSocket* PepperPacketSocketFactory::CreateClientTcpSocket( | 427 talk_base::AsyncPacketSocket* PepperPacketSocketFactory::CreateClientTcpSocket( |
428 const rtc::SocketAddress& local_address, | 428 const talk_base::SocketAddress& local_address, |
429 const rtc::SocketAddress& remote_address, | 429 const talk_base::SocketAddress& remote_address, |
430 const rtc::ProxyInfo& proxy_info, | 430 const talk_base::ProxyInfo& proxy_info, |
431 const std::string& user_agent, | 431 const std::string& user_agent, |
432 int opts) { | 432 int opts) { |
433 // We don't use TCP sockets for remoting connections. | 433 // We don't use TCP sockets for remoting connections. |
434 NOTREACHED(); | 434 NOTREACHED(); |
435 return NULL; | 435 return NULL; |
436 } | 436 } |
437 | 437 |
438 rtc::AsyncResolverInterface* | 438 talk_base::AsyncResolverInterface* |
439 PepperPacketSocketFactory::CreateAsyncResolver() { | 439 PepperPacketSocketFactory::CreateAsyncResolver() { |
440 NOTREACHED(); | 440 NOTREACHED(); |
441 return NULL; | 441 return NULL; |
442 } | 442 } |
443 | 443 |
444 } // namespace remoting | 444 } // namespace remoting |
OLD | NEW |