Update SocketAdapter.m
Improved scalability of write operation for iOS platform
This commit is contained in:
parent
7c12fbab8d
commit
04cb562ac3
@ -1,6 +1,7 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <math.h>
|
||||||
#import "SocketAdapter.h"
|
#import "SocketAdapter.h"
|
||||||
|
|
||||||
CFReadStreamRef readStream;
|
CFReadStreamRef readStream;
|
||||||
@ -11,6 +12,8 @@ NSOutputStream *outputStream;
|
|||||||
|
|
||||||
BOOL wasOpenned = FALSE;
|
BOOL wasOpenned = FALSE;
|
||||||
|
|
||||||
|
int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||||
|
|
||||||
@implementation SocketAdapter
|
@implementation SocketAdapter
|
||||||
|
|
||||||
- (void)open:(NSString *)host port:(NSNumber*)port {
|
- (void)open:(NSString *)host port:(NSNumber*)port {
|
||||||
@ -169,14 +172,27 @@ BOOL wasOpenned = FALSE;
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)write:(NSArray *)dataArray {
|
- (void)write:(NSArray *)dataArray {
|
||||||
uint8_t buf[dataArray.count];
|
int numberOfBatches = ceil((float)dataArray.count / (float)WRITE_BUFFER_SIZE);
|
||||||
for (int i = 0; i < dataArray.count; i++) {
|
for (int i = 0; i < (numberOfBatches - 1); i++) {
|
||||||
buf[i] = (unsigned char)[[dataArray objectAtIndex:i] integerValue];
|
[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) {
|
if (bytesWritten == -1) {
|
||||||
@throw [NSException exceptionWithName:@"SocketException" reason:[outputStream.streamError localizedDescription] userInfo:nil];
|
@throw [NSException exceptionWithName:@"SocketException" reason:[outputStream.streamError localizedDescription] userInfo:nil];
|
||||||
}
|
}
|
||||||
|
if (bytesWritten != length) {
|
||||||
|
[self writeSubarray:dataArray offset:(offset + bytesWritten) length:(length - bytesWritten)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)close {
|
- (void)close {
|
||||||
@ -189,4 +205,4 @@ BOOL wasOpenned = FALSE;
|
|||||||
[self closeInputStream];
|
[self closeInputStream];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
Loading…
Reference in New Issue
Block a user