CCReachability.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "CCReachability.h"
  21. #include "base/ccMacros.h"
  22. #include <SystemConfiguration/SystemConfiguration.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <ifaddrs.h>
  26. #include <netdb.h>
  27. #include <sys/socket.h>
  28. namespace {
  29. #define ShouldPrintReachabilityFlags 0
  30. static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
  31. {
  32. #if ShouldPrintReachabilityFlags
  33. printf("Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
  34. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  35. (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
  36. #else
  37. '-',
  38. #endif
  39. (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
  40. (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
  41. (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
  42. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
  43. (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
  44. (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
  45. (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
  46. (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-',
  47. comment
  48. );
  49. #endif
  50. }
  51. cocos2d::Reachability::NetworkStatus getNetworkStatusForFlags(SCNetworkReachabilityFlags flags)
  52. {
  53. PrintReachabilityFlags(flags, "networkStatusForFlags");
  54. if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
  55. {
  56. // The target host is not reachable.
  57. return cocos2d::Reachability::NetworkStatus::NOT_REACHABLE;
  58. }
  59. cocos2d::Reachability::NetworkStatus returnValue = cocos2d::Reachability::NetworkStatus::NOT_REACHABLE;
  60. if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
  61. {
  62. /*
  63. If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
  64. */
  65. returnValue = cocos2d::Reachability::NetworkStatus::REACHABLE_VIA_WIFI;
  66. }
  67. if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
  68. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
  69. {
  70. /*
  71. ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
  72. */
  73. if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
  74. {
  75. /*
  76. ... and no [user] intervention is needed...
  77. */
  78. returnValue = cocos2d::Reachability::NetworkStatus::REACHABLE_VIA_WIFI;
  79. }
  80. }
  81. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  82. if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
  83. {
  84. /*
  85. ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
  86. */
  87. returnValue = cocos2d::Reachability::NetworkStatus::REACHABLE_VIA_WWAN;
  88. }
  89. #endif
  90. return returnValue;
  91. }
  92. }
  93. NS_CC_BEGIN
  94. Reachability* Reachability::createWithHostName(const std::string& hostName)
  95. {
  96. Reachability* returnValue = nullptr;
  97. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(nullptr, hostName.c_str());
  98. if (reachability != nullptr)
  99. {
  100. returnValue = new (std::nothrow) Reachability();
  101. if (returnValue != nullptr)
  102. {
  103. returnValue->autorelease();
  104. returnValue->_reachabilityRef = reachability;
  105. }
  106. else {
  107. CFRelease(reachability);
  108. }
  109. }
  110. return returnValue;
  111. }
  112. Reachability* Reachability::createWithAddress(const struct sockaddr* hostAddress)
  113. {
  114. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress);
  115. Reachability* returnValue = nullptr;
  116. if (reachability != nullptr)
  117. {
  118. returnValue = new (std::nothrow) Reachability();
  119. if (returnValue != nullptr)
  120. {
  121. returnValue->autorelease();
  122. returnValue->_reachabilityRef = reachability;
  123. }
  124. else {
  125. CFRelease(reachability);
  126. }
  127. }
  128. return returnValue;
  129. }
  130. Reachability* Reachability::createForInternetConnection()
  131. {
  132. struct sockaddr_in zeroAddress;
  133. bzero(&zeroAddress, sizeof(zeroAddress));
  134. zeroAddress.sin_len = sizeof(zeroAddress);
  135. zeroAddress.sin_family = AF_INET;
  136. return createWithAddress((const struct sockaddr*) &zeroAddress);
  137. }
  138. Reachability::Reachability()
  139. : _callback(nullptr)
  140. , _userData(nullptr)
  141. , _reachabilityRef(nullptr)
  142. {
  143. }
  144. Reachability::~Reachability()
  145. {
  146. stopNotifier();
  147. if (_reachabilityRef != nullptr)
  148. {
  149. CFRelease(_reachabilityRef);
  150. }
  151. }
  152. void Reachability::onReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
  153. {
  154. CCASSERT(info != nullptr, "info was nullptr in onReachabilityCallback");
  155. cocos2d::Reachability* thiz = reinterpret_cast<cocos2d::Reachability*>(info);
  156. if (thiz->_callback != nullptr)
  157. {
  158. NetworkStatus status = getNetworkStatusForFlags(flags);
  159. thiz->_callback(thiz, status, thiz->_userData);
  160. }
  161. }
  162. bool Reachability::startNotifier(const ReachabilityCallback& cb, void* userData)
  163. {
  164. _callback = cb;
  165. _userData = userData;
  166. bool returnValue = false;
  167. SCNetworkReachabilityContext context = {0, this, nullptr, nullptr, nullptr};
  168. if (SCNetworkReachabilitySetCallback(_reachabilityRef, onReachabilityCallback, &context))
  169. {
  170. if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
  171. {
  172. returnValue = true;
  173. }
  174. }
  175. return returnValue;
  176. }
  177. void Reachability::stopNotifier()
  178. {
  179. if (_reachabilityRef != nullptr)
  180. {
  181. SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  182. }
  183. }
  184. bool Reachability::isConnectionRequired() const
  185. {
  186. CCASSERT(_reachabilityRef != nullptr, "connectionRequired called with nullptr reachabilityRef");
  187. SCNetworkReachabilityFlags flags;
  188. if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
  189. {
  190. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  191. }
  192. return false;
  193. }
  194. Reachability::NetworkStatus Reachability::getCurrentReachabilityStatus() const
  195. {
  196. CCASSERT(_reachabilityRef != nullptr, "currentNetworkStatus called with nullptr SCNetworkReachabilityRef");
  197. NetworkStatus returnValue = NetworkStatus::NOT_REACHABLE;
  198. SCNetworkReachabilityFlags flags;
  199. if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
  200. {
  201. returnValue = getNetworkStatusForFlags(flags);
  202. }
  203. return returnValue;
  204. }
  205. NS_CC_END