-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
374 lines (330 loc) · 11.9 KB
/
Client.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "Client.h"
#include <string>
#include "OutputValues.h"
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <vector>
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma warning(disable: 4996)
Client::Client() : logFileName("client_log.txt")
{
// Initialize Winsock.
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0)
{
throw std::runtime_error("WSAStartup failed: " + std::to_string(result));
}
// Create a socket.
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET)
{
WSACleanup();
throw std::runtime_error("Failed to create socket: " + std::to_string(WSAGetLastError()));
}
// Create a UDP socket.
udpClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpClientSocket == INVALID_SOCKET)
{
WSACleanup();
throw std::runtime_error("Failed to create UDP socket: " + std::to_string(WSAGetLastError()));
}
// Set socket options for broadcast and reuse.
int broadcastEnable = 1;
result = setsockopt(udpClientSocket, SOL_SOCKET, SO_BROADCAST, (char*)&broadcastEnable, sizeof(broadcastEnable));
if (result == SOCKET_ERROR)
{
closesocket(udpClientSocket);
WSACleanup();
throw std::runtime_error("Failed to set UDP socket options: " + std::to_string(WSAGetLastError()));
}
connected = false;
}
Client::~Client()
{
if (connected)
{
closeConnection();
}
// Close the UDP socket.
closesocket(udpClientSocket);
WSACleanup();
}
void Client::listenForUdpBroadcast()
{
sockaddr_in udpClientAddr{};
udpClientAddr.sin_family = AF_INET;
udpClientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
udpClientAddr.sin_port = htons(5000); //static_cast<unsigned short>(std::stoi(port)) in case of having a port number as const char* port similar to the server
// Bind the UDP socket.
int result = bind(udpClientSocket, (sockaddr*)&udpClientAddr, sizeof(udpClientAddr));
if (result == SOCKET_ERROR) {
throw std::runtime_error("Failed to bind UDP socket: " + std::to_string(WSAGetLastError()));
}
//// Set the UDP socket to non-blocking mode. Could fix the problem but It gives the WSAEWOULDBLOCK non blocking error 1003
//u_long mode = 1;
//if (ioctlsocket(udpClientSocket, FIONBIO, &mode) != 0) {
// throw std::runtime_error("Failed to set UDP socket to non-blocking mode: " + std::to_string(WSAGetLastError()));
//}
// Set the UDP socket to receive broadcast messages.
int broadcastPermission = 1;
if (setsockopt(udpClientSocket, SOL_SOCKET, SO_BROADCAST, (char*)&broadcastPermission, sizeof(broadcastPermission)) < 0)
{
throw std::runtime_error("Failed to set SO_BROADCAST for UDP client socket: " + std::to_string(WSAGetLastError()));
}
// Receive server broadcast.
char recvBuffer[256];
sockaddr_in serverBroadcastAddr{};
int serverBroadcastAddrSize = sizeof(serverBroadcastAddr);
result = recvfrom(udpClientSocket, recvBuffer, sizeof(recvBuffer) - 1, 0, (sockaddr*)&serverBroadcastAddr, &serverBroadcastAddrSize);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to receive UDP broadcast: " + std::to_string(WSAGetLastError()));
}
recvBuffer[result] = '\0';
// Extract server IP and port from the received broadcast.
std::string broadcastMessage(recvBuffer);
size_t separatorPos = broadcastMessage.find(':');
if (separatorPos == std::string::npos) {
throw std::runtime_error("Invalid broadcast message received");
}
std::string receivedServerIP = broadcastMessage.substr(0, separatorPos);
std::string receivedServerPort = broadcastMessage.substr(separatorPos + 1);
// Connect to the server using the received IP and port.
connectToServer(receivedServerIP.c_str(), receivedServerPort.c_str());
closesocket(udpClientSocket);
}
void Client::connectToServer(const char* serverIP, const char* port)
{
// Create a sockaddr_in object and set its values.
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(serverIP);
serverAddress.sin_port = htons(atoi(port));
// Call the connect function, passing the created socket and the sockaddr_in structure as parameters.
int result = connect(clientSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress));
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to connect to server: " + std::to_string(WSAGetLastError()));
}
connected = true;
}
void Client::registerUser(std::string username)
{
if (!connected)
{
throw std::runtime_error("Client is not connected to server");
}
// Save the provided username.
this->username = username;
// Create the register command string.
std::string registerCommand = "$register " + username;
// Send the size of the command first.
int commandSize = static_cast<int>(registerCommand.length());
int result = send(clientSocket, reinterpret_cast<char*>(&commandSize), sizeof(commandSize), 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to send command size: " + std::to_string(WSAGetLastError()));
}
// Send the register command to the server.
result = send(clientSocket, registerCommand.c_str(), commandSize, 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to send register command: " + std::to_string(WSAGetLastError()));
}
// Receive server response.
char responseBuffer[256];
result = recv(clientSocket, responseBuffer, sizeof(responseBuffer), 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to receive server response: " + std::to_string(WSAGetLastError()));
}
// Check server response.
std::string response(responseBuffer, result);
std::string success = "SV_SUCCESS\0";
std::string serverFullMessage = "SV_FULL\0";
if (response.back() == '\n')
{
response.pop_back();
}
if (response == "SV_FULL")
{
closeConnection();
throw std::runtime_error("Server is full. Please try again later.");
}
else if (response != success)
{
bool areEqual = true;
bool serverFull = true;
for (size_t i = 0; i < response.length(); i++)//loop to fix the server success message because the server sends it with garbage at the end
{
if (response[i] != success[i])
{
areEqual = false;
break;
}
}
for (size_t i = 0; i < response.length(); i++)//loop to fix the server full message because the server sends it with garbage at the end
{
if (response[i] != serverFullMessage[i])
{
serverFull = false;
break;
}
}
if (serverFull)
{
closeConnection();
throw std::runtime_error("Server is full. Please try again later.");
}
else if (!areEqual)
{
throw std::runtime_error("Failed to register user: " + response);
}
}
}
void Client::executeCommand(std::string command)
{
if (!connected)
{
throw std::runtime_error("Client is not connected to server");
}
// Send command size to server first
int commandSize = static_cast<int>(command.length());
int result = send(clientSocket, reinterpret_cast<char*>(&commandSize), sizeof(commandSize), 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to send command size: " + std::to_string(WSAGetLastError()));
}
// Send command to server
result = send(clientSocket, command.c_str(), commandSize, 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to send command: " + std::to_string(WSAGetLastError()));
}
std::cout << "[Executed] " << command << std::endl;
}
void Client::sendMessage(std::string message)
{
if (!connected)
{
throw std::runtime_error("Client is not connected to server");
}
std::string chatCommand = "$chat " + message;
// Send message size to server first
int messageSize = static_cast<int>(chatCommand.length());
int result = send(clientSocket, reinterpret_cast<char*>(&messageSize), sizeof(messageSize), 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to send message size: " + std::to_string(WSAGetLastError()));
}
// Send message to server
result = send(clientSocket, chatCommand.c_str(), messageSize, 0);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to send chat message: " + std::to_string(WSAGetLastError()));
}
std::cout<<"[Sent out] " << message << std::endl;
}
void Client::closeConnection()
{
if (!connected)
{
return;
}
// Shutdown the connection.
int result = shutdown(clientSocket, SD_SEND);
if (result == SOCKET_ERROR)
{
throw std::runtime_error("Failed to shutdown connection: " + std::to_string(WSAGetLastError()));
}
std::cout << "\nDisconnected\n";
closesocket(clientSocket);
connected = false;
}
std::string Client::receiveMessage(bool& flag)
{
if (!connected)
{
throw std::runtime_error("Client is not connected to server");
}
while (true)
{
// Receive the size of the incoming message first.
int messageSize = 0;
int nbytes = recv(clientSocket, reinterpret_cast<char*>(&messageSize), sizeof(messageSize), 0);
if (nbytes <= 0)
{
throw std::runtime_error("Failed to receive message size: " + std::to_string(WSAGetLastError()));
}
// Allocate a buffer of the appropriate size to receive the message.
char* buffer = new char[messageSize];
nbytes = recv(clientSocket, buffer, messageSize, 0);
if (nbytes <= 0)
{
delete[] buffer;
throw std::runtime_error("Failed to receive message: " + std::to_string(WSAGetLastError()));
}
// Convert the received message to a string and return it if valid.
std::string message(buffer, messageSize);
delete[] buffer;
if (message.find("SV_SUCCESS") == 0 || message.find("SV_FULL") == 0)
{
return message;
}
else if (message.find("CHAT") == 0)
{ // If the message is a chat message, print it to the console. And delete the current line
return ("\033[2K\r" + message + "\nEnter command or message: ");
}
else if (message.find("LIST") == 0)
{
return ("\033[2K\r" + message.substr(5) + "\nEnter command or message: ");
}
else if (message.find("LOG") == 0)
{
//check if clientLog.txt exists or else create it and write on it
std::ofstream logFile(logFileName, std::ios::app);
if (!logFile.good()) {
std::cerr << "Error opening log file." << std::endl;
return "Error opening file\nEnter command or message: ";
}
logFile << message.substr(4) << std::endl;
return ("\033[2K\r" + message.substr(4) + "\nEnter command or message: ");
}
else if (message.find("EXIT") == 0)
{
flag = true;
return ("\033[2K\r" + message.substr(5));
}
else if (message.find("SV_FULL") == 0)
{
flag = true;
return "Server is currently full";
}
else
{
return "";
}
}
}
bool Client::isConnected()
{
return connected;
}
void Client::setSocket(SOCKET newSocket)
{
clientSocket = newSocket;
}
SOCKET Client::getSocket() const
{
return clientSocket;
}
std::string Client::getUsername() const
{
return username;
}
void Client::setUsername(std::string _username)
{
this->username = _username;
}