-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathechoserver.cpp
More file actions
55 lines (46 loc) · 1.56 KB
/
Copy pathechoserver.cpp
File metadata and controls
55 lines (46 loc) · 1.56 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
#include "echoserver.h"
#include <iostream>
#include <functional>
EchoServer::EchoServer(EventLoop* loop, const int port, const int threadnum)
: tcpserver_(loop, port, threadnum)
{
tcpserver_.SetNewConnCallback(std::bind(&EchoServer::HandleNewConnection, this, std::placeholders::_1));
tcpserver_.SetMessageCallback(std::bind(&EchoServer::HandleMessage, this, std::placeholders::_1, std::placeholders::_2));
tcpserver_.SetSendCompleteCallback(std::bind(&EchoServer::HandleSendComplete, this, std::placeholders::_1));
tcpserver_.SetCloseCallback(std::bind(&EchoServer::HandleClose, this, std::placeholders::_1));
tcpserver_.SetErrorCallback(std::bind(&EchoServer::HandleError, this, std::placeholders::_1));
}
EchoServer::~EchoServer()
{
}
void EchoServer::Start()
{
tcpserver_.Start();
}
void EchoServer::HandleNewConnection(const spTcpConnection& sptcpconn)
{
std::cout << "New Connection Come in" << std::endl;
}
void EchoServer::HandleMessage(const spTcpConnection& sptcpconn, std::string &s)
{
//std::string msg("reply msg:");
//msg += s;
//s.clear();
//swap优化
std::string msg;
msg.swap(s);
msg.insert(0, "reply msg:");
sptcpconn->Send(msg);
}
void EchoServer::HandleSendComplete(const spTcpConnection& sptcpconn)
{
//std::cout << "Message send complete" << std::endl;
}
void EchoServer::HandleClose(const spTcpConnection& sptcpconn)
{
std::cout << "EchoServer conn close" << std::endl;
}
void EchoServer::HandleError(const spTcpConnection& sptcpconn)
{
std::cout << "EchoServer error" << std::endl;
}