-
Notifications
You must be signed in to change notification settings - Fork 3
/
echo_server.cc
executable file
·64 lines (63 loc) · 1.57 KB
/
echo_server.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include "proto_time.h"
#include "dmlc_logging.h"
#include "tcp/tcp_server.h"
#include "tcp/tcp_client.h"
#include "cmdline.h"
using namespace tcp;
bool m_running=true;
void signal_exit_handler(int sig)
{
m_running=false;
}
using namespace std;
class PeerCouner:public tcp::ActiveClientCounter{
public:
PeerCouner(int32_t clients){
clients_=clients;
}
void Decrease() override{
clients_--;
}
bool IsAllDeactive(){
bool ret=false;
if(clients_<=0){
ret=true;
}
return ret;
}
private:
int clients_;
};
int main(int argc, char *argv[]){
signal(SIGTERM, signal_exit_handler);
signal(SIGINT, signal_exit_handler);
signal(SIGTSTP, signal_exit_handler);
cmdline::parser a;
a.add<int>("port", 'p', "port number", false, 80, cmdline::range(1, 65535));
a.add<string>("log", 'l', "log file", false, "server_log.txt");
a.add<int>("flows", 'f', "connection", false, 200, cmdline::range(1, 65535));
a.parse_check(argc, argv);
int port=a.get<int>("port");
std::string log=a.get<string>("log");
int connection=a.get<int>("flows");
PeerCouner counter(connection);
TcpServer server(log);
server.RegisterCounter(&counter);
server.CreateSocket(port);
while(m_running){
server.LoopOnce();
if(counter.IsAllDeactive()){
break;
}
}
server.Close();
LOG(INFO)<<"server stop";
base::TimeSleep(1000);
return 0;
}