@@ -45,16 +45,43 @@ NetworkConnectionState ConnectionHandler::check()
4545 if ((now - _lastConnectionTickTime) > connectionTickTimeInterval)
4646 {
4747 _lastConnectionTickTime = now;
48+ NetworkConnectionState next_net_connection_state = _current_net_connection_state;
4849
50+ /* While the state machine is implemented here, the concrete implementation of the
51+ * states is done in the derived connection handlers.
52+ */
4953 switch (_current_net_connection_state)
5054 {
51- case NetworkConnectionState::INIT: _current_net_connection_state = update_handleInit (); break ;
52- case NetworkConnectionState::CONNECTING: _current_net_connection_state = update_handleConnecting (); break ;
53- case NetworkConnectionState::CONNECTED: _current_net_connection_state = update_handleConnected (); break ;
54- case NetworkConnectionState::DISCONNECTING: _current_net_connection_state = update_handleDisconnecting (); break ;
55- case NetworkConnectionState::DISCONNECTED: _current_net_connection_state = update_handleDisconnected (); break ;
56- case NetworkConnectionState::ERROR: break ;
57- case NetworkConnectionState::CLOSED: break ;
55+ case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break ;
56+ case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break ;
57+ case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break ;
58+ case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting (); break ;
59+ case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break ;
60+ case NetworkConnectionState::ERROR: break ;
61+ case NetworkConnectionState::CLOSED: break ;
62+ }
63+
64+ /* Here we are determining whether a state transition from one state to the next has
65+ * occurred - and if it has, we call eventually registered callbacks.
66+ */
67+ if (next_net_connection_state != _current_net_connection_state)
68+ {
69+ /* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */
70+ if (next_net_connection_state == NetworkConnectionState::CONNECTED)
71+ {
72+ if (_on_connect_event_callback) _on_connect_event_callback ();
73+ }
74+ if (next_net_connection_state == NetworkConnectionState::DISCONNECTED)
75+ {
76+ if (_on_disconnect_event_callback) _on_disconnect_event_callback ();
77+ }
78+ if (next_net_connection_state == NetworkConnectionState::ERROR)
79+ {
80+ if (_on_error_event_callback) _on_error_event_callback ();
81+ }
82+
83+ /* Assign new state to the member variable holding the state */
84+ _current_net_connection_state = next_net_connection_state;
5885 }
5986 }
6087
@@ -95,17 +122,3 @@ void ConnectionHandler::addDisconnectCallback(OnNetworkEventCallback callback) {
95122void ConnectionHandler::addErrorCallback (OnNetworkEventCallback callback) {
96123 _on_error_event_callback = callback;
97124}
98-
99- /* *****************************************************************************
100- PRIVATE MEMBER FUNCTIONS
101- ******************************************************************************/
102-
103- void ConnectionHandler::execCallback (NetworkConnectionEvent const event)
104- {
105- switch (event)
106- {
107- case NetworkConnectionEvent::CONNECTED: if (_on_connect_event_callback) _on_connect_event_callback (); break ;
108- case NetworkConnectionEvent::DISCONNECTED: if (_on_disconnect_event_callback) _on_disconnect_event_callback (); break ;
109- case NetworkConnectionEvent::ERROR: if (_on_error_event_callback) _on_error_event_callback (); break ;
110- }
111- }
0 commit comments