This repository was archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
262 lines (205 loc) · 8.15 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <wx/numdlg.h>
#include <format>
#include "ffmpegincludes.h"
#include "win32includes.h"
#include "client/window/window.hpp"
#include "server/window/window.hpp"
#include "common/modeselectdialog/modeselectdialog.hpp"
using namespace StudentSync;
using namespace StudentSync::Common;
constexpr int DEFAULT_PORT_NUMBER = 1000;
class App : public wxApp {
public:
bool OnInit() override;
int OnExit() override;
private:
ULONG_PTR gdiPlusToken = 0;
};
typedef struct {
std::string name;
std::wstring friendlyName;
std::vector<std::string> ipAddresses;
} SystemInterface;
std::optional<std::vector<SystemInterface>> GetNetworkAdapters();
std::optional<wxFrame*> DoClientStartup();
std::optional<wxFrame*> DoServerStartup();
bool App::OnInit() {
// Initialize GDI+
if (GdiPlusStartup(&gdiPlusToken) != 0) {
wxLogFatalError("GDI+ startup failed");
return false;
}
// Initialize WinSock2
int winsockStartupCode = Winsock2Startup();
if (winsockStartupCode == 1) {
// we have to perform shutdown BEFORE we log fatal error, as wxLogFatalError does not return
GdiPlusShutdown(gdiPlusToken);
wxLogFatalError("Winsock2 version != 2.2");
return false;
}
else if (winsockStartupCode != 0) {
// we have to perform shutdown BEFORE we log fatal error, as wxLogFatalError does not return
GdiPlusShutdown(gdiPlusToken);
wxLogFatalError("Winsock2 startup failed");
return false;
}
// Fire up the wxWidgets image handler(s)
wxImage::AddHandler(new wxPNGHandler());
// Determine main frame for this instance
std::optional<wxFrame*> mainFrame = std::nullopt;
ModeSelectDialog modeSelectDialog(nullptr);
if (modeSelectDialog.ShowModal() != wxID_OK) {
GdiPlusShutdown(gdiPlusToken);
return false;
}
switch (modeSelectDialog.GetValue()) {
case ModeSelectDialog::Result::Client: mainFrame = DoClientStartup(); break;
case ModeSelectDialog::Result::Server: mainFrame = DoServerStartup(); break;
default: mainFrame = std::nullopt; break;
}
if (!mainFrame) {
Winsock2Shutdown();
GdiPlusShutdown(gdiPlusToken);
return false;
}
(*mainFrame)->Show();
return true;
}
int App::OnExit() {
// if you ever set breakpoints in this function, please be aware that
// they may not hit when you use the Server mode... I have no idea why.
// But if you add wxLogError("HIT!"); you will get a popup when you close.
Winsock2Shutdown();
GdiPlusShutdown(gdiPlusToken);
return wxApp::OnExit();
}
wxIMPLEMENT_APP(App);
std::optional<wxFrame*> DoClientStartup() {
wxDialog dialog = wxDialog{ nullptr, wxID_ANY, "Client Settings", wxDefaultPosition, wxSize(300, 150) };
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
// Address entry
wxTextCtrl* addressCtrl = new wxTextCtrl(&dialog, wxID_ANY, "localhost");
mainSizer->Add(new wxStaticText(&dialog, wxID_ANY, "Enter server address:"), 0, wxEXPAND | wxALL, 5);
mainSizer->Add(addressCtrl, 0, wxEXPAND | wxALL, 10);
// Port entry
wxTextCtrl* portCtrl = new wxTextCtrl(&dialog, wxID_ANY, wxString::Format("%d", DEFAULT_PORT_NUMBER));
mainSizer->Add(new wxStaticText(&dialog, wxID_ANY, "Enter server port:"), 0, wxEXPAND | wxALL, 5);
mainSizer->Add(portCtrl, 0, wxEXPAND | wxALL, 10);
// OK and Cancel buttons
mainSizer->Add(dialog.CreateButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_CENTER | wxALL, 10);
dialog.SetSizerAndFit(mainSizer);
if (dialog.ShowModal() != wxID_OK) {
return std::nullopt;
}
long portValue;
if (!portCtrl->GetValue().ToLong(&portValue)) {
wxMessageBox("Invalid port value", "Error", wxICON_ERROR | wxOK);
return std::nullopt;
}
std::string hostname = addressCtrl->GetValue().ToStdString();
return new Client::Window("StudentSync - Client", hostname, portValue);
}
std::optional<wxFrame*> DoServerStartup() {
wxString title = "Select Network Interface";
wxString prompt = "Select the network interface to bind to:";
wxString promptPort = "Enter the server port:";
auto adapters = GetNetworkAdapters();
if (!adapters) {
wxMessageBox("Failed to enumerate network adapters.", "Server startup failed", wxICON_ERROR);
return std::nullopt;
}
wxArrayString options;
std::unordered_map<wxString, std::string> optionToAddress;
auto makeLabel = [](std::wstring friendly, std::string ip) { return wxString(ip + " (" + friendly + ")"); };
// add "All Interfaces" as the first option, since its likely to be the user preference
std::string allInterfacesIpAddress = "0.0.0.0";
wxString allInterfacesLabel = makeLabel(L"All Interfaces", allInterfacesIpAddress);
options.push_back(allInterfacesLabel);
optionToAddress.insert_or_assign(allInterfacesLabel, allInterfacesIpAddress);
for (auto& adapter : *adapters) {
for (auto& address : adapter.ipAddresses) {
wxString label = makeLabel(adapter.friendlyName, address);
options.push_back(label);
optionToAddress.insert_or_assign(label, address);
}
}
// Create a dialog to hold the controls
wxDialog dialog{ nullptr, wxID_ANY, title, wxDefaultPosition, wxSize(400, 200) };
// Interface selection
wxStaticText* interfaceLabel = new wxStaticText(&dialog, wxID_ANY, prompt);
wxChoice* interfaceChoice = new wxChoice(&dialog, wxID_ANY, wxDefaultPosition, wxDefaultSize, options);
interfaceChoice->SetSelection(0);
// Port entry
wxStaticText* portLabel = new wxStaticText(&dialog, wxID_ANY, promptPort);
wxTextCtrl* portCtrl = new wxTextCtrl(&dialog, wxID_ANY, wxString::Format("%d", DEFAULT_PORT_NUMBER));
// OK button
wxButton* okButton = new wxButton(&dialog, wxID_OK, "OK");
// Sizer
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(interfaceLabel, 0, wxEXPAND | wxALL, 10);
mainSizer->Add(interfaceChoice, 0, wxEXPAND | wxALL, 10);
mainSizer->Add(portLabel, 0, wxEXPAND | wxALL, 10);
mainSizer->Add(portCtrl, 0, wxEXPAND | wxALL, 10);
mainSizer->Add(okButton, 0, wxALIGN_CENTER | wxALL, 10);
// Set focus on the OK button
okButton->SetFocus();
dialog.SetSizerAndFit(mainSizer);
if (dialog.ShowModal() != wxID_OK) {
return std::nullopt;
}
int port;
if (!portCtrl->GetValue().ToStdString().empty() && portCtrl->GetValue().ToStdString().find_first_not_of("0123456789") == std::string::npos) {
port = std::stoi(portCtrl->GetValue().ToStdString());
}
else {
wxMessageBox("Invalid port value", "Error", wxICON_ERROR | wxOK);
return std::nullopt;
}
std::string address = optionToAddress.at(options[interfaceChoice->GetSelection()]);
return new Server::Window("StudentSync - Server", address, port);
}
// Retrieves information about all IPv4-compatible interfaces on the host.
std::optional<std::vector<SystemInterface>> GetNetworkAdapters() {
ULONG family = AF_INET;
PIP_ADAPTER_ADDRESSES adapters = nullptr;
ULONG bufferSize = 0;
int flags = GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST;
if (GetAdaptersAddresses(family, flags, NULL, adapters, &bufferSize) == ERROR_BUFFER_OVERFLOW) {
adapters = (PIP_ADAPTER_ADDRESSES)std::malloc(bufferSize);
if (adapters == nullptr) {
return std::nullopt;
}
if (GetAdaptersAddresses(family, flags, nullptr, adapters, &bufferSize) != NOERROR) {
std::free(adapters);
return std::nullopt;
}
}
std::vector<SystemInterface> result;
for (PIP_ADAPTER_ADDRESSES currentAdapter = adapters; currentAdapter != nullptr; currentAdapter = currentAdapter->Next) {
SystemInterface thisInterface{};
if (!currentAdapter->FirstUnicastAddress) {
continue;
}
std::vector<std::string> addresses;
for (PIP_ADAPTER_UNICAST_ADDRESS unicastAddr = currentAdapter->FirstUnicastAddress; unicastAddr != nullptr; unicastAddr = unicastAddr->Next) {
struct sockaddr* socketAddress = unicastAddr->Address.lpSockaddr;
if (socketAddress->sa_family != AF_INET) {
continue; // not IPv4, skip
}
struct sockaddr_in* ipv4 = (struct sockaddr_in*)socketAddress;
char ipString[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ipv4->sin_addr), ipString, sizeof(ipString));
addresses.push_back(std::string(ipString));
}
if (addresses.size() == 0) {
// ignore adapters with no assigned IP address
continue;
}
thisInterface.name = std::string(currentAdapter->AdapterName);
thisInterface.friendlyName = std::wstring(currentAdapter->FriendlyName);
thisInterface.ipAddresses = addresses;
result.push_back(thisInterface);
}
std::free(adapters);
return result;
}