forked from fastio/1store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_parser.hh
69 lines (67 loc) · 2.18 KB
/
protocol_parser.hh
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
/*
* Pedis is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* Copyright (c) 2016-2026, Peng Jian, pengjian.uestc@gmail.com. All rights reserved.
*
*/
#pragma once
#include "utils/bytes.hh"
#include "core/temporary_buffer.hh"
#include "util/eclipse.hh"
#include <algorithm>
#include <memory>
#include <cassert>
#include <cstring>
#include <experimental/optional>
#include "core/future.hh"
#include "exceptions/exceptions.hh"
#include "request_wrapper.hh"
namespace redis {
class protocol_parser final {
public:
using unconsumed_remainder = std::experimental::optional<temporary_buffer<char>>;
class impl {
public:
impl() {}
virtual ~impl() {}
virtual void init() = 0;
virtual char* parse(char* p, char* limit, char* eof) = 0;
virtual request_wrapper& request() = 0;
};
public:
explicit protocol_parser(std::unique_ptr<impl> p) : _impl(std::move(p)) {}
void init() { _impl->init(); }
inline future<unconsumed_remainder> operator()(temporary_buffer<char> buf) {
char* p = buf.get_write();
char* pe = p + buf.size();
char* eof = buf.empty() ? pe : nullptr;
char* parsed = _impl->parse(p, pe, eof);
if (parsed) {
buf.trim_front(parsed - p);
return make_ready_future<unconsumed_remainder>(std::move(buf));
}
return make_ready_future<unconsumed_remainder>();
}
inline request_wrapper& request() {
return _impl->request();
}
private:
std::unique_ptr<impl> _impl;
};
extern protocol_parser make_ragel_protocol_parser();
extern protocol_parser make_native_protocol_parser();
}