-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (136 loc) · 5.96 KB
/
main.cpp
File metadata and controls
157 lines (136 loc) · 5.96 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
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
#include "client_session_controller.h"
#include <atomic>
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <thread>
#include <memory>
#include <regex>
bool isNumeric(const std::string& string) {
static const std::regex numberRegex(
R"(^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?$)"
);
return std::regex_match(string, numberRegex);
}
std::string requestString(const std::string& message) {
std::string userInput;
std::wcout << message.c_str();
std::getline(std::cin, userInput);
return userInput;
}
int requestInt(const std::string& message) {
std::string userInput;
while (true) {
std::wcout << message.c_str();
std::getline(std::cin, userInput);
if (isNumeric(userInput)) {
return std::stoi(userInput);
}
std::wcout << "Invalid input! Try again\n";
}
}
int main() {
std::string ipAddress = requestString("Server ipv4 (string): ");
int port = requestInt("Server port (int): ");
int serverSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(port);
serverAddress.sin_addr.s_addr = inet_addr(ipAddress.c_str());
int connectionResult = connect(serverSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress));
if (connectionResult < 0 && errno != EINPROGRESS) {
std::wcout << "Connection failed!" << std::endl;
return -1;
}
auto clientSessionController = std::make_shared<ClientSessionController>(serverSocket);
std::thread networkThread([clientSessionController]() {
clientSessionController->networkingSession();
});
int count = 0;
while (true) {
if (!clientSessionController->isConnected()) {
std::wcout << "Disconnect!" << std::endl;
break;
}
int option = requestInt("Choose option\n(1) Send message\n(2) Read messages\n(3) Benchmark\n(4) Exit\nnumber: ");
if (option == 1) {
int method = requestInt("(int) Method: ");
std::string payload = requestString("(string) Payload: ");
ClientSessionController::Packet packet;
packet.id = count;
packet.method = method;
packet.payload = payload;
count++;
clientSessionController->pushRequest(packet);
} else if (option == 2) {
if (!clientSessionController->hasResponse()) {
std::wcout << "No Messages!" << std::endl;
continue;
}
while(clientSessionController->hasResponse()) {
ClientSessionController::Packet packet = clientSessionController->popResponse();
std::wcout << "------ Message ------" << std::endl;
std::wcout << "ID: " << packet.id << std::endl;
std::wcout << "Method: " << packet.method << std::endl;
std::wcout << "Payload: " << packet.payload.c_str() << std::endl;
std::wcout << "---------------------" << std::endl;
}
} else if (option == 3) {
std::wcout << "~~~~~~ ~~~~~~ Benchmark ~~~~~~ ~~~~~~" << std::endl;
option = requestInt("Choose option\n(1) Send continious stream\n(2) Send n packages\nnumber: ");
if (option == 1) {
int method = requestInt("(int) Method: ");
std::string payload = requestString("(string) Payload: ");
ClientSessionController::Packet packet;
packet.method = method;
while (true) {
packet.id = count;
packet.payload = payload;
count++;
clientSessionController->pushRequest(packet);
while(clientSessionController->hasResponse()) {
ClientSessionController::Packet packet = clientSessionController->popResponse();
std::wcout << "------ Message ------" << std::endl;
std::wcout << "ID: " << packet.id << std::endl;
std::wcout << "Method: " << packet.method << std::endl;
std::wcout << "Payload: " << packet.payload.c_str() << std::endl;
std::wcout << "---------------------" << std::endl;
}
}
} else if (option == 2) {
int count = requestInt("(int) Packet count: ");
int method = requestInt("(int) Method: ");
std::string payload = requestString("(string) Payload: ");
ClientSessionController::Packet packet;
packet.id = count;
packet.method = method;
packet.payload = payload;
count++;
for (int index = 0; index < count; index++) {
clientSessionController->pushRequest(packet);
}
while (count != 0) {
if (clientSessionController->hasResponse()) {
ClientSessionController::Packet packet = clientSessionController->popResponse();
std::wcout << "------ Message ------" << std::endl;
std::wcout << "ID: " << packet.id << std::endl;
std::wcout << "Method: " << packet.method << std::endl;
std::wcout << "Payload: " << packet.payload.c_str() << std::endl;
std::wcout << "---------------------" << std::endl;
count--;
}
}
} else {
std::wcout << "Invalid!" << std::endl;
}
} else if (option == 4) {
clientSessionController->disconnect();
} else {
std::wcout << "Invalid!" << std::endl;
}
}
networkThread.detach();
return 0;
}