-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (128 loc) · 5.81 KB
/
Copy pathmain.cpp
File metadata and controls
149 lines (128 loc) · 5.81 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
#include "client_session_controller.h"
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <thread>
#include <regex>
using namespace ttp2;
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();
});
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) {
std::string payload = requestString("(string) Payload: ");
ClientSessionController::Packet packet;
Networking::Standard standard;
standard.payload = payload;
packet.payload = standard;
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();
Networking::Standard standard = std::get<Networking::Standard>(packet.payload);
std::wcout << "------ Message ------" << std::endl;
std::wcout << "ID: " << packet.id << std::endl;
std::wcout << "Payload: " << standard.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) {
std::string payload = requestString("(string) Payload: ");
ClientSessionController::Packet packet;
while (true) {
Networking::Standard standard;
standard.payload = payload;
packet.payload = standard;
clientSessionController->pushRequest(packet);
while(clientSessionController->hasResponse()) {
ClientSessionController::Packet packet = clientSessionController->popResponse();
Networking::Standard standard = std::get<Networking::Standard>(packet.payload);
std::wcout << "------ Message ------" << std::endl;
std::wcout << "ID: " << packet.id << std::endl;
std::wcout << "Payload: " << standard.payload.c_str() << std::endl;
std::wcout << "---------------------" << std::endl;
}
}
} else if (option == 2) {
int count = requestInt("(int) Packet count: ");
std::string payload = requestString("(string) Payload: ");
ClientSessionController::Packet packet;
Networking::Standard standard;
standard.payload = payload;
packet.payload = standard;
for (int index = 0; index < count; index++) {
clientSessionController->pushRequest(packet);
}
while (count != 0) {
if (clientSessionController->hasResponse()) {
ClientSessionController::Packet packet = clientSessionController->popResponse();
Networking::Standard standard = std::get<Networking::Standard>(packet.payload);
std::wcout << "------ Message ------" << std::endl;
std::wcout << "ID: " << packet.id << std::endl;
std::wcout << "Payload: " << standard.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;
}