Skip to content

Commit 388d45e

Browse files
committed
Make it possible to specify host (name or IP) of WFB-API stats server
Some people run WFB on a different host than pixelpilot
1 parent 5854acb commit 388d45e

File tree

3 files changed

+62
-35
lines changed

3 files changed

+62
-35
lines changed

src/main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ void printHelp() {
680680
" --wfb-api-port - Port of wfb-server for cli statistics. (Default: 8003)\n"
681681
" Use \"0\" to disable this stats\n"
682682
"\n"
683+
" --wfb-api-host - Host or IP of wfb-server for cli statistics. (Default: 127.0.0.1)\n"
684+
"\n"
683685
" --version - Show program version\n"
684686
"\n", APP_VERSION_MAJOR, APP_VERSION_MINOR
685687
);
@@ -692,12 +694,13 @@ int main(int argc, char **argv)
692694
int ret;
693695
int i, j;
694696
int mavlink_thread = 0;
695-
int dvr_autostart = 0;
696697
int print_modelist = 0;
698+
int dvr_autostart = 0;
699+
bool dvr_filenames_with_sequence = false;
697700
int video_framerate = -1;
698701
int mp4_fragmentation_mode = 0;
699-
bool dvr_filenames_with_sequence = false;
700702
uint16_t wfb_port = 8003;
703+
const char *wfb_api_host = "127.0.0.1";
701704
uint16_t mode_width = 0;
702705
uint16_t mode_height = 0;
703706
uint32_t mode_vrefresh = 0;
@@ -853,6 +856,11 @@ int main(int argc, char **argv)
853856
continue;
854857
}
855858

859+
__OnArgument("--wfb-api-host") {
860+
wfb_api_host = const_cast<char *>(__ArgValue);
861+
continue;
862+
}
863+
856864
__OnArgument("--version") {
857865
printf("PixelPilot Rockchip %d.%d\n", APP_VERSION_MAJOR, APP_VERSION_MINOR);
858866
return 0;
@@ -1005,6 +1013,7 @@ int main(int argc, char **argv)
10051013
if (wfb_port) {
10061014
wfb_thread_params *wfb_args = (wfb_thread_params *)malloc(sizeof *wfb_args);
10071015
wfb_args->port = wfb_port;
1016+
wfb_args->host = wfb_api_host;
10081017
ret = pthread_create(&tid_wfbcli, NULL, __WFB_CLI_THREAD__, wfb_args);
10091018
assert(!ret);
10101019
}

src/wfbcli.cpp

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <arpa/inet.h>
66
#include <netinet/in.h>
7+
#include <netdb.h>
78
#include <pthread.h>
89
#include <string>
910
#include <sys/socket.h>
@@ -24,7 +25,6 @@ extern "C" {
2425
#include "osd.h"
2526
}
2627

27-
#define SERVER_IP "127.0.0.1"
2828
#define BUFFER_SIZE 10 * 1024
2929

3030
int wfb_thread_signal = 0;
@@ -289,44 +289,61 @@ void handle_server_connection(int sock) {
289289
}
290290
}
291291

292-
int reconnect_to_server(int port) {
293-
while (!wfb_thread_signal) {
294-
SPDLOG_DEBUG("Attempting to connect to WFB API server...");
295-
296-
int sock = socket(AF_INET, SOCK_STREAM, 0);
297-
if (sock < 0) {
298-
perror("Socket creation failed");
299-
} else {
300-
struct sockaddr_in server_address;
301-
server_address.sin_family = AF_INET;
302-
server_address.sin_port = htons(port);
303-
304-
if (inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr) > 0) {
305-
if (connect(sock, (struct sockaddr *)&server_address, sizeof(server_address)) == 0) {
306-
SPDLOG_DEBUG("Successfully connected to WFB API server.");
307-
return sock;
308-
} else {
309-
SPDLOG_ERROR("Connection failed");
310-
}
311-
} else {
312-
SPDLOG_ERROR("Invalid address/Address not supported");
313-
}
314-
315-
close(sock); // Clean up the socket if connection fails
316-
}
317-
318-
SPDLOG_WARN("Reconnection failed. Retrying in 1 second");
319-
std::this_thread::sleep_for(std::chrono::seconds(1));
320-
}
321-
return -1;
292+
int reconnect_to_server(const char *host, int port) {
293+
struct addrinfo hints, *res = nullptr, *p;
294+
int status, sock;
295+
// Clear hints structure
296+
memset(&hints, 0, sizeof(hints));
297+
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
298+
hints.ai_socktype = SOCK_STREAM; // TCP
299+
300+
while (!wfb_thread_signal) {
301+
SPDLOG_DEBUG("wfb-cli attempting to connect to API server...");
302+
// Get address info
303+
if ((status = getaddrinfo(host, std::to_string(port).c_str(), &hints, &res)) != 0) {
304+
SPDLOG_ERROR("wfb-cli incorrect host {} or port {}. Error: {}",
305+
host, port, gai_strerror(status));
306+
std::this_thread::sleep_for(std::chrono::seconds(1));
307+
continue;
308+
}
309+
// Loop through results and try to connect
310+
for (p = res; p != nullptr; p = p->ai_next) {
311+
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
312+
if (sock < 0) {
313+
SPDLOG_ERROR("wfb-cli socket creation failed");
314+
continue;
315+
}
316+
if (connect(sock, p->ai_addr, p->ai_addrlen) < 0) {
317+
char ip_str[INET6_ADDRSTRLEN];
318+
if (p->ai_family == AF_INET) {
319+
struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
320+
inet_ntop(AF_INET, &ipv4->sin_addr, ip_str, sizeof(ip_str));
321+
} else {
322+
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr;
323+
inet_ntop(AF_INET6, &ipv6->sin6_addr, ip_str, sizeof(ip_str));
324+
}
325+
SPDLOG_ERROR("wfb-cli connection to {}:{} ({}) failed", host, port, ip_str);
326+
close(sock); // Clean up the socket if connection fails
327+
continue;
328+
}
329+
SPDLOG_DEBUG("wfb-cli successfully connected to API server.");
330+
freeaddrinfo(res);
331+
return sock; // success
332+
}
333+
freeaddrinfo(res);
334+
res = nullptr;
335+
SPDLOG_WARN("Reconnection failed. Retrying in 1 second");
336+
std::this_thread::sleep_for(std::chrono::seconds(1));
337+
}
338+
return -1;
322339
}
323340

324341
void *__WFB_CLI_THREAD__(void *param) {
325342
wfb_thread_params *p = (wfb_thread_params *)param;
326343
pthread_setname_np(pthread_self(), "__WFB_CLI");
327344

328345
while (!wfb_thread_signal) {
329-
int sock = reconnect_to_server(p->port);
346+
int sock = reconnect_to_server(p->host, p->port);
330347
handle_server_connection(sock);
331348
// If we return from handle_server_connection, the server is disconnected
332349
}

src/wfbcli.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
extern int wfb_thread_signal;
55

66
typedef struct {
7-
int port;
7+
int port;
8+
const char *host;
89
} wfb_thread_params;
910

1011
void *__WFB_CLI_THREAD__(void *param);

0 commit comments

Comments
 (0)