Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
- add ObjectPool for connections
Browse files Browse the repository at this point in the history
- rewrite find free connections
- add doc
- add check is open socket
  • Loading branch information
k-morozov committed Aug 5, 2020
1 parent b31de6d commit a8c2f5e
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ add_executable(${CLIENT_BIN} ${CLIENT_SOURCES})
target_include_directories(${CLIENT_BIN} PUBLIC .)

#****************************************************************************************************
find_package(Boost COMPONENTS system date_time program_options regex REQUIRED)
find_package(Boost COMPONENTS system date_time REQUIRED)

set_target_properties(${CLIENT_BIN} PROPERTIES
CXX_STANDARD 17
Expand Down
42 changes: 24 additions & 18 deletions src/client/client/client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <client/client.h>
//#include <charconv>

void Client::write(const std::string& message) {
text_request_ptr text_request = std::make_shared<TextRequest>(login, room_id, message);
Expand Down Expand Up @@ -60,7 +59,9 @@ void Client::send_login_packet(packet_ptr packet) {
packet->get_length_data()), error_code);

if (error_code) {
sock.close();
if (sock.is_open()) {
sock.close();
}
std::cout << "error when send login" << std::endl;
return ;
}
Expand All @@ -69,7 +70,9 @@ void Client::send_login_packet(packet_ptr packet) {
boost::asio::read(sock, boost::asio::buffer(response->get_header(),
Block::Header), error_code);
if (error_code) {
sock.close();
if (sock.is_open()) {
sock.close();
}
std::cout << "error when read login-id" << std::endl;
return ;
}
Expand All @@ -89,19 +92,16 @@ void Client::send_login_packet(packet_ptr packet) {
}
else
{
// good_client_is_registred();
emit send_input_code(InputCode::RegistrOK);
}
else {
if (response->get_loginid()==-1) {
// emit bad_client_is_autorisation();
emit send_input_code(InputCode::IncorrectAutor);
this->close();
return;
}
else
{
// good_client_is_autorisation();
emit send_input_code(InputCode::AutorOK);
}
}
Expand Down Expand Up @@ -154,7 +154,9 @@ void Client::read_response_header() {
}
}
else {
sock.close();
if (sock.is_open()) {
sock.close();
}
}
});
}
Expand All @@ -165,12 +167,12 @@ void Client::read_response_data(registr_response_ptr packet) {
boost::asio::async_read(sock, boost::asio::buffer(packet->get_data(), packet->get_length_data()),
[this, packet](boost::system::error_code error, std::size_t) {
if (!error) {
// std::cout << "read_response_data" << std::endl;

read_response_header();
}
else {
sock.close();
if (sock.is_open()) {
sock.close();
}
}
});
}
Expand All @@ -181,11 +183,12 @@ void Client::read_response_data(autor_response_ptr packet) {
boost::asio::async_read(sock, boost::asio::buffer(packet->get_data(), packet->get_length_data()),
[this, packet](boost::system::error_code error, std::size_t) {
if (!error) {
// std::cout << "read_response_data" << std::endl;
read_response_header();
}
else {
sock.close();
if (sock.is_open()) {
sock.close();
}
}
});
}
Expand All @@ -204,14 +207,13 @@ void Client::read_response_data(text_response_ptr packet) {
read_response_header();
}
else {
sock.close();
if (sock.is_open()) {
sock.close();
}
}
});
}

//void Client::read_response_join_room(join_room_request_ptr) {

