blob: bf1326dad9fe47d58daa55417811ab891d96cdc8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_SOCKET_SERVER_H_
12#define RTC_BASE_SOCKET_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <memory>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Markus Handell9a21c492022-08-25 11:40:13 +000016#include "api/units/time_delta.h"
17#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/socket_factory.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020namespace rtc {
21
Sebastian Jansson290de822020-01-09 14:20:23 +010022class Thread;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023// Needs to be forward declared because there's a circular dependency between
24// NetworkMonitor and Thread.
25// TODO(deadbeef): Fix this.
26class 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.
33class SocketServer : public SocketFactory {
34 public:
Markus Handell9a21c492022-08-25 11:40:13 +000035 static constexpr webrtc::TimeDelta kForever = rtc::Event::kForever;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036
37 static std::unique_ptr<SocketServer> CreateDefault();
Niels Möller9bd24572021-04-19 12:18:27 +020038 // 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 Jansson290de822020-01-09 14:20:23 +010042 virtual void SetMessageQueue(Thread* queue) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
44 // Sleeps until:
Markus Handell9a21c492022-08-25 11:40:13 +000045 // 1) `max_wait_duration` has elapsed (unless `max_wait_duration` ==
46 // `kForever`)
47 // 2) WakeUp() is called
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048 // While sleeping, I/O is performed if process_io is true.
Markus Handell9a21c492022-08-25 11:40:13 +000049 virtual bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050
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.orgf0488722014-05-13 18:00:26 +000066
Steve Anton10542f22019-01-11 09:11:00 -080067#endif // RTC_BASE_SOCKET_SERVER_H_