3 * Copyright 2012, Google Inc
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "talk/base/maccocoasocketserver.h"
29 #import <Foundation/Foundation.h>
30 #import <AppKit/AppKit.h>
33 #include "talk/base/scoped_autorelease_pool.h"
35 // MacCocoaSocketServerHelper serves as a delegate to NSMachPort or a target for
37 @interface MacCocoaSocketServerHelper : NSObject {
38 // This is a weak reference. This works fine since the
39 // talk_base::MacCocoaSocketServer owns this object.
40 talk_base::MacCocoaSocketServer* socketServer_; // Weak.
44 @implementation MacCocoaSocketServerHelper
45 - (id)initWithSocketServer:(talk_base::MacCocoaSocketServer*)ss {
53 - (void)timerFired:(NSTimer*)timer {
54 socketServer_->WakeUp();
60 MacCocoaSocketServer::MacCocoaSocketServer() {
61 helper_ = [[MacCocoaSocketServerHelper alloc] initWithSocketServer:this];
64 // Initialize the shared NSApplication
65 [NSApplication sharedApplication];
68 MacCocoaSocketServer::~MacCocoaSocketServer() {
74 bool MacCocoaSocketServer::Wait(int cms, bool process_io) {
75 talk_base::ScopedAutoreleasePool pool;
76 if (!process_io && cms == 0) {
82 // No way to listen to common modes and not get socket events, unless
83 // we disable each one's callbacks.
84 EnableSocketCallbacks(false);
87 if (kForever != cms) {
88 // Install a timer that fires wakeup after cms has elapsed.
90 [NSTimer scheduledTimerWithTimeInterval:cms / 1000.0
92 selector:@selector(timerFired:)
98 // Run until WakeUp is called, which will call stop and exit this loop.
102 // Reenable them. Hopefully this won't cause spurious callbacks or
103 // missing ones while they were disabled.
104 EnableSocketCallbacks(true);
110 void MacCocoaSocketServer::WakeUp() {
111 // Timer has either fired or shortcutted.
117 // NSApp stop only exits after finishing processing of the
118 // current event. Since we're potentially in a timer callback
119 // and not an NSEvent handler, we need to trigger a dummy one
120 // and turn the loop over. We may be able to skip this if we're
121 // on the ss' thread and not inside the app loop already.
122 NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
123 location:NSMakePoint(0,0)
131 [NSApp postEvent:event atStart:YES];
134 } // namespace talk_base