Fixed ios multiple sockets streams
This commit is contained in:
parent
5f5b0264a1
commit
44dfa14566
@ -19,6 +19,11 @@
|
||||
|
||||
@interface SocketAdapter : NSObject <NSStreamDelegate> {
|
||||
@public
|
||||
CFReadStreamRef readStream1;
|
||||
CFWriteStreamRef writeStream1;
|
||||
|
||||
NSInputStream *inputStream1;
|
||||
NSOutputStream *outputStream1;
|
||||
}
|
||||
|
||||
- (void)open:(NSString *)host port:(NSNumber*)port;
|
||||
|
@ -35,29 +35,33 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
|
||||
- (void)open:(NSString *)host port:(NSNumber*)port {
|
||||
|
||||
CFReadStreamRef readStream2;
|
||||
CFWriteStreamRef writeStream2;
|
||||
|
||||
|
||||
NSLog(@"Setting up connection to %@ : %@", host, [port stringValue]);
|
||||
|
||||
if (![self isIp:host]) {
|
||||
host = [self resolveIp:host];
|
||||
}
|
||||
|
||||
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)host, [port intValue], &readStream, &writeStream);
|
||||
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)host, [port intValue], &readStream2, &writeStream2);
|
||||
|
||||
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
|
||||
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
|
||||
CFReadStreamSetProperty(readStream2, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
|
||||
CFWriteStreamSetProperty(writeStream2, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
|
||||
|
||||
if(!CFWriteStreamOpen(writeStream) || !CFReadStreamOpen(readStream)) {
|
||||
NSLog(@"Error, streams not open");
|
||||
if(!CFWriteStreamOpen(writeStream2) || !CFReadStreamOpen(readStream2)) {
|
||||
NSLog(@"Error, streams not open");
|
||||
|
||||
@throw [NSException exceptionWithName:@"SocketException" reason:@"Cannot open streams." userInfo:nil];
|
||||
}
|
||||
}
|
||||
|
||||
inputStream = (__bridge NSInputStream *)readStream;
|
||||
[inputStream setDelegate:self];
|
||||
[inputStream open];
|
||||
inputStream1 = (__bridge NSInputStream *)readStream2;
|
||||
[inputStream1 setDelegate:self];
|
||||
[inputStream1 open];
|
||||
|
||||
outputStream = (__bridge NSOutputStream *)writeStream;
|
||||
[outputStream open];
|
||||
outputStream1 = (__bridge NSOutputStream *)writeStream2;
|
||||
[outputStream1 open];
|
||||
|
||||
[self performSelectorOnMainThread:@selector(runReadLoop) withObject:nil waitUntilDone:NO];
|
||||
}
|
||||
@ -96,7 +100,7 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
}
|
||||
|
||||
- (void)runReadLoop {
|
||||
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[inputStream1 scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
}
|
||||
|
||||
- (void)shutdownWrite {
|
||||
@ -104,20 +108,20 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
|
||||
[self closeOutputStream];
|
||||
|
||||
int socket = [self socknumForStream: inputStream];
|
||||
int socket = [self socknumForStream: inputStream1];
|
||||
shutdown(socket, 1);
|
||||
}
|
||||
|
||||
- (void)closeInputStream {
|
||||
[inputStream close];
|
||||
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[inputStream setDelegate:nil];
|
||||
inputStream = nil;
|
||||
[inputStream1 close];
|
||||
[inputStream1 removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[inputStream1 setDelegate:nil];
|
||||
inputStream1 = nil;
|
||||
}
|
||||
|
||||
- (void)closeOutputStream {
|
||||
[outputStream close];
|
||||
outputStream = nil;
|
||||
[outputStream1 close];
|
||||
outputStream1 = nil;
|
||||
}
|
||||
|
||||
-(int) socknumForStream: (NSStream *)stream
|
||||
@ -142,9 +146,9 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
break;
|
||||
}
|
||||
case NSStreamEventHasBytesAvailable: {
|
||||
if(stream == inputStream) {
|
||||
if(stream == inputStream1) {
|
||||
uint8_t buf[65535];
|
||||
long len = [inputStream read:buf maxLength:65535];
|
||||
long len = [inputStream1 read:buf maxLength:65535];
|
||||
|
||||
if(len > 0) {
|
||||
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
|
||||
@ -159,7 +163,7 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
}
|
||||
case NSStreamEventEndEncountered: {
|
||||
|
||||
if(stream == inputStream) {
|
||||
if(stream == inputStream1) {
|
||||
[self closeInputStream];
|
||||
|
||||
self.closeEventHandler(FALSE);
|
||||
@ -203,9 +207,9 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
unsigned char byte = (unsigned char)[[dataArray objectAtIndex:(offset + i)] integerValue];
|
||||
buf[i] = byte;
|
||||
}
|
||||
NSInteger bytesWritten = [outputStream write:buf maxLength:length];
|
||||
NSInteger bytesWritten = [outputStream1 write:buf maxLength:length];
|
||||
if (bytesWritten == -1) {
|
||||
@throw [NSException exceptionWithName:@"SocketException" reason:[outputStream.streamError localizedDescription] userInfo:nil];
|
||||
@throw [NSException exceptionWithName:@"SocketException" reason:[outputStream1.streamError localizedDescription] userInfo:nil];
|
||||
}
|
||||
if (bytesWritten != length) {
|
||||
[self writeSubarray:dataArray offset:(offset + bytesWritten) length:(length - bytesWritten)];
|
||||
|
@ -24,15 +24,15 @@
|
||||
|
||||
- (void) open : (CDVInvokedUrlCommand*) command {
|
||||
|
||||
NSString *socketKey = [command.arguments objectAtIndex:0];
|
||||
NSString *host = [command.arguments objectAtIndex:1];
|
||||
NSNumber *port = [command.arguments objectAtIndex:2];
|
||||
NSString *socketKey = [command.arguments objectAtIndex:0];
|
||||
NSString *host = [command.arguments objectAtIndex:1];
|
||||
NSNumber *port = [command.arguments objectAtIndex:2];
|
||||
|
||||
if (socketAdapters == nil) {
|
||||
self->socketAdapters = [[NSMutableDictionary alloc] init];
|
||||
}
|
||||
self->socketAdapters = [[NSMutableDictionary alloc] init];
|
||||
}
|
||||
|
||||
__block SocketAdapter* socketAdapter = [[SocketAdapter alloc] init];
|
||||
__block SocketAdapter* socketAdapter = [[SocketAdapter alloc] init];
|
||||
socketAdapter.openEventHandler = ^ void () {
|
||||
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
|
||||
|
||||
@ -80,8 +80,8 @@
|
||||
}
|
||||
@catch (NSException *e) {
|
||||
[self.commandDelegate
|
||||
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason]
|
||||
callbackId:command.callbackId];
|
||||
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason]
|
||||
callbackId:command.callbackId];
|
||||
|
||||
socketAdapter = nil;
|
||||
}
|
||||
@ -95,7 +95,7 @@
|
||||
|
||||
SocketAdapter *socket = [self getSocketAdapter:socketKey];
|
||||
|
||||
[self.commandDelegate runInBackground:^{
|
||||
[self.commandDelegate runInBackground:^{
|
||||
@try {
|
||||
[socket write:data];
|
||||
[self.commandDelegate
|
||||
@ -114,19 +114,19 @@
|
||||
|
||||
NSString* socketKey = [command.arguments objectAtIndex:0];
|
||||
|
||||
SocketAdapter *socket = [self getSocketAdapter:socketKey];
|
||||
SocketAdapter *socket = [self getSocketAdapter:socketKey];
|
||||
|
||||
[self.commandDelegate runInBackground:^{
|
||||
@try {
|
||||
[socket shutdownWrite];
|
||||
[self.commandDelegate
|
||||
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK]
|
||||
callbackId:command.callbackId];
|
||||
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK]
|
||||
callbackId:command.callbackId];
|
||||
}
|
||||
@catch (NSException *e) {
|
||||
[self.commandDelegate
|
||||
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason]
|
||||
callbackId:command.callbackId];
|
||||
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason]
|
||||
callbackId:command.callbackId];
|
||||
}
|
||||
}];
|
||||
}
|
||||
@ -135,7 +135,7 @@
|
||||
|
||||
NSString* socketKey = [command.arguments objectAtIndex:0];
|
||||
|
||||
SocketAdapter *socket = [self getSocketAdapter:socketKey];
|
||||
SocketAdapter *socket = [self getSocketAdapter:socketKey];
|
||||
|
||||
[self.commandDelegate runInBackground:^{
|
||||
@try {
|
||||
@ -156,13 +156,13 @@
|
||||
}
|
||||
|
||||
- (SocketAdapter*) getSocketAdapter: (NSString*) socketKey {
|
||||
SocketAdapter* socketAdapter = [self->socketAdapters objectForKey:socketKey];
|
||||
if (socketAdapter == nil) {
|
||||
NSString *exceptionReason = [NSString stringWithFormat:@"Cannot find socketKey: %@. Connection is probably closed.", socketKey];
|
||||
SocketAdapter* socketAdapter = [self->socketAdapters objectForKey:socketKey];
|
||||
if (socketAdapter == nil) {
|
||||
NSString *exceptionReason = [NSString stringWithFormat:@"Cannot find socketKey: %@. Connection is probably closed.", socketKey];
|
||||
|
||||
@throw [NSException exceptionWithName:@"IllegalArgumentException" reason:exceptionReason userInfo:nil];
|
||||
}
|
||||
return socketAdapter;
|
||||
@throw [NSException exceptionWithName:@"IllegalArgumentException" reason:exceptionReason userInfo:nil];
|
||||
}
|
||||
return socketAdapter;
|
||||
}
|
||||
|
||||
- (void) removeSocketAdapter: (NSString*) socketKey {
|
||||
@ -171,8 +171,8 @@
|
||||
}
|
||||
|
||||
- (BOOL) socketAdapterExists: (NSString*) socketKey {
|
||||
SocketAdapter* socketAdapter = [self->socketAdapters objectForKey:socketKey];
|
||||
return socketAdapter != nil;
|
||||
SocketAdapter* socketAdapter = [self->socketAdapters objectForKey:socketKey];
|
||||
return socketAdapter != nil;
|
||||
}
|
||||
|
||||
- (void) dispatchEventWithDictionary: (NSDictionary*) dictionary {
|
||||
|
Loading…
Reference in New Issue
Block a user