summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/src/jsonrpc/TcpServer.cpp
blob: 799d0aec9adcd32ae2ddb4e155f98968c305bb5c (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// server.cpp

#include "LibLsp/JsonRpc/TcpServer.h"
#include <signal.h>
#include <utility>
#include <boost/bind/bind.hpp>

#include "LibLsp/JsonRpc/MessageIssue.h"
#include "LibLsp/JsonRpc/stream.h"


namespace lsp {
        struct tcp_connect_session;


                class tcp_stream_wrapper :public istream, public ostream
            {
            public:
                    tcp_stream_wrapper(tcp_connect_session& _w);

                tcp_connect_session& session;
                std::atomic<bool> quit{};
                std::shared_ptr < MultiQueueWaiter> request_waiter;
                ThreadedQueue< char > on_request;
            std::string error_message;


                bool fail() override
                {
                    return  bad();
                }



                bool eof() override
                {
                    return  bad();
                }
                bool good() override
                {
                    return  !bad();
                }
                tcp_stream_wrapper& read(char* str, std::streamsize count)
                  override
                {
                auto some = on_request.TryDequeueSome(static_cast<size_t>( count ));
                memcpy(str,some.data(),some.size());
                for (std::streamsize i = some.size(); i < count; ++i)
                {
                    str[i] = static_cast<char>(get());
                }

                    return *this;
                }
                int get() override
                {
                    return on_request.Dequeue();
                }

                bool bad() override;

                tcp_stream_wrapper& write(const std::string& c) override;

                tcp_stream_wrapper& write(std::streamsize _s) override;

                tcp_stream_wrapper& flush() override
                {
                    return *this;
                }
                void reset_state()
                {
                    return;
                }

                    void clear() override
                {

                }

                    std::string what() override;
                    bool need_to_clear_the_state() override
                    {
                return false;
                    }
            };
            struct tcp_connect_session:std::enable_shared_from_this<tcp_connect_session>
            {
            /// Buffer for incoming data.
            std::array<unsigned char, 8192> buffer_;
            boost::asio::ip::tcp::socket socket_;
            /// Strand to ensure the connection's handlers are not called concurrently.
            boost::asio::io_context::strand strand_;
            std::shared_ptr<tcp_stream_wrapper>  proxy_;
            explicit tcp_connect_session(boost::asio::io_context& io_context, boost::asio::ip::tcp::socket&& _socket)
                    : socket_(std::move(_socket)), strand_(io_context), proxy_(new tcp_stream_wrapper(*this))
            {
                do_read();
            }
            void do_write(const std::string& data)
            {
                socket_.async_write_some(boost::asio::buffer(data.data(), data.size()),
                    boost::asio::bind_executor(strand_,[this](boost::system::error_code ec, std::size_t n)
                   {
                        if (!ec)
                        {
                            return;
                        }
                        proxy_->error_message = ec.message();

                    }));
            }
            void do_read()
            {
                socket_.async_read_some(boost::asio::buffer(buffer_),
            boost::asio::bind_executor(strand_,
                [this](boost::system::error_code ec, size_t bytes_transferred)
                {
                    if (!ec)
                    {
                        std::vector<char> elements(buffer_.data(), buffer_.data() + bytes_transferred);
                        proxy_->on_request.EnqueueAll(std::move(elements), false);
                        do_read();
                        return;
                    }
                    proxy_->error_message = ec.message();

                }));
            }
            };

        tcp_stream_wrapper::tcp_stream_wrapper(tcp_connect_session& _w): session(_w)
        {
        }

        bool tcp_stream_wrapper::bad()
    {
        return !session.socket_.is_open();
    }

        tcp_stream_wrapper& tcp_stream_wrapper::write(const std::string& c)
        {
                session.do_write(c);
                return *this;
        }

    tcp_stream_wrapper& tcp_stream_wrapper::write(std::streamsize _s)
    {
        session.do_write(std::to_string(_s));
        return *this;
    }

        std::string tcp_stream_wrapper::what()
        {
        if (error_message.size())
            return error_message;

       if(! session.socket_.is_open())
       {
           return  "Socket is not open.";
       }
                return {};
        }

    struct TcpServer::Data
    {
        Data(
            lsp::Log& log, uint32_t _max_workers) :
                    acceptor_(io_context_), _log(log)
            {
            }

            ~Data()
            {

            }
        /// The io_context used to perform asynchronous operations.
        boost::asio::io_context io_context_;

        std::shared_ptr<boost::asio::io_service::work> work;

        std::shared_ptr<tcp_connect_session> _connect_session;
        /// Acceptor used to listen for incoming connections.
        boost::asio::ip::tcp::acceptor acceptor_;

        lsp::Log& _log;

    };

            TcpServer::~TcpServer()
            {
            delete d_ptr;
            }

        TcpServer::TcpServer(const std::string& address, const std::string& port,
            std::shared_ptr < MessageJsonHandler> json_handler,
            std::shared_ptr < Endpoint> localEndPoint, lsp::Log& log, uint32_t _max_workers)
            : point(json_handler, localEndPoint, log, _max_workers),d_ptr(new Data( log, _max_workers))

        {

            d_ptr->work = std::make_shared<boost::asio::io_service::work>(d_ptr->io_context_);

            // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
            boost::asio::ip::tcp::resolver resolver(d_ptr->io_context_);
            boost::asio::ip::tcp::endpoint endpoint =
                *resolver.resolve(address, port).begin();
            d_ptr->acceptor_.open(endpoint.protocol());
            d_ptr->acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
            try
            {
                d_ptr->acceptor_.bind(endpoint);
            }
            catch (boost::system::system_error & e)
            {
                std::string temp = "Socket Server  bind failed.";
                d_ptr->_log.log(lsp::Log::Level::INFO , temp + e.what());
                return;
            }
            d_ptr->acceptor_.listen();

            do_accept();
            std::string desc = "Socket TcpServer " + address + " " + port + " start.";
            d_ptr->_log.log(lsp::Log::Level::INFO, desc);
        }

        void TcpServer::run()
        {
            // The io_context::run() call will block until all asynchronous operations
            // have finished. While the TcpServer is running, there is always at least one
            // asynchronous operation outstanding: the asynchronous accept call waiting
            // for new incoming connections.
            d_ptr->io_context_.run();

        }

        void TcpServer::stop()
        {
            try
            {
                if(d_ptr->work)
                    d_ptr->work.reset();

                do_stop();
            }
            catch (...)
            {
            }
        }

        void TcpServer::do_accept()
        {
            d_ptr->acceptor_.async_accept(
                [this](boost::system::error_code ec, boost::asio::ip::tcp::socket socket)
                {
                    // Check whether the TcpServer was stopped by a signal before this
                    // completion handler had a chance to run.
                    if (!d_ptr->acceptor_.is_open())
                    {
                        return;
                    }

                    if (!ec)
                    {
                        if(d_ptr->_connect_session)
                        {
                                if(d_ptr->_connect_session->socket_.is_open())
                                {
                                std::string desc = "Disconnect previous client " + d_ptr->_connect_session->socket_.local_endpoint().address().to_string();
                                d_ptr->_log.log(lsp::Log::Level::INFO, desc);
                                d_ptr->_connect_session->socket_.close();
                                }

                            point.stop();
                        }
                        auto local_point = socket.local_endpoint();

                        std::string desc = ("New client " + local_point.address().to_string() + " connect.");
                        d_ptr->_log.log(lsp::Log::Level::INFO, desc);
                        d_ptr->_connect_session = std::make_shared<tcp_connect_session>(d_ptr->io_context_,std::move(socket));

                        point.startProcessingMessages(d_ptr->_connect_session->proxy_, d_ptr->_connect_session->proxy_);
                        do_accept();
                    }
                });
        }

        void TcpServer::do_stop()
        {
            d_ptr->acceptor_.close();

            point.stop();

        }

    } // namespace