-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpserver.h
More file actions
73 lines (56 loc) · 2.21 KB
/
Copy pathtcpserver.h
File metadata and controls
73 lines (56 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include "noncopyable.h"
#include "eventloopthreadpool.h"
#include "inetaddress.h"
#include "callbacks.h"
#include "eventloop.h"
#include "acceptor.h"
#include "tcpconnection.h"
#include "buffer.h"
#include <unordered_map>
#include <functional>
#include <string>
#include <memory>
#include <atomic>
/**
对外服务器编程使用的类
*/
class TcpServer : noncopyable
{
public:
using ThreadInitCallback = std::function<void(EventLoop*)>;
enum class Option
{
kNoReusePort,
kReusePort
};
TcpServer(EventLoop* loop, const InetAddress& listenAddr,
const std::string& name, Option option = Option::kNoReusePort);
~TcpServer();
// 设置底层subloop的个数
void setThreadNum(int numThreads);
void setThreadInitCallback(const ThreadInitCallback& cb) { threadInitCallback_ = cb; }
void setConnectionCallback(const ConnectionCallback& cb) { connectionCallback_ = cb; }
void setMessageCallback(const MessageCallback& cb) { messageCallback_ = cb; }
void setWriteCompleteCallback(const WriteCompleteCallback& cb) { writeCompleteCallback_ = cb; }
// 开启服务器监听
void start();
private:
// 有一个新客户端连接,acceptor会执行这个回调
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 用户定义的loop
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_; // loop线程初始化的回调
std::atomic_int started_;
int nextConnId_;
ConnectionMap connections_; // 保存所有的连接 <连接名name, 连接对象tcpconnection>
};