rpc_zero_copy_stream.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2020 sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_ZERO_COPY_STREAM_H__
  14. #define __RPC_ZERO_COPY_STREAM_H__
  15. #include <google/protobuf/io/zero_copy_stream.h>
  16. namespace srpc
  17. {
  18. class RPCOutputStream : public google::protobuf::io::ZeroCopyOutputStream
  19. {
  20. public:
  21. RPCOutputStream(RPCBuffer *buf, size_t size);
  22. bool Next(void **data, int *size) override;
  23. void BackUp(int count) override;
  24. int64_t ByteCount() const override;
  25. private:
  26. RPCBuffer *buf;
  27. size_t size;
  28. };
  29. class RPCInputStream : public google::protobuf::io::ZeroCopyInputStream
  30. {
  31. public:
  32. RPCInputStream(RPCBuffer *buf);
  33. bool Next(const void **data, int *size) override;
  34. void BackUp(int count) override;
  35. bool Skip(int count) override;
  36. int64_t ByteCount() const override;
  37. private:
  38. RPCBuffer *buf;
  39. };
  40. inline RPCOutputStream::RPCOutputStream(RPCBuffer *buf, size_t size)
  41. {
  42. this->buf = buf;
  43. this->size = size;
  44. }
  45. inline bool RPCOutputStream::Next(void **data, int *size)
  46. {
  47. size_t tmp;
  48. if (this->size > 0)
  49. {
  50. tmp = this->size;
  51. if (this->buf->acquire(data, &tmp))
  52. {
  53. this->size -= tmp;
  54. *size = (int)tmp;
  55. return true;
  56. }
  57. }
  58. else
  59. {
  60. tmp = this->buf->acquire(data);
  61. if (tmp > 0)
  62. {
  63. *size = (int)tmp;
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. inline void RPCOutputStream::BackUp(int count)
  70. {
  71. this->buf->backup(count);
  72. }
  73. inline int64_t RPCOutputStream::ByteCount() const
  74. {
  75. return this->buf->size();
  76. }
  77. inline RPCInputStream::RPCInputStream(RPCBuffer *buf)
  78. {
  79. this->buf = buf;
  80. }
  81. inline bool RPCInputStream::Next(const void **data, int *size)
  82. {
  83. size_t len = this->buf->fetch(data);
  84. if (len == 0)
  85. return false;
  86. *size = (int)len;
  87. return true;
  88. }
  89. inline bool RPCInputStream::Skip(int count)
  90. {
  91. return this->buf->seek(count) == count ? true : false;
  92. }
  93. inline void RPCInputStream::BackUp(int count)
  94. {
  95. this->buf->seek(0 - count);
  96. }
  97. inline int64_t RPCInputStream::ByteCount() const
  98. {
  99. return (int64_t)this->buf->size();
  100. }
  101. } // namespace srpc
  102. #endif