Skip to content

Commit 3bac201

Browse files
committed
Alpha 1.4 - Client
1 parent e1e5c67 commit 3bac201

File tree

7 files changed

+227
-155
lines changed

7 files changed

+227
-155
lines changed

Client/include/Client.h

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#define CURRENT_PROFILE profiles[current_profile]
4+
#define CURRENT_BUTTOM_M1_LOOP CURRENT_PROFILE.buttons[i-1]
5+
36
#include <sys/socket.h>
47
#include <netinet/in.h>
58
#include <arpa/inet.h>
@@ -11,9 +14,16 @@ using std::string;
1114
extern int sock;
1215
extern int send_result;
1316
extern int bytes_received;
17+
extern int port;
18+
extern int prev_port;
1419

20+
extern bool failed;
1521
extern bool connected;
1622
extern bool kill;
23+
extern bool have_ip;
24+
extern bool have_port;
25+
26+
extern char ip_address[16];
1727

1828
extern string kill_reason;
1929

Client/include/Config.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include <string>
77
#include <random>
8+
#include <atomic>
89

910
#include "../../json.hpp"
1011

@@ -16,17 +17,18 @@ using std::istreambuf_iterator;
1617
using std::random_device;
1718
using std::mt19937;
1819
using std::uniform_int_distribution;
20+
using std::atomic;
1921

2022
using std::filesystem::exists;
2123
using std::filesystem::remove;
2224
using std::filesystem::create_directory;
25+
using std::filesystem::create_directories;
2326

2427
using json = nlohmann::ordered_json;
2528

2629
extern json config;
2730

28-
extern bool have_ip;
29-
extern bool reconfiguring;
31+
extern atomic<bool> reconfiguring;
3032

3133
extern char* homedir;
3234

@@ -50,10 +52,14 @@ extern vector<profile> profiles;
5052
extern const string id_gen();
5153
extern const string rw_UUID();
5254

53-
extern bool rw_ipstore();
55+
extern bool read_ipstore();
56+
extern bool read_portstore();
57+
extern void write_ipstore();
58+
extern void write_portstore();
59+
extern void remove_ipstore();
60+
extern void remove_portstore();
5461

5562
extern void check_config();
56-
extern void set_properties();
57-
extern void remove_ipstore();
58-
extern void clear_config();
63+
extern void read_config();
64+
extern void remove_config();
5965
extern void write_config(string recvd_config);

Client/include/GUI.h

+8-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "../../imgui/imgui.h"
44
#include "../../imgui/backends/imgui_impl_sdl3.h"
55
#include "../../imgui/backends/imgui_impl_sdlrenderer3.h"
6-
76
#include "../../imgui/backends/SDL3/SDL.h"
87

98
#if defined(IMGUI_IMPL_OPENGL_ES2)
@@ -14,30 +13,11 @@
1413

1514
#include "../../Helvetica.h"
1615

17-
#define SDL_ERROR(fail_point) { \
18-
string what_error(fail_point); \
19-
string message = "Failed to initialize "; \
20-
message += what_error; \
21-
message += SDL_GetError(); \
22-
const SDL_MessageBoxButtonData button_data[] = { \
23-
{SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK"} \
24-
}; \
25-
int buttonid; \
26-
const SDL_MessageBoxData msgbox_data = { \
27-
SDL_MESSAGEBOX_ERROR, \
28-
NULL, \
29-
"Error!", \
30-
message.c_str(), \
31-
SDL_arraysize(button_data), \
32-
button_data, \
33-
NULL \
34-
}; \
35-
SDL_ShowMessageBox(&msgbox_data, &buttonid); \
36-
done = true; \
37-
exit(-1); \
38-
}
39-
40-
enum class screens { Home, Vol, Media, Spotify };
16+
#include <string>
17+
18+
extern bool disconnected_modal;
19+
20+
enum class screens { Home,/*, Vol, Media, Spotify */};
4121

4222
extern int gui_init();
4323

@@ -51,7 +31,9 @@ extern void draw_killed();
5131
extern void draw_setup();
5232
extern void draw_disconnected_alert();
5333

54-
screens get_default_screen();
34+
extern void error_dialog(int type, std::string message);
35+
36+
extern screens get_default_screen();
5537

5638
#ifdef _DEBUG
5739
void draw_performance();

Client/include/Header.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#define SHORTCUT_PREFIX "SH"
44

