-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.cc
More file actions
57 lines (54 loc) · 1.37 KB
/
context.cc
File metadata and controls
57 lines (54 loc) · 1.37 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
56
57
#include <ejson/base64.h>
#define ENABLE_ELG_LOG
#include <elog/logger.h>
#include <netpoll/core.h>
#include "context.h"
using namespace chat;
void Context::processCodec(const netpoll::TcpConnectionPtr &conn,
const netpoll::MessageBuffer *buffer)
{
for (;;)
{
switch (state_)
{
case kGetLength: {
if (buffer->readableBytes() < 8) { return; }
length_ = buffer->readInt64();
state_ = kGetBody;
break;
}
case kGetBody: {
if (buffer->readableBytes() < expectedLength())
{
text_.append(buffer->readAll());
return;
}
else
{
auto t = buffer->read(expectedLength());
ELG_TRACE("recv:{}", t);
text_.append(t);
state_ = kParseOk;
}
break;
}
case kParseOk: {
return;
}
}
}
}
void Context::Send(const netpoll::TcpConnectionPtr &conn, CommonData const &msg)
try
{
auto buffer = netpoll::MessageBuffer();
auto jsonMsg = ejson::Parser::ToJSON(msg);
ELG_TRACE("json msg:{}", jsonMsg);
buffer.pushFrontInt64(jsonMsg.size());
buffer.pushBack(jsonMsg);
conn->send(buffer);
}
catch (std::exception const &e)
{
ELG_WARN("exception:{}", e.what());
}