Skip to content

Commit

Permalink
Fixed a calibration issue with multiple peers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ge0rges committed Mar 9, 2017
1 parent 1913cec commit fea4f74
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion Synaction/Synaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef void(^ _Nullable calibrationBlock)(NSArray <MCPeerID *> * _Nullable peer
+ (instancetype _Nonnull)sharedManager;// Use this to get an instance of Synaction

- (void)askPeersToCalculateOffset:(NSArray <MCPeerID*>* _Nonnull)peers;// Asks the peers to call -calculateTimeOffsetWithHost, when completed the block of -executeBlockWhenPeerCalibrates will be called on host.
- (void)calculateTimeOffsetWithHost;// Calculate the time difference in nanoseconds between us and the host device.
- (void)calculateTimeOffsetWithHost:(MCPeerID * _Nonnull)hostPeer;// Calculate the time difference in nanoseconds between us and the host device.
- (uint64_t)currentNetworkTime;// The current host time adjusted for offset (offset = 0 if host)
- (void)atExactTime:(uint64_t)val runBlock:(dispatch_block_t _Nonnull)block;// Run block at the exact host adjusted time val adjusted
- (void)executeBlockWhenAllPeersCalibrate:(NSArray <MCPeerID *> * _Nonnull)peers block:(calibrationBlock)completionBlock;// Once EVERY peer in the array calibrates this will be called
Expand Down
8 changes: 4 additions & 4 deletions Synaction/Synaction.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ - (void)askPeersToCalculateOffset:(NSArray <MCPeerID*>* _Nonnull)peers {
}

// Meant for speakers.
- (void)calculateTimeOffsetWithHost {
- (void)calculateTimeOffsetWithHost:(MCPeerID *)hostPeer {
if (!isCalibrating) {
isCalibrating = YES;// Used to track the calibration
[calculatedOffsets removeAllObjects];// Remove all previously calculated offsets
Expand All @@ -126,7 +126,7 @@ - (void)calculateTimeOffsetWithHost {
}];
NSData *payload = [NSKeyedArchiver archivedDataWithRootObject:payloadDic];

[self.connectivityManager sendData:payload toPeers:self.connectivityManager.allPeers reliable:YES];
[self.connectivityManager sendData:payload toPeers:@[hostPeer] reliable:YES];
}

// Handle 0 calibrations
Expand All @@ -138,7 +138,7 @@ - (void)calculateTimeOffsetWithHost {
NSMutableDictionary *payloadDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"command": @"syncDone"}];
NSData *payload = [NSKeyedArchiver archivedDataWithRootObject:payloadDic];

[self.connectivityManager sendData:payload toPeers:self.connectivityManager.allPeers reliable:YES];
[self.connectivityManager sendData:payload toPeers:@[hostPeer] reliable:YES];
}
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCP

// Check if the host is asking us to sync
if ([payload[@"command"] isEqualToString:@"sync"]) {
[self calculateTimeOffsetWithHost];
[self calculateTimeOffsetWithHost:peerID];

return;

Expand Down

0 comments on commit fea4f74

Please sign in to comment.