File: client_connection.cpp

package info (click to toggle)
snapcast 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,252 kB
  • sloc: cpp: 40,067; python: 3,260; sh: 455; makefile: 16
file content (724 lines) | stat: -rw-r--r-- 23,432 bytes parent folder | download
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/***
    This file is part of snapcast
    Copyright (C) 2014-2025  Johannes Pohl

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***/

// prototype/interface header file
#include "client_connection.hpp"

// local headers
#include "common/aixlog.hpp"
#include "common/str_compat.hpp"

// 3rd party headers
#include <boost/asio/buffer.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/write.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/stream_traits.hpp>

// standard headers
#include <cstdint>
#include <iostream>
#include <mutex>
#include <optional>
#include <string>
#include <utility>


using namespace std;
namespace http = beast::http; // from <boost/beast/http.hpp>

static constexpr auto LOG_TAG = "Connection";

static constexpr const char* WS_CLIENT_NAME = "Snapcast";


PendingRequest::PendingRequest(const boost::asio::strand<boost::asio::any_io_executor>& strand, uint16_t reqId, const MessageHandler<msg::BaseMessage>& handler)
    : id_(reqId), timer_(strand), strand_(strand), handler_(handler)
{
}

PendingRequest::~PendingRequest()
{
    cancel();
}

void PendingRequest::setValue(std::unique_ptr<msg::BaseMessage> value)
{
    boost::asio::post(strand_, [this, self = shared_from_this(), val = std::move(value)]() mutable
    {
        timer_.cancel();
        if (handler_)
            handler_({}, std::move(val));
    });
}

uint16_t PendingRequest::id() const
{
    return id_;
}

void PendingRequest::startTimer(const chronos::usec& timeout)
{
    timer_.expires_after(timeout);
    timer_.async_wait([this, self = shared_from_this()](boost::system::error_code ec)
    {
        if (!handler_)
            return;

        if (!ec)
        {
            // !ec => expired => timeout
            handler_(boost::asio::error::timed_out, nullptr);
            handler_ = nullptr;
        }
        else if (ec != boost::asio::error::operation_aborted)
        {
            // ec != aborted => not cancelled (in setValue)
            //   => should not happen, but who knows => pass the error to the handler
            handler_(ec, nullptr);
        }
    });
}

bool PendingRequest::operator<(const PendingRequest& other) const
{
    return (id_ < other.id());
}

void PendingRequest::cancel()
{
    boost::asio::post(strand_, [this, me = weak_from_this()]() mutable
    {
        auto self = me.lock();
        if (!self)
            return;
        handler_ = nullptr;
        timer_.cancel();
    });
}



ClientConnection::ClientConnection(boost::asio::io_context& io_context, ClientSettings::Server server)
    : strand_(boost::asio::make_strand(io_context.get_executor())), resolver_(strand_), reqId_(1), server_(std::move(server)),
      base_msg_size_(base_message_.getSize())
{
}


void ClientConnection::connect(const ResultHandler& handler)
{
    boost::system::error_code ec;
    if (!server_.uri.port.has_value())
    {
        uint16_t default_port = 1704;
        LOG(WARNING, LOG_TAG) << "Port not configured, using default port: " << default_port << "\n";
        server_.uri.port = default_port;
    }
    LOG(INFO, LOG_TAG) << "Resolving host IP for: " << server_.uri.host << "\n";
    auto iterator = resolver_.resolve(server_.uri.host, cpt::to_string(server_.uri.port.value()), boost::asio::ip::resolver_query_base::numeric_service, ec);
    if (ec)
    {
        LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << server_.uri.host << "', error: " << ec.message() << "\n";
        handler(ec);
        return;
    }

    for (const auto& iter : iterator)
        LOG(DEBUG, LOG_TAG) << "Resolved IP: " << iter.endpoint().address().to_string() << "\n";

    for (const auto& iter : iterator)
    {
        LOG(INFO, LOG_TAG) << "Connecting to host: " << iter.endpoint() << ", port: " << server_.uri.port.value() << ", protocol: " << server_.uri.scheme
                           << "\n";
        ec = doConnect(iter.endpoint());
        if (!ec || (ec == boost::system::errc::interrupted))
        {
            // We were successful or interrupted, e.g. by sig int
            break;
        }
    }

    if (ec)
    {
        LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << server_.uri.host << "', error: " << ec.message() << "\n";
        disconnect();
    }
    else
        LOG(NOTICE, LOG_TAG) << "Connected to " << server_.uri.host << "\n";

    handler(ec);

#if 0
    resolver_.async_resolve(query, host_, cpt::to_string(port_), [this, handler](const boost::system::error_code& ec, tcp::resolver::results_type results) {
        if (ec)
        {
            LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << host_ << "', error: " << ec.message() << "\n";
            handler(ec);
            return;
        }

        resolver_.cancel();
        socket_.async_connect(*results, [this, handler](const boost::system::error_code& ec) {
            if (ec)
            {
                LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << host_ << "', error: " << ec.message() << "\n";
                handler(ec);
                return;
            }

            LOG(NOTICE, LOG_TAG) << "Connected to " << socket_.remote_endpoint().address().to_string() << "\n";
            handler(ec);
            getNextMessage();
        });
    });
#endif
}


