xref: /aosp_15_r20/external/webrtc/rtc_base/socket_server.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2004 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 RTC_BASE_SOCKET_SERVER_H_
12 #define RTC_BASE_SOCKET_SERVER_H_
13 
14 #include <memory>
15 
16 #include "api/units/time_delta.h"
17 #include "rtc_base/event.h"
18 #include "rtc_base/socket_factory.h"
19 
20 namespace rtc {
21 
22 class Thread;
23 // Needs to be forward declared because there's a circular dependency between
24 // NetworkMonitor and Thread.
25 // TODO(deadbeef): Fix this.
26 class NetworkBinderInterface;
27 
28 // Provides the ability to wait for activity on a set of sockets.  The Thread
29 // class provides a nice wrapper on a socket server.
30 //
31 // The server is also a socket factory.  The sockets it creates will be
32 // notified of asynchronous I/O from this server's Wait method.
33 class SocketServer : public SocketFactory {
34  public:
35   static constexpr webrtc::TimeDelta kForever = rtc::Event::kForever;
36 
37   static std::unique_ptr<SocketServer> CreateDefault();
38   // When the socket server is installed into a Thread, this function is called
39   // to allow the socket server to use the thread's message queue for any
40   // messaging that it might need to perform. It is also called with a null
41   // argument before the thread is destroyed.
SetMessageQueue(Thread * queue)42   virtual void SetMessageQueue(Thread* queue) {}
43 
44   // Sleeps until:
45   //  1) `max_wait_duration` has elapsed (unless `max_wait_duration` ==
46   //  `kForever`)
47   // 2) WakeUp() is called
48   // While sleeping, I/O is performed if process_io is true.
49   virtual bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) = 0;
50 
51   // Causes the current wait (if one is in progress) to wake up.
52   virtual void WakeUp() = 0;
53 
54   // A network binder will bind the created sockets to a network.
55   // It is only used in PhysicalSocketServer.
set_network_binder(NetworkBinderInterface * binder)56   void set_network_binder(NetworkBinderInterface* binder) {
57     network_binder_ = binder;
58   }
network_binder()59   NetworkBinderInterface* network_binder() const { return network_binder_; }
60 
61  private:
62   NetworkBinderInterface* network_binder_ = nullptr;
63 };
64 
65 }  // namespace rtc
66 
67 #endif  // RTC_BASE_SOCKET_SERVER_H_
68