Buffer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/xia-chu/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef ZLTOOLKIT_BUFFER_H
  11. #define ZLTOOLKIT_BUFFER_H
  12. #include <assert.h>
  13. #include <memory>
  14. #include <string>
  15. #include <deque>
  16. #include <mutex>
  17. #include <vector>
  18. #include <atomic>
  19. #include <sstream>
  20. #include <type_traits>
  21. #include <functional>
  22. #include "Util/util.h"
  23. #include "Util/List.h"
  24. #include "Util/uv_errno.h"
  25. #include "Util/ResourcePool.h"
  26. #include "Network/sockutil.h"
  27. using namespace std;
  28. namespace toolkit {
  29. template <typename T> struct is_pointer : public std::false_type {};
  30. template <typename T> struct is_pointer<shared_ptr<T> > : public std::true_type {};
  31. template <typename T> struct is_pointer<shared_ptr<T const> > : public std::true_type {};
  32. template <typename T> struct is_pointer<T*> : public true_type {};
  33. template <typename T> struct is_pointer<const T*> : public true_type {};
  34. //缓存基类
  35. class Buffer : public noncopyable {
  36. public:
  37. typedef std::shared_ptr<Buffer> Ptr;
  38. Buffer(){};
  39. virtual ~Buffer(){};
  40. //返回数据长度
  41. virtual char *data() const = 0 ;
  42. virtual size_t size() const = 0;
  43. virtual string toString() const {
  44. return string(data(),size());
  45. }
  46. virtual size_t getCapacity() const{
  47. return size();
  48. }
  49. private:
  50. //对象个数统计
  51. ObjectStatistic<Buffer> _statistic;
  52. };
  53. template <typename C>
  54. class BufferOffset : public Buffer {
  55. public:
  56. typedef std::shared_ptr<BufferOffset> Ptr;
  57. BufferOffset(C data, size_t offset = 0, size_t len = 0) : _data(std::move(data)) {
  58. setup(offset, len);
  59. }
  60. ~BufferOffset() {}
  61. char *data() const override {
  62. return const_cast<char *>(getPointer<C>(_data)->data()) + _offset;
  63. }
  64. size_t size() const override{
  65. return _size;
  66. }
  67. string toString() const override {
  68. return string(data(),size());
  69. }
  70. private:
  71. void setup(size_t offset = 0, size_t size = 0) {
  72. auto max_size = getPointer<C>(_data)->size();
  73. assert(offset + size <= max_size);
  74. if (!size) {
  75. size = max_size - offset;
  76. }
  77. _size = size;
  78. _offset = offset;
  79. }
  80. template<typename T>
  81. static typename std::enable_if<toolkit::is_pointer<T>::value, const T &>::type
  82. getPointer(const T &data) {
  83. return data;
  84. }
  85. template<typename T>
  86. static typename std::enable_if<!toolkit::is_pointer<T>::value, const T *>::type
  87. getPointer(const T &data) {
  88. return &data;
  89. }
  90. private:
  91. C _data;
  92. size_t _offset;
  93. size_t _size;
  94. };
  95. typedef BufferOffset<string> BufferString;
  96. //指针式缓存对象,
  97. class BufferRaw : public Buffer{
  98. public:
  99. using Ptr = std::shared_ptr<BufferRaw>;
  100. static Ptr create();
  101. ~BufferRaw() override{
  102. if(_data){
  103. delete [] _data;
  104. }
  105. }
  106. //在写入数据时请确保内存是否越界
  107. char *data() const override {
  108. return _data;
  109. }
  110. //有效数据大小
  111. size_t size() const override{
  112. return _size;
  113. }
  114. //分配内存大小
  115. void setCapacity(size_t capacity){
  116. if(_data){
  117. do{
  118. if(capacity > _capacity){
  119. //请求的内存大于当前内存,那么重新分配
  120. break;
  121. }
  122. if(_capacity < 2 * 1024){
  123. //2K以下,不重复开辟内存,直接复用
  124. return;
  125. }
  126. if(2 * capacity > _capacity){
  127. //如果请求的内存大于当前内存的一半,那么也复用
  128. return;
  129. }
  130. }while(false);
  131. delete [] _data;
  132. }
  133. _data = new char[capacity];
  134. _capacity = capacity;
  135. }
  136. //设置有效数据大小
  137. void setSize(size_t size){
  138. if(size > _capacity){
  139. throw std::invalid_argument("Buffer::setSize out of range");
  140. }
  141. _size = size;
  142. }
  143. //赋值数据
  144. void assign(const char *data,size_t size = 0){
  145. if(size <=0 ){
  146. size = strlen(data);
  147. }
  148. setCapacity(size + 1);
  149. memcpy(_data,data,size);
  150. _data[size] = '\0';
  151. setSize(size);
  152. }
  153. size_t getCapacity() const override{
  154. return _capacity;
  155. }
  156. protected:
  157. friend class ResourcePool_l<BufferRaw>;
  158. BufferRaw(size_t capacity = 0) {
  159. if(capacity){
  160. setCapacity(capacity);
  161. }
  162. }
  163. BufferRaw(const char *data,size_t size = 0){
  164. assign(data,size);
  165. }
  166. private:
  167. size_t _size = 0;
  168. size_t _capacity = 0;
  169. char *_data = nullptr;
  170. //对象个数统计
  171. ObjectStatistic<BufferRaw> _statistic;
  172. };
  173. class BufferLikeString : public Buffer {
  174. public:
  175. ~BufferLikeString() override {}
  176. BufferLikeString() {
  177. _erase_head = 0;
  178. _erase_tail = 0;
  179. }
  180. BufferLikeString(string str) {
  181. _str = std::move(str);
  182. _erase_head = 0;
  183. _erase_tail = 0;
  184. }
  185. BufferLikeString& operator= (string str){
  186. _str = std::move(str);
  187. _erase_head = 0;
  188. _erase_tail = 0;
  189. return *this;
  190. }
  191. BufferLikeString(const char *str) {
  192. _str = str;
  193. _erase_head = 0;
  194. _erase_tail = 0;
  195. }
  196. BufferLikeString& operator= (const char *str){
  197. _str = str;
  198. _erase_head = 0;
  199. _erase_tail = 0;
  200. return *this;
  201. }
  202. BufferLikeString(BufferLikeString &&that) {
  203. _str = std::move(that._str);
  204. _erase_head = that._erase_head;
  205. _erase_tail = that._erase_tail;
  206. that._erase_head = 0;
  207. that._erase_tail = 0;
  208. }
  209. BufferLikeString& operator= (BufferLikeString &&that){
  210. _str = std::move(that._str);
  211. _erase_head = that._erase_head;
  212. _erase_tail = that._erase_tail;
  213. that._erase_head = 0;
  214. that._erase_tail = 0;
  215. return *this;
  216. }
  217. BufferLikeString(const BufferLikeString &that) {
  218. _str = that._str;
  219. _erase_head = that._erase_head;
  220. _erase_tail = that._erase_tail;
  221. }
  222. BufferLikeString& operator= (const BufferLikeString &that){
  223. _str = that._str;
  224. _erase_head = that._erase_head;
  225. _erase_tail = that._erase_tail;
  226. return *this;
  227. }
  228. char* data() const override{
  229. return (char *)_str.data() + _erase_head;
  230. }
  231. size_t size() const override{
  232. return _str.size() - _erase_tail - _erase_head;
  233. }
  234. BufferLikeString& erase(size_t pos = 0, size_t n = string::npos) {
  235. if (pos == 0) {
  236. //移除前面的数据
  237. if (n != string::npos) {
  238. //移除部分
  239. if (n > size()) {
  240. //移除太多数据了
  241. throw std::out_of_range("BufferLikeString::erase out_of_range in head");
  242. }
  243. //设置起始便宜量
  244. _erase_head += n;
  245. data()[size()] = '\0';
  246. return *this;
  247. }
  248. //移除全部数据
  249. _erase_head = 0;
  250. _erase_tail = _str.size();
  251. data()[0] = '\0';
  252. return *this;
  253. }
  254. if (n == string::npos || pos + n >= size()) {
  255. //移除末尾所有数据
  256. if (pos >= size()) {
  257. //移除太多数据
  258. throw std::out_of_range("BufferLikeString::erase out_of_range in tail");
  259. }
  260. _erase_tail += size() - pos;
  261. data()[size()] = '\0';
  262. return *this;
  263. }
  264. //移除中间的
  265. if (pos + n > size()) {
  266. //超过长度限制
  267. throw std::out_of_range("BufferLikeString::erase out_of_range in middle");
  268. }
  269. _str.erase(_erase_head + pos, n);
  270. return *this;
  271. }
  272. BufferLikeString& append(const BufferLikeString &str){
  273. return append(str.data(), str.size());
  274. }
  275. BufferLikeString& append(const string &str){
  276. return append(str.data(), str.size());
  277. }
  278. BufferLikeString& append(const char *data){
  279. return append(data, strlen(data));
  280. }
  281. BufferLikeString& append(const char *data, size_t len){
  282. if (len <= 0) {
  283. return *this;
  284. }
  285. if (_erase_head > _str.capacity() / 2) {
  286. moveData();
  287. }
  288. if (_erase_tail == 0) {
  289. _str.append(data, len);
  290. return *this;
  291. }
  292. _str.insert(_erase_head + size(), data, len);
  293. return *this;
  294. }
  295. void push_back(char c){
  296. if(_erase_tail == 0){
  297. _str.push_back(c);
  298. return;
  299. }
  300. data()[size()] = c;
  301. --_erase_tail;
  302. data()[size()] = '\0';
  303. }
  304. BufferLikeString& insert(size_t pos, const char* s, size_t n){
  305. _str.insert(_erase_head + pos, s, n);
  306. return *this;
  307. }
  308. BufferLikeString& assign(const char *data) {
  309. return assign(data, strlen(data));
  310. }
  311. BufferLikeString& assign(const char *data, size_t len) {
  312. if (len <= 0) {
  313. return *this;
  314. }
  315. if (data >= _str.data() && data < _str.data() + _str.size()) {
  316. _erase_head = data - _str.data();
  317. if (data + len > _str.data() + _str.size()) {
  318. throw std::out_of_range("BufferLikeString::assign out_of_range");
  319. }
  320. _erase_tail = _str.data() + _str.size() - (data + len);
  321. return *this;
  322. }
  323. _str.assign(data, len);
  324. _erase_head = 0;
  325. _erase_tail = 0;
  326. return *this;
  327. }
  328. void clear() {
  329. _erase_head = 0;
  330. _erase_tail = 0;
  331. _str.clear();
  332. }
  333. char& operator[](size_t pos){
  334. if (pos >= size()) {
  335. throw std::out_of_range("BufferLikeString::operator[] out_of_range");
  336. }
  337. return data()[pos];
  338. }
  339. const char& operator[](size_t pos) const{
  340. return (*const_cast<BufferLikeString *>(this))[pos];
  341. }
  342. size_t capacity() const{
  343. return _str.capacity();
  344. }
  345. void reserve(size_t size){
  346. _str.reserve(size);
  347. }
  348. bool empty() const{
  349. return size() <= 0;
  350. }
  351. string substr(size_t pos, size_t n = string::npos) const{
  352. if (n == string::npos) {
  353. //获取末尾所有的
  354. if (pos >= size()) {
  355. throw std::out_of_range("BufferLikeString::substr out_of_range");
  356. }
  357. return _str.substr(_erase_head + pos, size() - pos);
  358. }
  359. //获取部分
  360. if (pos + n > size()) {
  361. throw std::out_of_range("BufferLikeString::substr out_of_range");
  362. }
  363. return _str.substr(_erase_head + pos, n);
  364. }
  365. private:
  366. void moveData(){
  367. if (_erase_head) {
  368. _str.erase(0, _erase_head);
  369. _erase_head = 0;
  370. }
  371. }
  372. private:
  373. size_t _erase_head;
  374. size_t _erase_tail;
  375. string _str;
  376. //对象个数统计
  377. ObjectStatistic<BufferLikeString> _statistic;
  378. };
  379. #if defined(_WIN32)
  380. struct iovec {
  381. void * iov_base; /* [XSI] Base address of I/O memory region */
  382. size_t iov_len; /* [XSI] Size of region iov_base points to */
  383. };
  384. struct msghdr {
  385. void *msg_name; /* [XSI] optional address */
  386. size_t msg_namelen; /* [XSI] size of address */
  387. struct iovec *msg_iov; /* [XSI] scatter/gather array */
  388. size_t msg_iovlen; /* [XSI] # elements in msg_iov */
  389. void *msg_control; /* [XSI] ancillary data, see below */
  390. int msg_controllen; /* [XSI] ancillary data buffer len */
  391. int msg_flags; /* [XSI] flags on received message */
  392. };
  393. #else
  394. #include <sys/uio.h>
  395. #include <limits.h>
  396. #endif
  397. #if !defined(IOV_MAX)
  398. #define IOV_MAX 1024
  399. #endif
  400. class BufferList;
  401. class BufferSock : public Buffer {
  402. public:
  403. using Ptr = std::shared_ptr<BufferSock>;
  404. friend class BufferList;
  405. BufferSock(Buffer::Ptr ptr, struct sockaddr *addr = nullptr, int addr_len = 0);
  406. ~BufferSock();
  407. char *data() const override;
  408. size_t size() const override;
  409. private:
  410. int _addr_len = 0;
  411. struct sockaddr *_addr = nullptr;
  412. Buffer::Ptr _buffer;
  413. };
  414. class BufferList : public noncopyable {
  415. public:
  416. using Ptr = std::shared_ptr<BufferList>;
  417. using SendResult = function<void(const Buffer::Ptr &buffer, bool send_success)>;
  418. BufferList(List<std::pair<Buffer::Ptr, bool> > &list, SendResult cb = nullptr);
  419. ~BufferList();
  420. bool empty();
  421. size_t count();
  422. ssize_t send(int fd, int flags, bool udp);
  423. private:
  424. void reOffset(size_t n);
  425. ssize_t send_l(int fd, int flags, bool udp);
  426. private:
  427. size_t _iovec_off = 0;
  428. size_t _remain_size = 0;
  429. vector<struct iovec> _iovec;
  430. List<std::pair<Buffer::Ptr, bool> > _pkt_list;
  431. SendResult _cb;
  432. //对象个数统计
  433. ObjectStatistic<BufferList> _statistic;
  434. };
  435. }//namespace toolkit
  436. #endif //ZLTOOLKIT_BUFFER_H