mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-07-27 18:47:55 +00:00
Added message MtClientReady in MtService
This commit is contained in:
+23
-2
@@ -12,7 +12,8 @@ enum MessageType
|
||||
EVENT = 2,
|
||||
EXPERT_LIST = 3,
|
||||
EXPERT_ADDED = 4,
|
||||
EXPERT_REMOVED = 5
|
||||
EXPERT_REMOVED = 5,
|
||||
CLIENT_READY = 6
|
||||
};
|
||||
|
||||
class MtMessage
|
||||
@@ -23,8 +24,9 @@ public:
|
||||
return std::to_string(GetType()) + MT_MESSAGE_DELIMETER + GetBody();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual MessageType GetType() const = 0;
|
||||
|
||||
protected:
|
||||
virtual std::string GetBody() const = 0;
|
||||
};
|
||||
|
||||
@@ -101,6 +103,25 @@ private:
|
||||
std::string payload_;
|
||||
};
|
||||
|
||||
class MtClientReady : public MtMessage
|
||||
{
|
||||
public:
|
||||
MtClientReady()
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
MessageType GetType() const override
|
||||
{
|
||||
return MessageType::CLIENT_READY;
|
||||
}
|
||||
|
||||
std::string GetBody() const override
|
||||
{
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
class MtEvent : public MtMessage
|
||||
{
|
||||
public:
|
||||
|
||||
+46
-21
@@ -2,6 +2,7 @@
|
||||
#include "MtServer.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <boost/spirit/include/qi.hpp>
|
||||
#include <boost/phoenix/core.hpp>
|
||||
#include <boost/phoenix/operator.hpp>
|
||||
@@ -123,13 +124,6 @@ void MtServer::OnAccept(boost::beast::error_code ec,
|
||||
pending_connections_.erase(it);
|
||||
connections_.push_back(connection);
|
||||
log_.Trace("%s: Pushed connection %p to collection", __FUNCTION__, connection.get());
|
||||
|
||||
std::vector<int> expert_list;
|
||||
for (const auto& e : experts_)
|
||||
expert_list.push_back(e.first);
|
||||
|
||||
MtExpertListMsg msg(std::move(expert_list));
|
||||
connection->Send(msg.Serialize());
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -172,27 +166,56 @@ void MtServer::OnAccept(boost::beast::error_code ec,
|
||||
});
|
||||
connection->OnMessageReceived.connect([con = connection->weak_from_this(), this](const std::string& msg) {
|
||||
log_.Trace("MtServer::OnMessageReceived: %s", msg.c_str());
|
||||
auto command = ParseMessage(msg);
|
||||
if (command)
|
||||
|
||||
std::string::size_type pos = msg.find(';');
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
if (experts_.count(command->getExpertHandle()) > 0)
|
||||
log_.Warning("MtServer::OnMessageReceived: invalid message type.");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string msg_type_str = msg.substr(0, pos);;
|
||||
auto msg_type = std::atoi(msg_type_str.c_str());
|
||||
if (msg_type == MessageType::COMMAND)
|
||||
{
|
||||
auto command = ParseCommand(msg);
|
||||
if (command)
|
||||
{
|
||||
experts_[command->getExpertHandle()]->expert->Process(std::move(command), [&](const MtResponse& response) {
|
||||
auto connection = con.lock();
|
||||
if (connection)
|
||||
connection->Send(response.Serialize());
|
||||
else
|
||||
log_.Warning("MtServer::OnMessageReceived: connection is not valid (null)");
|
||||
});
|
||||
if (experts_.count(command->getExpertHandle()) > 0)
|
||||
{
|
||||
experts_[command->getExpertHandle()]->expert->Process(std::move(command), [&](const MtResponse& response) {
|
||||
auto connection = con.lock();
|
||||
if (connection)
|
||||
connection->Send(response.Serialize());
|
||||
else
|
||||
log_.Warning("MtServer::OnMessageReceived: connection is not valid (null)");
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
log_.Warning("MtServer::OnMessageReceived: Expert is not found with handle %d", command->getExpertHandle());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log_.Warning("MtServer::OnMessageReceived: Expert is not found with handle %d", command->getExpertHandle());
|
||||
log_.Warning("MtServer::OnMessageReceived: Failed to parse command from message: %s", msg.c_str());
|
||||
}
|
||||
}
|
||||
else if (msg_type == MessageType::CLIENT_READY)
|
||||
{
|
||||
std::vector<int> expert_list;
|
||||
for (const auto& e : experts_)
|
||||
expert_list.push_back(e.first);
|
||||
|
||||
MtExpertListMsg msg(std::move(expert_list));
|
||||
auto connection = con.lock();
|
||||
if (connection)
|
||||
connection->Send(msg.Serialize());
|
||||
}
|
||||
else
|
||||
{
|
||||
log_.Warning("MtServer::OnMessageReceived: Failed to parse command from message: %s", msg.c_str());
|
||||
log_.Warning("MtServer::OnMessageReceived: unsupported message type %d", msg_type);
|
||||
return;
|
||||
}
|
||||
});
|
||||
pending_connections_.push_back(connection);
|
||||
@@ -203,9 +226,9 @@ void MtServer::OnAccept(boost::beast::error_code ec,
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<MtCommand> MtServer::ParseMessage(const std::string& msg)
|
||||
std::unique_ptr<MtCommand> MtServer::ParseCommand(const std::string& msg)
|
||||
{
|
||||
std::unique_ptr<MtCommand> command;
|
||||
std::string::size_type pos = msg.find(';');
|
||||
|
||||
using namespace boost::spirit;
|
||||
using rule_s = qi::rule<std::string::const_iterator, std::string()>;
|
||||
@@ -224,6 +247,8 @@ std::unique_ptr<MtCommand> MtServer::ParseMessage(const std::string& msg)
|
||||
rule command_type_p = +qi::char_(';') >> word_p[boost::phoenix::ref(command_type) = qi::_1];
|
||||
rule payload_p = +qi::char_(';') >> word_p[boost::phoenix::ref(payload) = qi::_1];
|
||||
|
||||
std::unique_ptr<MtCommand> command;
|
||||
|
||||
bool ok = qi::parse(msg.begin(), msg.end(), message_type_p >> -expert_handle_p >> -command_id_p >> -command_type_p >> -payload_p);
|
||||
if (ok)
|
||||
{
|
||||
|
||||
@@ -48,7 +48,7 @@ private:
|
||||
void OnAccept(boost::beast::error_code ec,
|
||||
boost::asio::ip::tcp::socket socket);
|
||||
|
||||
std::unique_ptr<MtCommand> ParseMessage(const std::string& msg);
|
||||
std::unique_ptr<MtCommand> ParseCommand(const std::string& msg);
|
||||
|
||||
void OnEvent(const MtEvent& event);
|
||||
void OnDeinitExpert(int handle);
|
||||
|
||||
Reference in New Issue
Block a user