forked from yedf2/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cc
59 lines (54 loc) · 1.61 KB
/
test.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
#include "proto_msg.h"
#include "msg.pb.h"
using namespace std;
using namespace handy;
void handleQuery(TcpConnPtr con, Query* query) {
info("query recved name %s id %d", query->name().c_str(), query->id());
delete query;
con->getBase()->exit();
}
void testencode() {
Query q;
q.set_name("hello");
q.set_id(123);
Buffer b;
ProtoMsgCodec dis;
dis.encode(&q, b);
Query* p = dynamic_cast<Query*> (dis.decode(b));
info("p name %s id %d", p->name().c_str(), p->id());
delete p;
}
int main() {
Logger::getLogger().setLogLevel(Logger::LDEBUG);
testencode();
EventBase base;
TcpServer echo(&base);
int r = echo.bind("", 99);
exitif(r, "bind failed %d %s", errno, strerror(errno));
ProtoMsgDispatcher dispatch;
echo.onConnRead(
[&](TcpConnPtr con) {
if (ProtoMsgCodec::msgComplete(con->getInput())) {
Message* msg = ProtoMsgCodec::decode(con->getInput());
if (msg) {
dispatch.handle(con, msg);
} else {
error("bad msg from connection data");
con->close();
}
}
}
);
dispatch.onMsg<Query>(handleQuery);
TcpConnPtr cmd = TcpConn::createConnection(&base, "localhost", 99);
cmd->onState([](const TcpConnPtr& con) {
if (con->getState() == TcpConn::Connected) {
Query query;
query.set_name("hello", 5);
query.set_id(123);
ProtoMsgCodec::encode(&query, con->getOutput());
con->sendOutput();
}
});
base.loop();
}