-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
99 lines (78 loc) · 2.7 KB
/
client.cpp
File metadata and controls
99 lines (78 loc) · 2.7 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
#include "client.h"
#include "chat.h"
Client::Client(ChatWindow& chatWindow) : chatWindowRef(chatWindow), resolver(context)
{
this->host = host;
this->port = port;
this->name = name;
}
Client::~Client()
{
if (sock) {
asio::error_code error;
sock->shutdown(asio::ip::tcp::socket::shutdown_both, error);
sock->close(error);
}
}
int Client::run(const std::string& host, const std::string& port, const std::string& name) {
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host, port);
this->name = name;
try {
sock = std::make_shared<asio::ip::tcp::socket>(context);
asio::connect(*sock, endpoints);
std::cout << "Connected to the server.\n";
connected = true;
// Send initial name broadcast to the server
std::string initial_message = "#new client:" + name;
send_message(initial_message);
// Start a thread for receiving messages
std::thread recv_thread([this] () { receive_message(sock); });
recv_thread.join();
}
catch (std::exception& e) {
std::cerr << "Client exception: " << e.what() << "\n";
return -1;
}
return 0;
}
void Client::send_message(const std::string& message) {
if (!sock || !sock->is_open()) return; // Avoid sending if socket is not ready
std::string full_message = "[" + this->name + "] " + message;
asio::error_code error;
asio::write(*sock, asio::buffer(full_message), error);
cout << "Sent message: " << full_message << endl;
if (error) {
std::cerr << "Send failed: " << error.message() << "\n";
}
}
void Client::receive_message(std::shared_ptr<asio::ip::tcp::socket> sock) {
char msg[BUF_SIZE];
asio::error_code error;
while (true) {
size_t len = sock->read_some(asio::buffer(msg), error);
if (error == asio::error::eof) {
break;
}
else if (error) {
std::cerr << "Receive failed: " << error.message() << "\n";
break;
}
msg[len] = '\0';
std::string message(msg);
std::cout << "Received message on client: " << message << std::endl;
}
}
void Client::update_client_list(const std::string& client_list_str) {
std::lock_guard<std::mutex> lock(client_mutex);
connected_clients.clear();
std::stringstream ss(client_list_str);
std::string client_name;
while (std::getline(ss, client_name, ',')) {
if (!client_name.empty()) {
connected_clients.push_back(client_name);
}
}
}
bool Client::is_connected() {
return connected;
}