-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpServer.h
78 lines (55 loc) · 1.7 KB
/
TcpServer.h
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
74
75
76
77
78
#ifndef TCPSERVER_H_
#define TCPSERVER_H_
#include"base/noncopyable.h"
#include"TcpConnection.h"
#include<map>
#include<atomic>
class Acceptor;
class EventLoop;
class EventLoopThreadPool;
class TcpServer:noncopyable
{
public:
typedef std::function<void(EventLoop*)> ThreadInitCallback;
TcpServer(EventLoop* base,
const InetAddress& listenAddr,
const std::string& name,
bool option=false);
~TcpServer();
//start the server and listen
void start();
const std::string& name()const
{ return name_; }
const std::string& ipPort()const
{ }
EventLoop* getLoop()const
{ return loop_; }
void setThreadNum(int n);
//for user
void setThreadInitCallback(const ThreadInitCallback& cb)
{ initCallback_=cb; }
//for user
void setConnectionCallback(const ConnectionCallback& cb)
{ connectioncallback_=cb; }
//for user
void setMessageCallback(const MessageCallback& cb)
{ messageCallback_=cb; }
private:
typedef std::map<std::string,TcpConnectionPtr> ConnectionMap;
void newConnection(int sockfd,const InetAddress& peer);
void removeConnection(const TcpConnectionPtr& conn);
void removeConnectionInLoop(const TcpConnectionPtr& conn);
EventLoop* loop_;//loop for accept
const std::string ipPort_;
// InetAddress listenAddr_;
std::string name_;
std::unique_ptr<Acceptor> acceptor_;
std::shared_ptr<EventLoopThreadPool> threadPool_;
ThreadInitCallback initCallback_;
MessageCallback messageCallback_;
ConnectionCallback connectioncallback_;
ConnectionMap connections_;
std::atomic<bool> started_;
uint32_t nextConnId_;
};
#endif