Skip to content

Commit 21751c5

Browse files
committed
Improve server socket handling and add signal handling for graceful shutdown
1 parent bdffc8a commit 21751c5

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

include/vive_ros2/server.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/socket.h>
88
#include <netinet/in.h>
99
#include <signal.h>
10+
#include <csignal>
1011

1112
#include "json.hpp"
1213
#include "VRUtils.hpp"
@@ -25,12 +26,21 @@ class Server {
2526

2627
json prepareData();
2728

29+
static void signalHandler(int signum) {
30+
std::cout << "Interrupt signal (" << signum << ") received.\n";
31+
exit(signum); // terminate program
32+
}
33+
2834
public:
2935
Server(int port, std::mutex &mutex, std::condition_variable &cv, VRControllerData &data);
3036
~Server();
3137

3238
void start();
3339
static std::string getCurrentTimeWithMilliseconds();
40+
static void setupSignalHandlers() {
41+
signal(SIGINT, signalHandler);
42+
signal(SIGTERM, signalHandler);
43+
}
3444
};
3545

3646
#endif // SERVER_HPP

src/server.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Server::Server(int port, std::mutex &mutex, std::condition_variable &cv, VRContr
1414
exit(EXIT_FAILURE);
1515
}
1616

17-
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
17+
int reuse = 1;
18+
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &reuse, sizeof(opt))) {
1819
perror("setsockopt");
1920
exit(EXIT_FAILURE);
2021
}
@@ -29,7 +30,11 @@ Server::Server(int port, std::mutex &mutex, std::condition_variable &cv, VRContr
2930
}
3031
}
3132
Server::~Server() {
32-
close(server_fd);
33+
if (server_fd != -1) {
34+
shutdown(server_fd, SHUT_RDWR);
35+
close(server_fd);
36+
server_fd = -1;
37+
}
3338
}
3439

3540
void Server::start() {
@@ -58,7 +63,7 @@ void Server::start() {
5863
close(new_socket);
5964
break; // Exit the inner loop to wait for a new connection
6065
}
61-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
66+
std::this_thread::sleep_for(std::chrono::milliseconds(2)); // 500Hz
6267
}
6368
}
6469
}

src/vive_input.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ViveInput {
3232
std::chrono::steady_clock::time_point prev_time;
3333
bool first_run = true;
3434
// const float velocity_threshold = 2.5f;
35-
const float distance_threshold = 0.05f;
35+
const float distance_threshold = 0.1f;
3636

3737
};
3838

@@ -133,6 +133,10 @@ void ViveInput::runVR() {
133133
VRUtils::HapticFeedback(pHMD, i, vibrationDuration);
134134
previousStep = currentStep;
135135
}
136+
// haptic feedback after trigger value pass 0.6
137+
// if (local_data.trigger == 0.6) {
138+
// VRUtils::HapticFeedback(pHMD, i, 100);
139+
// }
136140

137141
// Check if the input data is reasonable
138142
auto current_time = std::chrono::steady_clock::now();
@@ -192,7 +196,7 @@ void ViveInput::runVR() {
192196
}
193197
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // ~20Hz
194198
} else {
195-
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // ~50Hz
199+
std::this_thread::sleep_for(std::chrono::milliseconds(5)); // ~200Hz
196200
lastLogTime = currentTime;
197201
}
198202
}
@@ -222,6 +226,8 @@ bool ViveInput::shutdownVR() {
222226
}
223227

224228
int main(int argc, char **argv) {
229+
Server::setupSignalHandlers();
230+
225231
std::mutex data_mutex;
226232
std::condition_variable data_cv;
227233
VRControllerData shared_data;
@@ -233,5 +239,7 @@ int main(int argc, char **argv) {
233239
ViveInput vive_input(data_mutex, data_cv, shared_data);
234240
vive_input.runVR();
235241

242+
serverThread.join(); // Wait for the server thread to finish
243+
236244
return 0;
237245
}

0 commit comments

Comments
 (0)