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