You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #import "talk/base/maccocoasocketserver.h"
  28. #import <Foundation/Foundation.h>
  29. #import <AppKit/AppKit.h>
  30. #include <assert.h>
  31. #include "talk/base/scoped_autorelease_pool.h"
  32. // MacCocoaSocketServerHelper serves as a delegate to NSMachPort or a target for
  33. // a timeout.
  34. @interface MacCocoaSocketServerHelper : NSObject {
  35. // This is a weak reference. This works fine since the
  36. // talk_base::MacCocoaSocketServer owns this object.
  37. talk_base::MacCocoaSocketServer* socketServer_; // Weak.
  38. }
  39. @end
  40. @implementation MacCocoaSocketServerHelper
  41. - (id)initWithSocketServer:(talk_base::MacCocoaSocketServer*)ss {
  42. self = [super init];
  43. if (self) {
  44. socketServer_ = ss;
  45. }
  46. return self;
  47. }
  48. - (void)timerFired:(NSTimer*)timer {
  49. socketServer_->WakeUp();
  50. }
  51. @end
  52. namespace talk_base {
  53. MacCocoaSocketServer::MacCocoaSocketServer() {
  54. helper_ = [[MacCocoaSocketServerHelper alloc] initWithSocketServer:this];
  55. timer_ = nil;
  56. // Initialize the shared NSApplication
  57. [NSApplication sharedApplication];
  58. }
  59. MacCocoaSocketServer::~MacCocoaSocketServer() {
  60. [timer_ invalidate];
  61. [timer_ release];
  62. [helper_ release];
  63. }
  64. bool MacCocoaSocketServer::Wait(int cms, bool process_io) {
  65. talk_base::ScopedAutoreleasePool pool;
  66. if (!process_io && cms == 0) {
  67. // No op.
  68. return true;
  69. }
  70. if (!process_io) {
  71. // No way to listen to common modes and not get socket events, unless
  72. // we disable each one's callbacks.
  73. EnableSocketCallbacks(false);
  74. }
  75. if (kForever != cms) {
  76. // Install a timer that fires wakeup after cms has elapsed.
  77. timer_ =
  78. [NSTimer scheduledTimerWithTimeInterval:cms / 1000.0
  79. target:helper_
  80. selector:@selector(timerFired:)
  81. userInfo:nil
  82. repeats:NO];
  83. [timer_ retain];
  84. }
  85. // Run until WakeUp is called, which will call stop and exit this loop.
  86. [NSApp run];
  87. if (!process_io) {
  88. // Reenable them. Hopefully this won't cause spurious callbacks or
  89. // missing ones while they were disabled.
  90. EnableSocketCallbacks(true);
  91. }
  92. return true;
  93. }
  94. void MacCocoaSocketServer::WakeUp() {
  95. // Timer has either fired or shortcutted.
  96. [timer_ invalidate];
  97. [timer_ release];
  98. timer_ = nil;
  99. [NSApp stop:nil];
  100. // NSApp stop only exits after finishing processing of the
  101. // current event. Since we're potentially in a timer callback
  102. // and not an NSEvent handler, we need to trigger a dummy one
  103. // and turn the loop over. We may be able to skip this if we're
  104. // on the ss' thread and not inside the app loop already.
  105. NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
  106. location:NSMakePoint(0,0)
  107. modifierFlags:0
  108. timestamp:0
  109. windowNumber:0
  110. context:nil
  111. subtype:1
  112. data1:1
  113. data2:1];
  114. [NSApp postEvent:event atStart:YES];
  115. }
  116. } // namespace talk_base