Added ios open and write timeouts, changed js errors format

This commit is contained in:
kitolog
2018-02-28 11:35:33 +03:00
parent 64acef4ce9
commit 462dfdafc8
7 changed files with 295 additions and 216 deletions

View File

@@ -24,6 +24,9 @@
NSInputStream *inputStream1;
NSOutputStream *outputStream1;
NSTimer *openTimer;
NSTimer *writeTimer;
}
- (void)open:(NSString *)host port:(NSNumber*)port;
@@ -36,6 +39,6 @@
@property (copy) void (^openErrorEventHandler)(NSString*);
@property (copy) void (^dataConsumer)(NSArray*);
@property (copy) void (^closeEventHandler)(BOOL);
@property (copy) void (^errorEventHandler)(NSString*);
@property (copy) void (^errorEventHandler)(NSString*, NSString *);
@end

View File

@@ -27,19 +27,24 @@ CFWriteStreamRef writeStream;
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSTimer *openTimer;
NSTimer *writeTimer;
BOOL wasOpenned = FALSE;
int const WRITE_BUFFER_SIZE = 10 * 1024;
int openTimeoutSeconds = 5.0;
int writeTimeoutSeconds = 5.0;
@implementation SocketAdapter
- (void)open:(NSString *)host port:(NSNumber*)port {
CFReadStreamRef readStream2;
CFWriteStreamRef writeStream2;
NSLog(@"Setting up connection to %@ : %@", host, [port stringValue]);
NSLog(@"[NATIVE] Setting up connection to %@ : %@", host, [port stringValue]);
if (![self isIp:host]) {
host = [self resolveIp:host];
@@ -51,7 +56,7 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
CFWriteStreamSetProperty(writeStream2, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
if(!CFWriteStreamOpen(writeStream2) || !CFReadStreamOpen(readStream2)) {
NSLog(@"Error, streams not open");
NSLog(@"[NATIVE] Error, streams not open");
@throw [NSException exceptionWithName:@"SocketException" reason:@"Cannot open streams." userInfo:nil];
}
@@ -60,12 +65,28 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
[inputStream1 setDelegate:self];
[inputStream1 open];
NSTimer *timer = [NSTimer timerWithTimeInterval:openTimeoutSeconds target:self selector:@selector(onOpenTimeout:) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
openTimer = timer;
outputStream1 = (__bridge NSOutputStream *)writeStream2;
[outputStream1 open];
[self performSelectorOnMainThread:@selector(runReadLoop) withObject:nil waitUntilDone:NO];
}
-(void)onOpenTimeout:(NSTimer *)timer {
NSLog(@"[NATIVE] Open timeout: %d", openTimeoutSeconds);
self.errorEventHandler(@"openTimeout", @"timeout");
openTimer = nil;
}
-(void)onWriteTimeout:(NSTimer *)timer {
NSLog(@"[NATIVE] Write timeout: %d", writeTimeoutSeconds);
self.errorEventHandler(@"writeTimeout", @"timeout");
writeTimer = nil;
}
-(BOOL)isIp:(NSString*) host {
const char *utf8 = [host UTF8String];
@@ -82,7 +103,7 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
-(NSString*)resolveIp:(NSString*) host {
NSLog(@"Resolving host: %@", host);
NSLog(@"[NATIVE] Resolving host: %@", host);
const char *buff = [host cStringUsingEncoding:NSUTF8StringEncoding];
struct hostent *host_entry = gethostbyname(buff);
@@ -94,7 +115,7 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
char *hostCstring = inet_ntoa(*((struct in_addr *)host_entry->h_addr_list[0]));
host = [NSString stringWithUTF8String:hostCstring];
NSLog(@"Resolved ip: %@", host);
NSLog(@"[NATIVE] Resolved ip: %@", host);
return host;
}
@@ -104,7 +125,7 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
}
- (void)shutdownWrite {
NSLog(@"Shuting down write on socket.");
NSLog(@"[NATIVE] Shuting down write on socket.");
[self closeOutputStream];
@@ -143,10 +164,21 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
case NSStreamEventOpenCompleted: {
self.openEventHandler();
wasOpenned = TRUE;
if(openTimer != nil){
NSLog(@"[NATIVE] openTimer invalidate on open event");
[openTimer invalidate];
openTimer = nil;
}
break;
}
case NSStreamEventHasBytesAvailable: {
if(stream == inputStream1) {
if(writeTimer != nil){
NSLog(@"[NATIVE] writeTimer invalidate on has bytes event");
[writeTimer invalidate];
writeTimer = nil;
}
uint8_t buf[65535];
long len = [inputStream1 read:buf maxLength:65535];
@@ -172,14 +204,14 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
}
case NSStreamEventErrorOccurred:
{
NSLog(@"Stream event error: %@", [[stream streamError] localizedDescription]);
NSLog(@"[NATIVE] Stream event error: %@", [[stream streamError] localizedDescription]);
if (wasOpenned) {
self.errorEventHandler([[stream streamError] localizedDescription]);
self.errorEventHandler([[stream streamError] localizedDescription], @"general");
self.closeEventHandler(TRUE);
}
else {
self.errorEventHandler([[stream streamError] localizedDescription]);
self.errorEventHandler([[stream streamError] localizedDescription], @"general");
self.openErrorEventHandler([[stream streamError] localizedDescription]);
}
//[self closeStreams];
@@ -198,6 +230,11 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
[self writeSubarray:dataArray offset:i * WRITE_BUFFER_SIZE length:WRITE_BUFFER_SIZE];
}
int lastBatchPosition = (numberOfBatches - 1) * WRITE_BUFFER_SIZE;
NSTimer *timer = [NSTimer timerWithTimeInterval:writeTimeoutSeconds target:self selector:@selector(onWriteTimeout:) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
writeTimer = timer;
[self writeSubarray:dataArray offset:lastBatchPosition length:(dataArray.count - lastBatchPosition)];
}
@@ -224,6 +261,18 @@ int const WRITE_BUFFER_SIZE = 10 * 1024;
- (void)closeStreams {
[self closeOutputStream];
[self closeInputStream];
if(writeTimer != nil){
[writeTimer invalidate];
writeTimer = nil;
NSLog(@"[NATIVE] writeTimer invalidate on close");
}
if(openTimer != nil){
[openTimer invalidate];
openTimer = nil;
NSLog(@"[NATIVE] openTimer invalidate on close");
}
}
@end

View File

@@ -47,9 +47,10 @@
socketAdapter = nil;
};
socketAdapter.errorEventHandler = ^ void (NSString *error){
socketAdapter.errorEventHandler = ^ void (NSString *error, NSString *errorType){
NSMutableDictionary *errorDictionaryData = [[NSMutableDictionary alloc] init];
[errorDictionaryData setObject:@"Error" forKey:@"type"];
[errorDictionaryData setObject:errorType forKey:@"errorType"];
[errorDictionaryData setObject:error forKey:@"errorMessage"];
[errorDictionaryData setObject:socketKey forKey:@"socketKey"];
@@ -97,7 +98,11 @@
[self.commandDelegate runInBackground:^{
@try {
[socket write:data];
if (socket != nil) {
[socket write:data];
}else{
NSLog(@"[NATIVE] Write: socket is nil. SocketKey: %@", socketKey);
}
[self.commandDelegate
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK]
callbackId:command.callbackId];
@@ -118,7 +123,12 @@
[self.commandDelegate runInBackground:^{
@try {
[socket shutdownWrite];
if (socket != nil) {
[socket shutdownWrite];
}else{
NSLog(@"[NATIVE] ShutdownWrite: socket is nil. SocketKey: %@", socketKey);
}
[self.commandDelegate
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK]
callbackId:command.callbackId];
@@ -139,7 +149,12 @@
[self.commandDelegate runInBackground:^{
@try {
[socket close];
if (socket != nil) {
[socket close];
}else{
NSLog(@"[NATIVE] Close: socket is nil. SocketKey: %@", socketKey);
}
[self.commandDelegate
sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK]
callbackId:command.callbackId];
@@ -158,15 +173,16 @@
- (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];
NSLog(@"[NATIVE] Cannot find socketKey: %@. Connection is probably closed.", socketKey);
//NSString *exceptionReason = [NSString stringWithFormat:@"Cannot find socketKey: %@. Connection is probably closed.", socketKey];
@throw [NSException exceptionWithName:@"IllegalArgumentException" reason:exceptionReason userInfo:nil];
//@throw [NSException exceptionWithName:@"IllegalArgumentException" reason:exceptionReason userInfo:nil];
}
return socketAdapter;
}
- (void) removeSocketAdapter: (NSString*) socketKey {
NSLog(@"Removing socket adapter from storage.");
NSLog(@"[NATIVE] Removing socket adapter from storage.");
[self->socketAdapters removeObjectForKey:socketKey];
}
@@ -178,7 +194,7 @@
- (void) dispatchEventWithDictionary: (NSDictionary*) dictionary {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[self dispatchEvent:jsonString];
}