1 Star 0 Fork 0

pulin88 / mymuduo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TcpServer.h 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
vmware_centos_pulin 提交于 2022-02-12 01:40 . mymuduo init
#pragma once
#include "noncopyable.h"
#include "EventLoop.h"
#include "Acceptor.h"
#include "InetAddress.h"
#include "EventLoopThreadPool.h"
#include "Callbacks.h"
#include "TcpConnection.h"
#include "Buffer.h"
#include <functional>
#include <string>
#include <memory>
#include <atomic>
#include <unordered_map>
// 对外的服务器编程使用的类
class TcpServer : noncopyable
{
public:
using ThreadInitCallback = std::function<void(EventLoop*)>;
enum Option{kNoReusePort, kReusePort};
TcpServer(EventLoop *loop,
const InetAddress &listenAddr,
const std::string &nameArg,
Option option = kNoReusePort);
~TcpServer();
void setThreadInitCallback(const ThreadInitCallback &cb){threadInitCallback_ = cb;}
void setConnectionCallback(const ConnectionCallback &cb){connectionCallback_ = cb;}
void setMessageCallback(const MessageCallback &cb){messageCallback_ = cb;}
void setWriteComplete(const WriteCompleteCallback &cb){writeCompleteCallback_ = cb;}
void setThreadNum(int numThreads);
void start();
private:
void newConnection(int sockfd, const InetAddress &peerAddr);
void removeConnection(const TcpConnectionPtr &conn);
void removeConnectionInLoop(const TcpConnectionPtr &conn);
using ConnectionMap = std::unordered_map<std::string, TcpConnectionPtr>;
EventLoop *loop_; // baseLoop
const std::string ipPort_;
const std::string name_;
std::unique_ptr<Acceptor> acceptor_; // 运行在mainLoop,监听新连接事件
std::shared_ptr<EventLoopThreadPool> threadPool_; // one loop per thread
ConnectionCallback connectionCallback_;
MessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
ThreadInitCallback threadInitCallback_;
std::atomic_int started_;
int nextConnId_;
ConnectionMap connections_; // 运行在subLoop,保存所有TcpConnection连接
};
C++
1
https://gitee.com/pulin88/mymuduo.git
git@gitee.com:pulin88/mymuduo.git
pulin88
mymuduo
mymuduo
main

搜索帮助