Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

send data in chunk mechanism architecture for multiple peers ios

i am working on multi-peer connectivity framework. I am using following code to send and received data in chunk.The code works in case of two devices. How ever for more than two devices it crash with
[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'.

I know the reason why the crash happens, its because call:
[self.mySession sendData:chunkData toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil]

at any point from with in MCSession Delegate didReceiveData delegate. Can some one help me how should i architect my code to send data to multipeer connectivity. I dont want to send complete data at once, and i know there will be no problem of crash in sending data and receiveing data all at once. I want to send data in chunks, to all peers.

The code i have tried for now:
-(void)sendData
{
[marrFileData removeAllObjects];

NSData *sendData = UIImagePNGRepresentation([UIImage imageNamed:@test2.PNG]);
NSUInteger length = [sendData length];
NSUInteger chunkSize = 100 * 1024;
NSUInteger offset = 0;
do {
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[sendData bytes] + offset
length:thisChunkSize
freeWhenDone:NO];
NSLog(@chunk length : %lu,(unsigned long)chunk.length);

[marrFileData addObject:[NSData dataWithData:chunk]];
offset += thisChunkSize;
} while (offset < length);

noOfDataSend = 0;

if ([marrFileData count] > 0) {
NSDictionary *dicContainer = [NSDictionary dictionaryWithObjectsAndKeys:[marrFileData objectAtIndex:noOfDataSend], @kChunkData, nil];
NSData *chunkData = [NSKeyedArchiver archivedDataWithRootObject:dicContainer];
[self.mySession sendData:chunkData toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
}
}

and receiver end is :
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
NSDictionary *dicContainer = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSData *chunkData = [dicContainer objectForKey:@kChunkData];
NSLog(@data receiveddddd : %lu,(unsigned long)chunkData.length);

if (chunkData.length > 0) {
if (chunkData.length < 2) {
noOfDataSend++;
NSLog(@noofdatasend : %d,noOfDataSend);
NSLog(@array count : %d,marrFileData.count);
if (noOfDataSend < ([marrFileData count])) {
NSDictionary *dicContainer = [NSDictionary dictionaryWithObjectsAndKeys:[marrFileData objectAtIndex:noOfDataSend], @kChunkData, nil];
NSData *chunkData = [NSKeyedArchiver archivedDataWithRootObject:dicContainer];
[self.mySession sendData:chunkData toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
}else {
NSDictionary *dicContainer = [NSDictionary dictionaryWithObjectsAndKeys:[@File Transfer Done dataUsingEncoding:NSUTF8StringEncoding], @kChunkData, nil];
NSData *chunkData = [NSKeyedArchiver archivedDataWithRootObject:dicContainer];
[self.mySession sendData:chunkData toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
}
} else {
if ([[[NSString alloc] initWithData:chunkData encoding:NSUTF8StringEncoding] isEqualToString:@File Transfer Done]) {
[self appendFileData];
}else {
NSDictionary *dicContainer = [NSDictionary dictionaryWithObjectsAndKeys:[@1 dataUsingEncoding:NSUTF8StringEncoding], @kChunkData, nil];
NSData *chunkData = [NSKeyedArchiver archivedDataWithRootObject:dicContainer];
[self.mySession sendData:chunkData toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
[marrReceiveData addObject:chunkData];
}
}
}

Note: i tried using
[self.session sendData:chunkData toPeers:[NSArray arrayWithObject:peerID] withMode:MCSessionSendDataReliable error:nil];
instead of
[self.session sendData:chunkData toPeers:[self.session connectedPeers] withMode:MCSessionSendDataReliable error:nil];
in
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID,
but data is not send to other devices.That is data initiated from device A receives on B or C but not on both B & C.
Sign In or Register to comment.