time_wheel

4502 ワード

time_wheel           ,                                ok ,muduo                      ,
               tick   ,                                            ,                       ,     tick      ,                     (             )
EchoServer::EchoServer(EventLoop* loop,
                       const InetAddress& listenAddr,
                       int idleSeconds)
  : server_(loop, listenAddr, "EchoServer"),
    connectionBuckets_(idleSeconds)
{
  connectionBuckets_.resize(idleSeconds);//        
}

typedef boost::shared_ptr EntryPtr; typedef boost::weak_ptr WeakEntryPtr; typedef boost::unordered_set Bucket; typedef boost::circular_buffer WeakConnectionList;
muduo::net::TcpServer server_; WeakConnectionList connectionBuckets_;

muduo , boost circular_buffer, , http://www.cnblogs.com/TianFang/archive/2013/02/05/2892503.html

muduo 0 , , tick circular_buffer, 1, , ,

```
void EchoServer::onConnection(const TcpConnectionPtr& conn)
{
  LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> "
           << conn->localAddress().toIpPort() << " is "
           << (conn->connected() ? "UP" : "DOWN");

  if (conn->connected())
  {
    EntryPtr entry(new Entry(conn));
    connectionBuckets_.back().insert(entry);
    dumpConnectionBuckets();
    WeakEntryPtr weakEntry(entry);
    conn->setContext(weakEntry);
  }
  else
  {
    assert(!conn->getContext().empty());
    WeakEntryPtr weakEntry(boost::any_cast<WeakEntryPtr>(conn->getContext()));
    LOG_DEBUG << "Entry use_count = " << weakEntry.use_count();
  }
}
void EchoServer::onMessage(const TcpConnectionPtr& conn,
                           Buffer* buf,
                           Timestamp time)
{
  string msg(buf->retrieveAllAsString());
  LOG_INFO << conn->name() << " echo " << msg.size()
           << " bytes at " << time.toString();
  conn->send(msg);

  assert(!conn->getContext().empty());
  WeakEntryPtr weakEntry(boost::any_cast<WeakEntryPtr>(conn->getContext()));
  EntryPtr entry(weakEntry.lock());
  if (entry)
  {
    connectionBuckets_.back().insert(entry);

  }
}

( muduo )