5-
#define LOG(x) cerr << x << "\n";
6-
#define VAR_LOG(x, y) cerr << x << ": " << y << "\n";
5+
#define LOG(x) cerr << x << "\n"
6+
#define VARLOG(x, y) cerr << x << ": " << y << "\n"
77

88
#include <iostream>
99
#include <thread>
@@ -25,9 +25,4 @@ using std::vector;
2525

2626
using std::to_string;
2727

28-
extern bool failed; // TODO: Move to specific headers
29-
extern bool failed_backup;
3028
extern bool done;
31-
extern bool disconnected_modal;
32-
33-
extern char ip_address[16];

Client/src/Client.cpp

+42-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#include "../include/Header.h"
22
#include "../include/Config.h"
33
#include "../include/Client.h"
4+
#include "../include/GUI.h"
45

56
struct timeval timeout;
67

78
bool failed = true;
89
bool connected = false;
9-
bool disconnected_modal = false;
1010
bool kill = false;
11+
bool have_ip = false;
12+
bool have_port = false;
1113

1214
int sock;
1315
int bytesReceived;
1416
int send_result;
17+
int port = 27015;
18+
int prev_port = port;
1519

1620
char ip_address[16];
1721

1822
string kill_reason;
1923

20-
json config;
21-
2224
int client_init() {
2325
const string ID = rw_UUID();
2426

@@ -28,36 +30,63 @@ int client_init() {
2830
timeout.tv_sec = 5;
2931
if ((setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) ||
3032
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) == -1)) {
31-
done = true; cerr << "setsockopt failed\n"; return 1;
33+
LOG("setsockopt failed"); done = true; return 1;
3234
}
35+
// TODO: multiple saved servers. select which one to connect to
36+
have_ip = read_ipstore();
37+
have_port = read_portstore();
3338

3439
struct sockaddr_in hint;
3540
hint.sin_family = AF_INET;
36-
hint.sin_port = htons(27015);
37-
have_ip = rw_ipstore();
38-
check_config(); // MAYBETODO: Multiple computers -- different configs
41+
check_config();
3942
do {
4043
if (!failed) {
44+
if (prev_port != port) {
45+
write_ipstore();
46+
write_portstore();
47+
prev_port = port;
48+
close(sock);
49+
LOG("------------REBOOTING SOCK------------");
50+
return client_init();
51+
}
52+
hint.sin_port = htons(port);
53+
VARLOG("port", port);
54+
VARLOG("hint.sin_port", hint.sin_port);
55+
VARLOG("IP address", ip_address);
4156
inet_pton(AF_INET, ip_address, &hint.sin_addr);
4257
int connectRes = connect(sock, (struct sockaddr*)&hint, sizeof(hint));
43-
cerr << "connectRes: " << connectRes << "\n";
58+
VARLOG("connectRes", connectRes);
4459
if (connectRes == -1) failed = true;
4560
else connected = true;
4661
}
4762
if (done) return 0;
4863
} while (!connected);
49-
if (!have_ip) rw_ipstore();
50-
// if previous MAYBETODO is implemented, add a check_config here with current connected ip as param
64+
65+
if (!have_ip) write_ipstore();
66+
if (!have_port) write_portstore();
5167

5268
timeout.tv_sec = 0;
5369
if ((setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) ||
5470
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) == -1)) {
55-
cerr << "setsockopt failed\n"; done = true; return 1;
71+
LOG("setsockopt failed"); done = true; return 1;
5672
}
5773

5874
disconnected_modal = false;
75+
5976
string message;
60-
char* buf = new char[1024 * 256];
77+
char* buf = nullptr;
78+
try {
79+
buf = new char[1024 * 256];
80+
} catch (std::bad_alloc) {
81+
LOG("Failed to allocate buffer.");
82+
error_dialog(1, "Failed to allocate buffer.\n");
83+
exit(-1);
84+
}
85+
if (buf == nullptr) {
86+
LOG("Failed to allocate buffer.");
87+
error_dialog(1, "Failed to allocate buffer.\n");
88+
exit(-1);
89+
}
6190
send_result = send(sock, ID.c_str(), ID.length() + 1, 0);
6291
do {
6392
memset(buf, 0, 1024 * 256);
@@ -78,7 +107,7 @@ int client_init() {
78107
connected = false;
79108
if (!kill) disconnected_modal = true;
80109
failed = true;
81-
cerr << "------------REBOOTING SOCK------------\n";
110+
LOG("------------REBOOTING SOCK------------");
82111
if (!done) return client_init();
83112
return 0;
84113
}

0 commit comments

Comments
 (0)