Run CLANGFORMAT on FlipperKit folder

Summary:
This diff runs CLANGFORMAT lint on FlipperKit. I have added CLANGFORMAT as the lint engined for objc files in xplat/sonar. Right now the iOS folder is not formatted according to CLANGFORMAT.
Ran `arc lint -a --paths-cmd "find ./iOS/FlipperKit -type f" --verbose`

Reviewed By: passy

Differential Revision: D19942170

fbshipit-source-id: af677323af4edb761f61f8f7e289cab743aa31f2
This commit is contained in:
Pritesh Nandgaonkar
2020-02-17 10:46:43 -08:00
committed by Facebook Github Bot
parent 127eec5fa1
commit ca513cf370
21 changed files with 510 additions and 360 deletions

View File

@@ -15,22 +15,23 @@
static const NSTimeInterval ReconnectDelay = 1.0;
@interface FKPortForwardingClient () <GCDAsyncSocketDelegate, PTChannelDelegate>
{
@interface FKPortForwardingClient ()<
GCDAsyncSocketDelegate,
PTChannelDelegate> {
NSUInteger _destPort;
NSUInteger _channelPort;
NSNumber *_connectingToDeviceID;
NSNumber *_connectedDeviceID;
NSDictionary *_connectedDeviceProperties;
NSNumber* _connectingToDeviceID;
NSNumber* _connectedDeviceID;
NSDictionary* _connectedDeviceProperties;
BOOL _notConnectedQueueSuspended;
PTChannel *_connectedChannel;
PTChannel* _connectedChannel;
dispatch_queue_t _notConnectedQueue;
dispatch_queue_t _clientSocketsQueue;
NSMutableDictionary *_clientSockets;
NSMutableDictionary* _clientSockets;
}
@property (atomic, readonly) NSNumber *connectedDeviceID;
@property (atomic, assign) PTChannel *connectedChannel;
@property(atomic, readonly) NSNumber* connectedDeviceID;
@property(atomic, assign) PTChannel* connectedChannel;
@end
@@ -38,42 +39,40 @@ static const NSTimeInterval ReconnectDelay = 1.0;
@synthesize connectedDeviceID = _connectedDeviceID;
- (instancetype)init
{
- (instancetype)init {
if (self = [super init]) {
_notConnectedQueue = dispatch_queue_create("FKPortForwarding.notConnectedQueue", DISPATCH_QUEUE_SERIAL);
_clientSocketsQueue = dispatch_queue_create("FKPortForwarding.clients", DISPATCH_QUEUE_SERIAL);
_notConnectedQueue = dispatch_queue_create(
"FKPortForwarding.notConnectedQueue", DISPATCH_QUEUE_SERIAL);
_clientSocketsQueue = dispatch_queue_create(
"FKPortForwarding.clients", DISPATCH_QUEUE_SERIAL);
_clientSockets = [NSMutableDictionary dictionary];
}
return self;
}
- (void)forwardConnectionsToPort:(NSUInteger)port
{
- (void)forwardConnectionsToPort:(NSUInteger)port {
_destPort = port;
}
- (void)connectToMultiplexingChannelOnPort:(NSUInteger)port
{
- (void)connectToMultiplexingChannelOnPort:(NSUInteger)port {
_channelPort = port;
[self startListeningForDevices];
[self enqueueConnectToLocalIPv4Port];
}
- (void)close
{
- (void)close {
[self.connectedChannel close];
}
- (PTChannel *)connectedChannel {
- (PTChannel*)connectedChannel {
return _connectedChannel;
}
- (void)setConnectedChannel:(PTChannel *)connectedChannel {
- (void)setConnectedChannel:(PTChannel*)connectedChannel {
_connectedChannel = connectedChannel;
if (!_connectedChannel) {
for (GCDAsyncSocket *sock in [_clientSockets objectEnumerator]) {
for (GCDAsyncSocket* sock in [_clientSockets objectEnumerator]) {
[sock setDelegate:nil];
[sock disconnect];
}
@@ -94,40 +93,52 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
#pragma mark - PTChannelDelegate
- (void)ioFrameChannel:(PTChannel *)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData *)payload {
//NSLog(@"received %@, %u, %u, %@", channel, type, tag, payload);
- (void)ioFrameChannel:(PTChannel*)channel
didReceiveFrameOfType:(uint32_t)type
tag:(uint32_t)tag
payload:(PTData*)payload {
// NSLog(@"received %@, %u, %u, %@", channel, type, tag, payload);
if (type == FKPortForwardingFrameTypeOpenPipe) {
GCDAsyncSocket *sock = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_clientSocketsQueue];
GCDAsyncSocket* sock =
[[GCDAsyncSocket alloc] initWithDelegate:self
delegateQueue:_clientSocketsQueue];
sock.userData = @(tag);
_clientSockets[@(tag)] = sock;
NSError *connectError;
if (![sock connectToHost:@"localhost" onPort:_destPort error:&connectError]) {
FBPFLog(@"Failed to connect to local %lu - %@", (unsigned long)_destPort, connectError);
NSError* connectError;
if (![sock connectToHost:@"localhost"
onPort:_destPort
error:&connectError]) {
FBPFLog(
@"Failed to connect to local %lu - %@",
(unsigned long)_destPort,
connectError);
}
FBPFTrace(@"open socket (%d)", tag);
}
if (type == FKPortForwardingFrameTypeWriteToPipe) {
GCDAsyncSocket *sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length] withTimeout:-1 tag:0];
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length]
withTimeout:-1
tag:0];
FBPFTrace(@"channel -> socket (%d) %zu bytes", tag, payload.length);
}
if (type == FKPortForwardingFrameTypeClosePipe) {
GCDAsyncSocket *sock = _clientSockets[@(tag)];
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock disconnectAfterWriting];
FBPFTrace(@"close socket (%d)", tag);
}
}
- (void)ioFrameChannel:(PTChannel *)channel didEndWithError:(NSError *)error {
if (_connectedDeviceID && [_connectedDeviceID isEqualToNumber:channel.userInfo]) {
- (void)ioFrameChannel:(PTChannel*)channel didEndWithError:(NSError*)error {
if (_connectedDeviceID &&
[_connectedDeviceID isEqualToNumber:channel.userInfo]) {
[self didDisconnectFromDevice:_connectedDeviceID];
}
@@ -137,69 +148,85 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
FBPFTrace(@"socket (%ld) connected to %@", (long)[sock.userData integerValue], host);
- (void)socket:(GCDAsyncSocket*)sock
didConnectToHost:(NSString*)host
port:(uint16_t)port {
FBPFTrace(
@"socket (%ld) connected to %@",
(long)[sock.userData integerValue],
host);
[sock readDataWithTimeout:-1 tag:0];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
- (void)socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err {
UInt32 tag = [sock.userData unsignedIntValue];
[_clientSockets removeObjectForKey:@(tag)];
FBPFTrace(@"socket (%d) disconnected", (unsigned int)tag);
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe tag:tag withPayload:nil callback:nil];
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe
tag:tag
withPayload:nil
callback:nil];
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)_
{
- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)_ {
UInt32 tag = [sock.userData unsignedIntValue];
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe tag:tag withPayload:NSDataToGCDData(data) callback:^(NSError *error) {
FBPFTrace(@"channel -> socket (%d), %lu bytes", (unsigned int)tag, (unsigned long)data.length);
[sock readDataWithTimeout:-1 tag:0];
}];
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe
tag:tag
withPayload:NSDataToGCDData(data)
callback:^(NSError* error) {
FBPFTrace(
@"channel -> socket (%d), %lu bytes",
(unsigned int)tag,
(unsigned long)data.length);
[sock readDataWithTimeout:-1 tag:0];
}];
}
#pragma mark - Wired device connections
- (void)startListeningForDevices {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
__weak typeof(self) weakSelf = self;
[nc addObserverForName:PTUSBDeviceDidAttachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
NSNumber *deviceID = [note.userInfo objectForKey:@"DeviceID"];
//NSLog(@"PTUSBDeviceDidAttachNotification: %@", note.userInfo);
FBPFTrace(@"PTUSBDeviceDidAttachNotification: %@", deviceID);
[nc addObserverForName:PTUSBDeviceDidAttachNotification
object:PTUSBHub.sharedHub
queue:nil
usingBlock:^(NSNotification* note) {
NSNumber* deviceID = [note.userInfo objectForKey:@"DeviceID"];
// NSLog(@"PTUSBDeviceDidAttachNotification: %@",
// note.userInfo);
FBPFTrace(@"PTUSBDeviceDidAttachNotification: %@", deviceID);
typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
dispatch_async(strongSelf->_notConnectedQueue, ^{
[strongSelf didAttachToDevice:deviceID note:note];
});
}];
dispatch_async(strongSelf->_notConnectedQueue, ^{
[strongSelf didAttachToDevice:deviceID note:note];
});
}];
[nc addObserverForName:PTUSBDeviceDidDetachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
NSNumber *deviceID = [note.userInfo objectForKey:@"DeviceID"];
//NSLog(@"PTUSBDeviceDidDetachNotification: %@", note.userInfo);
FBPFTrace(@"PTUSBDeviceDidDetachNotification: %@", deviceID);
[nc addObserverForName:PTUSBDeviceDidDetachNotification
object:PTUSBHub.sharedHub
queue:nil
usingBlock:^(NSNotification* note) {
NSNumber* deviceID = [note.userInfo objectForKey:@"DeviceID"];
// NSLog(@"PTUSBDeviceDidDetachNotification: %@",
// note.userInfo);
FBPFTrace(@"PTUSBDeviceDidDetachNotification: %@", deviceID);
[weakSelf didDetachFromDevice:deviceID];
}];
[weakSelf didDetachFromDevice:deviceID];
}];
}
- (void)didAttachToDevice:(NSNumber *)deviceID note:(NSNotification *)note
{
if (!_connectingToDeviceID || ![deviceID isEqualToNumber:_connectingToDeviceID]) {
- (void)didAttachToDevice:(NSNumber*)deviceID note:(NSNotification*)note {
if (!_connectingToDeviceID ||
![deviceID isEqualToNumber:_connectingToDeviceID]) {
[self disconnectFromCurrentChannel];
_connectingToDeviceID = deviceID;
_connectedDeviceProperties = [note.userInfo objectForKey:@"Properties"];
@@ -207,8 +234,7 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
- (void)didDetachFromDevice:(NSNumber *)deviceID
{
- (void)didDetachFromDevice:(NSNumber*)deviceID {
if ([_connectingToDeviceID isEqualToNumber:deviceID]) {
_connectedDeviceProperties = nil;
_connectingToDeviceID = nil;
@@ -218,8 +244,7 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
- (void)didDisconnectFromDevice:(NSNumber *)deviceID {
- (void)didDisconnectFromDevice:(NSNumber*)deviceID {
FBPFLog(@"Disconnected from device #%@", deviceID);
if ([_connectedDeviceID isEqualToNumber:deviceID]) {
[self willChangeValueForKey:@"connectedDeviceID"];
@@ -228,7 +253,6 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
- (void)disconnectFromCurrentChannel {
if (_connectedDeviceID && _connectedChannel) {
[_connectedChannel close];
@@ -244,25 +268,34 @@ static const NSTimeInterval ReconnectDelay = 1.0;
});
}
- (void)connectToLocalIPv4Port {
PTChannel *channel = [PTChannel channelWithDelegate:self];
channel.userInfo = [NSString stringWithFormat:@"127.0.0.1:%lu", (unsigned long)_channelPort];
[channel connectToPort:_channelPort IPv4Address:INADDR_LOOPBACK callback:^(NSError *error, PTAddress *address) {
if (error) {
if (error.domain == NSPOSIXErrorDomain && (error.code == ECONNREFUSED || error.code == ETIMEDOUT)) {
// this is an expected state
} else {
FBPFTrace(@"Failed to connect to 127.0.0.1:%lu: %@", (unsigned long)_channelPort, error);
}
} else {
[self disconnectFromCurrentChannel];
self.connectedChannel = channel;
channel.userInfo = address;
FBPFLog(@"Connected to %@", address);
}
[self performSelector:@selector(enqueueConnectToLocalIPv4Port) withObject:nil afterDelay:ReconnectDelay];
}];
PTChannel* channel = [PTChannel channelWithDelegate:self];
channel.userInfo =
[NSString stringWithFormat:@"127.0.0.1:%lu", (unsigned long)_channelPort];
[channel
connectToPort:_channelPort
IPv4Address:INADDR_LOOPBACK
callback:^(NSError* error, PTAddress* address) {
if (error) {
if (error.domain == NSPOSIXErrorDomain &&
(error.code == ECONNREFUSED || error.code == ETIMEDOUT)) {
// this is an expected state
} else {
FBPFTrace(
@"Failed to connect to 127.0.0.1:%lu: %@",
(unsigned long)_channelPort,
error);
}
} else {
[self disconnectFromCurrentChannel];
self.connectedChannel = channel;
channel.userInfo = address;
FBPFLog(@"Connected to %@", address);
}
[self performSelector:@selector(enqueueConnectToLocalIPv4Port)
withObject:nil
afterDelay:ReconnectDelay];
}];
}
- (void)enqueueConnectToUSBDevice {
@@ -273,30 +306,35 @@ static const NSTimeInterval ReconnectDelay = 1.0;
});
}
- (void)connectToUSBDevice {
PTChannel *channel = [PTChannel channelWithDelegate:self];
PTChannel* channel = [PTChannel channelWithDelegate:self];
channel.userInfo = _connectingToDeviceID;
channel.delegate = self;
[channel connectToPort:(int)_channelPort overUSBHub:PTUSBHub.sharedHub deviceID:_connectingToDeviceID callback:^(NSError *error) {
[self didConnectToChannel:channel withError:error];
}];
[channel connectToPort:(int)_channelPort
overUSBHub:PTUSBHub.sharedHub
deviceID:_connectingToDeviceID
callback:^(NSError* error) {
[self didConnectToChannel:channel withError:error];
}];
}
- (void)didConnectToChannel:(PTChannel *)channel withError:(NSError *)error
{
- (void)didConnectToChannel:(PTChannel*)channel withError:(NSError*)error {
if (error) {
FBPFTrace(@"Failed to connect to device #%@: %@", channel.userInfo, error);
if (channel.userInfo == _connectingToDeviceID) {
[self performSelector:@selector(enqueueConnectToUSBDevice) withObject:nil afterDelay:ReconnectDelay];
[self performSelector:@selector(enqueueConnectToUSBDevice)
withObject:nil
afterDelay:ReconnectDelay];
}
} else {
_connectedDeviceID = _connectingToDeviceID;
self.connectedChannel = channel;
FBPFLog(@"Connected to device #%@\n%@", _connectingToDeviceID, _connectedDeviceProperties);
FBPFLog(
@"Connected to device #%@\n%@",
_connectingToDeviceID,
_connectedDeviceProperties);
}
}
@end

View File

@@ -16,8 +16,8 @@ enum {
FKPortForwardingFrameTypeClosePipe = 203,
};
static dispatch_data_t NSDataToGCDData(NSData *data) {
__block NSData *retainedData = data;
static dispatch_data_t NSDataToGCDData(NSData* data) {
__block NSData* retainedData = data;
return dispatch_data_create(data.bytes, data.length, nil, ^{
retainedData = nil;
});

View File

@@ -14,26 +14,27 @@
#import "FKPortForwardingCommon.h"
@interface FKPortForwardingServer () <PTChannelDelegate, GCDAsyncSocketDelegate>
{
__weak PTChannel *_serverChannel;
__weak PTChannel *_peerChannel;
@interface FKPortForwardingServer ()<
PTChannelDelegate,
GCDAsyncSocketDelegate> {
__weak PTChannel* _serverChannel;
__weak PTChannel* _peerChannel;
GCDAsyncSocket *_serverSocket;
NSMutableDictionary *_clientSockets;
GCDAsyncSocket* _serverSocket;
NSMutableDictionary* _clientSockets;
UInt32 _lastClientSocketTag;
dispatch_queue_t _socketQueue;
PTProtocol *_protocol;
PTProtocol* _protocol;
}
@end
@implementation FKPortForwardingServer
- (instancetype)init
{
- (instancetype)init {
if (self = [super init]) {
_socketQueue = dispatch_queue_create("FKPortForwardingServer", DISPATCH_QUEUE_SERIAL);
_socketQueue =
dispatch_queue_create("FKPortForwardingServer", DISPATCH_QUEUE_SERIAL);
_lastClientSocketTag = 0;
_clientSockets = [NSMutableDictionary dictionary];
_protocol = [[PTProtocol alloc] initWithDispatchQueue:_socketQueue];
@@ -41,24 +42,27 @@
return self;
}
- (void)dealloc
{
- (void)dealloc {
[self close];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)forwardConnectionsFromPort:(NSUInteger)port
{
- (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];
}];
[[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;
- (void)_forwardConnectionsFromPort:(NSUInteger)port
reportError:(BOOL)shouldReportError {
GCDAsyncSocket* serverSocket =
[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_socketQueue];
NSError* listenError;
if ([serverSocket acceptOnPort:port error:&listenError]) {
_serverSocket = serverSocket;
} else {
@@ -68,31 +72,40 @@
}
}
- (void)listenForMultiplexingChannelOnPort:(NSUInteger)port
{
- (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];
}];
[[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) {
FBPFLog(@"Failed to listen on 127.0.0.1:%lu: %@", (unsigned long)port, error);
}
} else {
FBPFTrace(@"Listening on 127.0.0.1:%lu", (unsigned long)port);
self->_serverChannel = channel;
}
}];
- (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) {
FBPFLog(
@"Failed to listen on 127.0.0.1:%lu: %@",
(unsigned long)port,
error);
}
} else {
FBPFTrace(@"Listening on 127.0.0.1:%lu", (unsigned long)port);
self->_serverChannel = channel;
}
}];
}
- (void)close
{
- (void)close {
if (_serverChannel) {
[_serverChannel close];
_serverChannel = nil;
@@ -102,7 +115,9 @@
#pragma mark - PTChannelDelegate
- (void)ioFrameChannel:(PTChannel *)channel didAcceptConnection:(PTChannel *)otherChannel fromAddress:(PTAddress *)address {
- (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 (_peerChannel) {
@@ -116,22 +131,27 @@
FBPFTrace(@"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);
- (void)ioFrameChannel:(PTChannel*)channel
didReceiveFrameOfType:(uint32_t)type
tag:(uint32_t)tag
payload:(PTData*)payload {
// NSLog(@"didReceiveFrameOfType: %u, %u, %@", type, tag, payload);
if (type == FKPortForwardingFrameTypeWriteToPipe) {
GCDAsyncSocket *sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length] withTimeout:-1 tag:0];
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length]
withTimeout:-1
tag:0];
FBPFTrace(@"channel -> socket (%d), %zu bytes", tag, payload.length);
}
if (type == FKPortForwardingFrameTypeClosePipe) {
GCDAsyncSocket *sock = _clientSockets[@(tag)];
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock disconnectAfterWriting];
}
}
- (void)ioFrameChannel:(PTChannel *)channel didEndWithError:(NSError *)error {
for (GCDAsyncSocket *sock in [_clientSockets objectEnumerator]) {
- (void)ioFrameChannel:(PTChannel*)channel didEndWithError:(NSError*)error {
for (GCDAsyncSocket* sock in [_clientSockets objectEnumerator]) {
[sock setDelegate:nil];
[sock disconnect];
}
@@ -139,11 +159,10 @@
FBPFTrace(@"Disconnected from %@, error = %@", channel.userInfo, error);
}
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
- (void)socket:(GCDAsyncSocket*)sock
didAcceptNewSocket:(GCDAsyncSocket*)newSocket {
dispatch_block_t block = ^() {
if (!self->_peerChannel) {
[newSocket setDelegate:nil];
@@ -154,37 +173,60 @@
newSocket.userData = @(tag);
newSocket.delegate = self;
self->_clientSockets[@(tag)] = newSocket;
[self->_peerChannel sendFrameOfType:FKPortForwardingFrameTypeOpenPipe tag:self->_lastClientSocketTag withPayload:nil callback:^(NSError *error) {
FBPFTrace(@"open socket (%d), error = %@", (unsigned int)tag, error);
[newSocket readDataWithTimeout:-1 tag:0];
}];
[self->_peerChannel
sendFrameOfType:FKPortForwardingFrameTypeOpenPipe
tag:self->_lastClientSocketTag
withPayload:nil
callback:^(NSError* error) {
FBPFTrace(
@"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);
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
_socketQueue,
block);
}
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)_
{
- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)_ {
UInt32 tag = [[sock userData] unsignedIntValue];
FBPFTrace(@"Incoming data on socket (%d) - %lu bytes", (unsigned int)tag, (unsigned long)data.length);
[_peerChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe tag:tag withPayload:NSDataToGCDData(data) callback:^(NSError *error) {
FBPFTrace(@"socket (%d) -> channel %lu bytes, error = %@", (unsigned int)tag, (unsigned long)data.length, error);
[sock readDataWithTimeout:-1 tag:_];
}];
FBPFTrace(
@"Incoming data on socket (%d) - %lu bytes",
(unsigned int)tag,
(unsigned long)data.length);
[_peerChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe
tag:tag
withPayload:NSDataToGCDData(data)
callback:^(NSError* error) {
FBPFTrace(
@"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
{
- (void)socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err {
UInt32 tag = [sock.userData unsignedIntValue];
[_clientSockets removeObjectForKey:@(tag)];
[_peerChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe tag:tag withPayload:nil callback:^(NSError *error) {
FBPFTrace(@"socket (%d) disconnected, err = %@, peer error = %@", (unsigned int)tag, err, error);
}];
[_peerChannel
sendFrameOfType:FKPortForwardingFrameTypeClosePipe
tag:tag
withPayload:nil
callback:^(NSError* error) {
FBPFTrace(
@"socket (%d) disconnected, err = %@, peer error = %@",
(unsigned int)tag,
err,
error);
}];
}
@end