forked from baidu/sofa-pbrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.h
124 lines (97 loc) · 3.36 KB
/
buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2014 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: qinzuoyan01@baidu.com (Qin Zuoyan)
#ifndef _SOFA_PBRPC_BUFFER_H_
#define _SOFA_PBRPC_BUFFER_H_
#include <deque>
#include <google/protobuf/io/zero_copy_stream.h>
#include <sofa/pbrpc/common.h>
#include <sofa/pbrpc/buf_handle.h>
namespace sofa {
namespace pbrpc {
// Defined in this file.
class ReadBuffer;
typedef sofa::pbrpc::shared_ptr<ReadBuffer> ReadBufferPtr;
class WriteBuffer;
typedef sofa::pbrpc::shared_ptr<WriteBuffer> WriteBufferPtr;
typedef std::deque<BufHandle> BufHandleList;
typedef std::deque<BufHandle>::iterator BufHandleListIterator;
typedef std::deque<BufHandle>::reverse_iterator BufHandleListReverseIterator;
class ReadBuffer : public google::protobuf::io::ZeroCopyInputStream
{
public:
ReadBuffer();
virtual ~ReadBuffer();
// Append a buf handle to the buffer.
//
// Preconditions:
// * No method Next(), Backup() or Skip() have been called before.
// * The size of "buf_handle" should be greater than 0.
void Append(const BufHandle& buf_handle);
// Get the total byte count of the buffer.
int64 TotalCount() const;
// Get the block count occupied by the buffer.
int BlockCount() const;
// implements ZeroCopyInputStream ----------------------------------
bool Next(const void** data, int* size);
void BackUp(int count);
bool Skip(int count);
int64 ByteCount() const;
private:
BufHandleList _buf_list;
int64 _total_bytes; // total bytes in the buffer
BufHandleListIterator _cur_it;
int _cur_pos;
int _last_bytes; // last read bytes
int64 _read_bytes; // total read bytes
SOFA_PBRPC_DISALLOW_EVIL_CONSTRUCTORS(ReadBuffer);
}; // class ReadBuffer
class WriteBuffer : public google::protobuf::io::ZeroCopyOutputStream
{
public:
WriteBuffer();
virtual ~WriteBuffer();
// Get the total capacity of the buffer.
int64 TotalCapacity() const;
// Get the block count occupied by the buffer.
int BlockCount() const;
// Swap out data from this output stream and append to the input stream "is".
// The "is" should not be null.
//
// Postconditions:
// * This buffer is cleared afterward, just as a new output buffer.
void SwapOut(ReadBuffer* is);
// Reserve some space in the buffer.
// If succeed, return the head position of reserved space.
// If failed, return -1.
//
// Preconditions:
// * "count" > 0
int64 Reserve(int count);
// Set data in the buffer, start from "pos".
//
// Preconditions:
// * "pos" >= 0
// * "data" != NULL && "size" > 0
// * "pos" + "size" <= ByteCount()
void SetData(int64 pos, const char* data, int size);
// implements ZeroCopyOutputStream ---------------------------------
bool Next(void** data, int* size);
void BackUp(int count);
int64 ByteCount() const;
private:
// Add a new block to the end of the buffer.
bool Extend();
private:
BufHandleList _buf_list;
int64 _total_capacity;
int _last_bytes; // last write bytes
int64 _write_bytes; // total write bytes
SOFA_PBRPC_DISALLOW_EVIL_CONSTRUCTORS(WriteBuffer);
}; // class WriteBuffer
} // namespace pbrpc
} // namespace sofa
#endif // _SOFA_PBRPC_BUFFER_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */