1
// Copyright 2007, Google Inc.
4
#ifndef TALK_BASE_MACSOCKETSERVER_H__
5
#define TALK_BASE_MACSOCKETSERVER_H__
8
#ifdef OSX // Invalid on IOS
9
#include <Carbon/Carbon.h>
11
#include "talk/base/physicalsocketserver.h"
15
///////////////////////////////////////////////////////////////////////////////
16
// MacBaseSocketServer
17
///////////////////////////////////////////////////////////////////////////////
20
class MacBaseSocketServer : public PhysicalSocketServer {
22
MacBaseSocketServer();
23
virtual ~MacBaseSocketServer();
25
// SocketServer Interface
26
virtual Socket* CreateSocket(int type) { return NULL; }
27
virtual Socket* CreateSocket(int family, int type) { return NULL; }
29
virtual AsyncSocket* CreateAsyncSocket(int type);
30
virtual AsyncSocket* CreateAsyncSocket(int family, int type);
32
virtual bool Wait(int cms, bool process_io) = 0;
33
virtual void WakeUp() = 0;
35
void RegisterSocket(MacAsyncSocket* socket);
36
void UnregisterSocket(MacAsyncSocket* socket);
38
// PhysicalSocketServer Overrides
39
virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
42
void EnableSocketCallbacks(bool enable);
43
const std::set<MacAsyncSocket*>& sockets() {
48
static void FileDescriptorCallback(CFFileDescriptorRef ref,
52
std::set<MacAsyncSocket*> sockets_;
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
59
class MacCFSocketServer : public MacBaseSocketServer {
62
virtual ~MacCFSocketServer();
64
// SocketServer Interface
65
virtual bool Wait(int cms, bool process_io);
66
virtual void WakeUp();
67
void OnWakeUpCallback();
70
CFRunLoopRef run_loop_;
71
CFRunLoopSourceRef wake_up_;
76
///////////////////////////////////////////////////////////////////////////////
77
// MacCarbonSocketServer
78
///////////////////////////////////////////////////////////////////////////////
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 {
87
MacCarbonSocketServer();
88
virtual ~MacCarbonSocketServer();
90
// SocketServer Interface
91
virtual bool Wait(int cms, bool process_io);
92
virtual void WakeUp();
95
EventQueueRef event_queue_;
99
///////////////////////////////////////////////////////////////////////////////
100
// MacCarbonAppSocketServer
101
///////////////////////////////////////////////////////////////////////////////
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
108
class MacCarbonAppSocketServer : public MacBaseSocketServer {
110
MacCarbonAppSocketServer();
111
virtual ~MacCarbonAppSocketServer();
113
// SocketServer Interface
114
virtual bool Wait(int cms, bool process_io);
115
virtual void WakeUp();
118
static OSStatus WakeUpEventHandler(EventHandlerCallRef next, EventRef event,
120
static void TimerHandler(EventLoopTimerRef timer, void *data);
122
EventQueueRef event_queue_;
123
EventHandlerRef event_handler_;
124
EventLoopTimerRef timer_;
129
///////////////////////////////////////////////////////////////////////////////
130
// MacNotificationsSocketServer
131
///////////////////////////////////////////////////////////////////////////////
133
// The name "SocketServer" is misleading for this class. This class inherits
134
// from SocketServer, some variants of which create/use physical sockets
135
// (specifically, PhysicalSocketServer). But generally, this class is a way for
136
// a thread to schedule tasks (see task.h, thread.h and taskrunner.h).
138
// Since we don't want to write a custom Cocoa event loop, we will use this
139
// in a non-standard way. The "Wait" method will never actually wait - it will
140
// return false if cms > 0. Whenever a task needs to be woken up, the WakeUp
141
// method here will get called, and will cause the thread to cycle through all
142
// messages currently available.
144
class MacNotificationsSocketServer : public SocketServer {
146
MacNotificationsSocketServer();
147
virtual ~MacNotificationsSocketServer();
149
// SocketServer Interface
150
virtual Socket* CreateSocket(int type) { return NULL; }
151
virtual AsyncSocket* CreateAsyncSocket(int type) { return NULL; }
152
// process_io argument is ignored.
153
virtual bool Wait(int cms, bool process_io);
154
virtual void WakeUp();
157
static void NotificationCallBack(CFNotificationCenterRef center,
161
CFDictionaryRef userInfo);
163
bool sent_notification_;
166
///////////////////////////////////////////////////////////////////////////////
168
} // namespace talk_base
170
#endif // TALK_BASE_MACSOCKETSERVER_H__