@@ -46,49 +46,69 @@ NetworkConnectionState ConnectionHandler::check()
46
46
if ((now - _lastConnectionTickTime) > connectionTickTimeInterval)
47
47
{
48
48
_lastConnectionTickTime = now;
49
- NetworkConnectionState next_net_connection_state = _current_net_connection_state;
50
49
51
- /* While the state machine is implemented here, the concrete implementation of the
52
- * states is done in the derived connection handlers.
53
- */
54
- switch (_current_net_connection_state)
55
- {
56
- case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break ;
57
- case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break ;
58
- case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break ;
59
- case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting (); break ;
60
- case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break ;
61
- case NetworkConnectionState::ERROR: break ;
62
- case NetworkConnectionState::CLOSED: break ;
63
- }
50
+ NetworkConnectionState old_net_connection_state = _current_net_connection_state;
51
+ NetworkConnectionState next_net_connection_state = updateConnectionState ();
64
52
65
53
/* Here we are determining whether a state transition from one state to the next has
66
54
* occurred - and if it has, we call eventually registered callbacks.
67
55
*/
68
- if (next_net_connection_state != _current_net_connection_state)
69
- {
70
- /* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */
71
- if (next_net_connection_state == NetworkConnectionState::CONNECTED)
72
- {
73
- if (_on_connect_event_callback) _on_connect_event_callback ();
74
- }
75
- if (next_net_connection_state == NetworkConnectionState::DISCONNECTED)
76
- {
77
- if (_on_disconnect_event_callback) _on_disconnect_event_callback ();
78
- }
79
- if (next_net_connection_state == NetworkConnectionState::ERROR)
80
- {
81
- if (_on_error_event_callback) _on_error_event_callback ();
82
- }
83
-
84
- /* Assign new state to the member variable holding the state */
56
+
57
+ if (old_net_connection_state != next_net_connection_state) {
58
+ updateCallback (next_net_connection_state);
59
+
60
+ /* It may happen that the local _current_net_connection_state
61
+ * is not updated by the updateConnectionState() call. This is the case for GenericConnection handler
62
+ * where the call of updateConnectionState() is replaced by the inner ConnectionHandler call
63
+ * that updates its state, but not the outer one. For this reason it is required to perform this call twice
64
+ */
85
65
_current_net_connection_state = next_net_connection_state;
86
66
}
87
67
}
88
68
89
69
return _current_net_connection_state;
90
70
}
91
71
72
+ NetworkConnectionState ConnectionHandler::updateConnectionState () {
73
+ NetworkConnectionState next_net_connection_state = _current_net_connection_state;
74
+
75
+ /* While the state machine is implemented here, the concrete implementation of the
76
+ * states is done in the derived connection handlers.
77
+ */
78
+ switch (_current_net_connection_state)
79
+ {
80
+ case NetworkConnectionState::INIT: next_net_connection_state = update_handleInit (); break ;
81
+ case NetworkConnectionState::CONNECTING: next_net_connection_state = update_handleConnecting (); break ;
82
+ case NetworkConnectionState::CONNECTED: next_net_connection_state = update_handleConnected (); break ;
83
+ case NetworkConnectionState::DISCONNECTING: next_net_connection_state = update_handleDisconnecting (); break ;
84
+ case NetworkConnectionState::DISCONNECTED: next_net_connection_state = update_handleDisconnected (); break ;
85
+ case NetworkConnectionState::ERROR: break ;
86
+ case NetworkConnectionState::CLOSED: break ;
87
+ }
88
+
89
+ /* Assign new state to the member variable holding the state */
90
+ _current_net_connection_state = next_net_connection_state;
91
+
92
+ return next_net_connection_state;
93
+ }
94
+
95
+ void ConnectionHandler::updateCallback (NetworkConnectionState next_net_connection_state) {
96
+
97
+ /* Check the next state to determine the kind of state conversion which has occurred (and call the appropriate callback) */
98
+ if (next_net_connection_state == NetworkConnectionState::CONNECTED)
99
+ {
100
+ if (_on_connect_event_callback) _on_connect_event_callback ();
101
+ }
102
+ if (next_net_connection_state == NetworkConnectionState::DISCONNECTED)
103
+ {
104
+ if (_on_disconnect_event_callback) _on_disconnect_event_callback ();
105
+ }
106
+ if (next_net_connection_state == NetworkConnectionState::ERROR)
107
+ {
108
+ if (_on_error_event_callback) _on_error_event_callback ();
109
+ }
110
+ }
111
+
92
112
void ConnectionHandler::connect ()
93
113
{
94
114
if (_current_net_connection_state != NetworkConnectionState::INIT && _current_net_connection_state != NetworkConnectionState::CONNECTING)
0 commit comments