Skip to content

Commit 1ecaf82

Browse files
committed
修改缓冲区方案
1 parent ca3b115 commit 1ecaf82

File tree

8 files changed

+74
-77
lines changed

8 files changed

+74
-77
lines changed

CodeFormatServer/src/CodeFormatServer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace asio::ip;
1818

1919
int main(int argc, char** argv)
2020
{
21-
// std::this_thread::sleep_for(std::chrono::seconds(10));
21+
2222
if(argc > 1)
2323
{
2424
int port = std::stoi(argv[1]);
@@ -33,6 +33,7 @@ int main(int argc, char** argv)
3333
}
3434
else
3535
{
36+
// std::this_thread::sleep_for(std::chrono::seconds(10));
3637
SET_BINARY_MODE();
3738
LanguageClient::GetInstance().SetSession(std::make_shared<StandardIOSession>());
3839
LanguageClient::GetInstance().Run();

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
8282
return;
8383
}
8484

85-
86-
8785
parser->BuildAstWithComment();
8886

8987
LuaFormatter formatter(parser, *options);

CodeFormatServer/src/Protocol/ProtocolBuffer.cpp

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,44 @@
33

44
ProtocolBuffer::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

1312
char* ProtocolBuffer::GetWritableCursor()
1413
{
15-
return _buffer.data() + _writeIndex;
14+
return _readBuffer.data();
1615
}
1716

1817

1918
std::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

2934
bool 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

6246
std::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

7458
void 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
}

CodeFormatServer/src/Session/IOSession.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "CodeFormatServer/Protocol/ProtocolParser.h"
55

66
IOSession::IOSession()
7+
: _protocolBuffer(65535)
78
{
89
_service.Initialize();
910
}
@@ -12,21 +13,33 @@ IOSession::~IOSession()
1213
{
1314
}
1415

