@@ -45,10 +45,8 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
4545 State state () const {return state_;}
4646
4747protected:
48- using Message = WampMessage;
49- using Handler = std::function<void (std::error_code, Message)>;
50- using RxHandler = std::function<void (Message)>;
51- using LogHandler = std::function<void (std::string)>;
48+ using Message = WampMessage;
49+ using Handler = std::function<void (std::error_code, Message)>;
5250
5351 struct RequestOptions
5452 {
@@ -79,7 +77,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
7977 void start ()
8078 {
8179 assert (state_ == State::closed || state_ == State::disconnected);
82- state_ = State::establishing;
80+ setState ( State::establishing) ;
8381
8482 if (!transport_->isStarted ())
8583 {
@@ -106,7 +104,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
106104 {
107105 assert (state_ == State::established);
108106
109- state_ = State::shuttingDown;
107+ setState ( State::shuttingDown) ;
110108 using std::move;
111109 Message msg = { WampMsgType::goodbye,
112110 {0u , move (reason.options ({})), move (reason.uri ({}))} };
@@ -116,7 +114,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
116114 {
117115 if (!ec)
118116 {
119- state_ = State::closed;
117+ setState ( State::closed) ;
120118 abortPending (make_error_code (SessionErrc::sessionEnded));
121119 post (handler, make_error_code (ProtocolErrc::success),
122120 move (reply));
@@ -128,7 +126,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
128126
129127 void close (bool terminating)
130128 {
131- state_ = State::disconnected;
129+ setState ( State::disconnected) ;
132130 abortPending (make_error_code (SessionErrc::sessionEnded), terminating);
133131 transport_->close ();
134132 }
@@ -214,6 +212,9 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
214212 void setTraceHandler (AsyncTask<std::string> handler)
215213 {traceHandler_ = std::move (handler);}
216214
215+ void setStateChangeHandler (AsyncTask<State> handler)
216+ {stateChangeHandler_ = std::move (handler);}
217+
217218 template <typename TFunctor>
218219 void post (TFunctor&& fn)
219220 {
@@ -254,6 +255,16 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
254255 using RequestKey = typename Message::RequestKey;
255256 using RequestMap = std::map<RequestKey, RequestRecord>;
256257
258+ void setState (State state)
259+ {
260+ if (state != state_)
261+ {
262+ state_ = state;
263+ if (stateChangeHandler_)
264+ stateChangeHandler_ (state);
265+ }
266+ }
267+
257268 RequestId sendMessage (Message& msg, Handler&& handler = nullptr ,
258269 RequestOptions opts = {})
259270 {
@@ -412,15 +423,15 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
412423 void processHello (Message&& msg)
413424 {
414425 assert (state_ == State::establishing);
415- state_ = State::established;
426+ setState ( State::established) ;
416427 auto self = this ->shared_from_this ();
417428 post (std::bind (&Peer::onInbound, self, std::move (msg)));
418429 }
419430
420431 void processChallenge (Message&& msg)
421432 {
422433 assert (state_ == State::establishing);
423- state_ = State::authenticating;
434+ setState ( State::authenticating) ;
424435 auto self = this ->shared_from_this ();
425436 post (std::bind (&Peer::onInbound, self, std::move (msg)));
426437 }
@@ -429,7 +440,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
429440 {
430441 assert ((state_ == State::establishing) ||
431442 (state_ == State::authenticating));
432- state_ = State::established;
443+ setState ( State::established) ;
433444 processWampReply ( RequestKey (WampMsgType::hello, msg.requestId ()),
434445 std::move (msg) );
435446 }
@@ -438,7 +449,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
438449 {
439450 assert ((state_ == State::establishing) ||
440451 (state_ == State::authenticating));
441- state_ = State::closed;
452+ setState ( State::closed) ;
442453 processWampReply ( RequestKey (WampMsgType::hello, msg.requestId ()),
443454 std::move (msg) );
444455 }
@@ -447,7 +458,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
447458 {
448459 if (state_ == State::shuttingDown)
449460 {
450- state_ = State::closed;
461+ setState ( State::closed) ;
451462 processWampReply (msg.requestKey (), std::move (msg));
452463 }
453464 else
@@ -458,7 +469,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
458469 Message reply{ WampMsgType::goodbye,
459470 {0u , Object{}, " wamp.error.goodbye_and_out" } };
460471 send (reply);
461- state_ = State::closed;
472+ setState ( State::closed) ;
462473 }
463474 }
464475
@@ -515,7 +526,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
515526
516527 void fail (std::error_code ec)
517528 {
518- state_ = State::failed;
529+ setState ( State::failed) ;
519530 abortPending (ec);
520531 transport_->close ();
521532 }
@@ -557,6 +568,7 @@ class Peer : public std::enable_shared_from_this<Peer<TCodec, TTransport>>
557568 TransportPtr transport_;
558569 AnyExecutor executor_;
559570 AsyncTask<std::string> traceHandler_;
571+ AsyncTask<State> stateChangeHandler_;
560572 State state_ = State::closed;
561573 RequestMap requestMap_;
562574 RequestId nextRequestId_ = 0 ;
0 commit comments