-
Notifications
You must be signed in to change notification settings - Fork 1
/
非阻塞echo_polloop.cpp
78 lines (59 loc) · 1.97 KB
/
非阻塞echo_polloop.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/******************************************************************************
*
* t_echo_server_0 - echo服务器例程
*
* 单线程
*
* 对应运行t_echo_client_0
*
*****************************************************************************/
#include <XiaoTuNetBox/TcpServer.h>
#include <XiaoTuNetBox/InBufObserver.h>
#include <memory>
#include <vector>
#include <functional>
using namespace std::placeholders;
using namespace xiaotu::net;
class Session {
public:
Session(ConnectionPtr const & conn)
: mConn(conn)
{
mObserver = conn->GetInputBuffer().CreateObserver();
mObserver->SetRecvCallBk(std::bind(&Session::Echo, this));
}
void Echo()
{
size_t size = mObserver->Size();
std::cout << "observer size:" << size << std::endl;
std::cout << "buffer size:" << mConn->GetInputBuffer().Size() << std::endl;
std::vector<uint8_t> msg(size);
mObserver->PopFront(msg.data(), size);
for (unsigned int i = 0; i < msg.size(); i++)
std::cout << msg[i];
std::cout << std::endl;
mConn->SendBytes(msg.data(), msg.size());
}
private:
ConnectionPtr mConn;
InBufObserverPtr mObserver;
};
typedef std::shared_ptr<Session> SessionPtr;
std::vector<SessionPtr> sessions;
void OnNewConnection(ConnectionPtr const & conn) {
std::cout << "新建连接:" << conn->GetInfo() << std::endl;
conn->GetHandler()->SetNonBlock(true);
sessions.push_back(SessionPtr(new Session(conn)));
}
void OnCloseConnection(ConnectionPtr const & conn) {
std::cout << "关闭连接:" << conn->GetInfo() << std::endl;
}
int main() {
PollLoopPtr loop = CreatePollLoop();
TcpServer tcp(loop, 65530, 3);
tcp.SetTimeOut(10, 0, 5);
tcp.SetNewConnCallBk(std::bind(OnNewConnection, _1));
tcp.SetCloseConnCallBk(std::bind(OnCloseConnection, _1));
loop->Loop(10);
return 0;
}