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.

139 lines
4.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_PHYSICALSOCKETSERVER_H__
  28. #define TALK_BASE_PHYSICALSOCKETSERVER_H__
  29. #include <vector>
  30. #include "talk/base/asyncfile.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/socketserver.h"
  33. #include "talk/base/criticalsection.h"
  34. #ifdef POSIX
  35. typedef int SOCKET;
  36. #endif // POSIX
  37. namespace talk_base {
  38. // Event constants for the Dispatcher class.
  39. enum DispatcherEvent {
  40. DE_READ = 0x0001,
  41. DE_WRITE = 0x0002,
  42. DE_CONNECT = 0x0004,
  43. DE_CLOSE = 0x0008,
  44. DE_ACCEPT = 0x0010,
  45. };
  46. class Signaler;
  47. #ifdef POSIX
  48. class PosixSignalDispatcher;
  49. #endif
  50. class Dispatcher {
  51. public:
  52. virtual ~Dispatcher() {}
  53. virtual uint32 GetRequestedEvents() = 0;
  54. virtual void OnPreEvent(uint32 ff) = 0;
  55. virtual void OnEvent(uint32 ff, int err) = 0;
  56. #ifdef WIN32
  57. virtual WSAEVENT GetWSAEvent() = 0;
  58. virtual SOCKET GetSocket() = 0;
  59. virtual bool CheckSignalClose() = 0;
  60. #elif POSIX
  61. virtual int GetDescriptor() = 0;
  62. virtual bool IsDescriptorClosed() = 0;
  63. #endif
  64. };
  65. // A socket server that provides the real sockets of the underlying OS.
  66. class PhysicalSocketServer : public SocketServer {
  67. public:
  68. PhysicalSocketServer();
  69. virtual ~PhysicalSocketServer();
  70. // SocketFactory:
  71. virtual Socket* CreateSocket(int type);
  72. virtual Socket* CreateSocket(int family, int type);
  73. virtual AsyncSocket* CreateAsyncSocket(int type);
  74. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  75. // Internal Factory for Accept
  76. AsyncSocket* WrapSocket(SOCKET s);
  77. // SocketServer:
  78. virtual bool Wait(int cms, bool process_io);
  79. virtual void WakeUp();
  80. void Add(Dispatcher* dispatcher);
  81. void Remove(Dispatcher* dispatcher);
  82. #ifdef POSIX
  83. AsyncFile* CreateFile(int fd);
  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. protected:
  96. Dispatcher* signal_dispatcher();
  97. #endif
  98. private:
  99. typedef std::vector<Dispatcher*> DispatcherList;
  100. typedef std::vector<size_t*> IteratorList;
  101. #ifdef POSIX
  102. static bool InstallSignal(int signum, void (*handler)(int));
  103. scoped_ptr<PosixSignalDispatcher> signal_dispatcher_;
  104. #endif
  105. DispatcherList dispatchers_;
  106. IteratorList iterators_;
  107. Signaler* signal_wakeup_;
  108. CriticalSection crit_;
  109. bool fWait_;
  110. uint32 last_tick_tracked_;
  111. int last_tick_dispatch_count_;
  112. #ifdef WIN32
  113. WSAEVENT socket_ev_;
  114. #endif
  115. };
  116. } // namespace talk_base
  117. #endif // TALK_BASE_PHYSICALSOCKETSERVER_H__