LKS_ConnectionManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LookinServer.m
  4. // LookinServer
  5. //
  6. // Created by Li Kai on 2018/8/5.
  7. // https://lookin.work
  8. //
  9. #import "LKS_ConnectionManager.h"
  10. #import "Lookin_PTChannel.h"
  11. #import "LKS_RequestHandler.h"
  12. #import "LookinConnectionResponseAttachment.h"
  13. #import "LKS_ExportManager.h"
  14. #import "LookinServerDefines.h"
  15. #import "LKS_TraceManager.h"
  16. #import "LKS_MultiplatformAdapter.h"
  17. NSString *const LKS_ConnectionDidEndNotificationName = @"LKS_ConnectionDidEndNotificationName";
  18. @interface LKS_ConnectionManager () <Lookin_PTChannelDelegate>
  19. @property(nonatomic, weak) Lookin_PTChannel *peerChannel_;
  20. @property(nonatomic, strong) LKS_RequestHandler *requestHandler;
  21. @end
  22. @implementation LKS_ConnectionManager
  23. + (instancetype)sharedInstance {
  24. static LKS_ConnectionManager *sharedInstance;
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. sharedInstance = [[LKS_ConnectionManager alloc] init];
  28. });
  29. return sharedInstance;
  30. }
  31. + (void)load {
  32. // 触发 init 方法
  33. [LKS_ConnectionManager sharedInstance];
  34. }
  35. - (instancetype)init {
  36. if (self = [super init]) {
  37. NSLog(@"LookinServer - Will launch. Framework version: %@", LOOKIN_SERVER_READABLE_VERSION);
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleApplicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleWillResignActiveNotification) name:UIApplicationWillResignActiveNotification object:nil];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleLocalInspect:) name:@"Lookin_2D" object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleLocalInspect:) name:@"Lookin_3D" object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserverForName:@"Lookin_Export" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  43. [[LKS_ExportManager sharedInstance] exportAndShare];
  44. }];
  45. [[NSNotificationCenter defaultCenter] addObserverForName:@"Lookin_RelationSearch" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  46. [[LKS_TraceManager sharedInstance] addSearchTarger:note.object];
  47. }];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleGetLookinInfo:) name:@"GetLookinInfo" object:nil];
  49. self.requestHandler = [LKS_RequestHandler new];
  50. }
  51. return self;
  52. }
  53. - (void)_handleWillResignActiveNotification {
  54. self.applicationIsActive = NO;
  55. if (self.peerChannel_ && ![self.peerChannel_ isConnected]) {
  56. [self.peerChannel_ close];
  57. self.peerChannel_ = nil;
  58. }
  59. }
  60. - (void)_handleApplicationDidBecomeActive {
  61. self.applicationIsActive = YES;
  62. [self searchPortToListenIfNoConnection];
  63. }
  64. - (void)searchPortToListenIfNoConnection {
  65. if ([self.peerChannel_ isConnected]) {
  66. NSLog(@"LookinServer - Abort to search ports. Already has connected channel.");
  67. return;
  68. }
  69. NSLog(@"LookinServer - Searching port to listen...");
  70. [self.peerChannel_ close];
  71. self.peerChannel_ = nil;
  72. if ([self isiOSAppOnMac]) {
  73. [self _tryToListenOnPortFrom:LookinSimulatorIPv4PortNumberStart to:LookinSimulatorIPv4PortNumberEnd current:LookinSimulatorIPv4PortNumberStart];
  74. } else {
  75. [self _tryToListenOnPortFrom:LookinUSBDeviceIPv4PortNumberStart to:LookinUSBDeviceIPv4PortNumberEnd current:LookinUSBDeviceIPv4PortNumberStart];
  76. }
  77. }
  78. - (BOOL)isiOSAppOnMac {
  79. #if TARGET_OS_SIMULATOR
  80. return YES;
  81. #else
  82. if (@available(iOS 14.0, *)) {
  83. // isiOSAppOnMac 这个 API 看似在 iOS 14.0 上可用,但其实在 iOS 14 beta 上是不存在的、有 unrecognized selector 问题,因此这里要用 respondsToSelector 做一下保护
  84. NSProcessInfo *info = [NSProcessInfo processInfo];
  85. if ([info respondsToSelector:@selector(isiOSAppOnMac)]) {
  86. return [info isiOSAppOnMac];
  87. } else if ([info respondsToSelector:@selector(isMacCatalystApp)]) {
  88. return [info isMacCatalystApp];
  89. } else {
  90. return NO;
  91. }
  92. }
  93. if (@available(iOS 13.0, tvOS 13.0, *)) {
  94. return [NSProcessInfo processInfo].isMacCatalystApp;
  95. }
  96. return NO;
  97. #endif
  98. }
  99. - (void)_tryToListenOnPortFrom:(int)fromPort to:(int)toPort current:(int)currentPort {
  100. Lookin_PTChannel *channel = [Lookin_PTChannel channelWithDelegate:self];
  101. channel.targetPort = currentPort;
  102. [channel listenOnPort:currentPort IPv4Address:INADDR_LOOPBACK callback:^(NSError *error) {
  103. if (error) {
  104. if (error.code == 48) {
  105. // 该地址已被占用
  106. } else {
  107. // 未知失败
  108. }
  109. if (currentPort < toPort) {
  110. // 尝试下一个端口
  111. NSLog(@"LookinServer - 127.0.0.1:%d is unavailable(%@). Will try anothor address ...", currentPort, error);
  112. [self _tryToListenOnPortFrom:fromPort to:toPort current:(currentPort + 1)];
  113. } else {
  114. // 所有端口都尝试完毕,全部失败
  115. NSLog(@"LookinServer - 127.0.0.1:%d is unavailable(%@).", currentPort, error);
  116. NSLog(@"LookinServer - Connect failed in the end.");
  117. }
  118. } else {
  119. // 成功
  120. NSLog(@"LookinServer - Connected successfully on 127.0.0.1:%d", currentPort);
  121. // 此时 peerChannel_ 状态为 listening
  122. self.peerChannel_ = channel;
  123. }
  124. }];
  125. }
  126. - (void)dealloc {
  127. if (self.peerChannel_) {
  128. [self.peerChannel_ close];
  129. }
  130. [[NSNotificationCenter defaultCenter] removeObserver:self];
  131. }
  132. - (void)respond:(LookinConnectionResponseAttachment *)data requestType:(uint32_t)requestType tag:(uint32_t)tag {
  133. [self _sendData:data frameOfType:requestType tag:tag];
  134. }
  135. - (void)pushData:(NSObject *)data type:(uint32_t)type {
  136. [self _sendData:data frameOfType:type tag:0];
  137. }
  138. - (void)_sendData:(NSObject *)data frameOfType:(uint32_t)frameOfType tag:(uint32_t)tag {
  139. if (self.peerChannel_) {
  140. NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:data];
  141. dispatch_data_t payload = [archivedData createReferencingDispatchData];
  142. [self.peerChannel_ sendFrameOfType:frameOfType tag:tag withPayload:payload callback:^(NSError *error) {
  143. if (error) {
  144. }
  145. }];
  146. }
  147. }
  148. #pragma mark - Lookin_PTChannelDelegate
  149. - (BOOL)ioFrameChannel:(Lookin_PTChannel*)channel shouldAcceptFrameOfType:(uint32_t)type tag:(uint32_t)tag payloadSize:(uint32_t)payloadSize {
  150. if (channel != self.peerChannel_) {
  151. return NO;
  152. } else if ([self.requestHandler canHandleRequestType:type]) {
  153. return YES;
  154. } else {
  155. [channel close];
  156. return NO;
  157. }
  158. }
  159. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(Lookin_PTData*)payload {
  160. id object = nil;
  161. if (payload) {
  162. id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfDispatchData:payload.dispatchData]];
  163. if ([unarchivedObject isKindOfClass:[LookinConnectionAttachment class]]) {
  164. LookinConnectionAttachment *attachment = (LookinConnectionAttachment *)unarchivedObject;
  165. object = attachment.data;
  166. } else {
  167. object = unarchivedObject;
  168. }
  169. }
  170. [self.requestHandler handleRequestType:type tag:tag object:object];
  171. }
  172. /// 当 Client 端链接成功时,该方法会被调用,然后 channel 的状态会变成 connected
  173. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didAcceptConnection:(Lookin_PTChannel*)otherChannel fromAddress:(Lookin_PTAddress*)address {
  174. NSLog(@"LookinServer - channel:%@, acceptConnection:%@", channel.debugTag, otherChannel.debugTag);
  175. Lookin_PTChannel *previousChannel = self.peerChannel_;
  176. otherChannel.targetPort = address.port;
  177. self.peerChannel_ = otherChannel;
  178. [previousChannel cancel];
  179. }
  180. /// 当连接过 Lookin 客户端,然后 Lookin 客户端又被关闭时,会走到这里
  181. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didEndWithError:(NSError*)error {
  182. if (self.peerChannel_ != channel) {
  183. // Client 端第一次连接上时,之前 listen 的 port 会被 Peertalk 内部 cancel(并在 didAcceptConnection 方法里给业务抛一个新建的 connected 状态的 channel),那个被 cancel 的 channel 会走到这里
  184. NSLog(@"LookinServer - Ignore channel%@ end.", channel.debugTag);
  185. return;
  186. }
  187. // Client 端关闭时,会走到这里
  188. NSLog(@"LookinServer - channel%@ DidEndWithError:%@", channel.debugTag, error);
  189. [[NSNotificationCenter defaultCenter] postNotificationName:LKS_ConnectionDidEndNotificationName object:self];
  190. [self searchPortToListenIfNoConnection];
  191. }
  192. #pragma mark - Handler
  193. - (void)_handleLocalInspect:(NSNotification *)note {
  194. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Lookin" message:@"Failed to run local inspection. The feature has been removed. Please use the computer version of Lookin or consider SDKs like FLEX for similar functionality." preferredStyle:UIAlertControllerStyleAlert];
  195. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
  196. [alertController addAction:okAction];
  197. UIWindow *keyWindow = [LKS_MultiplatformAdapter keyWindow];
  198. UIViewController *rootViewController = [keyWindow rootViewController];
  199. [rootViewController presentViewController:alertController animated:YES completion:nil];
  200. NSLog(@"LookinServer - Failed to run local inspection. The feature has been removed. Please use the computer version of Lookin or consider SDKs like FLEX for similar functionality.");
  201. }
  202. - (void)handleGetLookinInfo:(NSNotification *)note {
  203. NSDictionary* userInfo = note.userInfo;
  204. if (!userInfo) {
  205. return;
  206. }
  207. NSMutableDictionary* infoWrapper = userInfo[@"infos"];
  208. if (![infoWrapper isKindOfClass:[NSMutableDictionary class]]) {
  209. NSLog(@"LookinServer - GetLookinInfo failed. Params invalid.");
  210. return;
  211. }
  212. infoWrapper[@"lookinServerVersion"] = LOOKIN_SERVER_READABLE_VERSION;
  213. }
  214. @end
  215. /// 这个类使得用户可以通过 NSClassFromString(@"Lookin") 来判断 LookinServer 是否被编译进了项目里
  216. @interface Lookin : NSObject
  217. @end
  218. @implementation Lookin
  219. @end
  220. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */