2024-03-08 14:03:37 +08:00

56 lines
1.5 KiB
C++

#ifndef MUDUO_EXAMPLES_IDLECONNECTION_ECHO_H
#define MUDUO_EXAMPLES_IDLECONNECTION_ECHO_H
#include "muduo/net/TcpServer.h"
// #include <muduo/base/Types.h>
#include <boost/circular_buffer.hpp>
#include <unordered_set>
// RFC 862
class EchoServer {
public:
EchoServer(muduo::net::EventLoop* loop,
const muduo::net::InetAddress& listenAddr, int idleSeconds);
void start();
private:
void onConnection(const muduo::net::TcpConnectionPtr& conn);
void onMessage(const muduo::net::TcpConnectionPtr& conn,
muduo::net::Buffer* buf, muduo::Timestamp time);
void onTimer();
void dumpConnectionBuckets() const;
typedef std::weak_ptr<muduo::net::TcpConnection> WeakTcpConnectionPtr;
struct Entry : public muduo::copyable {
explicit Entry(const WeakTcpConnectionPtr& weakConn)
: weakConn_(weakConn)
{
}
~Entry()
{
muduo::net::TcpConnectionPtr conn = weakConn_.lock();
if (conn) {
conn->shutdown();
}
}
WeakTcpConnectionPtr weakConn_;
};
typedef std::shared_ptr<Entry> EntryPtr;
typedef std::weak_ptr<Entry> WeakEntryPtr;
typedef std::unordered_set<EntryPtr> Bucket;
typedef boost::circular_buffer<Bucket> WeakConnectionList;
muduo::net::TcpServer server_;
WeakConnectionList connectionBuckets_;
};
#endif // MUDUO_EXAMPLES_IDLECONNECTION_ECHO_H