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