-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.cpp
More file actions
66 lines (59 loc) · 1.97 KB
/
ChatServer.cpp
File metadata and controls
66 lines (59 loc) · 1.97 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
#include "ChatServer.hpp"
#include <iostream>
using asio::ip::tcp;
ChatServer::ChatServer(asio::io_service& io_service, uint16_t port)
: _io_service(io_service),
_acceptor(io_service, tcp::endpoint(tcp::v4(), port)),
_counter(0)
{
start_accept();
}
void ChatServer::start_accept()
{
std::cout << "ChatServer::start_accept\n";
ChatSessionPtr new_session(new ChatSession(this, _io_service));
_acceptor.async_accept(new_session->socket(),
std::bind(&ChatServer::handle_accept,
this, new_session, asio::placeholders::error));
}
void ChatServer::handle_accept(ChatSessionPtr new_session, const std::error_code& error)
{
std::lock_guard<std::mutex> guard(_lock);
if (!error)
{
std::cout << "ChatServer::handle_accept starting session\n";
_sessions.push_back(new_session);
new_session->start(_sessions);
}
else
{
std::cout << "ChatServer::handle_accept error: " << error << std::endl;
new_session.reset();
}
start_accept();
}
void ChatServer::publish(const asio::mutable_buffer& buf)
{
std::lock_guard<std::mutex> guard(_lock);
std::cout << "ChatServer::publish" << std::endl;
for_each(_sessions.begin(), _sessions.end(), [buf](ChatSessionPtr p){ p->write(buf); });
}
bool ChatServer::register_user(const std::string& nick, const std::string& password) {
std::lock_guard<std::mutex> guard(_lock);
std::cout << "ChatServer::register_user " << nick << " " << password << std::endl;
auto it = _registry.find(nick);
if(it!=_registry.end()) {
if(it->second.empty()) {
if(password.empty()) {
return true;
} else {
_registry[nick] = password;
return true;
}
} else {
return (it->second == password);
}
}
_registry.insert({nick, password});
return true;
}