-// Shim that turns forwards packets from a test::PacketPipe to a
-// PacketReceiverCallback.
-class LoopBackPacketPipe : public test::PacketPipe {
- public:
- LoopBackPacketPipe(const transport::PacketReceiverCallback& packet_receiver)
- : packet_receiver_(packet_receiver) {}
-
- virtual ~LoopBackPacketPipe() {}
-
- // PacketPipe implementations.
- virtual void Send(scoped_ptr<transport::Packet> packet) OVERRIDE {
- packet_receiver_.Run(packet.Pass());
- }
-
- private:
- transport::PacketReceiverCallback packet_receiver_;
-};
-
-// Class that sends the packet direct from sender into the receiver with the
-// ability to drop packets between the two.
-// TODO(hubbe): Break this out and share code with end2end_unittest.cc
-class LoopBackTransport : public transport::PacketSender {
- public:
- explicit LoopBackTransport(scoped_refptr<CastEnvironment> cast_environment)
- : cast_environment_(cast_environment) {}
-
- void SetPacketReceiver(
- const transport::PacketReceiverCallback& packet_receiver,
- const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
- base::TickClock* clock) {
- scoped_ptr<test::PacketPipe> loopback_pipe(
- new LoopBackPacketPipe(packet_receiver));
- if (packet_pipe_) {
- packet_pipe_->AppendToPipe(loopback_pipe.Pass());
- } else {
- packet_pipe_ = loopback_pipe.Pass();
- }
- packet_pipe_->InitOnIOThread(task_runner, clock);
- }
-
- virtual bool SendPacket(transport::PacketRef packet,
- const base::Closure& cb) OVERRIDE {
- DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
- scoped_ptr<Packet> packet_copy(new Packet(packet->data));
- packet_pipe_->Send(packet_copy.Pass());
- return true;
- }
-
- void SetPacketPipe(scoped_ptr<test::PacketPipe> pipe) {
- // Append the loopback pipe to the end.
- pipe->AppendToPipe(packet_pipe_.Pass());
- packet_pipe_ = pipe.Pass();
- }
-
- private:
- scoped_refptr<CastEnvironment> cast_environment_;
- scoped_ptr<test::PacketPipe> packet_pipe_;
-};
-