@@ -68,10 +68,17 @@ - (void)_handleWillResignActiveNotification {
6868}
6969
7070- (void )_handleApplicationDidBecomeActive {
71- // NSLog(@"LookinServer(0.8.0) - UIApplicationDidBecomeActiveNotification");
7271 self.applicationIsActive = YES ;
73- if (self.peerChannel_ && (self.peerChannel_ .isConnected || self.peerChannel_ .isListening )) {
74- return ;
72+ [self tryToListenPorts ];
73+ }
74+
75+ - (void )tryToListenPorts {
76+ if (self.peerChannel_ ) {
77+ // 除了 connected 和 listenin 状态之外,还可能是 close 状态。如果连接了 client 端,而 client 端又关闭了,那么这里的 channel 就会变成 close
78+ if ([self .peerChannel_ isConnected ] || [self .peerChannel_ isListening ]) {
79+ // NSLog(@"LookinServer - Abort connect trying. Already has active channel.");
80+ return ;
81+ }
7582 }
7683 NSLog (@" LookinServer - Trying to connect ..." );
7784 if ([self isiOSAppOnMac ]) {
@@ -120,9 +127,6 @@ - (void)_tryToListenOnPortFrom:(int)fromPort to:(int)toPort current:(int)current
120127 NSLog (@" LookinServer - Connected successfully on 127.0.0.1:%d " , currentPort);
121128 // 此时 peerChannel_ 状态为 listening
122129 self.peerChannel_ = channel;
123-
124- // UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@", @(currentPort)] message:nil preferredStyle:UIAlertControllerStyleAlert];
125- // [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
126130 }
127131 }];
128132}
@@ -187,17 +191,21 @@ - (void)ioFrameChannel:(Lookin_PTChannel*)channel didReceiveFrameOfType:(uint32_
187191
188192// / 当连接过 Lookin 客户端,然后 Lookin 客户端又被关闭时,会走到这里
189193- (void )ioFrameChannel : (Lookin_PTChannel*)channel didEndWithError : (NSError *)error {
194+ // NSLog(@"LookinServer - didEndWithError:%@", channel);
190195 [[NSNotificationCenter defaultCenter ] postNotificationName: LKS_ConnectionDidEndNotificationName object: self ];
196+ [self tryToListenPorts ];
191197}
192198
193199// / 当 Client 端链接成功时,该方法会被调用,然后 channel 的状态会变成 connected
194200- (void )ioFrameChannel : (Lookin_PTChannel*)channel didAcceptConnection : (Lookin_PTChannel*)otherChannel fromAddress : (Lookin_PTAddress*)address {
195- if (self.peerChannel_ ) {
196- [self .peerChannel_ cancel ];
197- }
198-
201+ // NSLog(@"LookinServer - didAcceptConnection:%@, current:%@", otherChannel, self.peerChannel_);
202+ Lookin_PTChannel *previousChannel = self.peerChannel_ ;
199203 self.peerChannel_ = otherChannel;
200204 self.peerChannel_ .userInfo = address;
205+
206+ if (previousChannel && previousChannel != self.peerChannel_ ) {
207+ [previousChannel cancel ];
208+ }
201209}
202210
203211#pragma mark - Handler
0 commit comments