blob: 3437eb8cb3e0392a3fa7538b81928d0856157957 [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
11#ifndef WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
12#define WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
13
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <vector>
16
17#include "webrtc/base/asyncfile.h"
jbauch095ae152015-12-18 01:39:55 -080018#include "webrtc/base/nethelpers.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#include "webrtc/base/socketserver.h"
20#include "webrtc/base/criticalsection.h"
21
22#if defined(WEBRTC_POSIX)
23typedef int SOCKET;
24#endif // WEBRTC_POSIX
25
26namespace rtc {
27
28// Event constants for the Dispatcher class.
29enum DispatcherEvent {
30 DE_READ = 0x0001,
31 DE_WRITE = 0x0002,
32 DE_CONNECT = 0x0004,
33 DE_CLOSE = 0x0008,
34 DE_ACCEPT = 0x0010,
35};
36
37class Signaler;
38#if defined(WEBRTC_POSIX)
39class PosixSignalDispatcher;
40#endif
41
42class Dispatcher {
43 public:
44 virtual ~Dispatcher() {}
Peter Boström0c4e06b2015-10-07 12:23:21 +020045 virtual uint32_t GetRequestedEvents() = 0;
46 virtual void OnPreEvent(uint32_t ff) = 0;
47 virtual void OnEvent(uint32_t ff, int err) = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048#if defined(WEBRTC_WIN)
49 virtual WSAEVENT GetWSAEvent() = 0;
50 virtual SOCKET GetSocket() = 0;
51 virtual bool CheckSignalClose() = 0;
52#elif defined(WEBRTC_POSIX)
53 virtual int GetDescriptor() = 0;
54 virtual bool IsDescriptorClosed() = 0;
55#endif
56};
57
58// A socket server that provides the real sockets of the underlying OS.
59class PhysicalSocketServer : public SocketServer {
60 public:
61 PhysicalSocketServer();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000062 ~PhysicalSocketServer() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
64 // SocketFactory:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065 Socket* CreateSocket(int type) override;
66 Socket* CreateSocket(int family, int type) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000068 AsyncSocket* CreateAsyncSocket(int type) override;
69 AsyncSocket* CreateAsyncSocket(int family, int type) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070
jbauchf2a2bf42016-02-03 16:45:32 -080071 // Internal Factory for Accept (virtual so it can be overwritten in tests).
72 virtual AsyncSocket* WrapSocket(SOCKET s);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
74 // SocketServer:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000075 bool Wait(int cms, bool process_io) override;
76 void WakeUp() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077
78 void Add(Dispatcher* dispatcher);
79 void Remove(Dispatcher* dispatcher);
80
81#if defined(WEBRTC_POSIX)
82 AsyncFile* CreateFile(int fd);
83
84 // Sets the function to be executed in response to the specified POSIX signal.
85 // The function is executed from inside Wait() using the "self-pipe trick"--
86 // regardless of which thread receives the signal--and hence can safely
87 // manipulate user-level data structures.
88 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like
89 // with signal(2).
90 // Only one PhysicalSocketServer should have user-level signal handlers.
91 // Dispatching signals on multiple PhysicalSocketServers is not reliable.
92 // The signal mask is not modified. It is the caller's responsibily to
93 // maintain it as desired.
94 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
95
96 protected:
97 Dispatcher* signal_dispatcher();
98#endif
99
100 private:
101 typedef std::vector<Dispatcher*> DispatcherList;
102 typedef std::vector<size_t*> IteratorList;
103
104#if defined(WEBRTC_POSIX)
105 static bool InstallSignal(int signum, void (*handler)(int));
106
jbauch555604a2016-04-26 03:13:22 -0700107 std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108#endif
109 DispatcherList dispatchers_;
110 IteratorList iterators_;
111 Signaler* signal_wakeup_;
112 CriticalSection crit_;
113 bool fWait_;
114#if defined(WEBRTC_WIN)
115 WSAEVENT socket_ev_;
116#endif
117};
118
jbauch095ae152015-12-18 01:39:55 -0800119class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> {
120 public:
121 PhysicalSocket(PhysicalSocketServer* ss, SOCKET s = INVALID_SOCKET);
122 ~PhysicalSocket() override;
123
124 // Creates the underlying OS socket (same as the "socket" function).
125 virtual bool Create(int family, int type);
126
127 SocketAddress GetLocalAddress() const override;
128 SocketAddress GetRemoteAddress() const override;
129
130 int Bind(const SocketAddress& bind_addr) override;
131 int Connect(const SocketAddress& addr) override;
132
133 int GetError() const override;
134 void SetError(int error) override;
135
136 ConnState GetState() const override;
137
138 int GetOption(Option opt, int* value) override;
139 int SetOption(Option opt, int value) override;
140
141 int Send(const void* pv, size_t cb) override;
142 int SendTo(const void* buffer,
143 size_t length,
144 const SocketAddress& addr) override;
145
Stefan Holmer9131efd2016-05-23 18:19:26 +0200146 int Recv(void* buffer, size_t length, int64_t* timestamp) override;
147 int RecvFrom(void* buffer,
148 size_t length,
149 SocketAddress* out_addr,
150 int64_t* timestamp) override;
jbauch095ae152015-12-18 01:39:55 -0800151
152 int Listen(int backlog) override;
153 AsyncSocket* Accept(SocketAddress* out_addr) override;
154
155 int Close() override;
156
157 int EstimateMTU(uint16_t* mtu) override;
158
159 SocketServer* socketserver() { return ss_; }
160
161 protected:
162 int DoConnect(const SocketAddress& connect_addr);
163
164 // Make virtual so ::accept can be overwritten in tests.
165 virtual SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen);
166
jbauchf2a2bf42016-02-03 16:45:32 -0800167 // Make virtual so ::send can be overwritten in tests.
168 virtual int DoSend(SOCKET socket, const char* buf, int len, int flags);
169
170 // Make virtual so ::sendto can be overwritten in tests.
171 virtual int DoSendTo(SOCKET socket, const char* buf, int len, int flags,
172 const struct sockaddr* dest_addr, socklen_t addrlen);
173
jbauch095ae152015-12-18 01:39:55 -0800174 void OnResolveResult(AsyncResolverInterface* resolver);
175
176 void UpdateLastError();
177 void MaybeRemapSendError();
178
179 static int TranslateOption(Option opt, int* slevel, int* sopt);
180
181 PhysicalSocketServer* ss_;
182 SOCKET s_;
183 uint8_t enabled_events_;
184 bool udp_;
pbos5ad935c2016-01-25 03:52:44 -0800185 CriticalSection crit_;
jbauch095ae152015-12-18 01:39:55 -0800186 int error_ GUARDED_BY(crit_);
187 ConnState state_;
188 AsyncResolver* resolver_;
189
190#if !defined(NDEBUG)
191 std::string dbg_addr_;
192#endif
193};
194
195class SocketDispatcher : public Dispatcher, public PhysicalSocket {
196 public:
197 explicit SocketDispatcher(PhysicalSocketServer *ss);
198 SocketDispatcher(SOCKET s, PhysicalSocketServer *ss);
199 ~SocketDispatcher() override;
200
201 bool Initialize();
202
203 virtual bool Create(int type);
204 bool Create(int family, int type) override;
205
206#if defined(WEBRTC_WIN)
207 WSAEVENT GetWSAEvent() override;
208 SOCKET GetSocket() override;
209 bool CheckSignalClose() override;
210#elif defined(WEBRTC_POSIX)
211 int GetDescriptor() override;
212 bool IsDescriptorClosed() override;
213#endif
214
215 uint32_t GetRequestedEvents() override;
216 void OnPreEvent(uint32_t ff) override;
217 void OnEvent(uint32_t ff, int err) override;
218
219 int Close() override;
220
221#if defined(WEBRTC_WIN)
222 private:
223 static int next_id_;
224 int id_;
225 bool signal_close_;
226 int signal_err_;
227#endif // WEBRTC_WIN
228};
229
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230} // namespace rtc
231
232#endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__