//}
void Client::send_request_header() {
boost::asio::async_write(sock, boost::asio::buffer(packets_to_server.front()->get_header(), Block::Header),
[this](boost::system::error_code ec, std::size_t) {
Expand All @@ -220,7 +222,9 @@ void Client::send_request_header() {
}
else {
std::cout << "error start_sending" << std::endl;
sock.close();
if (sock.is_open()) {
sock.close();
}
}
});
}
Expand All @@ -234,7 +238,9 @@ void Client::send_request_data() {
if (!packets_to_server.empty()) send_request_header();
}
else {
sock.close();
if (sock.is_open()) {
sock.close();
}
}
});
}
4 changes: 3 additions & 1 deletion src/client/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class Client: public QObject {
*/
void close() {
boost::asio::post(io_service, [this]() {
sock.close();
if (sock.is_open()) {
sock.close();
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/control/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Control::Control() {
void Control::connect_to_server(const std::string& login, const std::string& password, TypeCommand command) {
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
auto endpoints = resolver.resolve("127.0.0.1", "7777");
auto endpoints = resolver.resolve("172.17.0.1", "7777");

input_request_ptr request;
if (command==TypeCommand::RegistrationRequest) {
Expand Down
4 changes: 4 additions & 0 deletions src/client/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void MainWindow::on_push_autorisation_clicked()
password = ui->password->text();
if (logon.isEmpty() || password.isEmpty()) return;
send_autorisation_info(logon.toStdString(), password.toStdString());
ui->logon->clear();
ui->password->clear();
}

void MainWindow::on_push_send_clicked()
Expand Down Expand Up @@ -68,6 +70,8 @@ void MainWindow::on_push_registration_clicked()
password = ui->password->text();
if (logon.isEmpty() || password.isEmpty()) return;
send_registration_info(logon.toStdString(), password.toStdString());
ui->logon->clear();
ui->password->clear();
}

void MainWindow::on_push_change_room_id_clicked()
Expand Down
3 changes: 2 additions & 1 deletion src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(CHANNELS_SRC
set(CONNECTIONS_SRC
${DIR_CONNECTION}/isubscriber.h ${DIR_CONNECTION}/isubscriber.cpp
${DIR_CONNECTION}/connection.h ${DIR_CONNECTION}/connection.cpp
${DIR_CONNECTION}/connection_manager.h ${DIR_CONNECTION}/connection_manager.cpp
)

#****************************************************************************************************
Expand All @@ -38,7 +39,7 @@ set(SERVER_SOURCES
add_executable(${SERVER_BIN} ${SERVER_SOURCES})
target_include_directories(${SERVER_BIN} PUBLIC .)
#****************************************************************************************************
find_package(Boost COMPONENTS system date_time program_options regex REQUIRED)
find_package(Boost COMPONENTS system date_time REQUIRED)

set_target_properties(${SERVER_BIN} PROPERTIES
CXX_STANDARD 17
Expand Down
1 change: 1 addition & 0 deletions src/server/channel/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void Channel::join(subscriber_ptr new_subsciber) {
}

void Channel::leave(subscriber_ptr subsciber) {
subsciber->set_busy(false);
mutex_subs.lock();
subscribers.erase(subsciber->get_client_id());
mutex_subs.unlock();
Expand Down
5 changes: 4 additions & 1 deletion src/server/channel/channels_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

ChannelsManager::ChannelsManager()
{
LOG4CPLUS_INFO(logger, "create ChannelsManager");
}

void ChannelsManager::join(subscriber_ptr new_sub, identifier_t room_id, database_ptr db) {
Expand All @@ -28,7 +29,7 @@ void ChannelsManager::join(subscriber_ptr new_sub, identifier_t room_id, databas
else {
LOG4CPLUS_INFO(logger,
"New subsciber client_id="<< it2->first
<< "in room_id=" << it2->second);
<< " in room_id=" << it2->second);
}
}

Expand Down Expand Up @@ -60,5 +61,7 @@ void ChannelsManager::leave(subscriber_ptr sub) {
else {
LOG4CPLUS_ERROR(logger, "no room room_id=" << room_id);
}

sub->set_busy(false);
}

38 changes: 25 additions & 13 deletions src/server/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,38 @@ void Connection::read_request_header() {

switch (request->get_type_data()) {
case TypeCommand::Unknown:
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << "--> ");
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << ": ");
break;
case TypeCommand::RegistrationRequest:
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << "--> ");
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << ": ");
read_request_body(std::make_shared<RegistrationRequest>(request));
break;
case TypeCommand::RegistrationResponse:
case TypeCommand::AuthorisationRequest:
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << "--> ");
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << ": ");
read_request_body(std::make_shared<AutorisationRequest>(request));
break;
case TypeCommand::AutorisationResponse:
case TypeCommand::EchoRequest:
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << "--> ");
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << ": ");
read_request_body(std::make_shared<TextRequest>(request));
break;
case TypeCommand::EchoResponse:
case TypeCommand::JoinRoomRequest:
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << "--> ");
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << ": ");
read_request_body(std::make_shared<JoinRoomRequest>(request));
break;
case TypeCommand::JoinRoomResponse:
case TypeCommand::LeaveRoomRequest:
default:
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << "--> ");
LOG4CPLUS_INFO(logger, get_command_str(request->get_type_data()) << ": ");
break;
}
} else {
ChannelsManager::Instance().leave(shared_from_this());
socket.close();
if (socket.is_open()) {
socket.close();
}
}
});

Expand All @@ -73,6 +75,7 @@ void Connection::read_request_body(registr_request_ptr request) {
else {
LOG4CPLUS_WARN(logger, "this client was add to db early");
client_id=-1;
busy = false;
}

input_res_ptr response = std::make_shared<RegistrationResponse>(client_id);
Expand All @@ -89,7 +92,9 @@ void Connection::read_request_body(registr_request_ptr request) {
}
else {
ChannelsManager::Instance().leave(shared_from_this());
socket.close();
if (socket.is_open()) {
socket.close();
}
}
});
}
Expand All @@ -105,7 +110,6 @@ void Connection::read_request_body(autor_request_ptr request) {
LOG4CPLUS_INFO(logger, "login=" << request->get_login() << ", pwd=" << request->get_password());

client_id = db->check_client(login, password);

input_res_ptr response = std::make_shared<AutorisationResponse>(client_id);
LOG4CPLUS_INFO(logger,
"AutorisationResponse: vers=" << response->get_protocol_version() << ", command="
Expand All @@ -114,15 +118,19 @@ void Connection::read_request_body(autor_request_ptr request) {

if (client_id!=-1) {
db->add_logins(login, response->get_loginid(), password);
} else {
busy = false;
}
boost::asio::write(socket, boost::asio::buffer(response->get_header(), Block::Header));
boost::asio::write(socket, boost::asio::buffer(response->get_data(), response->get_length_data()));

read_request_header();
if (client_id!=-1) read_request_header();
}
else {
ChannelsManager::Instance().leave(shared_from_this());
socket.close();
if (socket.is_open()) {
socket.close();
}
}
});
}
Expand All @@ -145,7 +153,9 @@ void Connection::read_request_body(text_request_ptr request) {
}
else {
ChannelsManager::Instance().leave(shared_from_this());
socket.close();
if (socket.is_open()) {
socket.close();
}
}
});

Expand All @@ -167,7 +177,9 @@ void Connection::read_request_body(join_room_request_ptr request) {
}
else {
ChannelsManager::Instance().leave(shared_from_this());
socket.close();
if (socket.is_open()) {
socket.close();
}
}
});

Expand Down
Loading

0 comments on commit a8c2f5e

Please sign in to comment.