16+
void IOSession::Run()
17+
{
18+
}
19+
1520
std::string IOSession::Handle(std::string_view msg)
1621
{
17-
ProtocolParser parser;
22+
try
23+
{
24+
ProtocolParser parser;
1825

19-
parser.Parse(msg);
26+
parser.Parse(msg);
2027

21-
auto param = parser.GetParam();
28+
auto param = parser.GetParam();
2229

23-
if (param)
24-
{
25-
auto result = _service.Dispatch(parser.GetMethod(), param);
26-
if (result) {
27-
return parser.SerializeProtocol(result);
30+
if (param)
31+
{
32+
auto result = _service.Dispatch(parser.GetMethod(), param);
33+
if (result)
34+
{
35+
return parser.SerializeProtocol(result);
36+
}
2837
}
2938
}
39+
catch (std::exception& e)
40+
{
41+
std::cerr << e.what() << std::endl;
42+
}
3043

3144
return "";
3245
}

CodeFormatServer/src/Session/SocketIOSession.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "CodeFormatServer/Session/SocketIOSession.h"
2-
32
#include <iostream>
4-
53
#include "CodeFormatServer/Protocol/ProtocolParser.h"
64
#include "CodeFormatServer/Protocol/ProtocolBuffer.h"
75
using namespace asio::ip;
@@ -13,43 +11,40 @@ SocketIOSession::SocketIOSession(asio::ip::tcp::socket&& socket)
1311

1412
void SocketIOSession::Run()
1513
{
16-
ProtocolBuffer protocolBuffer(1024);
1714
while (true)
1815
{
19-
asio::error_code code;
20-
std::size_t readSize = 0;
21-
22-
2316
do
2417
{
25-
char* writableCursor = protocolBuffer.GetWritableCursor();
26-
std::size_t capacity = protocolBuffer.GetRestCapacity();
18+
asio::error_code code;
19+
20+
char* writableCursor = _protocolBuffer.GetWritableCursor();
21+
std::size_t capacity = _protocolBuffer.GetRestCapacity();
2722

28-
readSize += _socket.read_some(asio::buffer(writableCursor, capacity), code);
23+
std::size_t readSize = _socket.read_some(asio::buffer(writableCursor, capacity), code);
2924
if (code == asio::error::eof || code)
3025
{
3126
goto endLoop;
3227
}
3328

34-
protocolBuffer.SetReadableSize(readSize);
29+
_protocolBuffer.WriteBuff(readSize);
3530

36-
if (protocolBuffer.CanReadOneProtocol())
31+
if (_protocolBuffer.CanReadOneProtocol())
3732
{
3833
break;
3934
}
4035
}
4136
while (true);
4237

4338
do {
44-
auto content = protocolBuffer.ReadOneProtocol();
39+
auto content = _protocolBuffer.ReadOneProtocol();
4540
// std::cout << content << std::endl;
4641
std::string result = Handle(content);
4742

48-
protocolBuffer.Reset();
43+
_protocolBuffer.Reset();
4944
if (!result.empty()) {
5045
Send(result);
5146
}
52-
} while (protocolBuffer.CanReadOneProtocol());
47+
} while (_protocolBuffer.CanReadOneProtocol());
5348
}
5449
endLoop:
5550
return;

CodeFormatServer/src/Session/StandardIOSession.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
void StandardIOSession::Run()
99
{
10-
ProtocolBuffer protocolBuffer(1024);
10+
1111
while (true)
1212
{
1313
std::size_t readSize = 0;
1414
do
1515
{
16-
char* writableCursor = protocolBuffer.GetWritableCursor();
17-
std::size_t capacity = protocolBuffer.GetRestCapacity();
16+
char* writableCursor = _protocolBuffer.GetWritableCursor();
17+
std::size_t capacity = _protocolBuffer.GetRestCapacity();
1818

1919
std::cin.peek();
2020
readSize += std::cin.readsome(writableCursor, capacity);
@@ -23,23 +23,23 @@ void StandardIOSession::Run()
2323
goto endLoop;
2424
}
2525

26-
protocolBuffer.SetReadableSize(readSize);
26+
_protocolBuffer.WriteBuff(readSize);
2727

28-
if (protocolBuffer.CanReadOneProtocol())
28+
if (_protocolBuffer.CanReadOneProtocol())
2929
{
3030
break;
3131
}
3232

3333
} while (true);
3434

3535
do {
36-
std::string result = Handle(protocolBuffer.ReadOneProtocol());
36+
std::string result = Handle(_protocolBuffer.ReadOneProtocol());
3737

38-
protocolBuffer.Reset();
38+
_protocolBuffer.Reset();
3939
if (!result.empty()) {
4040
Send(result);
4141
}
42-
} while (protocolBuffer.CanReadOneProtocol());
42+
} while (_protocolBuffer.CanReadOneProtocol());
4343
}
4444
endLoop:
4545
return;

include/CodeFormatServer/Protocol/ProtocolBuffer.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ProtocolBuffer
1111

1212
std::size_t GetRestCapacity();
1313

14-
void SetReadableSize(std::size_t readableSize);
14+
void WriteBuff(std::size_t size);
1515

1616
bool CanReadOneProtocol();
1717

@@ -22,12 +22,13 @@ class ProtocolBuffer
2222
bool TryParseHead();
2323
// 代表从这个位置开始可写
2424
std::size_t _writeIndex;
25-
// 代表从这个位置开始可读
26-
// 同时如果writeIndex 和 startIndex 相同则认为可读长度为0
27-
std::size_t _startIndex;
28-
std::string _buffer;
25+
std::string _textProtocol;
26+
27+
std::string _readBuffer;
2928

3029
std::size_t _contentLength;
3130
std::size_t _bodyStartIndex;
31+
32+
3233
};
3334

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#pragma once
22
#include <string>
33
#include "CodeFormatServer/LanguageService.h"
4+
#include "CodeFormatServer/Protocol/ProtocolBuffer.h"
45

56
class IOSession
67
{
78
public:
89
IOSession();
910
virtual ~IOSession();
1011

11-
virtual void Run() = 0;
12+
virtual void Run();
1213
virtual void Send(std::string_view content) = 0;
1314
protected:
1415
std::string Handle(std::string_view msg);
16+
ProtocolBuffer _protocolBuffer;
1517
private:
1618
LanguageService _service;
1719
};

0 commit comments

Comments
 (0)