同步处理异步输入

发布于 2024-12-28 19:44:00 字数 334 浏览 1 评论 0原文

我正在解析由模拟键盘的 USB 设备生成的相当复杂的数据流。对我来说,概念化和处理数据的最简单方法是,如果我有一个名为 GetNextInputCharacter 之类的方法,并且我可以一次性完成所有解析,而不必一次完成一次我收到输入的时间。不幸的是,在我显着地解析它之前,我不知道流中需要多少字节,而且我宁愿不等到最后才解析它。

是否有任何机制或设计模式可以从按键事件获取异步输入并按需将其传递给我的解析方法?我能想到的就是一个 IEnumerable,它在关键事件填充的 FIFO 上进行繁忙的等待,并一次生成一个。这看起来有点像黑客,但也许它会起作用。我只是想要一种让解析例程假装输入已经存在并接受它的方法,而不知道它必须等待事件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2025-01-04 19:44:00

解析一个流,并使解析器阻塞,直到它有足够的字符来产生合理的结果怎么样?然后来自 USB 设备的异步数据就可以写入流中。您可能必须编写自己的 Stream 实现,但这并不太难。

顺便说一下,这是一个足够常见的模式 - 当您使用内置的 .net 序列化时,反序列化器会阻止读取可能通过网络套接字传入的输入流。

How about parsing a Stream, and making the parser block until it has enough characters to make a sensible result? Then the async data from the USB device can just write to the stream. You'd probably have to write your own Stream implementation, but that isn't too hard.

This is a common enough pattern by the way- when you use the built-in .net serialization, the deserializers block on reading an input stream which may be coming over a network socket.

咽泪装欢 2025-01-04 19:44:00

像这样的事情怎么样:

        ...
        var stream = //Set up stream 
        var data = from dataStream in StreamStuff(stream) select dataStream;
        ...
        private IEnumerable<String> StreamStuff(Stream stream)
        {
            stream.Open();
            while(stream.Read())
            {
                //Do some stuff to your read value
                yield return yourProcessedData;
            }
            stream.Close();
        }

How about something like this:

        ...
        var stream = //Set up stream 
        var data = from dataStream in StreamStuff(stream) select dataStream;
        ...
        private IEnumerable<String> StreamStuff(Stream stream)
        {
            stream.Open();
            while(stream.Read())
            {
                //Do some stuff to your read value
                yield return yourProcessedData;
            }
            stream.Close();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文