You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef RTC_BASE_PHYSICAL_SOCKET_SERVER_H_
  11. #define RTC_BASE_PHYSICAL_SOCKET_SERVER_H_
  12. #if defined(WEBRTC_POSIX) && defined(WEBRTC_LINUX)
  13. #include <sys/epoll.h>
  14. #define WEBRTC_USE_EPOLL 1
  15. #endif
  16. #include <memory>
  17. #include <set>
  18. #include <vector>
  19. #include "rtc_base/critical_section.h"
  20. #include "rtc_base/net_helpers.h"
  21. #include "rtc_base/socket_server.h"
  22. #include "rtc_base/system/rtc_export.h"
  23. #if defined(WEBRTC_POSIX)
  24. typedef int SOCKET;
  25. #endif // WEBRTC_POSIX
  26. namespace rtc {
  27. // Event constants for the Dispatcher class.
  28. enum DispatcherEvent {
  29. DE_READ = 0x0001,
  30. DE_WRITE = 0x0002,
  31. DE_CONNECT = 0x0004,
  32. DE_CLOSE = 0x0008,
  33. DE_ACCEPT = 0x0010,
  34. };
  35. class Signaler;
  36. #if defined(WEBRTC_POSIX)
  37. class PosixSignalDispatcher;
  38. #endif
  39. class Dispatcher {
  40. public:
  41. virtual ~Dispatcher() {}
  42. virtual uint32_t GetRequestedEvents() = 0;
  43. virtual void OnPreEvent(uint32_t ff) = 0;
  44. virtual void OnEvent(uint32_t ff, int err) = 0;
  45. #if defined(WEBRTC_WIN)
  46. virtual WSAEVENT GetWSAEvent() = 0;
  47. virtual SOCKET GetSocket() = 0;
  48. virtual bool CheckSignalClose() = 0;
  49. #elif defined(WEBRTC_POSIX)
  50. virtual int GetDescriptor() = 0;
  51. virtual bool IsDescriptorClosed() = 0;
  52. #endif
  53. };
  54. // A socket server that provides the real sockets of the underlying OS.
  55. class RTC_EXPORT PhysicalSocketServer : public SocketServer {
  56. public:
  57. PhysicalSocketServer();
  58. ~PhysicalSocketServer() override;
  59. // SocketFactory:
  60. Socket* CreateSocket(int family, int type) override;
  61. AsyncSocket* CreateAsyncSocket(int family, int type) override;
  62. // Internal Factory for Accept (virtual so it can be overwritten in tests).
  63. virtual AsyncSocket* WrapSocket(SOCKET s);
  64. // SocketServer:
  65. bool Wait(int cms, bool process_io) override;
  66. void WakeUp() override;
  67. void Add(Dispatcher* dispatcher);
  68. void Remove(Dispatcher* dispatcher);
  69. void Update(Dispatcher* dispatcher);
  70. #if defined(WEBRTC_POSIX)
  71. // Sets the function to be executed in response to the specified POSIX signal.
  72. // The function is executed from inside Wait() using the "self-pipe trick"--
  73. // regardless of which thread receives the signal--and hence can safely
  74. // manipulate user-level data structures.
  75. // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like
  76. // with signal(2).
  77. // Only one PhysicalSocketServer should have user-level signal handlers.
  78. // Dispatching signals on multiple PhysicalSocketServers is not reliable.
  79. // The signal mask is not modified. It is the caller's responsibily to
  80. // maintain it as desired.
  81. virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
  82. protected:
  83. Dispatcher* signal_dispatcher();
  84. #endif
  85. private:
  86. typedef std::set<Dispatcher*> DispatcherSet;
  87. void AddRemovePendingDispatchers();
  88. #if defined(WEBRTC_POSIX)
  89. bool WaitSelect(int cms, bool process_io);
  90. static bool InstallSignal(int signum, void (*handler)(int));
  91. std::unique_ptr<PosixSignalDispatcher> signal_dispatcher_;
  92. #endif // WEBRTC_POSIX
  93. #if defined(WEBRTC_USE_EPOLL)
  94. void AddEpoll(Dispatcher* dispatcher);
  95. void RemoveEpoll(Dispatcher* dispatcher);
  96. void UpdateEpoll(Dispatcher* dispatcher);
  97. bool WaitEpoll(int cms);
  98. bool WaitPoll(int cms, Dispatcher* dispatcher);
  99. int epoll_fd_ = INVALID_SOCKET;
  100. std::vector<struct epoll_event> epoll_events_;
  101. #endif // WEBRTC_USE_EPOLL
  102. DispatcherSet dispatchers_;
  103. DispatcherSet pending_add_dispatchers_;
  104. DispatcherSet pending_remove_dispatchers_;
  105. bool processing_dispatchers_ = false;
  106. Signaler* signal_wakeup_;
  107. CriticalSection crit_;
  108. bool fWait_;
  109. #if defined(WEBRTC_WIN)
  110. WSAEVENT socket_ev_;
  111. #endif
  112. };
  113. class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> {
  114. public:
  115. PhysicalSocket(PhysicalSocketServer* ss, SOCKET s = INVALID_SOCKET);
  116. ~PhysicalSocket() override;
  117. // Creates the underlying OS socket (same as the "socket" function).
  118. virtual bool Create(int family, int type);
  119. SocketAddress GetLocalAddress() const override;
  120. SocketAddress GetRemoteAddress() const override;
  121. int Bind(const SocketAddress& bind_addr) override;
  122. int Connect(const SocketAddress& addr) override;
  123. int GetError() const override;
  124. void SetError(int error) override;
  125. ConnState GetState() const override;
  126. int GetOption(Option opt, int* value) override;
  127. int SetOption(Option opt, int value) override;
  128. int Send(const void* pv, size_t cb) override;
  129. int SendTo(const void* buffer,
  130. size_t length,
  131. const SocketAddress& addr) override;
  132. int Recv(void* buffer, size_t length, int64_t* timestamp) override;
  133. int RecvFrom(void* buffer,
  134. size_t length,
  135. SocketAddress* out_addr,
  136. int64_t* timestamp) override;
  137. int Listen(int backlog) override;
  138. AsyncSocket* Accept(SocketAddress* out_addr) override;
  139. int Close() override;
  140. SocketServer* socketserver() { return ss_; }
  141. protected:
  142. int DoConnect(const SocketAddress& connect_addr);
  143. // Make virtual so ::accept can be overwritten in tests.
  144. virtual SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen);
  145. // Make virtual so ::send can be overwritten in tests.
  146. virtual int DoSend(SOCKET socket, const char* buf, int len, int flags);
  147. // Make virtual so ::sendto can be overwritten in tests.
  148. virtual int DoSendTo(SOCKET socket,
  149. const char* buf,
  150. int len,
  151. int flags,
  152. const struct sockaddr* dest_addr,
  153. socklen_t addrlen);
  154. void OnResolveResult(AsyncResolverInterface* resolver);
  155. void UpdateLastError();
  156. void MaybeRemapSendError();
  157. uint8_t enabled_events() const { return enabled_events_; }
  158. virtual void SetEnabledEvents(uint8_t events);
  159. virtual void EnableEvents(uint8_t events);
  160. virtual void DisableEvents(uint8_t events);
  161. static int TranslateOption(Option opt, int* slevel, int* sopt);
  162. PhysicalSocketServer* ss_;
  163. SOCKET s_;
  164. bool udp_;
  165. CriticalSection crit_;
  166. int error_ RTC_GUARDED_BY(crit_);
  167. ConnState state_;
  168. AsyncResolver* resolver_;
  169. #if !defined(NDEBUG)
  170. std::string dbg_addr_;
  171. #endif
  172. private:
  173. uint8_t enabled_events_ = 0;
  174. };
  175. class SocketDispatcher : public Dispatcher, public PhysicalSocket {
  176. public:
  177. explicit SocketDispatcher(PhysicalSocketServer* ss);
  178. SocketDispatcher(SOCKET s, PhysicalSocketServer* ss);
  179. ~SocketDispatcher() override;
  180. bool Initialize();
  181. virtual bool Create(int type);
  182. bool Create(int family, int type) override;
  183. #if defined(WEBRTC_WIN)
  184. WSAEVENT GetWSAEvent() override;
  185. SOCKET GetSocket() override;
  186. bool CheckSignalClose() override;
  187. #elif defined(WEBRTC_POSIX)
  188. int GetDescriptor() override;
  189. bool IsDescriptorClosed() override;
  190. #endif
  191. uint32_t GetRequestedEvents() override;
  192. void OnPreEvent(uint32_t ff) override;
  193. void OnEvent(uint32_t ff, int err) override;
  194. int Close() override;
  195. #if defined(WEBRTC_USE_EPOLL)
  196. protected:
  197. void StartBatchedEventUpdates();
  198. void FinishBatchedEventUpdates();
  199. void SetEnabledEvents(uint8_t events) override;
  200. void EnableEvents(uint8_t events) override;
  201. void DisableEvents(uint8_t events) override;
  202. #endif
  203. private:
  204. #if defined(WEBRTC_WIN)
  205. static int next_id_;
  206. int id_;
  207. bool signal_close_;
  208. int signal_err_;
  209. #endif // WEBRTC_WIN
  210. #if defined(WEBRTC_USE_EPOLL)
  211. void MaybeUpdateDispatcher(uint8_t old_events);
  212. int saved_enabled_events_ = -1;
  213. #endif
  214. };
  215. } // namespace rtc
  216. #endif // RTC_BASE_PHYSICAL_SOCKET_SERVER_H_