void ClientConnection::sendNext()
{
    auto& message = messages_.front();
    std::ostream stream(&streambuf_);
    tv t;
    message.msg->sent = t;
    message.msg->serialize(stream);
    ResultHandler handler = message.handler;

    write(streambuf_, [this, handler](boost::system::error_code ec, std::size_t length)
    {
        streambuf_.consume(length);
        if (ec)
            LOG(ERROR, LOG_TAG) << "Failed to send message, error: " << ec.message() << "\n";
        else
            LOG(TRACE, LOG_TAG) << "Wrote " << length << " bytes to socket\n";

        messages_.pop_front();
        if (handler)
            handler(ec);

        if (!messages_.empty())
            sendNext();
    });
}


void ClientConnection::send(const msg::message_ptr& message, const ResultHandler& handler)
{
    boost::asio::post(strand_, [this, message, handler]()
    {
        messages_.emplace_back(message, handler);
        if (messages_.size() > 1)
        {
            LOG(DEBUG, LOG_TAG) << "outstanding async_write\n";
            return;
        }
        sendNext();
    });
}


void ClientConnection::sendRequest(const msg::message_ptr& message, const chronos::usec& timeout, const MessageHandler<msg::BaseMessage>& handler)
{
    boost::asio::post(strand_, [this, message, timeout, handler]()
    {
        pending_requests_.erase(
            std::remove_if(pending_requests_.begin(), pending_requests_.end(), [](const std::weak_ptr<PendingRequest>& request) { return request.expired(); }),
            pending_requests_.end());
        unique_ptr<msg::BaseMessage> response(nullptr);
        static constexpr uint16_t max_req_id = 10000;
        if (++reqId_ >= max_req_id)
            reqId_ = 1;
        message->id = reqId_;
        auto request = make_shared<PendingRequest>(strand_, reqId_, handler);
        pending_requests_.push_back(request);
        request->startTimer(timeout);
        send(message, [handler](const boost::system::error_code& ec)
        {
            if (ec)
                handler(ec, nullptr);
        });
    });
}


void ClientConnection::messageReceived(std::unique_ptr<msg::BaseMessage> message, const MessageHandler<msg::BaseMessage>& handler)
{
    for (auto iter = pending_requests_.begin(); iter != pending_requests_.end(); ++iter)
    {
        const auto& request = *iter;
        if (auto req = request.lock())
        {
            if (req->id() == base_message_.refersTo)
            {
                req->setValue(std::move(message));
                pending_requests_.erase(iter);
                getNextMessage(handler);
                return;
            }
        }
    }

    if (handler)
        handler({}, std::move(message));
}


void ClientConnection::cancelRequests()
{
    boost::asio::post(strand_, [this]()
    {
        for (const auto& pending_request : pending_requests_)
        {
            auto req = pending_request.lock();
            if (req)
                req->cancel();
        }
        pending_requests_.clear();
    });
}


///////////////////////////////////// TCP /////////////////////////////////////

ClientConnectionTcp::ClientConnectionTcp(boost::asio::io_context& io_context, ClientSettings::Server server)
    : ClientConnection(io_context, std::move(server)), socket_(strand_)
{
    buffer_.resize(base_msg_size_);
}

ClientConnectionTcp::~ClientConnectionTcp()
{
    disconnect(); // NOLINT
}


void ClientConnectionTcp::disconnect()
{
    LOG(DEBUG, LOG_TAG) << "Disconnecting\n";
    if (!socket_.is_open())
    {
        LOG(DEBUG, LOG_TAG) << "Not connected\n";
        return;
    }
    boost::system::error_code ec;
    socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
    if (ec)
        LOG(ERROR, LOG_TAG) << "Error in socket shutdown: " << ec.message() << "\n";
    socket_.close(ec);
    if (ec)
        LOG(ERROR, LOG_TAG) << "Error in socket close: " << ec.message() << "\n";

    cancelRequests();
    LOG(DEBUG, LOG_TAG) << "Disconnected\n";
}


