- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法结合两个条件读取,直到找到匹配字符或收到 128 个字节?
可以限制 streambuf 的大小:
inBuf = std::make_shared< boost::asio::streambuf>(limit_size);
但是当处理程序的原因是“找不到元素”时,流中没有可用数据,传输的字节数也为 0。
这个想法是读到';'或接收到 128 个字节
我的代码如下:
class match_char
{
public:
explicit match_char(char c, int len) : c_(c), len_(len) {}
template <typename Iterator>
std::pair<Iterator, bool> operator()(
Iterator begin, Iterator end) const
{
Iterator i = begin;
const size_t buffer_len = std::distance(begin,end); <-- this is not work usualy distance here is 1
if(buffer_len >= len_)
{
std::advance(i, len_);
return std::make_pair(i, true);
}
while (i != end)
if (c_ == *i++) {
return std::make_pair(i, true);
}
return std::make_pair(i, false);
}
private:
char c_;
int len_;
};
namespace boost {
namespace asio {
template <> struct is_match_condition<match_char>
: public boost::true_type {};
}
}
boost::asio::streambuf b;
void main() {
boost::asio::async_read_until(port, b,
match_char(';', 128) , &handler);
}
最佳答案
自定义 MatchCondition
条件不允许限制从流中读取的字节数不超过 n
字节数。 read_until()
系列函数都允许将超出分隔符的数据从 Stream
中读取到 streambuf
中。本质上,read_until()
将从 streambuf
中准备一个缓冲区,从 Stream
中读取一 block 数据到准备好的缓冲区中,然后使用MatchCondition
检查是否满足完成条件。
为了读取不超过 n
字节并在找到所需的分隔符时尽早完成,请考虑构造 boost::asio::streambuf
使用最大大小,然后使用 read_until()
操作。例如,以下内容将从流中读取不超过 128
字节,如果找到“;
”分隔符,则会提前完成:
boost::asio::streambuf buffer(128);
boost::system::error_code error;
std::size_t bytes_transferred = boost::asio::read_until(
stream, buffer, ';', error);
在上面的例子中
128
字节但未找到定界符,则 error
将为 boost::asio::error::not_found
buffer.size()
将为 128
。error
将成功,bytes_transferred
将是驻留在 缓冲区中的字节数,包括分隔符在内
的输入序列。请注意,buffer.size()
可能大于 bytes_transferred
,但不会超过 128
。128
字节且未找到定界符,则 error
将包含有关读取操作失败原因的适当错误条件,例如 boost::asio::error::eof
如果远程端关闭了连接。这是一个完整的例子demonstrating streambuf
的行为:
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
// This example is not interested in the handlers, so provide a noop function
// that will be passed to bind to meet the handler concept requirements.
void noop() {}
std::string make_string(boost::asio::streambuf& streambuf, std::size_t n)
{
return {buffers_begin(streambuf.data()),
buffers_begin(streambuf.data()) + n};
}
int main()
{
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
// Create all I/O objects.
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 0));
tcp::socket server_socket(io_service);
tcp::socket client_socket(io_service);
// Connect client and server sockets.
acceptor.async_accept(server_socket, boost::bind(&noop));
client_socket.async_connect(acceptor.local_endpoint(), boost::bind(&noop));
io_service.run();
// Write data to server that contains a delimiter.
auto bytes_written = boost::asio::write(server_socket,
boost::asio::buffer(std::string("12345;67890")));
// Wait for all data to be received.
while (bytes_written != client_socket.available())
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// Create a streambuf too small to read up to the delimter.
{
const std::size_t MAX_SIZE = 2;
boost::asio::streambuf read_buffer(MAX_SIZE);
boost::system::error_code error;
auto bytes_read = boost::asio::read_until(
client_socket, read_buffer, ';', error);
// Expect an error as the delim was not found, so the read_until
// return value is 0. However, data was read into the buffer.
assert(boost::asio::error::not_found == error);
assert(bytes_read == 0);
assert(read_buffer.size() == 2); // "12" out of "12345;67890";
// Verify additional data was not read from the stream.
assert((bytes_written - MAX_SIZE) == client_socket.available());
}
// Default construct a streambuf, which has a large max.
{
boost::asio::streambuf read_buffer;
// Read from the socket.
boost::system::error_code error;
auto bytes_read = boost::asio::read_until(
client_socket, read_buffer, ';', error);
// Verify that at least "345;" was read.
assert(!error);
assert(bytes_read != 0);
assert(make_string(read_buffer, bytes_read) == "345;");
// Not a guarantee, but the implementation may (and likely will)
// read beyond the delimiter. If so, the streambuf's size will
// be larger than return value from read_until, as it returns the
// number of bytes up to and including the delimiter.
if (bytes_read < read_buffer.size())
{
std::cout << "Read beyond delimiter" << std::endl;
}
}
std::cout << "bytes remaining: " << client_socket.available() <<
std::endl;
}
输出:
Read beyond delimiter
bytes remaining: 0
关于c++ - boost async_read_until 将 match_condition 与限制大小结合起来读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32273929/
我正在尝试使用 boost asio 库来实现网络编程。这是当一些数据(以“##”结尾)到达端点时调用的代码。 { boost::asio::async_read_until(m_socket
我在此处记录的函数中实现第三个参数时遇到问题: http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/async_rea
我在使用 async_read_until 时遇到问题。我最初使用的是 async_receive,但我的服务器的响应增长到不止一个数据包,我遇到了一个问题,即 async_receive 在收到第一
我正在尝试使用 boost::shared_ptr与 async_read_until并且无法让它工作。这是我的简化代码: 标题 boost::asio::io_service io_service;
我正在使用带有 '\n' 分隔符的 asio::async_read_until 来支持从服务器获取字符数据的 TCP 客户端。此服务器不断发送 '\n' 终止行;准确地说,它可以同时写入单行或多行的
在 boost::asio::async_read_until 之后,我面临周期性的无限等待处理程序调用。这出现在两种情况下: 在服务器上,尽管客户端 boost::asio::async_write
我正在尝试将 c++11 lambda 与 boost::asio::async_read_until 一起使用,如下所示: void TCPSession::readData() { auto
我正在尝试使用自定义匹配函数作为 boost::asio 组合操作的一部分。当传入 std::move(*this) 作为异步操作的处理程序时,VS2017 无法使用自定义匹配条件推断参数。 组合操作
我想知道如何限制使用 async_read_until 读取的字节大小。从现在开始,我将它与定界符字符一起使用,但我想更改。这是我所做的: void Client::doRead()
我正在学习如何将 Boost:asio 库与串行端口一起使用。我使用同步写入和读取编写了一些代码,现在我想使用异步,但它不起作用。简单示例: void readHandler(const boost:
我正在尝试修改来自 boost asio 的 echo 服务器示例,当我尝试使用 boost::asio::async_read_until 时遇到了问题。这是代码: #include #i
我遇到了一个不寻常的问题。我有一个 C++ Boost.ASIO 网络服务器,为了处理传入的请求,我正在使用以下代码: boost::asio::async_read_until( socke
我有一个关于使用 boost asio 的 async_read_until 的一般性问题。文档说调用处理程序时缓冲区内可能有更多数据。有没有什么办法可以解决这个问题,并在序列条件匹配后立即阻止缓冲区
我正在尝试在本地网络上的两台计算机之间建立连接,一台使用 Boost Asio C++ TCP 异步服务器示例的略微修改版本,另一台使用 NodeJS。 tcp_client.js : var net
我正在从串行设备读取,其中必须特别请求每条消息。例如。您发送请求并获得带有序列化负载的响应。 每条消息按顺序包含这些部分: 序言(2 字节,“$M”) HEADER(3字节,包含有效载荷长度N) 有效
有没有办法结合两个条件读取,直到找到匹配字符或收到 128 个字节? 可以限制 streambuf 的大小: inBuf = std::make_shared(limit_size); 但是当处理程序
我正在使用以下方法: boost::asio::streambuf io_bufstream; boost::asio::async_read_until(socket_, io_bufstream,
我正在用 boost asio 制作一个 Irc 机器人,我在编译它时遇到了一些问题,而且错误不是真正可读的。 似乎其中一个async_read_until进行得不是很好,但我不知道为什么。 这是 g
我使用了来自 boost 的异步 tcp 服务器示例,它与我的应用程序正在做的很接近。下面的代码示例是一个完整的示例。 首先,我开始异步读取操作,直到分隔符 char。在这种情况下,它是 http h
我正在修改 Boost Asio 回显示例以使用 async_read_until 逐字读取输入。即使我使用 async_read_until,所有发送的数据似乎都是从套接字读取的。有人可以建议: #
我是一名优秀的程序员,十分优秀!