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