diff --git a/SonarKit.podspec b/SonarKit.podspec index 11b5bcb3c..3b449eccb 100644 --- a/SonarKit.podspec +++ b/SonarKit.podspec @@ -17,7 +17,7 @@ Pod::Spec.new do |spec| spec.dependency 'Folly' spec.dependency 'Sonar' spec.dependency 'CocoaAsyncSocket', '~> 7.6' - # spec.dependency 'PeerTalk' + spec.dependency 'PeerTalk' spec.dependency 'OpenSSL-Static', '1.0.2.c1' spec.source_files = 'iOS/FBDefines/*.{h,cpp,m,mm}', 'iOS/SonarKit/**/*.{h,cpp,m,mm}', 'iOS/SonarKit/FBCxxUtils/*.{h, mm}', spec.public_header_files = 'iOS/SonarKit/CppBridge/*.{h}', diff --git a/iOS/Sample/Podfile.lock b/iOS/Sample/Podfile.lock index e37b3891d..790647b0b 100644 --- a/iOS/Sample/Podfile.lock +++ b/iOS/Sample/Podfile.lock @@ -106,19 +106,19 @@ EXTERNAL SOURCES: CHECKOUT OPTIONS: ComponentKit: - :commit: 368a8b74e1940b9c86b1a883cd7668bc5a204536 + :commit: 73acd7995610ad6b009a9a74b8290a54dab7c716 :git: https://github.com/facebook/ComponentKit.git PeerTalk: :commit: 588303b43efa5082d654b6f75d1b84a6ba4b5b9e :git: https://github.com/rsms/PeerTalk.git RSocket: - :commit: 193002818e53e7a7d73ee362627ddf1fdabc4493 + :commit: 89b368ce04d9b319593815c6a69ad0a681596146 :git: https://github.com/rsocket/rsocket-cpp.git Sonar: - :commit: 8d48f5a3eb26c6e982abefa30f5a8e0c19b5b4a6 + :commit: f1a3e6c1bdbae8df4fa787fb8f51a622baab0f5e :git: https://github.com/facebook/Sonar.git SonarKit: - :commit: 8d48f5a3eb26c6e982abefa30f5a8e0c19b5b4a6 + :commit: f1a3e6c1bdbae8df4fa787fb8f51a622baab0f5e :git: https://github.com/facebook/Sonar.git SPEC CHECKSUMS: @@ -132,8 +132,8 @@ SPEC CHECKSUMS: OpenSSL-Static: bd17e34564a8591ad76b740318683a6caa19a13e PeerTalk: aadc42bc7d7f19e89f817b59ef8196305a711504 RSocket: e9ee232080f995ba8f403ccf4cb61238a5646cb6 - Sonar: 29d8fb9b010e56fa1358be996d68d4b436dd8c21 - SonarKit: f127bb6582dd1d2590e91937bea9793e95d8a159 + Sonar: a7d8648c0536858abf352d481d57957adf01859b + SonarKit: 926549191a4b239da9274539d8ff28b761b4b1f3 Yoga: e6f1fed82138c17da5332e15e5770abf0e9cc386 YogaKit: bb90d11e297e06abef7e0cfb20e035a6bd00cdc4 diff --git a/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.h b/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.h index 3118a523b..72d4514a7 100644 --- a/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.h +++ b/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.h @@ -1,18 +1,18 @@ -// /* -// * Copyright (c) 2018-present, Facebook, Inc. -// * -// * This source code is licensed under the MIT license found in the LICENSE -// * file in the root directory of this source tree. -// * -// */ -// #import -// -// @interface SKPortForwardingServer : NSObject -// -// - (instancetype)init; -// -// - (void)listenForMultiplexingChannelOnPort:(NSUInteger)port; -// - (void)forwardConnectionsFromPort:(NSUInteger)port; -// - (void)close; -// -// @end +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + * + */ +#import + +@interface SKPortForwardingServer : NSObject + +- (instancetype)init; + +- (void)listenForMultiplexingChannelOnPort:(NSUInteger)port; +- (void)forwardConnectionsFromPort:(NSUInteger)port; +- (void)close; + +@end diff --git a/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.m b/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.m index f83ca06fd..7bb61075a 100644 --- a/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.m +++ b/iOS/SonarKit/Utilities/PortForwarding/SKPortForwardingServer.m @@ -1,190 +1,190 @@ -// /* -// * Copyright (c) 2018-present, Facebook, Inc. -// * -// * This source code is licensed under the MIT license found in the LICENSE -// * file in the root directory of this source tree. -// * -// */ -// #import "SKPortForwardingServer.h" -// -// #import -// -// #import -// #import -// -// #import "SKMacros.h" -// #import "SKPortForwardingCommon.h" -// -// @interface SKPortForwardingServer () -// -// @property (nonatomic, weak) PTChannel *serverChannel; -// @property (nonatomic, weak) PTChannel *peerChannel; -// -// @property (nonatomic, strong) GCDAsyncSocket *serverSocket; -// @property (nonatomic, strong) NSMutableDictionary *clientSockets; -// @property (nonatomic, assign) UInt32 lastClientSocketTag; -// @property (nonatomic, strong) dispatch_queue_t socketQueue; -// @property (nonatomic, strong) PTProtocol *protocol; -// -// @end -// -// @implementation SKPortForwardingServer -// -// - (instancetype)init -// { -// if (self = [super init]) { -// _socketQueue = dispatch_queue_create("SKPortForwardingServer", DISPATCH_QUEUE_SERIAL); -// _lastClientSocketTag = 0; -// _clientSockets = [NSMutableDictionary dictionary]; -// _protocol = [[PTProtocol alloc] initWithDispatchQueue:_socketQueue]; -// } -// return self; -// } -// -// - (void)dealloc -// { -// [self close]; -// [[NSNotificationCenter defaultCenter] removeObserver:self]; -// } -// -// - (void)forwardConnectionsFromPort:(NSUInteger)port -// { -// [self _forwardConnectionsFromPort:port reportError:YES]; -// [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) { -// [self _forwardConnectionsFromPort:port reportError:NO]; -// }]; -// } -// -// - (void)_forwardConnectionsFromPort:(NSUInteger)port reportError:(BOOL)shouldReportError -// { -// GCDAsyncSocket *serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_socketQueue]; -// NSError *listenError; -// if ([serverSocket acceptOnPort:port error:&listenError]) { -// self.serverSocket = serverSocket; -// } else { -// if (shouldReportError) { -// SKLog(@"Failed to listen: %@", listenError); -// } -// } -// } -// -// - (void)listenForMultiplexingChannelOnPort:(NSUInteger)port -// { -// [self _listenForMultiplexingChannelOnPort:port reportError:YES]; -// [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) { -// [self _listenForMultiplexingChannelOnPort:port reportError:NO]; -// }]; -// } -// -// - (void)_listenForMultiplexingChannelOnPort:(NSUInteger)port reportError:(BOOL)shouldReportError -// { -// PTChannel *channel = [[PTChannel alloc] initWithProtocol:_protocol delegate:self]; -// [channel listenOnPort:port IPv4Address:INADDR_LOOPBACK callback:^(NSError *error) { -// if (error) { -// if (shouldReportError) { -// SKLog(@"Failed to listen on 127.0.0.1:%lu: %@", (unsigned long)port, error); -// } -// } else { -// SKTrace(@"Listening on 127.0.0.1:%lu", (unsigned long)port); -// self.serverChannel = channel; -// } -// }]; -// } -// -// - (void)close -// { -// if (self.serverChannel) { -// [self.serverChannel close]; -// self.serverChannel = nil; -// } -// [self.serverSocket disconnect]; -// } -// -// #pragma mark - PTChannelDelegate -// -// - (void)ioFrameChannel:(PTChannel *)channel didAcceptConnection:(PTChannel *)otherChannel fromAddress:(PTAddress *)address { -// // Cancel any other connection. We are FIFO, so the last connection -// // established will cancel any previous connection and "take its place". -// if (self.peerChannel) { -// [self.peerChannel cancel]; -// } -// -// // Weak pointer to current connection. Connection objects live by themselves -// // (owned by its parent dispatch queue) until they are closed. -// self.peerChannel = otherChannel; -// self.peerChannel.userInfo = address; -// SKTrace(@"Connected to %@", address); -// } -// -// - (void)ioFrameChannel:(PTChannel *)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData *)payload { -// //NSLog(@"didReceiveFrameOfType: %u, %u, %@", type, tag, payload); -// if (type == SKPortForwardingFrameTypeWriteToPipe) { -// GCDAsyncSocket *sock = self.clientSockets[@(tag)]; -// [sock writeData:[NSData dataWithBytes:payload.data length:payload.length] withTimeout:-1 tag:0]; -// SKTrace(@"channel -> socket (%d), %zu bytes", tag, payload.length); -// } -// -// if (type == SKPortForwardingFrameTypeClosePipe) { -// GCDAsyncSocket *sock = self.clientSockets[@(tag)]; -// [sock disconnectAfterWriting]; -// } -// } -// -// - (void)ioFrameChannel:(PTChannel *)channel didEndWithError:(NSError *)error { -// for (GCDAsyncSocket *sock in [_clientSockets objectEnumerator]) { -// [sock setDelegate:nil]; -// [sock disconnect]; -// } -// [self.clientSockets removeAllObjects]; -// SKTrace(@"Disconnected from %@, error = %@", channel.userInfo, error); -// } -// -// -// #pragma mark - GCDAsyncSocketDelegate -// -// - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket -// { -// dispatch_block_t block = ^() { -// if (!self.peerChannel) { -// [newSocket setDelegate:nil]; -// [newSocket disconnect]; -// } -// -// UInt32 tag = ++self->_lastClientSocketTag; -// newSocket.userData = @(tag); -// newSocket.delegate = self; -// self.clientSockets[@(tag)] = newSocket; -// [self.peerChannel sendFrameOfType:SKPortForwardingFrameTypeOpenPipe tag:self->_lastClientSocketTag withPayload:nil callback:^(NSError *error) { -// SKTrace(@"open socket (%d), error = %@", (unsigned int)tag, error); -// [newSocket readDataWithTimeout:-1 tag:0]; -// }]; -// }; -// -// if (_peerChannel) { -// block(); -// } else { -// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), _socketQueue, block); -// } -// } -// -// - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)_ -// { -// UInt32 tag = [[sock userData] unsignedIntValue]; -// SKTrace(@"Incoming data on socket (%d) - %lu bytes", (unsigned int)tag, (unsigned long)data.length); -// [_peerChannel sendFrameOfType:SKPortForwardingFrameTypeWriteToPipe tag:tag withPayload:NSDataToGCDData(data) callback:^(NSError *error) { -// SKTrace(@"socket (%d) -> channel %lu bytes, error = %@", (unsigned int)tag, (unsigned long)data.length, error); -// [sock readDataWithTimeout:-1 tag:_]; -// }]; -// } -// -// - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err -// { -// UInt32 tag = [sock.userData unsignedIntValue]; -// [_clientSockets removeObjectForKey:@(tag)]; -// [_peerChannel sendFrameOfType:SKPortForwardingFrameTypeClosePipe tag:tag withPayload:nil callback:^(NSError *error) { -// SKTrace(@"socket (%d) disconnected, err = %@, peer error = %@", (unsigned int)tag, err, error); -// }]; -// } -// -// -// @end +/* + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + * + */ +#import "SKPortForwardingServer.h" + +#import + +#import +#import + +#import "SKMacros.h" +#import "SKPortForwardingCommon.h" + +@interface SKPortForwardingServer () + +@property (nonatomic, weak) PTChannel *serverChannel; +@property (nonatomic, weak) PTChannel *peerChannel; + +@property (nonatomic, strong) GCDAsyncSocket *serverSocket; +@property (nonatomic, strong) NSMutableDictionary *clientSockets; +@property (nonatomic, assign) UInt32 lastClientSocketTag; +@property (nonatomic, strong) dispatch_queue_t socketQueue; +@property (nonatomic, strong) PTProtocol *protocol; + +@end + +@implementation SKPortForwardingServer + +- (instancetype)init +{ + if (self = [super init]) { + _socketQueue = dispatch_queue_create("SKPortForwardingServer", DISPATCH_QUEUE_SERIAL); + _lastClientSocketTag = 0; + _clientSockets = [NSMutableDictionary dictionary]; + _protocol = [[PTProtocol alloc] initWithDispatchQueue:_socketQueue]; + } + return self; +} + +- (void)dealloc +{ + [self close]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (void)forwardConnectionsFromPort:(NSUInteger)port +{ + [self _forwardConnectionsFromPort:port reportError:YES]; + [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) { + [self _forwardConnectionsFromPort:port reportError:NO]; + }]; +} + +- (void)_forwardConnectionsFromPort:(NSUInteger)port reportError:(BOOL)shouldReportError +{ + GCDAsyncSocket *serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_socketQueue]; + NSError *listenError; + if ([serverSocket acceptOnPort:port error:&listenError]) { + self.serverSocket = serverSocket; + } else { + if (shouldReportError) { + SKLog(@"Failed to listen: %@", listenError); + } + } +} + +- (void)listenForMultiplexingChannelOnPort:(NSUInteger)port +{ + [self _listenForMultiplexingChannelOnPort:port reportError:YES]; + [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) { + [self _listenForMultiplexingChannelOnPort:port reportError:NO]; + }]; +} + +- (void)_listenForMultiplexingChannelOnPort:(NSUInteger)port reportError:(BOOL)shouldReportError +{ + PTChannel *channel = [[PTChannel alloc] initWithProtocol:_protocol delegate:self]; + [channel listenOnPort:port IPv4Address:INADDR_LOOPBACK callback:^(NSError *error) { + if (error) { + if (shouldReportError) { + SKLog(@"Failed to listen on 127.0.0.1:%lu: %@", (unsigned long)port, error); + } + } else { + SKTrace(@"Listening on 127.0.0.1:%lu", (unsigned long)port); + self.serverChannel = channel; + } + }]; +} + +- (void)close +{ + if (self.serverChannel) { + [self.serverChannel close]; + self.serverChannel = nil; + } + [self.serverSocket disconnect]; +} + +#pragma mark - PTChannelDelegate + +- (void)ioFrameChannel:(PTChannel *)channel didAcceptConnection:(PTChannel *)otherChannel fromAddress:(PTAddress *)address { + // Cancel any other connection. We are FIFO, so the last connection + // established will cancel any previous connection and "take its place". + if (self.peerChannel) { + [self.peerChannel cancel]; + } + + // Weak pointer to current connection. Connection objects live by themselves + // (owned by its parent dispatch queue) until they are closed. + self.peerChannel = otherChannel; + self.peerChannel.userInfo = address; + SKTrace(@"Connected to %@", address); +} + +- (void)ioFrameChannel:(PTChannel *)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData *)payload { + //NSLog(@"didReceiveFrameOfType: %u, %u, %@", type, tag, payload); + if (type == SKPortForwardingFrameTypeWriteToPipe) { + GCDAsyncSocket *sock = self.clientSockets[@(tag)]; + [sock writeData:[NSData dataWithBytes:payload.data length:payload.length] withTimeout:-1 tag:0]; + SKTrace(@"channel -> socket (%d), %zu bytes", tag, payload.length); + } + + if (type == SKPortForwardingFrameTypeClosePipe) { + GCDAsyncSocket *sock = self.clientSockets[@(tag)]; + [sock disconnectAfterWriting]; + } +} + +- (void)ioFrameChannel:(PTChannel *)channel didEndWithError:(NSError *)error { + for (GCDAsyncSocket *sock in [_clientSockets objectEnumerator]) { + [sock setDelegate:nil]; + [sock disconnect]; + } + [self.clientSockets removeAllObjects]; + SKTrace(@"Disconnected from %@, error = %@", channel.userInfo, error); +} + + +#pragma mark - GCDAsyncSocketDelegate + +- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket +{ + dispatch_block_t block = ^() { + if (!self.peerChannel) { + [newSocket setDelegate:nil]; + [newSocket disconnect]; + } + + UInt32 tag = ++self->_lastClientSocketTag; + newSocket.userData = @(tag); + newSocket.delegate = self; + self.clientSockets[@(tag)] = newSocket; + [self.peerChannel sendFrameOfType:SKPortForwardingFrameTypeOpenPipe tag:self->_lastClientSocketTag withPayload:nil callback:^(NSError *error) { + SKTrace(@"open socket (%d), error = %@", (unsigned int)tag, error); + [newSocket readDataWithTimeout:-1 tag:0]; + }]; + }; + + if (_peerChannel) { + block(); + } else { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), _socketQueue, block); + } +} + +- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)_ +{ + UInt32 tag = [[sock userData] unsignedIntValue]; + SKTrace(@"Incoming data on socket (%d) - %lu bytes", (unsigned int)tag, (unsigned long)data.length); + [_peerChannel sendFrameOfType:SKPortForwardingFrameTypeWriteToPipe tag:tag withPayload:NSDataToGCDData(data) callback:^(NSError *error) { + SKTrace(@"socket (%d) -> channel %lu bytes, error = %@", (unsigned int)tag, (unsigned long)data.length, error); + [sock readDataWithTimeout:-1 tag:_]; + }]; +} + +- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err +{ + UInt32 tag = [sock.userData unsignedIntValue]; + [_clientSockets removeObjectForKey:@(tag)]; + [_peerChannel sendFrameOfType:SKPortForwardingFrameTypeClosePipe tag:tag withPayload:nil callback:^(NSError *error) { + SKTrace(@"socket (%d) disconnected, err = %@, peer error = %@", (unsigned int)tag, err, error); + }]; +} + + +@end diff --git a/iOS/third-party-podspecs/Folly.podspec b/iOS/third-party-podspecs/Folly.podspec index 150250f2f..1623e2bfd 100755 --- a/iOS/third-party-podspecs/Folly.podspec +++ b/iOS/third-party-podspecs/Folly.podspec @@ -11,8 +11,8 @@ Pod::Spec.new do |spec| spec.dependency 'boost-for-react-native' spec.dependency 'DoubleConversion' spec.dependency 'glog' - #spec.dependency 'OpenSSL-Static', '1.0.2.c1' - #spec.dependency 'CocoaLibEvent', '~> 1.0' + spec.dependency 'OpenSSL-Static', '1.0.2.c1' + spec.dependency 'CocoaLibEvent', '~> 1.0' spec.compiler_flags = '-DFOLLY_HAVE_PTHREAD=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_HAVE_LIBGFLAGS=0 -DFOLLY_HAVE_LIBJEMALLOC=0 -DFOLLY_HAVE_PREADV=0 -DFOLLY_HAVE_PWRITEV=0 -DFOLLY_HAVE_TFO=0 -DFOLLY_USE_SYMBOLIZER=0 -frtti -fexceptions -std=c++14