33
44ProtocolBuffer::ProtocolBuffer (std::size_t capacity)
55 : _writeIndex(0 ),
6- _startIndex( 0 ),
6+ _readBuffer(capacity, 0 ),
77 _contentLength(0 ),
88 _bodyStartIndex(0 )
99{
10- _buffer.resize (capacity);
1110}
1211
1312char * ProtocolBuffer::GetWritableCursor ()
1413{
15- return _buffer .data () + _writeIndex ;
14+ return _readBuffer .data ();
1615}
1716
1817
1918std::size_t ProtocolBuffer::GetRestCapacity ()
2019{
21- return _buffer .size () - _writeIndex ;
20+ return _readBuffer .size ();
2221}
2322
24- void ProtocolBuffer::SetReadableSize (std::size_t readableSize )
23+ void ProtocolBuffer::WriteBuff (std::size_t size )
2524{
26- _writeIndex = _startIndex + readableSize;
25+ if (_textProtocol.size () < _writeIndex + size)
26+ {
27+ _textProtocol.resize (_writeIndex + size);
28+ }
29+
30+ std::copy_n (_readBuffer.begin (), size, _textProtocol.begin () + _writeIndex);
31+ _writeIndex += size;
2732}
2833
2934bool ProtocolBuffer::CanReadOneProtocol ()
3035{
31- if (_startIndex == _writeIndex )
36+ if (_writeIndex == 0 )
3237 {
3338 return false ;
3439 }
3540
36-
3741 bool success = TryParseHead ();
38- bool completeOneProtocol = false ;
39-
40- if (success)
41- {
42- completeOneProtocol = _contentLength + _bodyStartIndex <= _writeIndex;
43- }
44-
45- // 如果头解析不完整或者协议接收不完整
46- // 试图扩容
47- if ((!success || !completeOneProtocol) && (_buffer.size () - _writeIndex <= 1 ))
48- {
49- if (_contentLength != 0 && _bodyStartIndex != 0 )
50- {
51- _buffer.resize (_bodyStartIndex + _contentLength);
52- }
53- else
54- {
55- _buffer.resize (_buffer.size () * 2 );
56- }
57- }
5842
59- return completeOneProtocol ;
43+ return success && (_contentLength + _bodyStartIndex <= _writeIndex) ;
6044}
6145
6246std::string_view ProtocolBuffer::ReadOneProtocol ()
@@ -65,26 +49,29 @@ std::string_view ProtocolBuffer::ReadOneProtocol()
6549 {
6650 if (_writeIndex >= _contentLength + _bodyStartIndex)
6751 {
68- return std::string_view (_buffer .data () + _bodyStartIndex, _contentLength);
52+ return std::string_view (_textProtocol .data () + _bodyStartIndex, _contentLength);
6953 }
7054 }
7155 return " " ;
7256}
7357
7458void ProtocolBuffer::Reset ()
7559{
76- if (_writeIndex > _contentLength + _bodyStartIndex)
60+ if (_writeIndex > _contentLength + _bodyStartIndex)
7761 {
7862 std::size_t doneIndex = _contentLength + _bodyStartIndex;
79- std::memmove (_buffer .data (), _buffer. data () + doneIndex, _writeIndex - doneIndex);
63+ std::copy_n (_textProtocol .data () + doneIndex, _writeIndex - doneIndex, _textProtocol. data () );
8064
81- _startIndex = 0 ;
82- _writeIndex -= _contentLength + _bodyStartIndex;
65+ _writeIndex -= doneIndex;
66+ _textProtocol.resize (std::max (_writeIndex, _readBuffer.size ()));
67+ _textProtocol.shrink_to_fit ();
8368 }
8469 else
8570 {
86- _startIndex = 0 ;
8771 _writeIndex = 0 ;
72+ _textProtocol.clear ();
73+ _textProtocol.resize (_readBuffer.size ());
74+ _textProtocol.shrink_to_fit ();
8875 }
8976
9077 _contentLength = 0 ;
@@ -100,7 +87,7 @@ bool ProtocolBuffer::TryParseHead()
10087 CRLF_CRLF
10188 } state = ParseState::HeadStart;
10289
103- std::string_view text (_buffer .data () + _startIndex , _writeIndex - _startIndex );
90+ std::string_view text (_textProtocol .data (), _writeIndex);
10491 for (std::size_t index = 0 ; index < text.size ();)
10592 {
10693 switch (state)
@@ -153,5 +140,5 @@ bool ProtocolBuffer::TryParseHead()
153140 }
154141 }
155142
156- return true ;
143+ return false ;
157144}
0 commit comments