std::string ClientConnectionTcp::getMacAddress()
{
    std::string mac =
#ifndef WINDOWS
        ::getMacAddress(socket_.native_handle());
#else
        ::getMacAddress(socket_.local_endpoint().address().to_string());
#endif
    if (mac.empty())
        mac = "00:00:00:00:00:00";
    LOG(INFO, LOG_TAG) << "My MAC: \"" << mac << "\", socket: " << socket_.native_handle() << "\n";
    return mac;
}


void ClientConnectionTcp::getNextMessage(const MessageHandler<msg::BaseMessage>& handler)
{
    boost::asio::async_read(socket_, boost::asio::buffer(buffer_, base_msg_size_), [this, handler](boost::system::error_code ec, std::size_t length) mutable
    {
        if (ec)
        {
            LOG(ERROR, LOG_TAG) << "Error reading message header of length " << length << ": " << ec.message() << "\n";
            if (handler)
                handler(ec, nullptr);
            return;
        }

        base_message_.deserialize(buffer_.data());
        tv t;
        base_message_.received = t;
        // LOG(TRACE, LOG_TAG) << "getNextMessage: " << base_message_.type << ", size: " << base_message_.size << ", id: " <<
        // base_message_.id << ", refers: " << base_message_.refersTo << "\n";
        if (base_message_.type > message_type::kLast)
        {
            LOG(ERROR, LOG_TAG) << "unknown message type received: " << base_message_.type << ", size: " << base_message_.size << "\n";
            if (handler)
                handler(boost::asio::error::invalid_argument, nullptr);
            return;
        }
        else if (base_message_.size > msg::max_size)
        {
            LOG(ERROR, LOG_TAG) << "received message of type " << base_message_.type << " to large: " << base_message_.size << "\n";
            if (handler)
                handler(boost::asio::error::invalid_argument, nullptr);
            return;
        }

        if (base_message_.size > buffer_.size())
            buffer_.resize(base_message_.size);

        boost::asio::async_read(socket_, boost::asio::buffer(buffer_, base_message_.size),
                                [this, handler](boost::system::error_code ec, std::size_t length) mutable
        {
            if (ec)
            {
                LOG(ERROR, LOG_TAG) << "Error reading message body of length " << length << ": " << ec.message() << "\n";
                if (handler)
                    handler(ec, nullptr);
                return;
            }

            auto response = msg::factory::createMessage(base_message_, buffer_.data());
            if (!response)
                LOG(WARNING, LOG_TAG) << "Failed to deserialize message of type: " << base_message_.type << "\n";

            messageReceived(std::move(response), handler);
        });
    });
}


boost::system::error_code ClientConnectionTcp::doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint)
{
    boost::system::error_code ec;
    socket_.connect(endpoint, ec);
    return ec;
}


void ClientConnectionTcp::write(boost::asio::streambuf& buffer, WriteHandler&& write_handler)
{
    boost::asio::async_write(socket_, buffer, write_handler);
}


///////////////////////////////// Websockets //////////////////////////////////


ClientConnectionWs::ClientConnectionWs(boost::asio::io_context& io_context, ClientSettings::Server server)
    : ClientConnection(io_context, std::move(server)), tcp_ws_(strand_)
{
}


ClientConnectionWs::~ClientConnectionWs()
{
    disconnect(); // NOLINT
}


tcp_websocket& ClientConnectionWs::getWs()
{
    // Looks like the websocket must be recreated after disconnect:
    // https://github.com/boostorg/beast/issues/2409#issuecomment-1103685782

    std::lock_guard lock(ws_mutex_);
    if (tcp_ws_.has_value())
        return tcp_ws_.value();

    tcp_ws_.emplace(strand_);
    return tcp_ws_.value();
}


void ClientConnectionWs::disconnect()
{
    LOG(DEBUG, LOG_TAG) << "Disconnecting\n";
    boost::system::error_code ec;

    if (getWs().is_open())
        getWs().close(websocket::close_code::normal, ec);
    // if (ec)
    //     LOG(ERROR, LOG_TAG) << "Error in socket close: " << ec.message() << "\n";
    if (getWs().next_layer().is_open())
    {
        getWs().next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
        getWs().next_layer().close(ec);
    }

    cancelRequests();
    tcp_ws_ = std::nullopt;
    LOG(DEBUG, LOG_TAG) << "Disconnected\n";
}


