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.

130 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2007, Google Inc.
  2. #ifndef TALK_BASE_MACSOCKETSERVER_H__
  3. #define TALK_BASE_MACSOCKETSERVER_H__
  4. #include <set>
  5. #ifdef OSX // Invalid on IOS
  6. #include <Carbon/Carbon.h>
  7. #endif
  8. #include "talk/base/physicalsocketserver.h"
  9. namespace talk_base {
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // MacBaseSocketServer
  12. ///////////////////////////////////////////////////////////////////////////////
  13. class MacAsyncSocket;
  14. class MacBaseSocketServer : public PhysicalSocketServer {
  15. public:
  16. MacBaseSocketServer();
  17. virtual ~MacBaseSocketServer();
  18. // SocketServer Interface
  19. virtual Socket* CreateSocket(int type) { return NULL; }
  20. virtual Socket* CreateSocket(int family, int type) { return NULL; }
  21. virtual AsyncSocket* CreateAsyncSocket(int type);
  22. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  23. virtual bool Wait(int cms, bool process_io) = 0;
  24. virtual void WakeUp() = 0;
  25. void RegisterSocket(MacAsyncSocket* socket);
  26. void UnregisterSocket(MacAsyncSocket* socket);
  27. // PhysicalSocketServer Overrides
  28. virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
  29. protected:
  30. void EnableSocketCallbacks(bool enable);
  31. const std::set<MacAsyncSocket*>& sockets() {
  32. return sockets_;
  33. }
  34. private:
  35. static void FileDescriptorCallback(CFFileDescriptorRef ref,
  36. CFOptionFlags flags,
  37. void* context);
  38. std::set<MacAsyncSocket*> sockets_;
  39. };
  40. // Core Foundation implementation of the socket server. While idle it
  41. // will run the current CF run loop. When the socket server has work
  42. // to do the run loop will be paused. Does not support Carbon or Cocoa
  43. // UI interaction.
  44. class MacCFSocketServer : public MacBaseSocketServer {
  45. public:
  46. MacCFSocketServer();
  47. virtual ~MacCFSocketServer();
  48. // SocketServer Interface
  49. virtual bool Wait(int cms, bool process_io);
  50. virtual void WakeUp();
  51. void OnWakeUpCallback();
  52. private:
  53. CFRunLoopRef run_loop_;
  54. CFRunLoopSourceRef wake_up_;
  55. };
  56. #ifndef CARBON_DEPRECATED
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // MacCarbonSocketServer
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // Interacts with the Carbon event queue. While idle it will block,
  61. // waiting for events. When the socket server has work to do, it will
  62. // post a 'wake up' event to the queue, causing the thread to exit the
  63. // event loop until the next call to Wait. Other events are dispatched
  64. // to their target. Supports Carbon and Cocoa UI interaction.
  65. class MacCarbonSocketServer : public MacBaseSocketServer {
  66. public:
  67. MacCarbonSocketServer();
  68. virtual ~MacCarbonSocketServer();
  69. // SocketServer Interface
  70. virtual bool Wait(int cms, bool process_io);
  71. virtual void WakeUp();
  72. private:
  73. EventQueueRef event_queue_;
  74. EventRef wake_up_;
  75. };
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // MacCarbonAppSocketServer
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // Runs the Carbon application event loop on the current thread while
  80. // idle. When the socket server has work to do, it will post an event
  81. // to the queue, causing the thread to exit the event loop until the
  82. // next call to Wait. Other events are automatically dispatched to
  83. // their target.
  84. class MacCarbonAppSocketServer : public MacBaseSocketServer {
  85. public:
  86. MacCarbonAppSocketServer();
  87. virtual ~MacCarbonAppSocketServer();
  88. // SocketServer Interface
  89. virtual bool Wait(int cms, bool process_io);
  90. virtual void WakeUp();
  91. private:
  92. static OSStatus WakeUpEventHandler(EventHandlerCallRef next, EventRef event,
  93. void *data);
  94. static void TimerHandler(EventLoopTimerRef timer, void *data);
  95. EventQueueRef event_queue_;
  96. EventHandlerRef event_handler_;
  97. EventLoopTimerRef timer_;
  98. };
  99. #endif
  100. } // namespace talk_base
  101. #endif // TALK_BASE_MACSOCKETSERVER_H__