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