11#include " CodeFormatServer/Session/StandardIOSession.h"
22
33#include < iostream>
4- #include < thread>
4+
5+ #ifndef _WIN32
6+ #include < asio.hpp>
7+ #include < unistd.h>
8+ #endif
59#include " CodeFormatServer/Protocol/ProtocolParser.h"
610#include " CodeFormatServer/Protocol/ProtocolBuffer.h"
711
12+
13+ class StandardIO
14+ {
15+ public:
16+ static StandardIO& GetInstance ();
17+
18+ StandardIO ();
19+
20+ std::size_t ReadSome (char * buffer, std::size_t maxSize);
21+
22+ bool HasError ();
23+
24+ void Write (std::string_view content);
25+ private:
26+ #ifndef _WIN32
27+ asio::io_context _ioc;
28+ asio::error_code _code;
29+ std::shared_ptr<asio::posix::stream_descriptor> _in;
30+ std::shared_ptr<asio::posix::stream_descriptor> _out;
31+ #endif
32+ };
33+
34+ StandardIO& StandardIO::GetInstance ()
35+ {
36+ static StandardIO instance;
37+ return instance;
38+ }
39+
40+ #ifndef _WIN32
41+ StandardIO::StandardIO ()
42+ :_ioc(1 )
43+ {
44+ _in = std::make_shared<asio::posix::stream_descriptor>(_ioc, STDIN_FILENO);
45+ _out = std::make_shared<asio::posix::stream_descriptor>(_ioc, STDOUT_FILENO);
46+ }
47+
48+ std::size_t StandardIO::ReadSome (char * buffer, std::size_t maxSize)
49+ {
50+ return _in->read_some (asio::buffer (buffer, maxSize), _code);
51+ }
52+
53+ bool StandardIO::HasError ()
54+ {
55+ return _code == asio::error::eof || _code;
56+ }
57+
58+ void StandardIO::Write (std::string_view content)
59+ {
60+ _out->write_some (asio::buffer (content.data (),content.size ()));
61+ }
62+
63+ #else
64+ StandardIO::StandardIO ()
65+ {
66+ }
67+
68+ std::size_t StandardIO::ReadSome (char * buffer, std::size_t maxSize)
69+ {
70+ std::cin.peek ();
71+ return std::cin.readsome (buffer, maxSize);
72+ }
73+
74+ bool StandardIO::HasError ()
75+ {
76+ return !std::cin;
77+ }
78+
79+ void StandardIO::Write (std::string_view content)
80+ {
81+ std::cout.write (content.data (), content.size ());
82+ }
83+
84+
85+ #endif
86+
87+
888void StandardIOSession::Run ()
989{
1090
@@ -15,9 +95,9 @@ void StandardIOSession::Run()
1595 char * writableCursor = _protocolBuffer.GetWritableCursor ();
1696 std::size_t capacity = _protocolBuffer.GetRestCapacity ();
1797
18- std::cin. peek ( );
19- std:: size_t readSize = std::cin. readsome (writableCursor, capacity);
20- if (!std::cin )
98+ std::size_t readSize = StandardIO::GetInstance (). ReadSome (writableCursor, capacity );
99+
100+ if ( StandardIO::GetInstance (). HasError () )
21101 {
22102 goto endLoop;
23103 }
@@ -30,23 +110,26 @@ void StandardIOSession::Run()
30110 }
31111
32112 _protocolBuffer.FitCapacity ();
113+ }
114+ while (true );
33115
34- } while (true );
35-
36- do {
116+ do
117+ {
37118 std::string result = Handle (_protocolBuffer.ReadOneProtocol ());
38119
39120 _protocolBuffer.Reset ();
40- if (!result.empty ()) {
121+ if (!result.empty ())
122+ {
41123 Send (result);
42124 }
43- } while (_protocolBuffer.CanReadOneProtocol ());
125+ }
126+ while (_protocolBuffer.CanReadOneProtocol ());
44127 }
45128endLoop:
46129 return ;
47130}
48131
49132void StandardIOSession::Send (std::string_view content)
50133{
51- std::cout. write (content. data (), content. size () );
134+ StandardIO::GetInstance (). Write (content );
52135}
0 commit comments