network_tester: Remove usage of rtc::Thread::socketserver() and cleanup

Instead of creating a TaskQueue from packet_sender, create a rtc::Thread
in test_controller so that test_controller instantiates a SocketServer,
eliminating the use of rtc::Thread::socketserver().
Also did various cleanups, such as adding threading annotations, and
ensuring that all network operations are done in dedicated threads.

Bug: webrtc:13145
Test: Unittest, and manually verified using Android clients and Linux servers
Change-Id: I05ebe5e29bd80f14a193c9ee8b0bf63a1b6b94d7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/263321
Commit-Queue: Daniel.l Lee <daniel.l@hpcnt.com>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37411}
diff --git a/rtc_tools/network_tester/test_controller.h b/rtc_tools/network_tester/test_controller.h
index 3933b46..b08fbd5 100644
--- a/rtc_tools/network_tester/test_controller.h
+++ b/rtc_tools/network_tester/test_controller.h
@@ -25,6 +25,7 @@
 #include "rtc_base/ignore_wundef.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/synchronization/mutex.h"
+#include "rtc_base/system/no_unique_address.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread_annotations.h"
 #include "rtc_tools/network_tester/packet_logger.h"
@@ -49,12 +50,11 @@
                  int max_port,
                  const std::string& config_file_path,
                  const std::string& log_file_path);
+  ~TestController() override;
 
   TestController(const TestController&) = delete;
   TestController& operator=(const TestController&) = delete;
 
-  void Run();
-
   void SendConnectTo(const std::string& hostname, int port);
 
   void SendData(const NetworkTesterPacket& packet,
@@ -70,18 +70,24 @@
                     size_t len,
                     const rtc::SocketAddress& remote_addr,
                     const int64_t& packet_time_us);
-  SequenceChecker test_controller_thread_checker_;
-  SequenceChecker packet_sender_checker_;
-  rtc::BasicPacketSocketFactory socket_factory_;
+  RTC_NO_UNIQUE_ADDRESS SequenceChecker test_controller_thread_checker_;
+  std::unique_ptr<rtc::SocketServer> socket_server_;
+  std::unique_ptr<rtc::Thread> packet_sender_thread_;
+  rtc::BasicPacketSocketFactory socket_factory_
+      RTC_GUARDED_BY(packet_sender_thread_);
   const std::string config_file_path_;
-  PacketLogger packet_logger_;
-  Mutex local_test_done_lock_;
-  bool local_test_done_ RTC_GUARDED_BY(local_test_done_lock_);
-  bool remote_test_done_;
-  std::array<char, kEthernetMtu> send_data_;
-  std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_;
+  PacketLogger packet_logger_ RTC_GUARDED_BY(packet_sender_thread_);
+  Mutex test_done_lock_ RTC_GUARDED_BY(test_controller_thread_checker_);
+  bool local_test_done_ RTC_GUARDED_BY(test_done_lock_);
+  bool remote_test_done_ RTC_GUARDED_BY(test_done_lock_);
+  std::array<char, kEthernetMtu> send_data_
+      RTC_GUARDED_BY(packet_sender_thread_);
+  std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_
+      RTC_GUARDED_BY(packet_sender_thread_);
   rtc::SocketAddress remote_address_;
-  std::unique_ptr<PacketSender> packet_sender_;
+  std::unique_ptr<PacketSender> packet_sender_
+      RTC_GUARDED_BY(packet_sender_thread_);
+  rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> task_safety_flag_;
 };
 
 }  // namespace webrtc