Skip to content

Commit c5b85bc

Browse files
committed
Add auto-reconnection and improved connection handling
- Add automatic reconnection thread with retry logic - Add IsConnected() method for thread-safe connection state checking - Improve connection state management with is_reconnecting_ flag - Add re-advertisement logic in ROSTopic on send/queue failures - Refactor code organization with helper functions - Improve error handling and logging - Add thread-safe connection checks in SendMessage methods - Better handling of connection failures and recovery
1 parent d88ae76 commit c5b85bc

File tree

3 files changed

+471
-217
lines changed

3 files changed

+471
-217
lines changed

include/client/socket_websocket_connection.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,30 @@ namespace rosbridge2cpp{
2626
SocketWebSocketConnection() = default;
2727

2828
~SocketWebSocketConnection() {
29-
std::cout << "WebSocket Connection Destructor called" << std::endl;
29+
std::cout << "[WebSocketConnection] Destructor called" << std::endl;
3030
if (is_connected_) {
3131
Disconnect();
3232
}
3333
terminate_receiver_thread_ = true;
34+
terminate_reconnect_thread_ = true;
3435
if (receiver_thread_set_up_) {
35-
std::cout << "Thread is set up: Calling .join() on it" << std::endl;
3636
receiver_thread_.join();
37-
std::cout << "join() in Connection Destructor done" << std::endl;
3837
} else {
39-
std::cout << "receiverThread hasn't been set up. Skipping join() on it" << std::endl;
38+
std::cout << "[WebSocketConnection] receiverThread hasn't been set up. Skipping join() on it" << std::endl;
39+
}
40+
if (reconnect_thread_set_up_) {
41+
reconnect_thread_.join();
4042
}
4143
}
4244

4345
bool Init(std::string p_ip_addr, int p_port);
4446
bool SendMessage(std::string data);
4547
bool SendMessage(const uint8_t *data, unsigned int length);
4648
std::string GetLastSentMessage() const;
49+
bool IsConnected() const;
4750
int ReceiverThreadFunction();
51+
void ReconnectThreadFunction();
52+
bool AttemptReconnect();
4853
void RegisterIncomingMessageCallback(std::function<void(json&)> fun);
4954
void RegisterIncomingMessageCallback(std::function<void(bson_t&)> fun);
5055
void RegisterErrorCallback(std::function<void(TransportError)> fun);
@@ -69,19 +74,34 @@ namespace rosbridge2cpp{
6974
bool terminate_receiver_thread_ = false;
7075
bool receiver_thread_set_up_ = false;
7176
bool is_connected_ = false;
77+
bool is_reconnecting_ = false;
7278
bool callback_function_defined_ = false;
7379
bool bson_only_mode_ = false;
80+
bool auto_reconnect_ = true;
81+
bool terminate_reconnect_thread_ = false;
82+
std::thread reconnect_thread_;
83+
bool reconnect_thread_set_up_ = false;
7484

7585
std::function<void(json&)> incoming_message_callback_;
7686
std::function<void(bson_t&)> incoming_message_callback_bson_;
7787
std::function<void(TransportError)> error_callback_ = nullptr;
7888

79-
std::mutex connection_mutex_;
89+
mutable std::mutex connection_mutex_;
8090
std::condition_variable connection_cv_;
8191

8292
mutable std::string last_sent_message_;
8393
mutable std::mutex last_message_mutex_;
8494

95+
// Reconnection state tracking
96+
mutable std::mutex reconnect_mutex_;
97+
bool last_send_failed_logged_ = false;
98+
99+
// Helper functions
100+
void RegisterWebSocketHandlers();
101+
void SetupASIOThread();
102+
bool IsConnectedOrReconnecting() const;
103+
104+
// WebSocket event handlers
85105
void on_open(connection_hdl hdl);
86106
void on_close(connection_hdl hdl);
87107
void on_fail(connection_hdl hdl);

0 commit comments

Comments
 (0)