Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit f63e1d8

Browse files
committed
Merge pull request #151 from thaliproject/cle_fixes
Cle fixes
2 parents 1695a31 + fe9d30f commit f63e1d8

15 files changed

+296
-59
lines changed

src/ios/MultipeerConnectivity/THEMultipeerClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535
// Service type here is what we're looking for, not what we may be advertising
3636
// (although they'll usually be the same)
37-
- (id)initWithPeerId:(MCPeerID *)peerId
37+
- (id)initWithPeerId:(MCPeerID *)peerId
38+
withPeerIdentifier:(NSString *)peerIdentifier
3839
withServiceType:(NSString *)serviceType
3940
withPeerNetworkingDelegate:(id<THEMultipeerSessionDelegate>)multipeerSessionDelegate;
4041

src/ios/MultipeerConnectivity/THEMultipeerClient.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ @implementation THEMultipeerClient
3838
// sessions (MCSessions) with this id and never the remote one
3939
MCPeerID * _localPeerId;
4040

41+
// App-level peer identifier
42+
NSString * _localPeerIdentifier;
43+
4144
// The multipeer browser
4245
MCNearbyServiceBrowser * _nearbyServiceBrowser;
4346

@@ -52,6 +55,7 @@ @implementation THEMultipeerClient
5255
}
5356

5457
- (id)initWithPeerId:(MCPeerID *)peerId
58+
withPeerIdentifier:(NSString *)peerIdentifier
5559
withServiceType:(NSString *)serviceType
5660
withPeerNetworkingDelegate:(id<THEMultipeerSessionDelegate>)multipeerSessionDelegate
5761
{
@@ -65,6 +69,7 @@ - (id)initWithPeerId:(MCPeerID *)peerId
6569

6670
_localPeerId = peerId;
6771
_serviceType = serviceType;
72+
_localPeerIdentifier = peerIdentifier;
6873

6974
_multipeerSessionDelegate = multipeerSessionDelegate;
7075

@@ -108,20 +113,21 @@ - (BOOL)connectToPeerWithPeerIdentifier:(NSString *)peerIdentifier
108113
if (![clientSession visible])
109114
{
110115
success = NO;
116+
NSLog(@"client: connect: unreachable %@", peerIdentifier);
111117
connectCallback(@"Peer unreachable", 0);
112118
}
113119
else
114120
{
115121
// Start connection process from the top
116122
if ([clientSession connectionState] == THEPeerSessionStateNotConnected)
117123
{
118-
119124
// connect will create the networking resources required to establish the session
120125
[clientSession connectWithConnectCallback:(ConnectCallback)connectCallback];
121126

122127
[_nearbyServiceBrowser invitePeer:[clientSession remotePeerID]
123128
toSession:[clientSession session]
124-
withContext:[peerIdentifier dataUsingEncoding:NSUTF8StringEncoding]
129+
withContext:
130+
[_localPeerIdentifier dataUsingEncoding:NSUTF8StringEncoding]
125131
timeout:30];
126132

127133
success = YES;
@@ -140,7 +146,7 @@ - (BOOL)connectToPeerWithPeerIdentifier:(NSString *)peerIdentifier
140146

141147
if (!clientSession)
142148
{
143-
NSLog(@"Unknown peer %@", peerIdentifier);
149+
NSLog(@"client: unknown peer %@", peerIdentifier);
144150
connectCallback(@"Unknown peer", 0);
145151
}
146152

@@ -253,6 +259,7 @@ - (void)browser:(MCNearbyServiceBrowser *)browser
253259

254260
clientSession = (THEMultipeerClientSession *)p;
255261
assert([[clientSession remotePeerID] hash] == [peerID hash]);
262+
NSLog(@"client: lost peer: %@", [clientSession remotePeerIdentifier]);
256263

257264
previouslyVisible = clientSession.visible;
258265

src/ios/MultipeerConnectivity/THEMultipeerClientSession.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#import "THEMultipeerClientSession.h"
3030
#import "THEMultipeerClientSocketRelay.h"
3131

32+
#include "jx.h"
33+
#import "JXcore.h"
34+
#import "THEThreading.h"
35+
3236
@implementation THEMultipeerClientSession
3337
{
3438
// Callback to fire when a connection completes (in fact when the relay
@@ -74,6 +78,7 @@ - (void)disconnect
7478
{
7579
if (prevState == THEPeerSessionStateConnecting)
7680
{
81+
NSLog(@"client session: disconnected: %@", [self remotePeerIdentifier]);
7782
_connectCallback(@"Peer disconnected", 0);
7883
}
7984
_connectCallback = nil;
@@ -120,4 +125,34 @@ - (void)didNotListenWithErrorMessage:(NSString *)errorMsg
120125
}
121126
}
122127

128+
- (void)didDisconnectFromPeer:(NSString *)peerIdentifier
129+
{
130+
NSString * const kPeerConnectionError = @"connectionError";
131+
NSString * const kEventValueStringPeerId = @"peerIdentifier";
132+
133+
// Socket disconnecting currently requires we tear down the entire p2p conection
134+
NSLog(@"client session: disconnecting due to socket close: %@", [self remotePeerIdentifier]);
135+
[self disconnect];
136+
137+
NSDictionary *connectionError = @{
138+
kEventValueStringPeerId : peerIdentifier
139+
};
140+
141+
NSError *error = nil;
142+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:connectionError
143+
options:NSJSONWritingPrettyPrinted
144+
error:&error];
145+
if (error != nil) {
146+
// Will never happen in practice
147+
NSLog(@"WARNING: Could not generate jsonString for disconnect message");
148+
return;
149+
}
150+
151+
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
152+
153+
OnMainThread(^{
154+
[JXcore callEventCallback:kPeerConnectionError withJSON:jsonString];
155+
});
156+
}
157+
123158
@end

src/ios/MultipeerConnectivity/THEMultipeerClientSocketRelay.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
- (instancetype)initWithPeerIdentifier:(NSString *)peerIdentifier
1313
withDelegate:(id<THEMultipeerClientSocketRelayDelegate>)delegate;
1414

15+
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
16+
1517
@end

src/ios/MultipeerConnectivity/THEMultipeerClientSocketRelay.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,12 @@ -(void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)accept
7474
[self didCreateSocket: acceptedSocket];
7575
}
7676

77+
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
78+
{
79+
// The upper layer has closed the client socket
80+
NSLog(@"client: socket closed");
81+
[super socketDidDisconnect:sock withError:err];
82+
[_delegate didDisconnectFromPeer:_peerIdentifier];
83+
}
84+
7785
@end

src/ios/MultipeerConnectivity/THEMultipeerClientSocketRelayDelegate.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
// THEMultipeerClientSocketRelayDelegate.m
2626
//
2727

28-
// Protocol implemented by classes wishing to know which port the client relay
29-
// is listening on
28+
// Protocol implemented by classes wishing to know about socket related events
3029
@protocol THEMultipeerClientSocketRelayDelegate <NSObject>
3130

3231
// Called when the client relay succesfully established it's listening port
@@ -35,4 +34,8 @@
3534
// Called when the client relay fails to listen
3635
- (void)didNotListenWithErrorMessage:(NSString *)errorMsg
3736
withPeerIdentifier:(NSString*)peerIdentifier;
37+
38+
// Called when the socket is disconnected (usually by the app)
39+
- (void)didDisconnectFromPeer:(NSString *)peerIdentifier;
40+
3841
@end

src/ios/MultipeerConnectivity/THEMultipeerPeerSession.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ -(void)connect
118118
_session = [[MCSession alloc] initWithPeer:_localPeerID
119119
securityIdentity:nil
120120
encryptionPreference:MCEncryptionNone];
121-
NSLog(@"Created session: %p", _session);
122121
_session.delegate = self;
123122
}
124123
}
@@ -139,8 +138,6 @@ -(void)disconnect
139138

