I am working on parsing a fairly complicated data stream generated by a usb device that emulates a keyboard. The easiest way for me to conceptualize and deal with the data would be if I had a method called something like GetNextInputCharacter, and I could do all the parsing in one go without having to do it one piece at a time as I receive input. Unfortunately I won’t know how many bytes to expect in the stream until I have parsed into it significantly, and I would rather not wait until the end to parse it anyway.
Is there any mechanism or design pattern that can take asynchronous input from the key event and pass it to my parse method on demand? All I can think of is an IEnumerable that does a busy wait on a FIFO that the key event populates and yields them out one at a time. That seems like a bit of a hack, but maybe it would work. I just want a way for the parse routine to pretend like the input is already there and take it without knowing that it has to wait for the events.
How about something like this: