Re: Wanted: Elegant way to manage this task
Re: Wanted: Elegant way to manage this task
- Subject: Re: Wanted: Elegant way to manage this task
- From: Roland King <email@hidden>
- Date: Tue, 09 Jun 2015 14:47:54 +0800
> On 9 Jun 2015, at 14:18, Graham Cox <email@hidden> wrote:
>
> I have the following task I need to implement.
>
>
> 1. I have a queue of NSData objects, usually representing a continuous stream, but delivered in variable-sized chunks. However some chunks could be missing, but the queue represents the correct order of the stream that was delivered, and the missing chunks can be flagged.
>
> 2. The data in the stream consists of packets of fixed length. I need to extract the packets and pass them as complete single entities to another object (a delegate) for further processing. All complete packets must be extractable, and I’d like to do this on a secondary thread if possible.
>
> 3. Due to the possibility of missing data chunks, resynchronisation for each new data may be required. I can detect when a data entry is contiguous with the previous one, or when one or more chunks has been skipped. A packet may be split across input data, or may be incomplete due to being split across data where the second part was missing.
>
> 4. Packet detection is based on a signature byte at the start of each packet, and the fixed length of the packet which is a few hundred bytes. If five consecutive packets are detected having the same signature byte, the packets are presumed to have been detected and can be extracted for processing.
>
> The object that handles this should accept NSData chunks as input and queue them, or if there is a missing chunk, a nil data object. The object’s delegate should be passed the extracted packets one at a time in order as they become available, again as NSData objects. No time synchronization between the input and output is required.
>
> This sort of processing isn’t really my strong point, so while I can think of various ways to solve it by brute force and ignorance, I would have thought this is something that would be recognisable as a standard sort of stream processing algorithm by those who’ve done it before. Even just an outline or pointer to something like this would be a help.
>
Without answering 98.7% of your question, or more. Are you wedded to NSData for this? I have a stream processor, it takes randomly chunked up data from a bluetooth dongle and .. processes it. I used dispatch_data_t for it. That was introduced back with GCD a few years ago. It’s proved very good for this kind of data manipulation. You can append them to each other, split them into pieces, iterate over them but under the hood all the while it keeps the original chunks of data and avoids a lot of copying around. Only if you at some point want N contiguous bytes out of the thing to process as a byte array will it do a copy if necessary into one single memory chunk, if the piece you want is already in one chunk, it just returns it to you. I’ve found it a good way to deal with buffers of bytes very efficiently and quite intuitively.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden