我正在用Boost.Asio用C++实现一个拍卖系统。只有一个集中的拍卖人(服务器)和一些连接的投标人(客户端)。我正在以异步方式实现这一点,并且我已经实现了投标人和拍卖人之间的基本通信(注册、ping、获取客户列表)。拍卖人的框架代码如下所示:
class talkToBidder : public boost::enable_shared_from_this<talkToBidder>
{
// Code for sending and receiving messages, which works fine
};
void on_round_end()
{
// Choose the best bid and message the winner
if (!itemList.empty())
timer_reset();
}
void timer_reset()
{
// Send the item information to the bidders
// When the round ends, call on_round_end()
auction_timer.expires_from_now(boost::posix_time::millisec(ROUND_TIME));
auction_timer.async_wait(boost::bind(on_round_end));
}
void handle_accept(...)
{
// Create new bidder...
acceptor.async_accept(bidder->sock(),boost::bind(handle_accept,bidder,_1));
}
int main()
{
// Create new bidder and handle accepting it
talkToBidder::ptr bidder = talkToBidder::new_();
acceptor.async_accept(bidder->sock(),boost::bind(handle_accept,bidder,_1));
service.run();
}我的问题是,在开始拍卖之前,我需要等待至少一个投标人连接,所以我不能在使用service.run()之前简单地调用timer_reset()。做这件事的Boost.Asio方法是什么?
发布于 2020-10-19 22:04:26
在异步协议设计中,绘制消息序列图是很有帮助的。Do include your timers。
代码现在变得微不足道了。当消息到达时,启动计时器,该消息将启动计时器。是的,这是将问题向前移动了一点。这里真正的问题是,这不是一个Boost Asio编码问题。在您的示例中,该特定消息看起来是第一个投标人的登录,它被实现为映射到代码中的handle_accept的TCP connect (SYN/ACK)。
https://stackoverflow.com/questions/64425283
复制相似问题