Seastar
High performance C++ framework for concurrent servers
scattered_message.hh
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 <seastar/util/std-compat.hh>
29 #include <seastar/util/modules.hh>
30 #ifndef SEASTAR_MODULE
31 #include <memory>
32 #include <vector>
33 #endif
34 
35 namespace seastar {
36 
37 SEASTAR_MODULE_EXPORT
38 template <typename CharType>
40 private:
41  using fragment = net::fragment;
42  using packet = net::packet;
43  using char_type = CharType;
44  packet _p;
45 public:
48  scattered_message(const scattered_message&) = delete;
49 
50  void append_static(const char_type* buf, size_t size) {
51  if (size) {
52  _p = packet(std::move(_p), fragment{(char_type*)buf, size}, deleter());
53  }
54  }
55 
56  template <size_t N>
57  void append_static(const char_type(&s)[N]) {
58  append_static(s, N - 1);
59  }
60 
61  void append_static(const char_type* s) {
62  append_static(s, strlen(s));
63  }
64 
65  template <typename size_type, size_type max_size>
66  void append_static(const basic_sstring<char_type, size_type, max_size>& s) {
67  append_static(s.begin(), s.size());
68  }
69 
70  void append_static(const std::string_view& s) {
71  append_static(s.data(), s.size());
72  }
73 
74  void append(std::string_view v) {
75  if (v.size()) {
76  _p = packet(std::move(_p), temporary_buffer<char>::copy_of(v));
77  }
78  }
79 
80  void append(temporary_buffer<CharType> buff) {
81  if (buff.size()) {
82  _p = packet(std::move(_p), std::move(buff));
83  }
84  }
85 
86  template <typename size_type, size_type max_size>
88  if (s.size()) {
89  _p = packet(std::move(_p), std::move(s).release());
90  }
91  }
92 
93  template <typename size_type, size_type max_size, typename Callback>
94  void append(const basic_sstring<char_type, size_type, max_size>& s, Callback callback) {
95  if (s.size()) {
96  _p = packet(std::move(_p), fragment{s.begin(), s.size()}, make_deleter(std::move(callback)));
97  }
98  }
99 
100  void reserve(int n_frags) {
101  _p.reserve(n_frags);
102  }
103 
104  packet release() && {
105  return std::move(_p);
106  }
107 
108  template <typename Callback>
109  void on_delete(Callback callback) {
110  _p = packet(std::move(_p), make_deleter(std::move(callback)));
111  }
112 
113  operator bool() const {
114  return _p.len();
115  }
116 
117  size_t size() {
118  return _p.len();
119  }
120 };
121 
122 }
Definition: sstring.hh:75
Definition: deleter.hh:51
Definition: packet.hh:87
Definition: scattered_message.hh:39
Definition: temporary_buffer.hh:67
size_t size() const noexcept
Gets the buffer size.
Definition: temporary_buffer.hh:130
Definition: packet.hh:46
Seastar API namespace.
Definition: abort_on_ebadf.hh:26