MCPcopy Create free account
hub / github.com/chenshuo/muduo / onMessage

Method onMessage

examples/asio/chat/codec.h:21–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19 }
20
21 void onMessage(const muduo::net::TcpConnectionPtr& conn,
22 muduo::net::Buffer* buf,
23 muduo::Timestamp receiveTime)
24 {
25 while (buf->readableBytes() >= kHeaderLen) // kHeaderLen == 4
26 {
27 // FIXME: use Buffer::peekInt32()
28 const void* data = buf->peek();
29 int32_t be32 = *static_cast<const int32_t*>(data); // SIGBUS
30 const int32_t len = muduo::net::sockets::networkToHost32(be32);
31 if (len > 65536 || len < 0)
32 {
33 LOG_ERROR << "Invalid length " << len;
34 conn->shutdown(); // FIXME: disable reading
35 break;
36 }
37 else if (buf->readableBytes() >= len + kHeaderLen)
38 {
39 buf->retrieve(kHeaderLen);
40 muduo::string message(buf->peek(), len);
41 messageCallback_(conn, message, receiveTime);
42 buf->retrieve(len);
43 }
44 else
45 {
46 break;
47 }
48 }
49 }
50
51 // FIXME: TcpConnectionPtr
52 void send(muduo::net::TcpConnection* conn,

Callers

nothing calls this directly

Calls 5

networkToHost32Function · 0.85
readableBytesMethod · 0.80
peekMethod · 0.80
shutdownMethod · 0.80
retrieveMethod · 0.80

Tested by

no test coverage detected