std::string ClientConnectionWs::getMacAddress()
{
    std::string mac =
#ifndef WINDOWS
        ::getMacAddress(getWs().next_layer().native_handle());
#else
        ::getMacAddress(getWs().next_layer().local_endpoint().address().to_string());
#endif
    if (mac.empty())
        mac = "00:00:00:00:00:00";
    LOG(INFO, LOG_TAG) << "My MAC: \"" << mac << "\", socket: " << getWs().next_layer().native_handle() << "\n";
    return mac;
}


void ClientConnectionWs::getNextMessage(const MessageHandler<msg::BaseMessage>& handler)
{
    getWs().async_read(buffer_, [this, handler](beast::error_code ec, std::size_t bytes_transferred) mutable
    {
        tv now;
        LOG(TRACE, LOG_TAG) << "on_read_ws, ec: " << ec << ", bytes_transferred: " << bytes_transferred << "\n";

        // This indicates that the session was closed
        if (ec == websocket::error::closed)
        {
            if (handler)
                handler(ec, nullptr);
            return;
        }

        if (ec)
        {
            LOG(ERROR, LOG_TAG) << "ControlSessionWebsocket::on_read_ws error: " << ec.message() << "\n";
            if (handler)
                handler(ec, nullptr);
            return;
        }

        buffer_.consume(bytes_transferred);

        auto* data = static_cast<char*>(buffer_.data().data());
        base_message_.deserialize(data);

        base_message_.received = now;

        auto response = msg::factory::createMessage(base_message_, data + base_msg_size_);
        if (!response)
            LOG(WARNING, LOG_TAG) << "Failed to deserialize message of type: " << base_message_.type << "\n";
        else
            LOG(TRACE, LOG_TAG) << "getNextMessage: " << response->type << ", size: " << response->size << ", id: " << response->id
                                << ", refers: " << response->refersTo << "\n";

        messageReceived(std::move(response), handler);
    });
}


boost::system::error_code ClientConnectionWs::doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint)
{
    boost::system::error_code ec;
    getWs().binary(true);
    getWs().next_layer().connect(endpoint, ec);
    if (ec.failed())
        return ec;

    // Set suggested timeout settings for the websocket
    getWs().set_option(websocket::stream_base::timeout::suggested(beast::role_type::client));

    // Set a decorator to change the User-Agent of the handshake
    getWs().set_option(websocket::stream_base::decorator([](websocket::request_type& req) { req.set(http::field::user_agent, WS_CLIENT_NAME); }));

    // Perform the websocket handshake
    getWs().handshake(server_.uri.host + ":" + std::to_string(server_.uri.port.value_or(0)), "/stream", ec);
    return ec;
}


void ClientConnectionWs::write(boost::asio::streambuf& buffer, WriteHandler&& write_handler)
{
    getWs().async_write(boost::asio::buffer(buffer.data()), write_handler); // NOLINT
}



/////////////////////////////// SSL Websockets ////////////////////////////////

#ifdef HAS_OPENSSL

ClientConnectionWss::ClientConnectionWss(boost::asio::io_context& io_context, boost::asio::ssl::context& ssl_context, ClientSettings::Server server)
    : ClientConnection(io_context, std::move(server)), ssl_context_(ssl_context)
{
    getWs();
}


ssl_websocket& ClientConnectionWss::getWs()
{
    // Looks like the websocket must be recreated after disconnect:
    // https://github.com/boostorg/beast/issues/2409#issuecomment-1103685782

    std::lock_guard lock(ws_mutex_);
    if (ssl_ws_.has_value())
        return ssl_ws_.value();

    ssl_ws_.emplace(strand_, ssl_context_);
    if (server_.server_certificate.has_value())
    {
        ssl_ws_->next_layer().set_verify_mode(boost::asio::ssl::verify_peer);
        ssl_ws_->next_layer().set_verify_callback([](bool preverified, boost::asio::ssl::verify_context& ctx)
        {
            // The verify callback can be used to check whether the certificate that is
            // being presented is valid for the peer. For example, RFC 2818 describes
            // the steps involved in doing this for HTTPS. Consult the OpenSSL
            // documentation for more details. Note that the callback is called once
            // for each certificate in the certificate chain, starting from the root
            // certificate authority.

            // In this example we will simply print the certificate's subject name.
            std::array<char, 256> subject_name;
            X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
            X509_NAME_oneline(X509_get_subject_name(cert), subject_name.data(), static_cast<int>(subject_name.size()));
            LOG(INFO, LOG_TAG) << "Verifying cert: '" << subject_name.data() << "', pre verified: " << preverified << "\n";

            return preverified;
        });
    }
    return ssl_ws_.value();
}


