]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
2 | * This file is open source software, licensed to you under the terms | |
3 | * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | |
4 | * distributed with this work for additional information regarding copyright | |
5 | * ownership. You may not use this file except in compliance with the License. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, | |
12 | * software distributed under the License is distributed on an | |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | * KIND, either express or implied. See the License for the | |
15 | * specific language governing permissions and limitations | |
16 | * under the License. | |
17 | */ | |
18 | /* | |
19 | * Copyright (C) 2014 Cloudius Systems, Ltd. | |
20 | */ | |
21 | ||
22 | #pragma once | |
23 | ||
24 | #include <seastar/core/deleter.hh> | |
25 | #include <seastar/core/temporary_buffer.hh> | |
26 | #include <seastar/net/packet.hh> | |
27 | #include <seastar/core/sstring.hh> | |
28 | #include <memory> | |
29 | #include <vector> | |
30 | #include <seastar/util/std-compat.hh> | |
31 | ||
32 | namespace seastar { | |
33 | ||
34 | template <typename CharType> | |
35 | class scattered_message { | |
36 | private: | |
37 | using fragment = net::fragment; | |
38 | using packet = net::packet; | |
39 | using char_type = CharType; | |
40 | packet _p; | |
41 | public: | |
42 | scattered_message() {} | |
43 | scattered_message(scattered_message&&) = default; | |
44 | scattered_message(const scattered_message&) = delete; | |
45 | ||
46 | void append_static(const char_type* buf, size_t size) { | |
47 | if (size) { | |
48 | _p = packet(std::move(_p), fragment{(char_type*)buf, size}, deleter()); | |
49 | } | |
50 | } | |
51 | ||
52 | template <size_t N> | |
53 | void append_static(const char_type(&s)[N]) { | |
54 | append_static(s, N - 1); | |
55 | } | |
56 | ||
57 | void append_static(const char_type* s) { | |
58 | append_static(s, strlen(s)); | |
59 | } | |
60 | ||
61 | template <typename size_type, size_type max_size> | |
62 | void append_static(const basic_sstring<char_type, size_type, max_size>& s) { | |
63 | append_static(s.begin(), s.size()); | |
64 | } | |
65 | ||
f67539c2 | 66 | void append_static(const std::string_view& s) { |
11fdf7f2 TL |
67 | append_static(s.data(), s.size()); |
68 | } | |
69 | ||
f67539c2 TL |
70 | void append(std::string_view v) { |
71 | if (v.size()) { | |
72 | _p = packet(std::move(_p), temporary_buffer<char>::copy_of(v)); | |
73 | } | |
74 | } | |
75 | ||
1e59de90 TL |
76 | void append(temporary_buffer<CharType> buff) { |
77 | if (buff.size()) { | |
78 | _p = packet(std::move(_p), std::move(buff)); | |
79 | } | |
80 | } | |
81 | ||
11fdf7f2 TL |
82 | template <typename size_type, size_type max_size> |
83 | void append(basic_sstring<char_type, size_type, max_size> s) { | |
84 | if (s.size()) { | |
85 | _p = packet(std::move(_p), std::move(s).release()); | |
86 | } | |
87 | } | |
88 | ||
89 | template <typename size_type, size_type max_size, typename Callback> | |
90 | void append(const basic_sstring<char_type, size_type, max_size>& s, Callback callback) { | |
91 | if (s.size()) { | |
92 | _p = packet(std::move(_p), fragment{s.begin(), s.size()}, make_deleter(std::move(callback))); | |
93 | } | |
94 | } | |
95 | ||
96 | void reserve(int n_frags) { | |
97 | _p.reserve(n_frags); | |
98 | } | |
99 | ||
100 | packet release() && { | |
101 | return std::move(_p); | |
102 | } | |
103 | ||
104 | template <typename Callback> | |
105 | void on_delete(Callback callback) { | |
106 | _p = packet(std::move(_p), make_deleter(std::move(callback))); | |
107 | } | |
108 | ||
109 | operator bool() const { | |
110 | return _p.len(); | |
111 | } | |
112 | ||
113 | size_t size() { | |
114 | return _p.len(); | |
115 | } | |
116 | }; | |
117 | ||
118 | } |