forked from joyieldInc/predixy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubscribe.cpp
59 lines (54 loc) · 1.48 KB
/
Subscribe.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
/*
* predixy - A high performance and full features proxy for redis.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
#include "Subscribe.h"
Subscribe::Subscribe():
mPendSub(0),
mSub(0)
{
}
SubscribeParser::Status SubscribeParser::parse(const Segment& body, int& chs)
{
SegmentStr<128> str(body);
Status st = Unknown;
chs = 0;
if (str.hasPrefix("*3\r\n$7\r\nmessage\r\n")) {
st = Message;
} else if (str.hasPrefix("*4\r\n$8\r\npmessage\r\n")) {
st = Pmessage;
} else if (str.hasPrefix("*3\r\n$9\r\nsubscribe\r\n")) {
st = Subscribe;
chs = -1;
} else if (str.hasPrefix("*3\r\n$10\r\npsubscribe\r\n")) {
st = Psubscribe;
chs = -1;
} else if (str.hasPrefix("*3\r\n$11\r\nunsubscribe\r\n")) {
st = Unsubscribe;
chs = -1;
} else if (str.hasPrefix("*3\r\n$12\r\npunsubscribe\r\n")) {
st = Punsubscribe;
chs = -1;
} else if (str.hasPrefix("-")) {
st = Error;
} else if (str.hasPrefix("$")) {
st = String;
}
if (chs < 0) {
if (!str.complete()) {
Segment tmp(body);
tmp.rewind();
tmp.cut(body.length() - 12);
str.set(tmp);
}
const char* p = str.data() + str.length();
for (int i = 0; i < str.length(); ++i) {
if (*--p == ':') {
chs = atoi(p + 1);
break;
}
}
}
return st;
}