xref: /aosp_15_r20/external/webrtc/test/network/fake_network_socket_server.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_FAKE_NETWORK_SOCKET_SERVER_H_
12 #define TEST_NETWORK_FAKE_NETWORK_SOCKET_SERVER_H_
13 
14 #include <set>
15 #include <vector>
16 
17 #include "api/units/timestamp.h"
18 #include "rtc_base/event.h"
19 #include "rtc_base/socket.h"
20 #include "rtc_base/socket_server.h"
21 #include "rtc_base/synchronization/mutex.h"
22 #include "system_wrappers/include/clock.h"
23 #include "test/network/network_emulation.h"
24 
25 namespace webrtc {
26 namespace test {
27 class FakeNetworkSocket;
28 
29 // FakeNetworkSocketServer must outlive any sockets it creates.
30 class FakeNetworkSocketServer : public rtc::SocketServer {
31  public:
32   explicit FakeNetworkSocketServer(EndpointsContainer* endpoints_controller);
33   ~FakeNetworkSocketServer() override;
34 
35 
36   // rtc::SocketFactory methods:
37   rtc::Socket* CreateSocket(int family, int type) override;
38 
39   // rtc::SocketServer methods:
40   // Called by the network thread when this server is installed, kicking off the
41   // message handler loop.
42   void SetMessageQueue(rtc::Thread* thread) override;
43   bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override;
44   void WakeUp() override;
45 
46  protected:
47   friend class FakeNetworkSocket;
48   EmulatedEndpointImpl* GetEndpointNode(const rtc::IPAddress& ip);
49   void Unregister(FakeNetworkSocket* socket);
50 
51  private:
52   const EndpointsContainer* endpoints_container_;
53   rtc::Event wakeup_;
54   rtc::Thread* thread_ = nullptr;
55 
56   Mutex lock_;
57   std::vector<FakeNetworkSocket*> sockets_ RTC_GUARDED_BY(lock_);
58 };
59 
60 }  // namespace test
61 }  // namespace webrtc
62 
63 #endif  // TEST_NETWORK_FAKE_NETWORK_SOCKET_SERVER_H_
64