cpp-console-game/eRPC/Response.cpp

61 lines
1005 B
C++
Raw Normal View History

2024-07-05 20:37:02 +01:00
#include "Response.hpp"
#include <sstream>
namespace eRPC
{
Response::Response() : msgid(-1), ok(false), result()
{
}
Response::Response(int msgid, bool ok, std::string result)
: msgid(msgid), ok(ok), result(result)
{
}
Response::Response(std::string msg) : msgid(-1), ok(false), result()
{
std::string line;
std::istringstream iss(msg);
std::getline(iss, line);
if (line != "eRPC 1.0")
{
throw std::runtime_error("Invalid message");
}
std::getline(iss, line);
msgid = std::stoi(line);
std::getline(iss, line);
ok = (line == "OK");
std::getline(iss, result);
}
std::string Response::serialize()
{
std::string msg = "eRPC 1.0\n";
msg += std::to_string(msgid) + "\n";
msg += ok ? "OK\n" : "ERROR\n";
msg += result;
return msg;
}
int Response::getMsgid()
{
return msgid;
}
bool Response::isOk()
{
return ok;
}
std::string Response::getResult()
{
return result;
}
}