140139
if (_session != nil)
141140
{
142-
NSLog(@"Destroying session: %p", _session);
143-
NSLog(@"%@",[NSThread callStackSymbols]);
144141
_session.delegate = nil;
145142
[_session disconnect];
146143
_session = nil;

src/ios/MultipeerConnectivity/THEMultipeerServer.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
134134
assert([[serverSession remotePeerIdentifier] isEqualToString:peerIdentifier]);
135135

136136
// Disconnect any existing session, see note below
137+
NSLog(@"server: disconnecting to refresh session (%@)", peerIdentifier);
137138
[serverSession disconnect];
138139
}
139140
else
@@ -154,6 +155,7 @@ - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
154155
return serverSession;
155156
}];
156157

158+
NSLog(@"server: accepting invitation %@", peerIdentifier);
157159
invitationHandler(YES, [_serverSession session]);
158160
}
159161

src/ios/MultipeerConnectivity/THEMultipeerSession.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ - (void)start
9494
];
9595

9696
_client = [[THEMultipeerClient alloc]
97-
initWithPeerId: _peerID withServiceType: _serviceType withPeerNetworkingDelegate: _delegate
98-
];
97+
initWithPeerId:_peerID
98+
withPeerIdentifier:_peerIdentifier
99+
withServiceType:_serviceType
100+
withPeerNetworkingDelegate:_delegate];
99101

100102
[_server start];
101103
[_client start];

0 commit comments

Comments
 (0)