OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/cast/test/utility/udp_proxy.h" | 5 #include "media/cast/test/utility/udp_proxy.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 public: | 403 public: |
404 UDPProxyImpl(const net::IPEndPoint& local_port, | 404 UDPProxyImpl(const net::IPEndPoint& local_port, |
405 const net::IPEndPoint& destination, | 405 const net::IPEndPoint& destination, |
406 scoped_ptr<PacketPipe> to_dest_pipe, | 406 scoped_ptr<PacketPipe> to_dest_pipe, |
407 scoped_ptr<PacketPipe> from_dest_pipe, | 407 scoped_ptr<PacketPipe> from_dest_pipe, |
408 net::NetLog* net_log) : | 408 net::NetLog* net_log) : |
409 local_port_(local_port), | 409 local_port_(local_port), |
410 destination_(destination), | 410 destination_(destination), |
411 proxy_thread_("media::cast::test::UdpProxy Thread"), | 411 proxy_thread_("media::cast::test::UdpProxy Thread"), |
412 to_dest_pipe_(to_dest_pipe.Pass()), | 412 to_dest_pipe_(to_dest_pipe.Pass()), |
413 from_dest_pipe_(to_dest_pipe.Pass()), | 413 from_dest_pipe_(to_dest_pipe.Pass()) { |
414 start_event_(false, false) { | |
415 proxy_thread_.StartWithOptions( | 414 proxy_thread_.StartWithOptions( |
416 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 415 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 416 base::WaitableEvent start_event(false, false); |
417 proxy_thread_.message_loop_proxy()->PostTask( | 417 proxy_thread_.message_loop_proxy()->PostTask( |
418 FROM_HERE, | 418 FROM_HERE, |
419 base::Bind(&UDPProxyImpl::Start, | 419 base::Bind(&UDPProxyImpl::Start, |
420 base::Unretained(this), | 420 base::Unretained(this), |
| 421 base::Unretained(&start_event), |
421 net_log)); | 422 net_log)); |
422 start_event_.Wait(); | 423 start_event.Wait(); |
423 } | 424 } |
424 | 425 |
425 void Start(net::NetLog* net_log) { | 426 virtual ~UDPProxyImpl() { |
| 427 base::WaitableEvent stop_event(false, false); |
| 428 proxy_thread_.message_loop_proxy()->PostTask( |
| 429 FROM_HERE, |
| 430 base::Bind(&UDPProxyImpl::Stop, |
| 431 base::Unretained(this), |
| 432 base::Unretained(&stop_event))); |
| 433 stop_event.Wait(); |
| 434 proxy_thread_.Stop(); |
| 435 } |
| 436 |
| 437 private: |
| 438 void Start(base::WaitableEvent* start_event, |
| 439 net::NetLog* net_log) { |
426 socket_.reset(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, | 440 socket_.reset(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, |
427 net::RandIntCallback(), | 441 net::RandIntCallback(), |
428 net_log, | 442 net_log, |
429 net::NetLog::Source())); | 443 net::NetLog::Source())); |
430 BuildPipe(&to_dest_pipe_, new PacketSender(socket_.get(), &destination_)); | 444 BuildPipe(&to_dest_pipe_, new PacketSender(socket_.get(), &destination_)); |
431 BuildPipe(&from_dest_pipe_, | 445 BuildPipe(&from_dest_pipe_, |
432 new PacketSender(socket_.get(), &return_address_)); | 446 new PacketSender(socket_.get(), &return_address_)); |
433 to_dest_pipe_->InitOnIOThread(); | 447 to_dest_pipe_->InitOnIOThread(); |
434 from_dest_pipe_->InitOnIOThread(); | 448 from_dest_pipe_->InitOnIOThread(); |
435 | 449 |
436 VLOG(0) << "From:" << local_port_.ToString(); | 450 VLOG(0) << "From:" << local_port_.ToString(); |
437 VLOG(0) << "To:" << destination_.ToString(); | 451 VLOG(0) << "To:" << destination_.ToString(); |
438 | 452 |
439 CHECK_GE(socket_->Bind(local_port_), 0); | 453 CHECK_GE(socket_->Bind(local_port_), 0); |
440 | 454 |
441 start_event_.Signal(); | 455 start_event->Signal(); |
442 PollRead(); | 456 PollRead(); |
443 } | 457 } |
444 | 458 |
445 virtual ~UDPProxyImpl() { | 459 void Stop(base::WaitableEvent* stop_event) { |
446 proxy_thread_.Stop(); | 460 to_dest_pipe_.reset(NULL); |
| 461 from_dest_pipe_.reset(NULL); |
| 462 socket_.reset(NULL); |
| 463 stop_event->Signal(); |
447 } | 464 } |
448 | 465 |
449 void ProcessPacket(scoped_refptr<net::IOBuffer> recv_buf, | 466 void ProcessPacket(scoped_refptr<net::IOBuffer> recv_buf, |
450 int len) { | 467 int len) { |
451 DCHECK_NE(len, net::ERR_IO_PENDING); | 468 DCHECK_NE(len, net::ERR_IO_PENDING); |
452 VLOG(1) << "Got packet, len = " << len; | 469 VLOG(1) << "Got packet, len = " << len; |
453 if (len < 0) { | 470 if (len < 0) { |
454 LOG(WARNING) << "Socket read error: " << len; | 471 LOG(WARNING) << "Socket read error: " << len; |
455 return; | 472 return; |
456 } | 473 } |
(...skipping 24 matching lines...) Expand all Loading... |
481 &recv_address_, | 498 &recv_address_, |
482 base::Bind(&UDPProxyImpl::ReadCallback, | 499 base::Bind(&UDPProxyImpl::ReadCallback, |
483 base::Unretained(this), | 500 base::Unretained(this), |
484 recv_buf)); | 501 recv_buf)); |
485 if (len == net::ERR_IO_PENDING) | 502 if (len == net::ERR_IO_PENDING) |
486 break; | 503 break; |
487 ProcessPacket(recv_buf, len); | 504 ProcessPacket(recv_buf, len); |
488 } | 505 } |
489 } | 506 } |
490 | 507 |
491 private: | 508 |
492 net::IPEndPoint local_port_; | 509 net::IPEndPoint local_port_; |
493 net::IPEndPoint destination_; | 510 net::IPEndPoint destination_; |
494 net::IPEndPoint recv_address_; | 511 net::IPEndPoint recv_address_; |
495 net::IPEndPoint return_address_; | 512 net::IPEndPoint return_address_; |
496 base::Thread proxy_thread_; | 513 base::Thread proxy_thread_; |
497 scoped_ptr<net::UDPSocket> socket_; | 514 scoped_ptr<net::UDPSocket> socket_; |
498 scoped_ptr<PacketPipe> to_dest_pipe_; | 515 scoped_ptr<PacketPipe> to_dest_pipe_; |
499 scoped_ptr<PacketPipe> from_dest_pipe_; | 516 scoped_ptr<PacketPipe> from_dest_pipe_; |
500 base::WaitableEvent start_event_; | |
501 scoped_ptr<transport::Packet> packet_; | 517 scoped_ptr<transport::Packet> packet_; |
502 }; | 518 }; |
503 | 519 |
504 scoped_ptr<UDPProxy> UDPProxy::Create( | 520 scoped_ptr<UDPProxy> UDPProxy::Create( |
505 const net::IPEndPoint& local_port, | 521 const net::IPEndPoint& local_port, |
506 const net::IPEndPoint& destination, | 522 const net::IPEndPoint& destination, |
507 scoped_ptr<PacketPipe> to_dest_pipe, | 523 scoped_ptr<PacketPipe> to_dest_pipe, |
508 scoped_ptr<PacketPipe> from_dest_pipe, | 524 scoped_ptr<PacketPipe> from_dest_pipe, |
509 net::NetLog* net_log) { | 525 net::NetLog* net_log) { |
510 scoped_ptr<UDPProxy> ret(new UDPProxyImpl(local_port, | 526 scoped_ptr<UDPProxy> ret(new UDPProxyImpl(local_port, |
511 destination, | 527 destination, |
512 to_dest_pipe.Pass(), | 528 to_dest_pipe.Pass(), |
513 from_dest_pipe.Pass(), | 529 from_dest_pipe.Pass(), |
514 net_log)); | 530 net_log)); |
515 return ret.Pass(); | 531 return ret.Pass(); |
516 } | 532 } |
517 | 533 |
518 } // namespace test | 534 } // namespace test |
519 } // namespace cast | 535 } // namespace cast |
520 } // namespace media | 536 } // namespace media |
OLD | NEW |