~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/base/macsocketserver.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifdef OSX
 
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
 
 
129
///////////////////////////////////////////////////////////////////////////////
 
130
// MacNotificationsSocketServer
 
131
///////////////////////////////////////////////////////////////////////////////
 
132
 
 
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).
 
137
//
 
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.
 
143
 
 
144
class MacNotificationsSocketServer : public SocketServer {
 
145
 public:
 
146
  MacNotificationsSocketServer();
 
147
  virtual ~MacNotificationsSocketServer();
 
148
 
 
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();
 
155
 
 
156
 private:
 
157
  static void NotificationCallBack(CFNotificationCenterRef center,
 
158
                                   void* observer,
 
159
                                   CFStringRef name,
 
160
                                   const void* object,
 
161
                                   CFDictionaryRef userInfo);
 
162
 
 
163
  bool sent_notification_;
 
164
};
 
165
 
 
166
///////////////////////////////////////////////////////////////////////////////
 
167
 
 
168
} // namespace talk_base
 
169
 
 
170
#endif  // TALK_BASE_MACSOCKETSERVER_H__