TcpServer类
- TcpServer中封装了EventLoopThreadPool,因此TcpServer中的EventLoop对象为main Reactor,EventLoopThreadPool为sub Reactor。当新建连接到达后,TcpServer创建一个新的TcpConnection对象来保存这个连接,设置这个新连接的回调函数,之后在EventLoopThreadPool中取一个EventLoop对象来作为这个新连接的reactor。
- 在TcpServer中,建立连接描述符和TcpConnection之间的关系,通过stl中map来实现,map<int,TcpConnectionPtr>这样可以高效的查找,删除,插入操作。
- TcpServer本身有一个EventLoop,主要用于对接受连接事件的监听,EventLoopPool也为TcpServer的成员,使用轮转法为每一个新的TcpConnection选择EventLoop。
- TcpServer类用来统一管理Acceptor和TcpConnection,为TcpConnection和Acceptor这两个类注册回调函数。
数据成员:
EventLoop* loop_:loop_是acceptor_对象所属的EventLoop
const string hostport_:服务端口
const string name_:服务名
boost::scoped_ptr<Acceptor> acceptor_:创建、绑定、监听套接字的acceptor_对象
boost::scoped_ptr<EventLoopThreadPool> threadPool_:线程池对象
ConnectionCallback connectionCallback_:连接到来的回调函数connectionCallback_,ConnectionCallback定义在Callbacks.h中
MessageCallback messageCallback_:消息到来的回调函数messageCallback_,MessageCallback定义在Callbacks.h中
WriteCompleteCallback writeCompleteCallback_:数据发送完毕时的回调此函数writeCompleteCallback_
ThreadInitCallback threadInitCallback_:IO线程池中的线程在进入事件循环前,会回调用此函数
bool started_:连接是否已经启动了
int nextConnId_:下一个连接的ID
ConnectionMap connections_:连接列表
typedef
typedef std::map<string, TcpConnectionPtr> ConnectionMap
成员函数:
TcpServer(EventLoop* loop,const InetAddress& listenAddr,const string& nameArg):构造函数,需要传递一个回调函数
~TcpServer():析构函数
const string& hostport() const:返回服务端口
const string& name() const:返回服务名
void start():启动函数
void setConnectionCallback(const ConnectionCallback& cb):设置连接到来或者连接关闭时的回调函数
void setMessageCallback(const MessageCallback& cb):设置消息到来时的回调函数
void setWriteCompleteCallback(const WriteCompleteCallback& cb):设置数据发送完毕时的回调此函数
void newConnection(int sockfd, const InetAddress& peerAddr):连接到来的时候会回调的一个函数
void removeConnection(const TcpConnectionPtr& conn):连接关闭时调用的函数
void removeConnectionInLoop(const TcpConnectionPtr& conn):连接关闭时被removeConnection()调用
TcpServer.h
// This is a public header file, it must only include public header files.
#ifndef MUDUO_NET_TCPSERVER_H
#define MUDUO_NET_TCPSERVER_H
#include <muduo/base/Types.h>
#include <muduo/net/TcpConnection.h>
#include <map>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
namespace muduo
{
namespace net
{
class Acceptor;
class EventLoop;
class EventLoopThreadPool;
///
/// TCP server, supports single-threaded and thread-pool models.
///
/// This is an interface class, so don't expose too much details.
class TcpServer : boost::noncopyable
{
public:
typedef boost::function<void(EventLoop*)> ThreadInitCallback;
//TcpServer(EventLoop* loop, const InetAddress& listenAddr);
TcpServer(EventLoop* loop,
const InetAddress& listenAddr,
const string& nameArg);
~TcpServer(); // force out-line dtor, for scoped_ptr members.
const string& hostport() const {
return hostport_; }
const string& name() const {
return name_; }
/// Set the number of threads for handling input.
///
/// Always accepts new connection in loop's thread.
/// Must be called before @c start
/// @param numThreads
/// - 0 means all I/O in loop's thread, no thread will created.
/// this is the default value.
/// - 1 means all I/O in another thread.
/// - N means a thread pool with N threads, new connections
/// are assigned on a round-robin basis.
void setThreadNum(int numThr

本文介绍了TcpServer类的设计,它结合EventLoopThreadPool,用于高效管理新连接、回调函数及线程分配。核心内容包括EventLoop的使用、连接描述符与TcpConnection的关系、回调函数设置以及连接与线程池的协调工作。

1225

被折叠的 条评论
为什么被折叠?



