blob: e7985db7db6c8d97edf672b0e5a277ea469da5d9 [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_PHYSICAL_SOCKET_SERVER_H_
12#define RTC_BASE_PHYSICAL_SOCKET_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_POSIX) && defined(WEBRTC_LINUX)
15#include <sys/epoll.h>
16#define WEBRTC_USE_EPOLL 1
17#endif
jbauchde4db112017-05-31 13:09:18 -070018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#include <memory>
20#include <set>
21#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
24#include "rtc_base/net_helpers.h"
25#include "rtc_base/socket_server.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020026#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
28#if defined(WEBRTC_POSIX)
29typedef int SOCKET;
Yves Gerey665174f2018-06-19 15:03:05 +020030#endif // WEBRTC_POSIX
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031
32namespace rtc {
33
34// Event constants for the Dispatcher class.
35enum DispatcherEvent {
Yves Gerey665174f2018-06-19 15:03:05 +020036 DE_READ = 0x0001,
37 DE_WRITE = 0x0002,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 DE_CONNECT = 0x0004,
Yves Gerey665174f2018-06-19 15:03:05 +020039 DE_CLOSE = 0x0008,
40 DE_ACCEPT = 0x0010,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041};
42
43class Signaler;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044
45class Dispatcher {
46 public:
47 virtual ~Dispatcher() {}
48 virtual uint32_t GetRequestedEvents() = 0;
49 virtual void OnPreEvent(uint32_t ff) = 0;
50 virtual void OnEvent(uint32_t ff, int err) = 0;
51#if defined(WEBRTC_WIN)
52 virtual WSAEVENT GetWSAEvent() = 0;
53 virtual SOCKET GetSocket() = 0;
54 virtual bool CheckSignalClose() = 0;
55#elif defined(WEBRTC_POSIX)
56 virtual int GetDescriptor() = 0;
57 virtual bool IsDescriptorClosed() = 0;
58#endif
59};
60
61// A socket server that provides the real sockets of the underlying OS.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020062class RTC_EXPORT PhysicalSocketServer : public SocketServer {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063 public:
64 PhysicalSocketServer();
65 ~PhysicalSocketServer() override;
66
67 // SocketFactory:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 Socket* CreateSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069 AsyncSocket* CreateAsyncSocket(int family, int type) override;
70
71 // Internal Factory for Accept (virtual so it can be overwritten in tests).
72 virtual AsyncSocket* WrapSocket(SOCKET s);
73
74 // SocketServer:
75 bool Wait(int cms, bool process_io) override;
76 void WakeUp() override;
77
78 void Add(Dispatcher* dispatcher);
79 void Remove(Dispatcher* dispatcher);
80 void Update(Dispatcher* dispatcher);
81
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082 private:
83 typedef std::set<Dispatcher*> DispatcherSet;
84
85 void AddRemovePendingDispatchers();
86
87#if defined(WEBRTC_POSIX)
88 bool WaitSelect(int cms, bool process_io);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089#endif // WEBRTC_POSIX
90#if defined(WEBRTC_USE_EPOLL)
91 void AddEpoll(Dispatcher* dispatcher);
92 void RemoveEpoll(Dispatcher* dispatcher);
93 void UpdateEpoll(Dispatcher* dispatcher);
94 bool WaitEpoll(int cms);
95 bool WaitPoll(int cms, Dispatcher* dispatcher);
96
Niels Möller611fba42020-05-13 14:42:22 +020097 const int epoll_fd_ = INVALID_SOCKET;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098 std::vector<struct epoll_event> epoll_events_;
99#endif // WEBRTC_USE_EPOLL
100 DispatcherSet dispatchers_;
101 DispatcherSet pending_add_dispatchers_;
102 DispatcherSet pending_remove_dispatchers_;
103 bool processing_dispatchers_ = false;
104 Signaler* signal_wakeup_;
105 CriticalSection crit_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106#if defined(WEBRTC_WIN)
Niels Möller611fba42020-05-13 14:42:22 +0200107 const WSAEVENT socket_ev_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108#endif
Niels Möller611fba42020-05-13 14:42:22 +0200109 bool fWait_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110};
111
112class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> {
113 public:
114 PhysicalSocket(PhysicalSocketServer* ss, SOCKET s = INVALID_SOCKET);
115 ~PhysicalSocket() override;
116
117 // Creates the underlying OS socket (same as the "socket" function).
118 virtual bool Create(int family, int type);
119
120 SocketAddress GetLocalAddress() const override;
121 SocketAddress GetRemoteAddress() const override;
122
123 int Bind(const SocketAddress& bind_addr) override;
124 int Connect(const SocketAddress& addr) override;
125
126 int GetError() const override;
127 void SetError(int error) override;
128
129 ConnState GetState() const override;
130
131 int GetOption(Option opt, int* value) override;
132 int SetOption(Option opt, int value) override;
133
134 int Send(const void* pv, size_t cb) override;
135 int SendTo(const void* buffer,
136 size_t length,
137 const SocketAddress& addr) override;
138
139 int Recv(void* buffer, size_t length, int64_t* timestamp) override;
140 int RecvFrom(void* buffer,
141 size_t length,
142 SocketAddress* out_addr,
143 int64_t* timestamp) override;
144
145 int Listen(int backlog) override;
146 AsyncSocket* Accept(SocketAddress* out_addr) override;
147
148 int Close() override;
149
150 SocketServer* socketserver() { return ss_; }
151
152 protected:
153 int DoConnect(const SocketAddress& connect_addr);
154
155 // Make virtual so ::accept can be overwritten in tests.
156 virtual SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen);
157
158 // Make virtual so ::send can be overwritten in tests.
159 virtual int DoSend(SOCKET socket, const char* buf, int len, int flags);
160
161 // Make virtual so ::sendto can be overwritten in tests.
Yves Gerey665174f2018-06-19 15:03:05 +0200162 virtual int DoSendTo(SOCKET socket,
163 const char* buf,
164 int len,
165 int flags,
166 const struct sockaddr* dest_addr,
167 socklen_t addrlen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200168
169 void OnResolveResult(AsyncResolverInterface* resolver);
170
171 void UpdateLastError();
172 void MaybeRemapSendError();
173
174 uint8_t enabled_events() const { return enabled_events_; }
175 virtual void SetEnabledEvents(uint8_t events);
176 virtual void EnableEvents(uint8_t events);
177 virtual void DisableEvents(uint8_t events);
178
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800179 int TranslateOption(Option opt, int* slevel, int* sopt);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200180
181 PhysicalSocketServer* ss_;
182 SOCKET s_;
183 bool udp_;
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800184 int family_ = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185 CriticalSection crit_;
danilchap3c6abd22017-09-06 05:46:29 -0700186 int error_ RTC_GUARDED_BY(crit_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200187 ConnState state_;
188 AsyncResolver* resolver_;
189
190#if !defined(NDEBUG)
191 std::string dbg_addr_;
192#endif
193
194 private:
195 uint8_t enabled_events_ = 0;
196};
197
198class SocketDispatcher : public Dispatcher, public PhysicalSocket {
199 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200200 explicit SocketDispatcher(PhysicalSocketServer* ss);
201 SocketDispatcher(SOCKET s, PhysicalSocketServer* ss);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200202 ~SocketDispatcher() override;
203
204 bool Initialize();
205
206 virtual bool Create(int type);
207 bool Create(int family, int type) override;
208
209#if defined(WEBRTC_WIN)
210 WSAEVENT GetWSAEvent() override;
211 SOCKET GetSocket() override;
212 bool CheckSignalClose() override;
213#elif defined(WEBRTC_POSIX)
214 int GetDescriptor() override;
215 bool IsDescriptorClosed() override;
216#endif
217
218 uint32_t GetRequestedEvents() override;
219 void OnPreEvent(uint32_t ff) override;
220 void OnEvent(uint32_t ff, int err) override;
221
222 int Close() override;
223
224#if defined(WEBRTC_USE_EPOLL)
225 protected:
226 void StartBatchedEventUpdates();
227 void FinishBatchedEventUpdates();
228
229 void SetEnabledEvents(uint8_t events) override;
230 void EnableEvents(uint8_t events) override;
231 void DisableEvents(uint8_t events) override;
232#endif
233
234 private:
235#if defined(WEBRTC_WIN)
236 static int next_id_;
237 int id_;
238 bool signal_close_;
239 int signal_err_;
Yves Gerey665174f2018-06-19 15:03:05 +0200240#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200241#if defined(WEBRTC_USE_EPOLL)
242 void MaybeUpdateDispatcher(uint8_t old_events);
243
244 int saved_enabled_events_ = -1;
245#endif
246};
247
Yves Gerey665174f2018-06-19 15:03:05 +0200248} // namespace rtc
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200249
Steve Anton10542f22019-01-11 09:11:00 -0800250#endif // RTC_BASE_PHYSICAL_SOCKET_SERVER_H_