| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/transport/transport.h" | 5 #include "media/cast/test/transport/transport.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 14 #include "net/base/completion_callback.h" | |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/rand_callback.h" | 16 #include "net/base/rand_callback.h" |
| 17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
| 18 | 18 |
| 19 namespace media { | 19 namespace media { |
| 20 namespace cast { | 20 namespace cast { |
| 21 namespace test { | 21 namespace test { |
| 22 | 22 |
| 23 const int kMaxPacketSize = 1500; | 23 const int kMaxPacketSize = 1500; |
| 24 | 24 |
| 25 class LocalUdpTransportData; | 25 class LocalUdpTransportData; |
| 26 | 26 |
| 27 void CreateUDPAddress(std::string ip_str, int port, net::IPEndPoint* address) { | 27 void CreateUDPAddress(std::string ip_str, int port, net::IPEndPoint* address) { |
| 28 net::IPAddressNumber ip_number; | 28 net::IPAddressNumber ip_number; |
| 29 bool rv = net::ParseIPLiteralToNumber(ip_str, &ip_number); | 29 bool rv = net::ParseIPLiteralToNumber(ip_str, &ip_number); |
| 30 if (!rv) | 30 if (!rv) |
| 31 return; | 31 return; |
| 32 *address = net::IPEndPoint(ip_number, port); | 32 *address = net::IPEndPoint(ip_number, port); |
| 33 } | 33 } |
| 34 | 34 |
| 35 class LocalUdpTransportData | 35 class LocalUdpTransportData |
| 36 : public base::RefCountedThreadSafe<LocalUdpTransportData> { | 36 : public base::RefCountedThreadSafe<LocalUdpTransportData> { |
| 37 public: | 37 public: |
| 38 LocalUdpTransportData(net::DatagramServerSocket* udp_socket, | 38 LocalUdpTransportData(net::UDPServerSocket* udp_socket, |
| 39 scoped_refptr<base::TaskRunner> io_thread_proxy) | 39 scoped_refptr<base::TaskRunner> io_thread_proxy) |
| 40 : udp_socket_(udp_socket), | 40 : udp_socket_(udp_socket), |
| 41 buffer_(new net::IOBufferWithSize(kMaxPacketSize)), | 41 buffer_(new net::IOBufferWithSize(kMaxPacketSize)), |
| 42 io_thread_proxy_(io_thread_proxy) { | 42 io_thread_proxy_(io_thread_proxy) { |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ListenTo(net::IPEndPoint bind_address) { | 45 void ListenTo(net::IPEndPoint bind_address) { |
| 46 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 46 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); |
| 47 | 47 |
| 48 bind_address_ = bind_address; | 48 bind_address_ = bind_address; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 void Close() { | 86 void Close() { |
| 87 udp_socket_->Close(); | 87 udp_socket_->Close(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 protected: | 90 protected: |
| 91 virtual ~LocalUdpTransportData() {} | 91 virtual ~LocalUdpTransportData() {} |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 friend class base::RefCountedThreadSafe<LocalUdpTransportData>; | 94 friend class base::RefCountedThreadSafe<LocalUdpTransportData>; |
| 95 | 95 |
| 96 net::DatagramServerSocket* udp_socket_; | 96 net::UDPServerSocket* udp_socket_; |
| 97 net::IPEndPoint bind_address_; | 97 net::IPEndPoint bind_address_; |
| 98 PacketReceiver* packet_receiver_; | 98 PacketReceiver* packet_receiver_; |
| 99 scoped_refptr<net::IOBufferWithSize> buffer_; | 99 scoped_refptr<net::IOBufferWithSize> buffer_; |
| 100 scoped_refptr<base::TaskRunner> io_thread_proxy_; | 100 scoped_refptr<base::TaskRunner> io_thread_proxy_; |
| 101 | 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(LocalUdpTransportData); | 102 DISALLOW_COPY_AND_ASSIGN(LocalUdpTransportData); |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 class LocalPacketSender : public PacketSender, | 105 class LocalPacketSender : public PacketSender, |
| 106 public base::RefCountedThreadSafe<LocalPacketSender> { | 106 public base::RefCountedThreadSafe<LocalPacketSender> { |
| 107 public: | 107 public: |
| 108 LocalPacketSender(net::DatagramServerSocket* udp_socket, | 108 LocalPacketSender(net::UDPServerSocket* udp_socket, |
| 109 scoped_refptr<base::TaskRunner> io_thread_proxy) | 109 scoped_refptr<base::TaskRunner> io_thread_proxy) |
| 110 : udp_socket_(udp_socket), | 110 : udp_socket_(udp_socket), |
| 111 send_address_(), | 111 send_address_(), |
| 112 loss_limit_(0), | 112 loss_limit_(0), |
| 113 io_thread_proxy_(io_thread_proxy) {} | 113 io_thread_proxy_(io_thread_proxy) {} |
| 114 | 114 |
| 115 virtual bool SendPacket(const Packet& packet) OVERRIDE { | 115 virtual bool SendPacket(const Packet& packet) OVERRIDE { |
| 116 io_thread_proxy_->PostTask(FROM_HERE, | 116 io_thread_proxy_->PostTask(FROM_HERE, |
| 117 base::Bind(&LocalPacketSender::SendPacketToNetwork, | 117 base::Bind(&LocalPacketSender::SendPacketToNetwork, |
| 118 this, packet)); | 118 this, packet)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void SetSendAddress(const net::IPEndPoint& send_address) { | 155 void SetSendAddress(const net::IPEndPoint& send_address) { |
| 156 send_address_ = send_address; | 156 send_address_ = send_address; |
| 157 } | 157 } |
| 158 | 158 |
| 159 protected: | 159 protected: |
| 160 virtual ~LocalPacketSender() {} | 160 virtual ~LocalPacketSender() {} |
| 161 | 161 |
| 162 private: | 162 private: |
| 163 friend class base::RefCountedThreadSafe<LocalPacketSender>; | 163 friend class base::RefCountedThreadSafe<LocalPacketSender>; |
| 164 | 164 |
| 165 net::DatagramServerSocket* udp_socket_; // Not owned by this class. | 165 net::UDPServerSocket* udp_socket_; // Not owned by this class. |
| 166 net::IPEndPoint send_address_; | 166 net::IPEndPoint send_address_; |
| 167 int loss_limit_; | 167 int loss_limit_; |
| 168 scoped_refptr<base::TaskRunner> io_thread_proxy_; | 168 scoped_refptr<base::TaskRunner> io_thread_proxy_; |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 Transport::Transport( | 171 Transport::Transport( |
| 172 scoped_refptr<base::TaskRunner> io_thread_proxy) | 172 scoped_refptr<base::TaskRunner> io_thread_proxy) |
| 173 : udp_socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), | 173 : udp_socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), |
| 174 local_udp_transport_data_(new LocalUdpTransportData(udp_socket_.get(), | 174 local_udp_transport_data_(new LocalUdpTransportData(udp_socket_.get(), |
| 175 io_thread_proxy)), | 175 io_thread_proxy)), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 209 |
| 210 void Transport::SetSendDestination(std::string ip_address, int port) { | 210 void Transport::SetSendDestination(std::string ip_address, int port) { |
| 211 net::IPEndPoint send_address; | 211 net::IPEndPoint send_address; |
| 212 CreateUDPAddress(ip_address, port, &send_address); | 212 CreateUDPAddress(ip_address, port, &send_address); |
| 213 packet_sender_->SetSendAddress(send_address); | 213 packet_sender_->SetSendAddress(send_address); |
| 214 } | 214 } |
| 215 | 215 |
| 216 } // namespace test | 216 } // namespace test |
| 217 } // namespace cast | 217 } // namespace cast |
| 218 } // namespace media | 218 } // namespace media |
| OLD | NEW |