@@ -415,12 +415,11 @@ class connection : public config::socket_type::socket_con_type {
415
415
// TODO: pre-init timeout. Right now no implemented socket policies
416
416
// actually have an asyncronous pre-init
417
417
418
- m_init_handler = callback;
419
-
420
418
socket_con_type::pre_init (
421
419
lib::bind (
422
420
&type::handle_pre_init,
423
421
get_shared (),
422
+ callback,
424
423
lib::placeholders::_1
425
424
)
426
425
);
@@ -465,26 +464,14 @@ class connection : public config::socket_type::socket_con_type {
465
464
m_strand = lib::make_shared<lib::asio::io_service::strand>(
466
465
lib::ref (*io_service));
467
466
}
468
- m_async_read_handler = lib::bind (&type::handle_async_read,
469
- get_shared (), lib::placeholders::_1, lib::placeholders::_2);
470
-
471
- m_async_write_handler = lib::bind (&type::handle_async_write,
472
- get_shared (), lib::placeholders::_1, lib::placeholders::_2);
473
467
474
468
lib::error_code ec = socket_con_type::init_asio (io_service, m_strand,
475
469
m_is_server);
476
470
477
- if (ec) {
478
- // reset the handlers to break the circular reference:
479
- // this->handler->this
480
- lib::clear_function (m_async_read_handler);
481
- lib::clear_function (m_async_write_handler);
482
- }
483
-
484
471
return ec;
485
472
}
486
473
487
- void handle_pre_init (lib::error_code const & ec) {
474
+ void handle_pre_init (init_handler callback, lib::error_code const & ec) {
488
475
if (m_alog.static_test (log ::alevel::devel)) {
489
476
m_alog.write (log ::alevel::devel," asio connection handle pre_init" );
490
477
}
@@ -494,19 +481,19 @@ class connection : public config::socket_type::socket_con_type {
494
481
}
495
482
496
483
if (ec) {
497
- m_init_handler (ec);
484
+ callback (ec);
498
485
}
499
486
500
487
// If we have a proxy set issue a proxy connect, otherwise skip to
501
488
// post_init
502
489
if (!m_proxy.empty ()) {
503
- proxy_write ();
490
+ proxy_write (callback );
504
491
} else {
505
- post_init ();
492
+ post_init (callback );
506
493
}
507
494
}
508
495
509
- void post_init () {
496
+ void post_init (init_handler callback ) {
510
497
if (m_alog.static_test (log ::alevel::devel)) {
511
498
m_alog.write (log ::alevel::devel," asio connection post_init" );
512
499
}
@@ -520,7 +507,7 @@ class connection : public config::socket_type::socket_con_type {
520
507
&type::handle_post_init_timeout,
521
508
get_shared (),
522
509
post_timer,
523
- m_init_handler ,
510
+ callback ,
524
511
lib::placeholders::_1
525
512
)
526
513
);
@@ -531,7 +518,7 @@ class connection : public config::socket_type::socket_con_type {
531
518
&type::handle_post_init,
532
519
get_shared (),
533
520
post_timer,
534
- m_init_handler ,
521
+ callback ,
535
522
lib::placeholders::_1
536
523
)
537
524
);
@@ -607,15 +594,15 @@ class connection : public config::socket_type::socket_con_type {
607
594
callback (ec);
608
595
}
609
596
610
- void proxy_write () {
597
+ void proxy_write (init_handler callback ) {
611
598
if (m_alog.static_test (log ::alevel::devel)) {
612
599
m_alog.write (log ::alevel::devel," asio connection proxy_write" );
613
600
}
614
601
615
602
if (!m_proxy_data) {
616
603
m_elog.write (log ::elevel::library,
617
604
" assertion failed: !m_proxy_data in asio::connection::proxy_write" );
618
- m_init_handler (make_error_code (error::general));
605
+ callback (make_error_code (error::general));
619
606
return ;
620
607
}
621
608
@@ -632,7 +619,7 @@ class connection : public config::socket_type::socket_con_type {
632
619
lib::bind (
633
620
&type::handle_proxy_timeout,
634
621
get_shared (),
635
- m_init_handler ,
622
+ callback ,
636
623
lib::placeholders::_1
637
624
)
638
625
);
@@ -644,7 +631,7 @@ class connection : public config::socket_type::socket_con_type {
644
631
m_bufs,
645
632
m_strand->wrap (lib::bind (
646
633
&type::handle_proxy_write, get_shared (),
647
- m_init_handler ,
634
+ callback ,
648
635
lib::placeholders::_1
649
636
))
650
637
);
@@ -654,7 +641,7 @@ class connection : public config::socket_type::socket_con_type {
654
641
m_bufs,
655
642
lib::bind (
656
643
&type::handle_proxy_write, get_shared (),
657
- m_init_handler ,
644
+ callback ,
658
645
lib::placeholders::_1
659
646
)
660
647
);
@@ -825,15 +812,11 @@ class connection : public config::socket_type::socket_con_type {
825
812
m_proxy_data.reset ();
826
813
827
814
// Continue with post proxy initialization
828
- post_init ();
815
+ post_init (callback );
829
816
}
830
817
}
831
818
832
819
// / read at least num_bytes bytes into buf and then call handler.
833
- /* *
834
- *
835
- *
836
- */
837
820
void async_read_at_least (size_t num_bytes, char *buf, size_t len,
838
821
read_handler handler)
839
822
{
@@ -843,13 +826,6 @@ class connection : public config::socket_type::socket_con_type {
843
826
m_alog.write (log ::alevel::devel,s.str ());
844
827
}
845
828
846
- if (!m_async_read_handler) {
847
- m_alog.write (log ::alevel::devel,
848
- " async_read_at_least called after async_shutdown" );
849
- handler (make_error_code (transport::error::action_after_shutdown),0 );
850
- return ;
851
- }
852
-
853
829
// TODO: safety vs speed ?
854
830
// maybe move into an if devel block
855
831
/* if (num_bytes > len) {
@@ -860,22 +836,19 @@ class connection : public config::socket_type::socket_con_type {
860
836
return;
861
837
}*/
862
838
863
- m_read_handler = handler;
864
-
865
- if (!m_read_handler) {
866
- m_alog.write (log ::alevel::devel,
867
- " asio con async_read_at_least called with bad handler" );
868
- }
869
-
870
839
if (config::enable_multithreading) {
871
840
lib::asio::async_read (
872
841
socket_con_type::get_socket (),
873
842
lib::asio::buffer (buf,len),
874
843
lib::asio::transfer_at_least (num_bytes),
875
- m_strand->wrap (
876
- make_custom_alloc_handler (
877
- m_read_handler_allocator,
878
- m_async_read_handler))
844
+ m_strand->wrap (make_custom_alloc_handler (
845
+ m_read_handler_allocator,
846
+ lib::bind (
847
+ &type::handle_async_read, get_shared (),
848
+ handler,
849
+ lib::placeholders::_1, lib::placeholders::_2
850
+ )
851
+ ))
879
852
);
880
853
} else {
881
854
lib::asio::async_read (
@@ -884,14 +857,18 @@ class connection : public config::socket_type::socket_con_type {
884
857
lib::asio::transfer_at_least (num_bytes),
885
858
make_custom_alloc_handler (
886
859
m_read_handler_allocator,
887
- m_async_read_handler
860
+ lib::bind (
861
+ &type::handle_async_read, get_shared (),
862
+ handler,
863
+ lib::placeholders::_1, lib::placeholders::_2
864
+ )
888
865
)
889
866
);
890
867
}
891
868
892
869
}
893
870
894
- void handle_async_read (lib::asio::error_code const & ec,
871
+ void handle_async_read (read_handler handler, lib::asio::error_code const & ec,
895
872
size_t bytes_transferred)
896
873
{
897
874
m_alog.write (log ::alevel::devel, " asio con handle_async_read" );
@@ -915,10 +892,8 @@ class connection : public config::socket_type::socket_con_type {
915
892
log_err (log ::elevel::info," asio async_read_at_least" ,ec);
916
893
}
917
894
}
918
- if (m_read_handler) {
919
- m_read_handler (tec,bytes_transferred);
920
- // TODO: why does this line break things?
921
- // m_read_handler = _WEBSOCKETPP_NULL_FUNCTION_;
895
+ if (handler) {
896
+ handler (tec,bytes_transferred);
922
897
} else {
923
898
// This can happen in cases where the connection is terminated while
924
899
// the transport is waiting on a read.
@@ -927,70 +902,71 @@ class connection : public config::socket_type::socket_con_type {
927
902
}
928
903
}
929
904
905
+ // / Initiate a potentially asyncronous write of the given buffer
930
906
void async_write (const char * buf, size_t len, write_handler handler) {
931
- if (!m_async_write_handler) {
932
- m_alog.write (log ::alevel::devel,
933
- " async_write (single) called after async_shutdown" );
934
- handler (make_error_code (transport::error::action_after_shutdown));
935
- return ;
936
- }
937
-
938
907
m_bufs.push_back (lib::asio::buffer (buf,len));
939
908
940
- m_write_handler = handler;
941
-
942
909
if (config::enable_multithreading) {
943
910
lib::asio::async_write (
944
911
socket_con_type::get_socket (),
945
912
m_bufs,
946
- m_strand->wrap (
947
- make_custom_alloc_handler (
948
- m_write_handler_allocator,
949
- m_async_write_handler))
913
+ m_strand->wrap (make_custom_alloc_handler (
914
+ m_write_handler_allocator,
915
+ lib::bind (
916
+ &type::handle_async_write, get_shared (),
917
+ handler,
918
+ lib::placeholders::_1, lib::placeholders::_2
919
+ )
920
+ ))
950
921
);
951
922
} else {
952
923
lib::asio::async_write (
953
924
socket_con_type::get_socket (),
954
925
m_bufs,
955
926
make_custom_alloc_handler (
956
927
m_write_handler_allocator,
957
- m_async_write_handler
928
+ lib::bind (
929
+ &type::handle_async_write, get_shared (),
930
+ handler,
931
+ lib::placeholders::_1, lib::placeholders::_2
932
+ )
958
933
)
959
934
);
960
935
}
961
936
}
962
937
938
+ // / Initiate a potentially asyncronous write of the given buffers
963
939
void async_write (std::vector<buffer> const & bufs, write_handler handler) {
964
- if (!m_async_write_handler) {
965
- m_alog.write (log ::alevel::devel,
966
- " async_write (vector) called after async_shutdown" );
967
- handler (make_error_code (transport::error::action_after_shutdown));
968
- return ;
969
- }
970
940
std::vector<buffer>::const_iterator it;
971
941
972
942
for (it = bufs.begin (); it != bufs.end (); ++it) {
973
943
m_bufs.push_back (lib::asio::buffer ((*it).buf ,(*it).len ));
974
944
}
975
945
976
- m_write_handler = handler;
977
-
978
946
if (config::enable_multithreading) {
979
947
lib::asio::async_write (
980
948
socket_con_type::get_socket (),
981
949
m_bufs,
982
- m_strand->wrap (
983
- make_custom_alloc_handler (
984
- m_write_handler_allocator,
985
- m_async_write_handler))
950
+ m_strand->wrap (make_custom_alloc_handler (
951
+ m_write_handler_allocator,
952
+ lib::bind (
953
+ &type::handle_async_write, get_shared (),
954
+ handler,
955
+ lib::placeholders::_1, lib::placeholders::_2
956
+ )
957
+ ))
986
958
);
987
959
} else {
988
960
lib::asio::async_write (
989
961
socket_con_type::get_socket (),
990
962
m_bufs,
991
963
make_custom_alloc_handler (
992
964
m_write_handler_allocator,
993
- m_async_write_handler
965
+ lib::bind (
966
+ &type::handle_async_write, get_shared (),
967
+ handler,
968
+ lib::placeholders::_1, lib::placeholders::_2
969
+ )
994
970
)
995
971
);
996
972
}
@@ -1001,17 +977,15 @@ class connection : public config::socket_type::socket_con_type {
1001
977
* @param ec The status code
1002
978
* @param bytes_transferred The number of bytes read
1003
979
*/
1004
- void handle_async_write (lib::asio::error_code const & ec, size_t ) {
980
+ void handle_async_write (write_handler handler, lib::asio::error_code const & ec, size_t ) {
1005
981
m_bufs.clear ();
1006
982
lib::error_code tec;
1007
983
if (ec) {
1008
984
log_err (log ::elevel::info," asio async_write" ,ec);
1009
985
tec = make_error_code (transport::error::pass_through);
1010
986
}
1011
- if (m_write_handler) {
1012
- m_write_handler (tec);
1013
- // TODO: why does this line break things?
1014
- // m_write_handler = _WEBSOCKETPP_NULL_FUNCTION_;
987
+ if (handler) {
988
+ handler (tec);
1015
989
} else {
1016
990
// This can happen in cases where the connection is terminated while
1017
991
// the transport is waiting on a read.
@@ -1064,16 +1038,6 @@ class connection : public config::socket_type::socket_con_type {
1064
1038
m_alog.write (log ::alevel::devel," asio connection async_shutdown" );
1065
1039
}
1066
1040
1067
- // Reset cached handlers now that we won't be reading or writing anymore
1068
- // These cached handlers store shared pointers to this connection and
1069
- // will leak the connection if not destroyed.
1070
- lib::clear_function (m_async_read_handler);
1071
- lib::clear_function (m_async_write_handler);
1072
- lib::clear_function (m_init_handler);
1073
-
1074
- lib::clear_function (m_read_handler);
1075
- lib::clear_function (m_write_handler);
1076
-
1077
1041
timer_ptr shutdown_timer;
1078
1042
shutdown_timer = set_timer (
1079
1043
config::timeout_socket_shutdown,
@@ -1230,13 +1194,6 @@ class connection : public config::socket_type::socket_con_type {
1230
1194
1231
1195
handler_allocator m_read_handler_allocator;
1232
1196
handler_allocator m_write_handler_allocator;
1233
-
1234
- read_handler m_read_handler;
1235
- write_handler m_write_handler;
1236
- init_handler m_init_handler;
1237
-
1238
- async_read_handler m_async_read_handler;
1239
- async_write_handler m_async_write_handler;
1240
1197
};
1241
1198
1242
1199
0 commit comments