我的缓冲区都会在所有连接中使用
Will my buffer used by all connections?
我有一个创建io_service
的主,并将其传递到TcpServer
的实例。
TcpServer
具有成员std::array<char, 8192> m_buffer
。它有4种方法:构造函数,startaccept, handleaccept 和handleread。构造函数仅初始化一些成员并调用startaccept。
StartAccept创建了TcpConnection
的共享指针,该指针扩展了std::enable_shared_from_this<TcpConnection
。在此之后,请访问m_acceptor.async_accept并将接受绑定到之前提到的处理方法。
这是我的 handleaccept 方法。它使用boost :: asio :: buffer调用async_read_some,该:: buffer使用tcpserver中声明的成员变量。
void TcpServer::handleAccept(std::shared_ptr<TcpConnection> newConnection, const boost::system::error_code &error)
{
if (!error) {
//newConnection->start();
std::cout << "Accepting new connection" << std::endl;
newConnection->getSocket().async_read_some(
boost::asio::buffer(m_buffer),
boost::bind(&TcpServer::handleRead, this, newConnection, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
);
}
startAccept();
}
我不确定,但是如果有多个连接,它们都会使用相同的缓冲对象,对吗?他们可能会覆盖它,不是吗?
是的,所有连接都将使用TcpServer
中定义的相同缓冲区。实际上,您应该将缓冲区存储在连接中,而不是在服务器中。
boost::asio::buffer
将使用该超载。因此,读取的数据将存储到您的m_buffer
中。您应该将buffer
存储在连接中,或使用一些同步(即,某些布尔标志,例如is_in_read
,但这是不好的主意(。
相关文章:
- C++字符*缓冲区的大小
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- ostream过载时的缓冲区冲洗
- 当套接字连接断开时检测C/C++Unix
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- Xaudio2在更改缓冲区或循环时弹出声音
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 我正在编写一个简单的客户端套接字应用程序,但在连接后服务器收到一个空缓冲区
- 如何通过解析缓冲区并将数据放入正确的结构来处理传入的数据包连接?
- 我的缓冲区都会在所有连接中使用
- 如何通过1024字节缓冲区和强力冲洗在插座连接上发送6个字符的C弦
- 套接字连接上的缓冲区溢出
- 使用协议缓冲区的 SciDB C++ (Windows) 连接
- 函数连接两个写入缓冲区开头的字符串
- 使用协议缓冲区连接c#和c++
- 连接多个缓冲区的有效方法
- 在C中连接两个字符串并动态分配缓冲区
- 字符串连接过程中可能出现缓冲区溢出
- 来自UDP连接的第一个缓冲区包含垃圾-随后的连接是好的