LKS_ConnectionManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // LookinServer.m
  3. // LookinServer
  4. //
  5. // Created by Li Kai on 2018/8/5.
  6. // https://lookin.work
  7. //
  8. #import "LKS_ConnectionManager.h"
  9. #import "Lookin_PTChannel.h"
  10. #import "LKS_RequestHandler.h"
  11. #import "LookinConnectionResponseAttachment.h"
  12. #import "LKS_LocalInspectManager.h"
  13. #import "LKS_ExportManager.h"
  14. #import "LKS_PerspectiveManager.h"
  15. #import "LookinServerDefines.h"
  16. NSString *const LKS_ConnectionDidEndNotificationName = @"LKS_ConnectionDidEndNotificationName";
  17. @interface LKS_ConnectionManager () <Lookin_PTChannelDelegate>
  18. @property(nonatomic, weak) Lookin_PTChannel *peerChannel_;
  19. @property(nonatomic, strong) LKS_RequestHandler *requestHandler;
  20. @end
  21. @implementation LKS_ConnectionManager
  22. + (instancetype)sharedInstance {
  23. static LKS_ConnectionManager *sharedInstance;
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. sharedInstance = [[LKS_ConnectionManager alloc] init];
  27. });
  28. return sharedInstance;
  29. }
  30. + (void)load {
  31. // 触发 init 方法
  32. [LKS_ConnectionManager sharedInstance];
  33. }
  34. - (instancetype)init {
  35. if (self = [super init]) {
  36. NSLog(@"LookinServer - Will launch. Framework version: %@", LOOKIN_SERVER_READABLE_VERSION);
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleApplicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleWillResignActiveNotification) name:UIApplicationWillResignActiveNotification object:nil];
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleLocalInspectIn2D:) name:@"Lookin_2D" object:nil];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleLocalInspectIn3D:) name:@"Lookin_3D" object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserverForName:@"Lookin_Export" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  42. [[LKS_ExportManager sharedInstance] exportAndShare];
  43. }];
  44. self.requestHandler = [LKS_RequestHandler new];
  45. }
  46. return self;
  47. }
  48. - (void)_handleWillResignActiveNotification {
  49. self.applicationIsActive = NO;
  50. }
  51. - (void)_handleApplicationDidBecomeActive {
  52. // NSLog(@"LookinServer(0.8.0) - UIApplicationDidBecomeActiveNotification");
  53. self.applicationIsActive = YES;
  54. if (self.peerChannel_ && (self.peerChannel_.isConnected || self.peerChannel_.isListening)) {
  55. return;
  56. }
  57. NSLog(@"LookinServer - Trying to connect ...");
  58. if ([self isiOSAppOnMac]) {
  59. [self _tryToListenOnPortFrom:LookinSimulatorIPv4PortNumberStart to:LookinSimulatorIPv4PortNumberEnd current:LookinSimulatorIPv4PortNumberStart];
  60. } else {
  61. [self _tryToListenOnPortFrom:LookinUSBDeviceIPv4PortNumberStart to:LookinUSBDeviceIPv4PortNumberEnd current:LookinUSBDeviceIPv4PortNumberStart];
  62. }
  63. }
  64. - (BOOL)isiOSAppOnMac {
  65. #if TARGET_OS_SIMULATOR
  66. return YES;
  67. #endif
  68. #ifdef IOS14_SDK_ALLOWED
  69. if (@available(iOS 14.0, *)) {
  70. return [NSProcessInfo processInfo].isiOSAppOnMac || [NSProcessInfo processInfo].isMacCatalystApp;
  71. }
  72. #endif
  73. if (@available(iOS 13.0, tvOS 13.0, *)) {
  74. return [NSProcessInfo processInfo].isMacCatalystApp;
  75. }
  76. return NO;
  77. }
  78. - (void)_tryToListenOnPortFrom:(int)fromPort to:(int)toPort current:(int)currentPort {
  79. Lookin_PTChannel *channel = [Lookin_PTChannel channelWithDelegate:self];
  80. [channel listenOnPort:currentPort IPv4Address:INADDR_LOOPBACK callback:^(NSError *error) {
  81. if (error) {
  82. if (error.code == 48) {
  83. // 该地址已被占用
  84. } else {
  85. // 未知失败
  86. }
  87. if (currentPort < toPort) {
  88. // 尝试下一个端口
  89. NSLog(@"LookinServer - 127.0.0.1:%d is unavailable(%@). Will try anothor address ...", currentPort, error);
  90. [self _tryToListenOnPortFrom:fromPort to:toPort current:(currentPort + 1)];
  91. } else {
  92. // 所有端口都尝试完毕,全部失败
  93. NSLog(@"LookinServer - 127.0.0.1:%d is unavailable(%@).", currentPort, error);
  94. NSLog(@"LookinServer - Connect failed in the end. Ask for help: lookin@lookin.work");
  95. }
  96. } else {
  97. // 成功
  98. NSLog(@"LookinServer - Connected successfully on 127.0.0.1:%d", currentPort);
  99. // UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@", @(currentPort)] message:nil preferredStyle:UIAlertControllerStyleAlert];
  100. // [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  101. }
  102. }];
  103. }
  104. - (void)dealloc {
  105. if (self.peerChannel_) {
  106. [self.peerChannel_ close];
  107. }
  108. [[NSNotificationCenter defaultCenter] removeObserver:self];
  109. }
  110. - (BOOL)isConnected {
  111. return self.peerChannel_ && self.peerChannel_.isConnected;
  112. }
  113. - (void)respond:(LookinConnectionResponseAttachment *)data requestType:(uint32_t)requestType tag:(uint32_t)tag {
  114. [self _sendData:data frameOfType:requestType tag:tag];
  115. }
  116. - (void)pushData:(NSObject *)data type:(uint32_t)type {
  117. [self _sendData:data frameOfType:type tag:0];
  118. }
  119. - (void)_sendData:(NSObject *)data frameOfType:(uint32_t)frameOfType tag:(uint32_t)tag {
  120. if (self.peerChannel_) {
  121. NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:data];
  122. dispatch_data_t payload = [archivedData createReferencingDispatchData];
  123. [self.peerChannel_ sendFrameOfType:frameOfType tag:tag withPayload:payload callback:^(NSError *error) {
  124. if (error) {
  125. }
  126. }];
  127. }
  128. }
  129. #pragma mark - Lookin_PTChannelDelegate
  130. - (BOOL)ioFrameChannel:(Lookin_PTChannel*)channel shouldAcceptFrameOfType:(uint32_t)type tag:(uint32_t)tag payloadSize:(uint32_t)payloadSize {
  131. if (channel != self.peerChannel_) {
  132. return NO;
  133. } else if ([self.requestHandler canHandleRequestType:type]) {
  134. return YES;
  135. } else {
  136. [channel close];
  137. return NO;
  138. }
  139. }
  140. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(Lookin_PTData*)payload {
  141. id object = nil;
  142. if (payload) {
  143. id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfDispatchData:payload.dispatchData]];
  144. if ([unarchivedObject isKindOfClass:[LookinConnectionAttachment class]]) {
  145. LookinConnectionAttachment *attachment = (LookinConnectionAttachment *)unarchivedObject;
  146. object = attachment.data;
  147. } else {
  148. object = unarchivedObject;
  149. }
  150. }
  151. [self.requestHandler handleRequestType:type tag:tag object:object];
  152. }
  153. /// 当连接过 Lookin 客户端,然后 Lookin 客户端又被关闭时,会走到这里
  154. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didEndWithError:(NSError*)error {
  155. [[NSNotificationCenter defaultCenter] postNotificationName:LKS_ConnectionDidEndNotificationName object:self];
  156. }
  157. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didAcceptConnection:(Lookin_PTChannel*)otherChannel fromAddress:(Lookin_PTAddress*)address {
  158. if (self.peerChannel_) {
  159. [self.peerChannel_ cancel];
  160. }
  161. self.peerChannel_ = otherChannel;
  162. self.peerChannel_.userInfo = address;
  163. }
  164. #pragma mark - Handler
  165. - (void)_handleLocalInspectIn2D:(NSNotification *)note {
  166. dispatch_async(dispatch_get_main_queue(), ^{
  167. NSArray<UIWindow *> *includedWindows = nil;
  168. NSArray<UIWindow *> *excludedWindows = nil;
  169. [self parseUserInfo:note.userInfo toIncludedWindows:&includedWindows excludedWindows:&excludedWindows];
  170. [[LKS_LocalInspectManager sharedInstance] startLocalInspectWithIncludedWindows:includedWindows excludedWindows:excludedWindows];
  171. });
  172. }
  173. - (void)_handleLocalInspectIn3D:(NSNotification *)note {
  174. NSArray<UIWindow *> *includedWindows = nil;
  175. NSArray<UIWindow *> *excludedWindows = nil;
  176. [self parseUserInfo:note.userInfo toIncludedWindows:&includedWindows excludedWindows:&excludedWindows];
  177. [[LKS_PerspectiveManager sharedInstance] showWithIncludedWindows:includedWindows excludedWindows:excludedWindows];
  178. }
  179. - (void)parseUserInfo:(NSDictionary *)info toIncludedWindows:(NSArray<UIWindow *> **)includedWindowsPtr excludedWindows:(NSArray<UIWindow *> **)excludedWindowsPtr {
  180. if (info[@"includedWindows"] && info[@"excludedWindows"]) {
  181. NSLog(@"LookinServer - Do not pass 'includedWindows' and 'excludedWindows' in the same time. Learn more: https://lookin.work/faq/lookin-ios/");
  182. }
  183. [info enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  184. if ([key isEqual:@"includedWindows"] || [key isEqual:@"excludedWindows"]) {
  185. return;
  186. }
  187. NSLog(@"LookinServer - The key '%@' you passed is not valid. Learn more: https://lookin.work/faq/lookin-ios/", key);
  188. }];
  189. NSArray<UIWindow *> *includedWindows = [info objectForKey:@"includedWindows"];
  190. if (includedWindows) {
  191. if ([includedWindows isKindOfClass:[NSArray class]]) {
  192. includedWindows = [includedWindows lookin_filter:^BOOL(UIWindow *obj) {
  193. if ([obj isKindOfClass:[UIWindow class]]) {
  194. return YES;
  195. }
  196. NSLog(@"LookinServer - Error. The class of element in 'includedWindows' array must be UIWindow, but you've passed '%@'. Learn more: https://lookin.work/faq/lookin-ios/", NSStringFromClass(obj.class));
  197. return NO;
  198. }];
  199. } else {
  200. NSLog(@"LookinServer - Error. The 'includedWindows' must be a NSArray, but you've passed '%@'. Learn more: https://lookin.work/faq/lookin-ios/", NSStringFromClass([includedWindows class]));
  201. includedWindows = nil;
  202. }
  203. }
  204. NSArray<UIWindow *> *excludedWindows = nil;
  205. // 只有当 includedWindows 无效时,才会应用 excludedWindows
  206. if (includedWindows.count == 0) {
  207. excludedWindows = [info objectForKey:@"excludedWindows"];
  208. if (excludedWindows) {
  209. if ([excludedWindows isKindOfClass:[NSArray class]]) {
  210. excludedWindows = [excludedWindows lookin_filter:^BOOL(UIWindow *obj) {
  211. if ([obj isKindOfClass:[UIWindow class]]) {
  212. return YES;
  213. }
  214. NSLog(@"LookinServer - Error. The class of element in 'excludedWindows' array must be UIWindow, but you've passed '%@'. Learn more: https://lookin.work/faq/lookin-ios/", NSStringFromClass(obj.class));
  215. return NO;
  216. }];
  217. } else {
  218. NSLog(@"LookinServer - Error. The 'excludedWindows' must be a NSArray, but you've passed '%@'. Learn more: https://lookin.work/faq/lookin-ios/", NSStringFromClass([excludedWindows class]));
  219. excludedWindows = nil;
  220. }
  221. }
  222. }
  223. if (includedWindowsPtr) {
  224. *includedWindowsPtr = includedWindows;
  225. }
  226. if (excludedWindowsPtr) {
  227. *excludedWindowsPtr = excludedWindows;
  228. }
  229. }
  230. @end
  231. /// 这个类使得用户可以通过 NSClassFromString(@"Lookin") 来判断 LookinServer 是否被编译进了项目里
  232. @interface Lookin : NSObject
  233. @end
  234. @implementation Lookin
  235. @end