xref: /aosp_15_r20/external/webrtc/test/network/traffic_route.h (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 #ifndef TEST_NETWORK_TRAFFIC_ROUTE_H_
12 #define TEST_NETWORK_TRAFFIC_ROUTE_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "api/test/network_emulation_manager.h"
18 #include "rtc_base/copy_on_write_buffer.h"
19 #include "system_wrappers/include/clock.h"
20 #include "test/network/network_emulation.h"
21 
22 namespace webrtc {
23 namespace test {
24 
25 // Represents the endpoint for cross traffic that is going through the network.
26 // It can be used to emulate unexpected network load.
27 class CrossTrafficRouteImpl final : public CrossTrafficRoute {
28  public:
29   CrossTrafficRouteImpl(Clock* clock,
30                         EmulatedNetworkReceiverInterface* receiver,
31                         EmulatedEndpointImpl* endpoint);
32   ~CrossTrafficRouteImpl();
33 
34   // Triggers sending of dummy packets with size `packet_size` bytes.
35   void TriggerPacketBurst(size_t num_packets, size_t packet_size) override;
36   // Sends a packet over the nodes and runs `action` when it has been delivered.
37   void NetworkDelayedAction(size_t packet_size,
38                             std::function<void()> action) override;
39 
40   void SendPacket(size_t packet_size) override;
41 
42  private:
43   void SendPacket(size_t packet_size, uint16_t dest_port);
44 
45   Clock* const clock_;
46   EmulatedNetworkReceiverInterface* const receiver_;
47   EmulatedEndpointImpl* const endpoint_;
48 
49   uint16_t null_receiver_port_;
50   std::unique_ptr<EmulatedNetworkReceiverInterface> null_receiver_;
51   std::vector<std::unique_ptr<EmulatedNetworkReceiverInterface>> actions_;
52 };
53 
54 }  // namespace test
55 }  // namespace webrtc
56 
57 #endif  // TEST_NETWORK_TRAFFIC_ROUTE_H_
58