Boost ASIO 拍卖

Auctions in Boost ASIO

我正在使用 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 方法是什么?

在异步协议设计中,有助于绘制消息序列图。 Do include your timers.

代码现在变得微不足道了。当应该启动计时器的消息到达时,您启动计时器。是的,这将问题向前移动了一点。这里的重点是它不是 Boost Asio 编码问题。在您的情况下,该特定消息似乎是第一位投标人的登录信息,作为 TCP 连接 (SYN/ACK) 实现,映射到您代码中的 handle_accept