@@ -68,10 +68,17 @@ - (void)_handleWillResignActiveNotification {
68
68
}
69
69
70
70
- (void )_handleApplicationDidBecomeActive {
71
- // NSLog(@"LookinServer(0.8.0) - UIApplicationDidBecomeActiveNotification");
72
71
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
+ }
75
82
}
76
83
NSLog (@" LookinServer - Trying to connect ..." );
77
84
if ([self isiOSAppOnMac ]) {
@@ -120,9 +127,6 @@ - (void)_tryToListenOnPortFrom:(int)fromPort to:(int)toPort current:(int)current
120
127
NSLog (@" LookinServer - Connected successfully on 127.0.0.1:%d " , currentPort);
121
128
// 此时 peerChannel_ 状态为 listening
122
129
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];
126
130
}
127
131
}];
128
132
}
@@ -187,17 +191,21 @@ - (void)ioFrameChannel:(Lookin_PTChannel*)channel didReceiveFrameOfType:(uint32_
187
191
188
192
// / 当连接过 Lookin 客户端,然后 Lookin 客户端又被关闭时,会走到这里
189
193
- (void )ioFrameChannel : (Lookin_PTChannel*)channel didEndWithError : (NSError *)error {
194
+ // NSLog(@"LookinServer - didEndWithError:%@", channel);
190
195
[[NSNotificationCenter defaultCenter ] postNotificationName: LKS_ConnectionDidEndNotificationName object: self ];
196
+ [self tryToListenPorts ];
191
197
}
192
198
193
199
// / 当 Client 端链接成功时,该方法会被调用,然后 channel 的状态会变成 connected
194
200
- (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_ ;
199
203
self.peerChannel_ = otherChannel;
200
204
self.peerChannel_ .userInfo = address;
205
+
206
+ if (previousChannel && previousChannel != self.peerChannel_ ) {
207
+ [previousChannel cancel ];
208
+ }
201
209
}
202
210
203
211
#pragma mark - Handler
0 commit comments