-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
117 lines (102 loc) · 4 KB
/
server.cpp
File metadata and controls
117 lines (102 loc) · 4 KB
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
#include "tcp_connection.h"
#include <iostream>
void PrintState() {
std::cout << "Select a command:\n";
std::cout << " [0] Send Cmd\n";
std::cout << " [1] Read Cmd\n";
std::cout << " [2] Read All Cmd\n";
std::cout << " [-1] Exit\n";
std::cout << "Enter choice: ";
}
uint32_t GetUserInput() {
int choice;
std::cin >> choice;
// Handle invalid input (non-numeric)
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Please enter a number.\n";
return GetUserInput(); // Retry
}
return choice;
}
std::vector<uint32_t> GetUserInputList() {
std::vector<uint32_t> numbers;
std::string line;
std::cout << "Enter numbers in one line: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, line);
std::stringstream ss(line);
int num;
while (ss >> num) {
numbers.push_back(num);
}
return numbers;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Please include IP address and port!" << std::endl;
std::cerr << "Usage: " << argv[0] << " <IP address> <port>\n";
return 1;
}
std::string ip_address = argv[1];
uint16_t port = std::stoi(argv[2]);
try {
asio::io_context io_context;
std::cout << "Starting server..." << std::endl;
std::shared_ptr<TCPConnection> server;
server = std::make_shared<TCPConnection>(io_context, ip_address, port, true, true, false);
server->Start();
std::cout << "Starting IO Context..." << std::endl;
// Guard to keep IO contex from completely before we want to quit
asio::executor_work_guard<asio::io_context::executor_type> work_guard(io_context.get_executor());
std::thread io_thread([&]() { io_context.run(); });
while (true) {
PrintState();
uint32_t input = GetUserInput();
if (input == -1) {
std::cout << "Exiting...\n";
break;
}
switch (input) {
case 0: {
std::cout << "Enter Cmd: ";
auto cmd = static_cast<uint16_t>(GetUserInput());
std::cout << "Enter Arg: \n";
std::vector<uint32_t> args = GetUserInputList();
std::cout << "Sending command" << std::endl;
server->WriteSendBuffer(cmd, args);
break;
}
case 1: {
std::cout << "Receiving command" << std::endl;
Command cmd = server->ReadRecvBuffer();
std::cout << "******************************" << std::endl;
std::cout << "Command: " << cmd.command << std::endl;
for (auto &arg : cmd.arguments) {std::cout << arg << std::endl;}
std::cout << "******************************" << std::endl;
break;
}
case 2: {
std::cout << "Receiving All command" << std::endl;
std::vector<Command> cmd_vec = server->ReadRecvBuffer(10000);
std::cout << "******************************" << std::endl;
for (auto &cmd : cmd_vec) {
std::cout << " -- Command: " << cmd.command << std::endl;
for (auto &arg : cmd.arguments) {std::cout << arg << std::endl;}
}
std::cout << "******************************" << std::endl;
break;
}
default:
std::cerr << "Invalid input." << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(3));
}
io_context.stop();
io_thread.join();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}