-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.h
141 lines (125 loc) · 4.19 KB
/
tcp_server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef __SYLAR_TCP_SERVER_H__
#define __SYLAR_TCP_SERVER_H__
#include <memory>
#include <functional>
#include "iomanager.h"
#include "address.h"
#include "socket.h"
#include "noncopyable.h"
#include "config.h"
namespace sylar {
struct TcpServerConf{
typedef std::shared_ptr<TcpServerConf> ptr;
std::vector<std::string> address;
int keepalive = 0;
int timeout = 1000 * 2 * 60;
std::string id;
std::string name;
std::string type = "http";
std::string cert_file;
std::string key_file;
std::string accept_worker;
std::string io_worker;
std::string process_worker;
std::map<std::string, std::string> args;
bool isValid() const {
return !address.empty();
}
bool operator==(const TcpServerConf& oth) const {
return address == oth.address
&& keepalive == oth.keepalive
&& timeout == oth.timeout
&& name == oth.name
&& cert_file== oth.cert_file
&& key_file == oth.key_file
&& accept_worker == oth.accept_worker
&& io_worker == oth.io_worker
&& process_worker == oth.process_worker
&& args == args
&& id == oth.id
&& type == oth.type;
}
};
template<>
class LexicalCast<std::string, TcpServerConf> {
public:
TcpServerConf operator()(const std::string& v) {
YAML::Node node = YAML::Load(v);
TcpServerConf conf;
conf.id = node["id"].as<std::string>(conf.id);
conf.type = node["type"].as<std::string>(conf.type);
conf.keepalive = node["keepalive"].as<int>(conf.keepalive);
conf.timeout = node["timeout"].as<int>(conf.timeout);
conf.name = node["name"].as<std::string>(conf.name);
conf.cert_file = node["cert_file"].as<std::string>(conf.cert_file);
conf.key_file = node["key_file"].as<std::string>(conf.key_file);
conf.accept_worker = node["accept_worker"].as<std::string>();
conf.io_worker = node["io_worker"].as<std::string>();
conf.process_worker = node["process_worker"].as<std::string>();
conf.args = LexicalCast<std::string
,std::map<std::string, std::string> >()(node["args"].as<std::string>(""));
if(node["address"].IsDefined()) {
for(size_t i = 0; i < node["address"].size(); ++i) {
conf.address.push_back(node["address"][i].as<std::string>());
}
}
return conf;
}
};
template<>
class LexicalCast<TcpServerConf, std::string> {
public:
std::string operator()(const TcpServerConf& conf) {
YAML::Node node;
node["id"] = conf.id;
node["type"] = conf.type;
node["name"] = conf.name;
node["keepalive"] = conf.keepalive;
node["timeout"] = conf.timeout;
node["cert_file"] = conf.cert_file;
node["key_file"] = conf.key_file;
node["accept_worker"] = conf.accept_worker;
node["io_worker"] = conf.io_worker;
node["process_worker"] = conf.process_worker;
node["args"] = YAML::Load(LexicalCast<std::map<std::string, std::string>
, std::string>()(conf.args));
for(auto& i : conf.address) {
node["address"].push_back(i);
}
std::stringstream ss;
ss << node;
return ss.str();
}
};
class TcpServer : public std::enable_shared_from_this<TcpServer>
, Noncopyable {
public:
typedef std::shared_ptr<TcpServer> ptr;
TcpServer(sylar::IOManager* worker = sylar::IOManager::GetThis()
,sylar::IOManager* io_worker = sylar::IOManager::GetThis()
,sylar::IOManager* accept_worker = sylar::IOManager::GetThis());
virtual ~TcpServer();
virtual bool bind(sylar::Address::ptr addr);
virtual bool bind(const std::vector<Address::ptr>& addrs
,std::vector<Address::ptr>& fails);
virtual bool start();
virtual void stop();
uint64_t getRecvTimeout() const {return m_recvTimeout; }
std::string getName() const { return m_name; }
void setRecvTimeout(uint64_t v) { m_recvTimeout = v; }
virtual void setName(const std::string& v) { m_name = v; }
bool isStop() const { return m_isStop; }
protected:
virtual void handleClient(Socket::ptr client);
virtual void startAccept(Socket::ptr sock);
private:
std::vector<Socket::ptr> m_socks;
IOManager* m_worker;
IOManager* m_ioWorker;
IOManager* m_accept_worker;
uint64_t m_recvTimeout;
std::string m_name;
bool m_isStop;
};
}
#endif