-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatSession.cpp
More file actions
99 lines (90 loc) · 3.34 KB
/
ChatSession.cpp
File metadata and controls
99 lines (90 loc) · 3.34 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
#include "ChatSession.hpp"
#include "ChatServer.hpp"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
using asio::ip::tcp;
ChatSession::ChatSession(ChatServer* server, asio::io_service& io_service)
: _server(server), _socket(io_service), _active(true)
{
std::stringstream user;
user << "Guest" << std::setfill('0') << std::setw(3) << server->counter();
_user = user.str();
}
void ChatSession::write(const asio::mutable_buffer& buf)
{
if(_active) {
asio::async_write(_socket, buf,
std::bind(&ChatSession::handle_write, this, asio::placeholders::error, asio::placeholders::bytes_transferred)
);
}
}
void ChatSession::write(const asio::const_buffer& buf)
{
if(_active) {
asio::async_write(_socket, buf,
std::bind(&ChatSession::handle_write, this, asio::placeholders::error, asio::placeholders::bytes_transferred)
);
}
}
void ChatSession::start(const std::vector<ChatSessionPtr> sessions)
{
std::for_each(sessions.cbegin(), sessions.cend(), [this](ChatSessionPtr p){
if(p->is_active()) {
std::string s = p->user() + ", ";
s.copy(_data, s.size());
this->write(asio::buffer(_data, s.size()));
}
});
_data[0] = '\n';
this->write(asio::buffer(_data, 1));
}
void ChatSession::handle_read(const std::error_code& error, size_t bytes_transferred)
{
static const std::string UNKNOWN_COMMAND {"Unknown command!\n"};
static const std::string NICK_TAKEN {"Nick taken!\n"};
static const std::string REGISTRATION_FAILED {"Registration failed!\n"};
if (!error) {
std::cout << "ChatSession::handle_read " << (int) bytes_transferred << " bytes\n";
if(_data[0] == '/') {
if(std::string(_data + 1, 5) == "nick ") {
std::string s(_data + 6, bytes_transferred - 6);
trim(s);
if(_server->register_user(s, _password)) {
_user = s;
} else {
this->write(asio::buffer(NICK_TAKEN.c_str(), NICK_TAKEN.size()));
}
} else if (std::string(_data + 1, 9) == "register ") {
std::string s(_data + 10, bytes_transferred - 10);
trim(s);
if(_server->register_user(_user, s)) {
_password = s;
} else {
this->write(asio::buffer(REGISTRATION_FAILED.c_str(), REGISTRATION_FAILED.size()));
}
} else {
this->write(asio::buffer(UNKNOWN_COMMAND.c_str(), UNKNOWN_COMMAND.size()));
}
} else {
_server->publish(asio::buffer(_data, bytes_transferred));
}
} else {
std::cout << "ChatSession::handle_read error: " << error << std::endl;
_active = false;
}
}
void ChatSession::handle_write(const std::error_code& error, size_t bytes_transferred)
{
if (!error) {
std::cout << "ChatSession::handle_write " << (int) bytes_transferred << " bytes\n";
_socket.async_read_some(
asio::buffer(_data, MAX_SIZE),
std::bind(&ChatSession::handle_read, this, asio::placeholders::error, asio::placeholders::bytes_transferred)
);
} else {
std::cout << "ChatSession::handle_write error: " << error << std::endl;
_active = false;
}
}