boost::bind函数缓冲区的最大大小

max size of buffer of boost::bind function

本文关键字:缓冲区 bind 函数 boost      更新时间:2023-10-16

我有一个关于处理程序函数中缓冲区最大大小的问题,该函数在boost::bind方法中调用。我有一个tcp套接字客户端:

chat_client(boost::asio::io_service& io_service,
tcp::resolver::iterator endpoint_iterator)
: io_service_(io_service),
socket_(io_service),
t(io_service)
{
boost::asio::async_connect(socket_, endpoint_iterator,
boost::bind(&chat_client::handle_connect, this,
boost::asio::placeholders::error));
}

在chat_client类中,我创建了一个将请求写入套接字缓冲区的方法

void set_timer(boost::posix_time::ptime time, boost::function<void(const 
boost::system::error_code&)> handler)
{
t.expires_at(time);
t.async_wait(handler);
}
void write(const chat_message& msg)
{
set_timer(boost::posix_time::microsec_clock::universal_time() + 
boost::posix_time::seconds(1),
boost::bind(&chat_client::do_write, this, msg, 
boost::asio::placeholders::error));
io_service_.run();
}
void do_write(chat_message msg, const boost::system::error_code& error)
{
std::cout << "dcm" << std::endl;
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&chat_client::handle_write, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
void handle_write(const boost::system::error_code& error, size_t 
bytes_transferred)
{
if (!error)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&chat_client::handle_write, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
else
{
do_close();
}
}

我的请求内容打包在chat_message类的一个实例中。这里是聊天消息类:

class chat_message
{
public:
enum { header_length = 7 };
enum { max_body_length = 0x1FFFFF };
chat_message()
: body_length_(0)
{
}
const char* data() const
{
return data_;
}
char* data()
{
return data_;
}
size_t length() const
{
return header_length + body_length_;
}
const char* body() const
{
return data_ + header_length;
}
char* body()
{
return data_ + header_length;
}
size_t body_length() const
{
return body_length_;
}
void body_length(size_t new_length)
{
body_length_ = new_length;
if (body_length_ > max_body_length)
body_length_ = max_body_length;
}
bool decode_header()
{
using namespace std; // For strncat and atoi.
char header[header_length + 1] = "";
strncat(header, data_, header_length);
body_length_ = atoi(header);
if (body_length_ > max_body_length)
{
body_length_ = 0;
return false;
}
return true;
}
void encode_header()
{
using namespace std; // For sprintf and memcpy.
char header[header_length + 1] = "";
sprintf(header, "%4d", static_cast<int>(body_length_));
memcpy(data_, header, header_length);
}
private:
char data_[header_length + max_body_length];
size_t body_length_;
};

我的问题是,当我设置max_body_length>0xFFFFF时,write((函数中的boost::bind((方法会导致分段错误。所以我怀疑boost::bind((方法的缓冲区大小最大值。有人能为我解释一下吗?谢谢

问题不在boost::bind()中,而是在以下方面:

char data_[header_length + max_body_length];

这一行可以编译,但当元素被访问时可能会崩溃。我强烈建议不要在这里使用如此庞大的字符数组。作为保持它的方法之一,您可以考虑动态分配。或者从数组中移出。STL和BOOST库提供了许多安全处理字符串和集合的工具。