diff --git a/src/ios/SocketsForCordova/Classes/SocketAdapter.m b/src/ios/SocketsForCordova/Classes/SocketAdapter.m index 7534ddc..6fb5ec0 100644 --- a/src/ios/SocketsForCordova/Classes/SocketAdapter.m +++ b/src/ios/SocketsForCordova/Classes/SocketAdapter.m @@ -1,6 +1,7 @@ #include #include #include +#include #import "SocketAdapter.h" CFReadStreamRef readStream; @@ -11,6 +12,8 @@ NSOutputStream *outputStream; BOOL wasOpenned = FALSE; +int const WRITE_BUFFER_SIZE = 10 * 1024; + @implementation SocketAdapter - (void)open:(NSString *)host port:(NSNumber*)port { @@ -169,14 +172,27 @@ BOOL wasOpenned = FALSE; } - (void)write:(NSArray *)dataArray { - uint8_t buf[dataArray.count]; - for (int i = 0; i < dataArray.count; i++) { - buf[i] = (unsigned char)[[dataArray objectAtIndex:i] integerValue]; + int numberOfBatches = ceil((float)dataArray.count / (float)WRITE_BUFFER_SIZE); + for (int i = 0; i < (numberOfBatches - 1); i++) { + [self writeSubarray:dataArray offset:i * WRITE_BUFFER_SIZE length:WRITE_BUFFER_SIZE]; } - NSInteger bytesWritten = [outputStream write:buf maxLength:dataArray.count]; + int lastBatchPosition = (numberOfBatches - 1) * WRITE_BUFFER_SIZE; + [self writeSubarray:dataArray offset:lastBatchPosition length:(dataArray.count - lastBatchPosition)]; +} + +- (void)writeSubarray:(NSArray *)dataArray offset:(long)offset length:(long)length { + uint8_t buf[length]; + for (long i = 0; i < length; i++) { + unsigned char byte = (unsigned char)[[dataArray objectAtIndex:(offset + i)] integerValue]; + buf[i] = byte; + } + NSInteger bytesWritten = [outputStream write:buf maxLength:length]; if (bytesWritten == -1) { @throw [NSException exceptionWithName:@"SocketException" reason:[outputStream.streamError localizedDescription] userInfo:nil]; } + if (bytesWritten != length) { + [self writeSubarray:dataArray offset:(offset + bytesWritten) length:(length - bytesWritten)]; + } } - (void)close { @@ -189,4 +205,4 @@ BOOL wasOpenned = FALSE; [self closeInputStream]; } -@end \ No newline at end of file +@end