xref: /aosp_15_r20/external/webrtc/test/network/traffic_route.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "test/network/traffic_route.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <utility>
16 
17 #include "absl/types/optional.h"
18 #include "rtc_base/logging.h"
19 #include "rtc_base/numerics/safe_minmax.h"
20 
21 namespace webrtc {
22 namespace test {
23 namespace {
24 
25 class NullReceiver : public EmulatedNetworkReceiverInterface {
26  public:
OnPacketReceived(EmulatedIpPacket packet)27   void OnPacketReceived(EmulatedIpPacket packet) override {}
28 };
29 
30 class ActionReceiver : public EmulatedNetworkReceiverInterface {
31  public:
ActionReceiver(std::function<void ()> action)32   explicit ActionReceiver(std::function<void()> action) : action_(action) {}
33   ~ActionReceiver() override = default;
34 
OnPacketReceived(EmulatedIpPacket packet)35   void OnPacketReceived(EmulatedIpPacket packet) override {
36     action_();
37   }
38 
39  private:
40   std::function<void()> action_;
41 };
42 
43 }  // namespace
44 
CrossTrafficRouteImpl(Clock * clock,EmulatedNetworkReceiverInterface * receiver,EmulatedEndpointImpl * endpoint)45 CrossTrafficRouteImpl::CrossTrafficRouteImpl(
46     Clock* clock,
47     EmulatedNetworkReceiverInterface* receiver,
48     EmulatedEndpointImpl* endpoint)
49     : clock_(clock), receiver_(receiver), endpoint_(endpoint) {
50   null_receiver_ = std::make_unique<NullReceiver>();
51   absl::optional<uint16_t> port =
52       endpoint_->BindReceiver(0, null_receiver_.get());
53   RTC_DCHECK(port);
54   null_receiver_port_ = port.value();
55 }
56 CrossTrafficRouteImpl::~CrossTrafficRouteImpl() = default;
57 
TriggerPacketBurst(size_t num_packets,size_t packet_size)58 void CrossTrafficRouteImpl::TriggerPacketBurst(size_t num_packets,
59                                                size_t packet_size) {
60   for (size_t i = 0; i < num_packets; ++i) {
61     SendPacket(packet_size);
62   }
63 }
64 
NetworkDelayedAction(size_t packet_size,std::function<void ()> action)65 void CrossTrafficRouteImpl::NetworkDelayedAction(size_t packet_size,
66                                                  std::function<void()> action) {
67   auto action_receiver = std::make_unique<ActionReceiver>(action);
68   // BindOneShotReceiver arranges to free the port in the endpoint after the
69   // action is done.
70   absl::optional<uint16_t> port =
71       endpoint_->BindOneShotReceiver(0, action_receiver.get());
72   RTC_DCHECK(port);
73   actions_.push_back(std::move(action_receiver));
74   SendPacket(packet_size, port.value());
75 }
76 
SendPacket(size_t packet_size)77 void CrossTrafficRouteImpl::SendPacket(size_t packet_size) {
78   SendPacket(packet_size, null_receiver_port_);
79 }
80 
SendPacket(size_t packet_size,uint16_t dest_port)81 void CrossTrafficRouteImpl::SendPacket(size_t packet_size, uint16_t dest_port) {
82   rtc::CopyOnWriteBuffer data(packet_size);
83   std::fill_n(data.MutableData(), data.size(), 0);
84   receiver_->OnPacketReceived(EmulatedIpPacket(
85       /*from=*/rtc::SocketAddress(),
86       rtc::SocketAddress(endpoint_->GetPeerLocalAddress(), dest_port), data,
87       clock_->CurrentTime()));
88 }
89 
90 }  // namespace test
91 }  // namespace webrtc
92