BufferSock.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/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_BUFFERSOCK_H
  11. #define ZLTOOLKIT_BUFFERSOCK_H
  12. #if !defined(_WIN32)
  13. #include <sys/uio.h>
  14. #include <limits.h>
  15. #endif
  16. #include <cassert>
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include <type_traits>
  21. #include <functional>
  22. #include "Util/util.h"
  23. #include "Util/List.h"
  24. #include "Util/ResourcePool.h"
  25. #include "sockutil.h"
  26. #include "Buffer.h"
  27. namespace toolkit {
  28. #if !defined(IOV_MAX)
  29. #define IOV_MAX 1024
  30. #endif
  31. class BufferSock : public Buffer {
  32. public:
  33. using Ptr = std::shared_ptr<BufferSock>;
  34. BufferSock(Buffer::Ptr ptr, struct sockaddr *addr = nullptr, int addr_len = 0);
  35. ~BufferSock() override = default;
  36. char *data() const override;
  37. size_t size() const override;
  38. const struct sockaddr *sockaddr() const;
  39. socklen_t socklen() const;
  40. private:
  41. int _addr_len = 0;
  42. struct sockaddr_storage _addr;
  43. Buffer::Ptr _buffer;
  44. };
  45. class BufferList : public noncopyable {
  46. public:
  47. using Ptr = std::shared_ptr<BufferList>;
  48. using SendResult = std::function<void(const Buffer::Ptr &buffer, bool send_success)>;
  49. BufferList() = default;
  50. virtual ~BufferList() = default;
  51. virtual bool empty() = 0;
  52. virtual size_t count() = 0;
  53. virtual ssize_t send(int fd, int flags) = 0;
  54. static Ptr create(List<std::pair<Buffer::Ptr, bool> > list, SendResult cb, bool is_udp);
  55. private:
  56. //对象个数统计
  57. ObjectStatistic<BufferList> _statistic;
  58. };
  59. }
  60. #endif //ZLTOOLKIT_BUFFERSOCK_H