简体   繁体   English

Boost :: Beast Non Blocking是否可以读取Websocket?

[英]Boost::Beast Non Blocking Read for Websockets?

We have an app that is entirely synchronous, and will always be because it is basically a command line interpreter to send low level commands to our hardware, and you cant have two commands going to the hardware at the same time. 我们有一个完全同步的应用程序,并且永远都是因为它基本上是一个命令行解释器,用于将低级命令发送到我们的硬件,并且您不能同时有两个命令发送到硬件。 I will only ever have 1 client socket for this configuration operating in a synchronous manner, one command to the server, it talks to hardware, and sends value back to client, but as far as i see it currently async_read is the only way to do non blocking reads. 对于此配置,我将只有1个客户端套接字以同步方式运行,向服务器发送一条命令,与硬件对话,然后将值发送回客户端,但据我所知,当前,async_read是唯一的方法非阻塞读取。

What is the best way to get a non blocking read/write via Beast? 通过Beast获得无阻塞读写的最佳方法是什么? For example in TCP and Serial in Windows you have ways to peek into the buffer to see if data is ready to be accessed, and if there is you can issue your read command knowing it wont block because data is there. 例如,在Windows中的TCP和串行中,您可以窥视缓冲区以查看是否已准备好访问数据,并且如果存在数据,则可以发出读命令,知道它不会阻塞,因为存在数据。 Not sure if I am just missing this functionality in Beast, although i will say having such functionality if possible would be nice. 不知道我是否只是在Beast中缺少此功能,尽管我会说如果可能的话拥有这样的功能会很好。

Anyways so based on this i have a question 无论如何,因此我有一个问题

First, can I take the Coroutine example and instead of using yield, to create and pass it a read_handler function? 首先,我可以以协程示例为例,而不是使用yield来创建并传递read_handler函数吗?

I've taken the coroutine example, and built the functions into my class, and used the exact same read_handler from this thread answer. 我以协程示例为例,并将函数内置到类中,并使用了与该线程答案完全相同的read_handler。 How to pass read handler to async_read for Beast websocket? 如何将Beast websocket的读取处理程序传递给async_read?

It compiles as he says, but setting a break point never triggers when data is received. 如他所说,它可以编译,但是设置断点永远不会在收到数据时触发。

I dont really need the full async functionality like the async example, pushing it into different threads, in fact that makes my life more difficult because the rest of the app is not async. 我真的不需要像异步示例那样的完整异步功能,将其推入不同的线程中,实际上这使我的生活更加困难,因为该应用程序的其余部分不是异步的。 And because we allow input from various sources(keyboard/TCP/Serial/File), we cant block waiting for data. 并且由于我们允许来自各种来源(键盘/ TCP /串行/文件)的输入,因此我们无法阻止等待数据。

What is the best way to get a non blocking read/write via Beast? 通过Beast获得无阻塞读写的最佳方法是什么?

Because of the way the websocket stream is implemented, it is not possible to support non-blocking socket modes. 由于实现了websocket流的方式,因此不可能支持非阻塞套接字模式。

can I take the Coroutine example and instead of using yield, to create and pass it a read_handler function? 我可以以协程示例为例,而不是使用yield来创建并通过read_handler函数吗?

If you want to use completion handlers, I would suggest that instead of starting with the coroutine example you start with one of the asynchronous examples, since these are already written to use completion handlers. 如果您要使用完成处理程序,我建议您不要以协程示例开头,而是以其中一个异步示例开头,因为这些示例已被编写为使用完成处理程序。

Coroutines have blocking semantics, while completion handlers do not. 协程具有阻塞语义,而完成处理程序则没有。 If you try to use the coroutine example and replace the yield expression with a completion handler, the call to the initiating function will not block the way it does when using coroutines. 如果您尝试使用协程示例,并用完成处理程序替换yield表达式,则对调用函数的调用将不会阻塞使用协程时的方式。 And you should not use spawn. 而且您不应该使用spawn。 You said that the coroutine example is much easier, probably this is because it resembles synchronous code. 您说过,协程示例容易得多,可能是因为它类似于同步代码。 If you want that ease of writing and understanding, then you have to use coroutines. 如果您想简化编写和理解,则必须使用协程。 Code using completion handlers will exhibit the "inversion of control" typically associated with callbacks. 使用完成处理程序的代码将表现出通常与回调关联的“控制反转”。 This is inherent to how they work and not something you can change by just starting with code that uses coroutines and changing the completion token. 这是它们工作方式所固有的,不是您可以通过从使用协程的代码开始并更改完成令牌来更改的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM