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