henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_SOCKET_SERVER_H_ |
| 12 | #define RTC_BASE_SOCKET_SERVER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <memory> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 16 | #include "api/units/time_delta.h" |
| 17 | #include "rtc_base/event.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "rtc_base/socket_factory.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 20 | namespace rtc { |
| 21 | |
Sebastian Jansson | 290de82 | 2020-01-09 13:20:23 | [diff] [blame] | 22 | class Thread; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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: |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 35 | static constexpr webrtc::TimeDelta kForever = rtc::Event::kForever; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 36 | |
| 37 | static std::unique_ptr<SocketServer> CreateDefault(); |
Niels Möller | 9bd2457 | 2021-04-19 10:18:27 | [diff] [blame] | 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. |
Sebastian Jansson | 290de82 | 2020-01-09 13:20:23 | [diff] [blame] | 42 | virtual void SetMessageQueue(Thread* queue) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 43 | |
| 44 | // Sleeps until: |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 45 | // 1) `max_wait_duration` has elapsed (unless `max_wait_duration` == |
| 46 | // `kForever`) |
| 47 | // 2) WakeUp() is called |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 48 | // While sleeping, I/O is performed if process_io is true. |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 49 | virtual bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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. |
| 56 | void set_network_binder(NetworkBinderInterface* binder) { |
| 57 | network_binder_ = binder; |
| 58 | } |
| 59 | NetworkBinderInterface* network_binder() const { return network_binder_; } |
| 60 | |
| 61 | private: |
| 62 | NetworkBinderInterface* network_binder_ = nullptr; |
| 63 | }; |
| 64 | |
| 65 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 66 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 67 | #endif // RTC_BASE_SOCKET_SERVER_H_ |