Skip to content

Commit 6030404

Browse files
committed
fix bug
1 parent 8b925e0 commit 6030404

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

LookinDemo/OC_Pod/LookinDemoOC/ViewController.m

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ - (void)viewDidLoad {
4040
[self.view addSubview:stackView];
4141

4242
stackView.frame = self.view.bounds;
43-
44-
45-
NSLog(@"UIStackViewAlignmentLeading %@", @(UIStackViewAlignmentLeading));
46-
NSLog(@"UIStackViewAlignmentTop %@", @(UIStackViewAlignmentTop));
47-
NSLog(@"UIStackViewAlignmentFirstBaseline %@", @(UIStackViewAlignmentFirstBaseline));
48-
NSLog(@"UIStackViewAlignmentCenter %@", @(UIStackViewAlignmentCenter));
49-
NSLog(@"UIStackViewAlignmentTrailing %@", @(UIStackViewAlignmentTrailing));
50-
NSLog(@"UIStackViewAlignmentBottom %@", @(UIStackViewAlignmentBottom));
51-
NSLog(@"UIStackViewAlignmentLastBaseline %@", @(UIStackViewAlignmentLastBaseline));
5243
}
5344

5445
@end

Src/Main/Server/Connection/LKS_ConnectionManager.m

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Src/Main/Shared/Peertalk/Lookin_PTChannel.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ - (BOOL)acceptIncomingConnection:(dispatch_fd_t)serverSocketFD {
424424

425425

426426
- (void)close {
427+
// NSLog(@"LookinServer - Channel will close: %@", self);
427428
if ((connState_ == kConnStateConnecting || connState_ == kConnStateConnected) && dispatchObj_channel_) {
428429
dispatch_io_close(dispatchObj_channel_, DISPATCH_IO_STOP);
429430
[self setDispatchChannel:NULL];
@@ -434,6 +435,7 @@ - (void)close {
434435

435436

436437
- (void)cancel {
438+
// NSLog(@"LookinServer - Channel will cancel: %@", self);
437439
if ((connState_ == kConnStateConnecting || connState_ == kConnStateConnected) && dispatchObj_channel_) {
438440
dispatch_io_close(dispatchObj_channel_, 0);
439441
[self setDispatchChannel:NULL];

0 commit comments

Comments
 (0)