Artem Titov | 0774bd9 | 2019-01-30 14:26:05 | [diff] [blame^] | 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_SCENARIO_NETWORK_FAKE_NETWORK_SOCKET_SERVER_H_ |
| 12 | #define TEST_SCENARIO_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/critical_section.h" |
| 20 | #include "rtc_base/event.h" |
| 21 | #include "rtc_base/message_queue.h" |
| 22 | #include "rtc_base/socket.h" |
| 23 | #include "rtc_base/socket_address.h" |
| 24 | #include "rtc_base/socket_server.h" |
| 25 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 26 | #include "system_wrappers/include/clock.h" |
| 27 | #include "test/scenario/network/fake_network_socket.h" |
| 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
| 32 | // FakeNetworkSocketServer must outlive any sockets it creates. |
| 33 | class FakeNetworkSocketServer : public rtc::SocketServer, |
| 34 | public sigslot::has_slots<>, |
| 35 | public SocketManager { |
| 36 | public: |
| 37 | FakeNetworkSocketServer(Clock* clock, std::vector<EndpointNode*> endpoints); |
| 38 | ~FakeNetworkSocketServer() override; |
| 39 | |
| 40 | EndpointNode* GetEndpointNode(const rtc::IPAddress& ip) override; |
| 41 | void Unregister(SocketIoProcessor* io_processor) override; |
| 42 | void OnMessageQueueDestroyed(); |
| 43 | |
| 44 | // rtc::SocketFactory methods: |
| 45 | rtc::Socket* CreateSocket(int family, int type) override; |
| 46 | rtc::AsyncSocket* CreateAsyncSocket(int family, int type) override; |
| 47 | |
| 48 | // rtc::SocketServer methods: |
| 49 | // Called by the network thread when this server is installed, kicking off the |
| 50 | // message handler loop. |
| 51 | void SetMessageQueue(rtc::MessageQueue* msg_queue) override; |
| 52 | bool Wait(int cms, bool process_io) override; |
| 53 | void WakeUp() override; |
| 54 | |
| 55 | private: |
| 56 | Timestamp Now() const; |
| 57 | |
| 58 | Clock* const clock_; |
| 59 | const std::vector<EndpointNode*> endpoints_; |
| 60 | rtc::Event wakeup_; |
| 61 | rtc::MessageQueue* msg_queue_; |
| 62 | |
| 63 | rtc::CriticalSection lock_; |
| 64 | std::set<SocketIoProcessor*> io_processors_ RTC_GUARDED_BY(lock_); |
| 65 | }; |
| 66 | |
| 67 | } // namespace test |
| 68 | } // namespace webrtc |
| 69 | |
| 70 | #endif // TEST_SCENARIO_NETWORK_FAKE_NETWORK_SOCKET_SERVER_H_ |