-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoServer.cpp
56 lines (45 loc) · 978 Bytes
/
EchoServer.cpp
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
#include "EchoServer.h"
#include "TcpConnection.h"
#include "Buffer.h"
#include "EventLoop.h"
#include <iostream>
EchoServer::EchoServer(EventLoop *_pLoop)
:pLoop(_pLoop),
pServer(_pLoop),
timer(0),
times(0)
{
pServer.setCallBack(this);
}
EchoServer::~EchoServer()
{}
void EchoServer::onConnection(TcpConnection * pTcpConn)
{
std::cout << "new connection" << std::endl;
}
void EchoServer::onMessage(TcpConnection * pTcpConn, Buffer &data)
{
if (pTcpConn)
{
std::string sdata(data.retriveAsAllString());
pTcpConn->sendData(sdata);
}
timer = pLoop->runEvery(1, this);
}
void EchoServer::onCompleteWrite()
{
std::cout << "write completed" << std::endl;
}
void EchoServer::start()
{
pServer.start();
}
void EchoServer::run(void * param)
{
std::cout << times <<std::endl;
if (times++ == 3)
{
pLoop->cancelTimer(timer);
times = 0;
}
}