代码拉取完成,页面将自动刷新
#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连接
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。