-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.h
More file actions
68 lines (56 loc) · 1.92 KB
/
Copy pathTcpServer.h
File metadata and controls
68 lines (56 loc) · 1.92 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
#ifndef TcpServer_H
#define TcpServer_H
#include "Acceptor.h"
#include "EventLoop.h"
#include "EventLoopThreadPool.h"
#include "noncopyable.h"
#include "TcpConnection.h"
#include "CallBacks.h"
#include <unordered_map>
#include <functional>
#include <string>
#include <atomic>
class TcpServer : noncopyable
{
public:
using ThreadInitCallback = std::function<void(EventLoop *)>;
enum option
{
kNoReusePort,
kReusePort,
};
void setConnectCallback(const ConnectCallback &cb) { connectCallback_ = cb; }
void setCloseCallback(const CloseCallback &cb) { closeCallback_ = cb; }
void setMessageCallback(const MessageCallback &cb) { messageCallback_ = cb; }
void setWriteCompleteCallback(const WriteCompleteCallback &cb){ writeCompleteCallback_ = cb; }
TcpServer(EventLoop *loop, InetAddr & listenAddr, std::string nameArg,option opt = kNoReusePort);
~TcpServer();
void start();
//设置线程数量
void setThreadNum(int count);
private:
/* 回调 */
void newConnection(int sockfd,const InetAddr& peerAddr);
void removeConnection(const TcpConnectionPtr& conn);
void removeConnectionInLoop(const TcpConnectionPtr& conn);
using ConnectionMap = std::unordered_map<std::string, TcpConnection>;
ConnectCallback connectCallback_;
CloseCallback closeCallback_;
MessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
// loop线程初始化的回调函数
ThreadInitCallback threadInitCallback_;
std::atomic_int32_t started_;
int nextConnId_;
// 保存的所有连接
ConnectionMap conntions_;
// baseloop mainloop用户定义的
EventLoop *loop_;
const std::string ipPort_;
const std::string name_;
// 运行在mainloop 任务就是监听新连接任务事件
std::unique_ptr<Acceptor> acceptor_;
// one loop pee thread
std::shared_ptr<EventLoopThreadPool> threadPool_;
};
#endif