58int main(
int argc,
char **argv)
65 bool defaultLog =
false;
66 bool defaultEcho =
false;
67 bool defaultDebug =
false;
68 bool defaultSenderMode =
true;
69 bool defaultMonoActive =
false;
70 bool defaultSenderOnly =
true;
71 unsigned int defaultLoopLimit = 0;
72 unsigned int defaultPacketDelay = 10;
73 unsigned int defaultPort = 9000;
74 std::string defaultIp =
"0.0.0.0";
76 int port = defaultPort;
77 bool is_sender = defaultSenderMode;
78 std::string ip = defaultIp;
79 bool echo = defaultEcho;
80 bool log = defaultLog;
81 bool debug = defaultDebug;
82 unsigned int maxRounds = defaultLoopLimit;
83 unsigned int packetDelay = defaultPacketDelay;
84 bool monoActive = defaultMonoActive;
85 bool senderOnly = defaultSenderOnly;
87 for (
int i = 1; i < argc; i++) {
88 std::string arg = std::string(argv[i]);
92 if (port < 1024 || port > 65535) {
93 std::cerr <<
"Invalid port number, please use a port number between 1024 and 65535" << std::endl;
98 std::cout <<
"Missing argument parameter, use -h for help" << std::endl;
100 }
else if (arg ==
"-i") {
105 std::cout <<
"Missing argument parameter, use -h for help" << std::endl;
107 }
else if (arg ==
"-r") {
109 }
else if (arg ==
"-s") {
111 }
else if (arg ==
"-d") {
113 }
else if (arg ==
"-l") {
115 }
else if (arg ==
"-m") {
120 std::cout <<
"Missing argument parameter, use -h for help" << std::endl;
122 }
else if (arg ==
"-mono") {
125 }
else if (arg ==
"-so") {
128 }
else if (arg ==
"-ro") {
131 }
else if (arg ==
"-e") {
133 }
else if (arg ==
"-a") {
135 return PROGRAM_SUCCESS;
136 }
else if (arg ==
"-v") {
137 std::cout <<
"The program's version is: " << VERSION << std::endl;
138 return PROGRAM_SUCCESS;
139 }
else if (arg ==
"--packet-delay" || arg ==
"-pd" || arg ==
"-packet-delay" || arg ==
"--pd" || arg ==
"-packetdelay" || arg ==
"--packetdelay") {
144 std::cout <<
"Missing argument parameter, use -h for help" << std::endl;
146 }
else if (arg ==
"-h" || arg ==
"--help") {
147 std::cout <<
"USAGE:\n";
148 std::cout << std::string(argv[0]) <<
" -p <port> -i <ip> [-r <receiver> | -s <sender>] -d -l\n";
150 std::cout <<
"OPTIONS:\n";
151 std::cout <<
"-p <port> : Set the port to connect to (default: " << defaultPort <<
")\n";
152 std::cout <<
"-i <ip> : Set the ip to connect to (default: " << defaultIp <<
")\n";
153 std::cout <<
"-r <receiver> : Set the client to receiver mode [This is for the connection reasons] (default: " <<
Recoded::myToString(!defaultSenderMode) <<
"\n";
154 std::cout <<
"-s <sender> : Set the client to sender mode [This is for the connection reasons] (default: " <<
Recoded::myToString(defaultSenderMode) <<
")\n";
155 std::cout <<
"-d : Enable debug mode (default: " <<
Recoded::myToString(defaultDebug) <<
")\n";
156 std::cout <<
"-l : Enable log mode (default: " <<
Recoded::myToString(defaultLog) <<
")\n";
157 std::cout <<
"-m <maxRounds> : Set the maximum number of rounds, 0 = endless (default: " << defaultLoopLimit <<
")\n";
158 std::cout <<
"-e : Enable echo mode for the user prompt (default: " <<
Recoded::myToString(defaultEcho) <<
")\n";
159 std::cout <<
"-a : Display all boot screens (Epilepsy warning, all the" <<
Recoded::myToString(
BootScreen.
getAvailableScreens()) <<
"logos will be displayed one after the other without any delay, meaning they will come out as fast as you terminal can diplay them)\n";
160 std::cout <<
"-h, --help : Display this help message\n";
161 std::cout <<
"-mono : Set the program to mono mode [This is for the audio management] (default " <<
Recoded::myToString(defaultMonoActive) <<
")\n";
162 std::cout <<
"-so : Set the program to sender only mode, will set mono mode to true [This is for the audio management] (default " <<
Recoded::myToString(defaultSenderOnly) <<
")\n";
163 std::cout <<
"-ro : Set the program to receiver only mode, will set mono mode to true [This is for the audio management] (default " <<
Recoded::myToString(!defaultSenderOnly) <<
")\n";
164 std::cout <<
"-v : Display the program's version\n";
165 std::cout <<
"--packet-delay, -pd, -packet-delay, --pd, -packetdelay, --packetdelay : Set the delay between packets in milliseconds (default: " <<
Recoded::myToString(defaultPacketDelay) <<
")\n";
167 std::cout <<
"VERSION:\n";
168 std::cout <<
"The program's version is: " << VERSION << std::endl;
170 std::cout <<
"AUTHOR:\n";
171 std::cout <<
"Written by:";
172 std::cout <<
"\t-\tHenry Letellier : https://github.com/HenraL\n";
173 std::cout <<
"Epitech student 2025\n";
174 std::cout <<
"\n" << std::flush;
175 return PROGRAM_SUCCESS;
177 std::cout <<
"Invalid argument, use -h for help" << std::endl;
178 std::cout <<
"The argument you provided was: " << argv[i] << std::endl;
188 PRETTY_INFO <<
"Starting the program" << std::endl;
191 asio::io_context io_context;
194 const std::string endMessage =
"END";
197 unsigned int rounds = 0;
198 bool continueRunning =
true;
199 std::vector<float> sound;
200 std::vector<unsigned char> compressedSound;
219 while ((rounds < maxRounds || maxRounds == 0) && continueRunning ==
true && myUDP.
isConnectionAlive()) {
224 continueRunning =
false;
226 std::cout <<
"Sending end message" << std::endl;
227 myUDP.
sendRaw(endMessage.c_str(), endMessage.size(), ip, port);
233 myManager.
encode(sound, compressedSound);
235 PRETTY_INFO <<
"compressedSound size: " << compressedSound.size() << std::endl;
236 PRETTY_DEBUG <<
"compressed sound data: " << compressedSound << std::endl;
237 myUDP.
sendRaw(
reinterpret_cast<const char *
>(compressedSound.data()), compressedSound.size(), ip, port);
239 std::string receivedData = myUDP.
receiveFrom(ip, port);
240 if (receivedData ==
"END") {
241 std::cout <<
"Received end message, ending program" << std::endl;
242 continueRunning =
false;
245 PRETTY_INFO <<
"Received data size: " << receivedData.size() << std::endl;
246 PRETTY_INFO <<
"Received data: " << receivedData << std::endl;
247 for (
int i = 0; i < receivedData.size(); i++) {
248 compressedSound.push_back(receivedData[i]);
250 PRETTY_INFO <<
"compressed sound data: " << compressedSound << std::endl;
251 myManager.
decode(compressedSound, sound);
257 continueRunning =
false;
259 std::cout <<
"Sending end message" << std::endl;
260 myUDP.
sendRaw(endMessage.c_str(), endMessage.size(), ip, port);
266 myManager.
encode(sound, compressedSound);
268 PRETTY_INFO <<
"compressedSound size: " << compressedSound.size() << std::endl;
269 PRETTY_DEBUG <<
"compressed sound data: " << compressedSound << std::endl;
270 myUDP.
sendRaw(
reinterpret_cast<const char *
>(compressedSound.data()), compressedSound.size(), ip, port);
271 std::this_thread::sleep_for(std::chrono::milliseconds(10));
272 std::string receivedData = myUDP.
receiveFrom(ip, port);
273 if (receivedData ==
"END") {
274 std::cout <<
"Received end message, ending program" << std::endl;
275 continueRunning =
false;
278 PRETTY_INFO <<
"Received data size: " << receivedData.size() << std::endl;
279 PRETTY_INFO <<
"Received data: " << receivedData << std::endl;
280 for (
int i = 0; i < receivedData.size(); i++) {
281 compressedSound.push_back(receivedData[i]);
283 PRETTY_INFO <<
"compressed sound data: " << compressedSound << std::endl;
284 myManager.
decode(compressedSound, sound);
287 PRETTY_INFO <<
"Round end " << rounds << std::endl;
288 PRETTY_INFO <<
"Clearing sound buffer and compressed buffer" << std::endl;
290 compressedSound.clear();
291 PRETTY_INFO <<
"Sound buffer and compressed buffer cleared" << std::endl;
298 myUDP.
sendRaw(endMessage.c_str(), endMessage.size(), ip, port);
305 myUDP.
sendRaw(endMessage.c_str(), endMessage.size(), ip, port);
316 PRETTY_ERROR <<
"The thread stopping did not happen in a clean manner" << std::endl;
320 std::cout <<
"Program ended successfully" << std::endl;
322 catch (
const std::exception &e) {
323 std::cerr <<
"An error occurred: " << e.what() << std::endl;
325 return PROGRAM_ERROR;
327 return PROGRAM_SUCCESS;
const unsigned int stringToUnsignedInt(const std::string &str, const unsigned int defaultValue=0)
Converts a string to an unsigned integer.
int main(int argc, char **argv)
Main function of the program.