ClientConnectionWss::~ClientConnectionWss()
{
    disconnect(); // NOLINT
}


void ClientConnectionWss::disconnect()
{
    LOG(DEBUG, LOG_TAG) << "Disconnecting\n";
    boost::system::error_code ec;

    if (getWs().is_open())
        getWs().close(websocket::close_code::normal, ec);
    // if (ec)
    //     LOG(ERROR, LOG_TAG) << "Error in socket close: " << ec.message() << "\n";
    if (getWs().next_layer().lowest_layer().is_open())
    {
        getWs().next_layer().lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
        getWs().next_layer().lowest_layer().close(ec);
    }

    cancelRequests();
    ssl_ws_ = std::nullopt;
    LOG(DEBUG, LOG_TAG) << "Disconnected\n";
}


std::string ClientConnectionWss::getMacAddress()
{
    std::string mac =
#ifndef WINDOWS
        ::getMacAddress(getWs().next_layer().lowest_layer().native_handle());
#else
        ::getMacAddress(getWs().next_layer().lowest_layer().local_endpoint().address().to_string());
#endif
    if (mac.empty())
        mac = "00:00:00:00:00:00";
    LOG(INFO, LOG_TAG) << "My MAC: \"" << mac << "\", socket: " << getWs().next_layer().lowest_layer().native_handle() << "\n";
    return mac;
}


void ClientConnectionWss::getNextMessage(const MessageHandler<msg::BaseMessage>& handler)
{
    getWs().async_read(buffer_, [this, handler](beast::error_code ec, std::size_t bytes_transferred) mutable
    {
        tv now;
        LOG(TRACE, LOG_TAG) << "on_read_ws, ec: " << ec << ", bytes_transferred: " << bytes_transferred << "\n";

        // This indicates that the session was closed
        if (ec == websocket::error::closed)
        {
            if (handler)
                handler(ec, nullptr);
            return;
        }

        if (ec)
        {
            LOG(ERROR, LOG_TAG) << "ControlSessionWebsocket::on_read_ws error: " << ec.message() << "\n";
            if (handler)
                handler(ec, nullptr);
            return;
        }

        buffer_.consume(bytes_transferred);

        auto* data = static_cast<char*>(buffer_.data().data());
        base_message_.deserialize(data);

        base_message_.received = now;

        auto response = msg::factory::createMessage(base_message_, data + base_msg_size_);
        if (!response)
            LOG(WARNING, LOG_TAG) << "Failed to deserialize message of type: " << base_message_.type << "\n";
        else
            LOG(TRACE, LOG_TAG) << "getNextMessage: " << response->type << ", size: " << response->size << ", id: " << response->id
                                << ", refers: " << response->refersTo << "\n";

        messageReceived(std::move(response), handler);
    });
}


boost::system::error_code ClientConnectionWss::doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint)
{
    boost::system::error_code ec;
    getWs().binary(true);
    beast::get_lowest_layer(getWs()).connect(endpoint, ec);
    if (ec.failed())
        return ec;

    // Set a timeout on the operation
    // beast::get_lowest_layer(getWs()).expires_after(std::chrono::seconds(30));

    // Set suggested timeout settings for the websocket
    getWs().set_option(websocket::stream_base::timeout::suggested(beast::role_type::client));

    // Set SNI Hostname (many hosts need this to handshake successfully)
    if (!SSL_set_tlsext_host_name(getWs().next_layer().native_handle(), server_.uri.host.c_str()))
    {
        LOG(ERROR, LOG_TAG) << "Failed to set SNI Hostname\n";
        return {static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
    }

    // Perform the SSL handshake
    getWs().next_layer().handshake(boost::asio::ssl::stream_base::client, ec);
    if (ec.failed())
        return ec;

    // Set a decorator to change the User-Agent of the handshake
    getWs().set_option(websocket::stream_base::decorator([](websocket::request_type& req) { req.set(http::field::user_agent, WS_CLIENT_NAME); }));

    // Perform the websocket handshake
    getWs().handshake(server_.uri.host + ":" + std::to_string(server_.uri.port.value_or(0)), "/stream", ec);

    return ec;
}


void ClientConnectionWss::write(boost::asio::streambuf& buffer, WriteHandler&& write_handler)
{
    getWs().async_write(boost::asio::buffer(buffer.data()), write_handler);
}

#endif // HAS_OPENSSL