10
10
#include < boost/redis/adapter/adapt.hpp>
11
11
#include < boost/redis/adapter/any_adapter.hpp>
12
12
#include < boost/redis/config.hpp>
13
- #include < boost/redis/detail/connector.hpp>
14
13
#include < boost/redis/detail/health_checker.hpp>
15
14
#include < boost/redis/detail/helper.hpp>
16
15
#include < boost/redis/detail/multiplexer.hpp>
17
- #include < boost/redis/detail/resolver .hpp>
16
+ #include < boost/redis/detail/redis_stream .hpp>
18
17
#include < boost/redis/detail/resp3_handshaker.hpp>
19
18
#include < boost/redis/error.hpp>
20
19
#include < boost/redis/logger.hpp>
48
47
#include < array>
49
48
#include < chrono>
50
49
#include < cstddef>
51
- #include < deque>
52
- #include < functional>
53
50
#include < memory>
51
+ #include < utility>
54
52
55
53
namespace boost ::redis {
56
54
namespace detail {
@@ -192,19 +190,11 @@ struct writer_op {
192
190
BOOST_ASIO_CORO_REENTER (coro) for (;;)
193
191
{
194
192
while (conn_->mpx_ .prepare_write () != 0 ) {
195
- if (conn_->use_ssl ()) {
196
- BOOST_ASIO_CORO_YIELD
197
- asio::async_write (
198
- conn_->next_layer (),
199
- asio::buffer (conn_->mpx_ .get_write_buffer ()),
200
- std::move (self));
201
- } else {
202
- BOOST_ASIO_CORO_YIELD
203
- asio::async_write (
204
- conn_->next_layer ().next_layer (),
205
- asio::buffer (conn_->mpx_ .get_write_buffer ()),
206
- std::move (self));
207
- }
193
+ BOOST_ASIO_CORO_YIELD
194
+ asio::async_write (
195
+ conn_->stream_ ,
196
+ asio::buffer (conn_->mpx_ .get_write_buffer ()),
197
+ std::move (self));
208
198
209
199
logger_.on_write (ec, conn_->mpx_ .get_write_buffer ());
210
200
@@ -262,21 +252,12 @@ struct reader_op {
262
252
BOOST_ASIO_CORO_REENTER (coro) for (;;)
263
253
{
264
254
// Appends some data to the buffer if necessary.
265
- if (conn_->use_ssl ()) {
266
- BOOST_ASIO_CORO_YIELD
267
- async_append_some (
268
- conn_->next_layer (),
269
- dyn_buffer_type{conn_->mpx_ .get_read_buffer (), conn_->cfg_ .max_read_size },
270
- conn_->mpx_ .get_parser ().get_suggested_buffer_growth (buffer_growth_hint),
271
- std::move (self));
272
- } else {
273
- BOOST_ASIO_CORO_YIELD
274
- async_append_some (
275
- conn_->next_layer ().next_layer (),
276
- dyn_buffer_type{conn_->mpx_ .get_read_buffer (), conn_->cfg_ .max_read_size },
277
- conn_->mpx_ .get_parser ().get_suggested_buffer_growth (buffer_growth_hint),
278
- std::move (self));
279
- }
255
+ BOOST_ASIO_CORO_YIELD
256
+ async_append_some (
257
+ conn_->stream_ ,
258
+ dyn_buffer_type{conn_->mpx_ .get_read_buffer (), conn_->cfg_ .max_read_size },
259
+ conn_->mpx_ .get_parser ().get_suggested_buffer_growth (buffer_growth_hint),
260
+ std::move (self));
280
261
281
262
logger_.on_read (ec, n);
282
263
@@ -350,6 +331,12 @@ class run_op {
350
331
, logger_{l}
351
332
{ }
352
333
334
+ template <class Self >
335
+ void operator ()(Self& self, system::error_code ec)
336
+ {
337
+ (*this )(self, order_t {}, ec);
338
+ }
339
+
353
340
template <class Self >
354
341
void operator ()(
355
342
Self& self,
@@ -362,45 +349,16 @@ class run_op {
362
349
{
363
350
BOOST_ASIO_CORO_REENTER (coro_) for (;;)
364
351
{
352
+ // Try to connect
365
353
BOOST_ASIO_CORO_YIELD
366
- conn_->resv_ .async_resolve (asio::prepend (std::move (self), order_t {}));
367
-
368
- logger_.on_resolve (ec0, conn_->resv_ .results ());
354
+ conn_->stream_ .async_connect (&conn_->cfg_ , logger_, std::move (self));
369
355
356
+ // If we failed, try again
370
357
if (ec0) {
371
358
self.complete (ec0);
372
359
return ;
373
360
}
374
361
375
- BOOST_ASIO_CORO_YIELD
376
- conn_->ctor_ .async_connect (
377
- conn_->next_layer ().next_layer (),
378
- conn_->resv_ .results (),
379
- asio::prepend (std::move (self), order_t {}));
380
-
381
- logger_.on_connect (ec0, conn_->ctor_ .endpoint ());
382
-
383
- if (ec0) {
384
- self.complete (ec0);
385
- return ;
386
- }
387
-
388
- if (conn_->use_ssl ()) {
389
- BOOST_ASIO_CORO_YIELD
390
- conn_->next_layer ().async_handshake (
391
- asio::ssl::stream_base::client,
392
- asio::prepend (
393
- asio::cancel_after (conn_->cfg_ .ssl_handshake_timeout , std::move (self)),
394
- order_t {}));
395
-
396
- logger_.on_ssl_handshake (ec0);
397
-
398
- if (ec0) {
399
- self.complete (ec0);
400
- return ;
401
- }
402
- }
403
-
404
362
conn_->mpx_ .reset ();
405
363
406
364
// Note: Order is important here because the writer might
@@ -459,8 +417,6 @@ class run_op {
459
417
self.complete (asio::error::operation_aborted);
460
418
return ;
461
419
}
462
-
463
- conn_->reset_stream ();
464
420
}
465
421
}
466
422
};
@@ -506,12 +462,10 @@ class basic_connection {
506
462
explicit basic_connection (
507
463
executor_type ex,
508
464
asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client})
509
- : ctx_{std::move (ctx)}
510
- , stream_{std::make_unique<next_layer_type>(ex, ctx_)}
465
+ : stream_{ex, std::move (ctx)}
511
466
, writer_timer_{ex}
512
467
, reconnect_timer_{ex}
513
468
, receive_channel_{ex, 256 }
514
- , resv_{ex}
515
469
, health_checker_{ex}
516
470
{
517
471
set_receive_response (ignore);
@@ -568,8 +522,6 @@ class basic_connection {
568
522
auto async_run (config const & cfg = {}, Logger l = Logger{}, CompletionToken&& token = {})
569
523
{
570
524
cfg_ = cfg;
571
- resv_.set_config (cfg);
572
- ctor_.set_config (cfg);
573
525
health_checker_.set_config (cfg);
574
526
handshaker_.set_config (cfg);
575
527
l.set_prefix (cfg.log_prefix );
@@ -711,7 +663,7 @@ class basic_connection {
711
663
void cancel (operation op = operation::all)
712
664
{
713
665
switch (op) {
714
- case operation::resolve: resv_. cancel (); break ;
666
+ case operation::resolve: stream_. cancel_resolve (); break ;
715
667
case operation::exec: mpx_.cancel_waiting (); break ;
716
668
case operation::reconnection:
717
669
cfg_.reconnect_wait_interval = std::chrono::seconds::zero ();
@@ -720,7 +672,7 @@ class basic_connection {
720
672
case operation::receive: receive_channel_.cancel (); break ;
721
673
case operation::health_check: health_checker_.cancel (); break ;
722
674
case operation::all:
723
- resv_. cancel ();
675
+ stream_. cancel_resolve ();
724
676
cfg_.reconnect_wait_interval = std::chrono::seconds::zero ();
725
677
health_checker_.cancel ();
726
678
cancel_run (); // run
@@ -740,25 +692,28 @@ class basic_connection {
740
692
}
741
693
742
694
// / Returns the ssl context.
743
- auto const & get_ssl_context () const noexcept { return ctx_; }
695
+ BOOST_DEPRECATED (
696
+ " ssl::context has no const methods, so this function should not be called. Set up any "
697
+ " required TLS configuration before passing the ssl::context to the connection's constructor." )
698
+ auto const & get_ssl_context () const noexcept { return stream_.get_ssl_context (); }
744
699
745
700
// / Resets the underlying stream.
746
- void reset_stream ()
747
- {
748
- stream_ = std::make_unique<next_layer_type>(writer_timer_. get_executor (), ctx_);
749
- }
701
+ BOOST_DEPRECATED (
702
+ " This function is no longer necessary and is currently a no-op. connection resets the stream "
703
+ " internally as required. This function will be removed in subsequent releases " )
704
+ void reset_stream () { }
750
705
751
706
// / Returns a reference to the next layer.
752
707
BOOST_DEPRECATED (
753
708
" Accessing the underlying stream is deprecated and will be removed in the next release. Use "
754
709
" the other member functions to interact with the connection." )
755
- auto & next_layer () noexcept { return * stream_; }
710
+ auto & next_layer () noexcept { return stream_. next_layer () ; }
756
711
757
712
// / Returns a const reference to the next layer.
758
713
BOOST_DEPRECATED (
759
714
" Accessing the underlying stream is deprecated and will be removed in the next release. Use "
760
715
" the other member functions to interact with the connection." )
761
- auto const & next_layer () const noexcept { return * stream_; }
716
+ auto const & next_layer () const noexcept { return stream_. next_layer () ; }
762
717
763
718
// / Sets the response object of `async_receive` operations.
764
719
template <class Response >
@@ -778,15 +733,14 @@ class basic_connection {
778
733
using receive_channel_type = asio::experimental::channel<
779
734
executor_type,
780
735
void (system::error_code, std::size_t )>;
781
- using resolver_type = detail::resolver<Executor>;
782
736
using health_checker_type = detail::health_checker<Executor>;
783
737
using resp3_handshaker_type = detail::resp3_handshaker<executor_type>;
784
738
785
739
auto use_ssl () const noexcept { return cfg_.use_ssl ; }
786
740
787
741
void cancel_run ()
788
742
{
789
- close ();
743
+ stream_. close ();
790
744
writer_timer_.cancel ();
791
745
receive_channel_.cancel ();
792
746
mpx_.cancel_on_conn_lost ();
@@ -815,30 +769,18 @@ class basic_connection {
815
769
writer_timer_);
816
770
}
817
771
818
- void close ()
819
- {
820
- if (stream_->next_layer ().is_open ()) {
821
- system::error_code ec;
822
- stream_->next_layer ().close (ec);
823
- }
824
- }
825
-
826
- auto is_open () const noexcept { return stream_->next_layer ().is_open (); }
827
- auto & lowest_layer () noexcept { return stream_->lowest_layer (); }
772
+ auto is_open () const noexcept { return stream_.is_open (); }
828
773
829
774
[[nodiscard]] bool trigger_write () const noexcept { return is_open () && !mpx_.is_writing (); }
830
775
831
- asio::ssl::context ctx_;
832
- std::unique_ptr<next_layer_type> stream_;
776
+ detail::redis_stream<Executor> stream_;
833
777
834
778
// Notice we use a timer to simulate a condition-variable. It is
835
779
// also more suitable than a channel and the notify operation does
836
780
// not suspend.
837
781
timer_type writer_timer_;
838
782
timer_type reconnect_timer_; // to wait the reconnection period
839
783
receive_channel_type receive_channel_;
840
- resolver_type resv_;
841
- detail::connector ctor_;
842
784
health_checker_type health_checker_;
843
785
resp3_handshaker_type handshaker_;
844
786
@@ -937,7 +879,10 @@ class connection {
937
879
auto const & next_layer () const noexcept { return impl_.next_layer (); }
938
880
939
881
// / Calls `boost::redis::basic_connection::reset_stream`.
940
- void reset_stream () { impl_.reset_stream (); }
882
+ BOOST_DEPRECATED (
883
+ " This function is no longer necessary and is currently a no-op. connection resets the stream "
884
+ " internally as required. This function will be removed in subsequent releases" )
885
+ void reset_stream () { }
941
886
942
887
// / Sets the response object of `async_receive` operations.
943
888
template <class Response >
@@ -950,6 +895,9 @@ class connection {
950
895
usage get_usage () const noexcept { return impl_.get_usage (); }
951
896
952
897
// / Returns the ssl context.
898
+ BOOST_DEPRECATED (
899
+ " ssl::context has no const methods, so this function should not be called. Set up any "
900
+ " required TLS configuration before passing the ssl::context to the connection's constructor." )
953
901
auto const & get_ssl_context () const noexcept { return impl_.get_ssl_context (); }
954
902
955
903
private:
0 commit comments