53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#include "base.h"
|
|
|
|
#include <sstream>
|
|
#include <strings.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <memory>
|
|
|
|
namespace Yc {
|
|
namespace Lib {
|
|
|
|
std::string Base::getJsonString(Json::Value json) {
|
|
std::string outString;
|
|
std::stringstream outStream;
|
|
outStream << json;
|
|
return outStream.str();
|
|
}
|
|
|
|
void Base::send(int socket, std::string out) {
|
|
write(socket, out.c_str(), out.length());
|
|
}
|
|
|
|
void Base::send(int socket, Json::Value out) {
|
|
std::string outString = getJsonString(out);
|
|
send(socket, outString);
|
|
}
|
|
|
|
std::string Base::readSocket(int socket)
|
|
{
|
|
std::string msg("");
|
|
char buffer[256];
|
|
bzero(buffer, 256);
|
|
while (int received = recv(socket, buffer, 255, 0) > 0) {
|
|
msg += std::string(buffer);
|
|
if (received < 255) {
|
|
break;
|
|
}
|
|
}
|
|
return msg;
|
|
}
|
|
|
|
Json::Value Base::getJsonTree(std::string msg) {
|
|
Json::Value inputTree;
|
|
Json::CharReaderBuilder rbuilder;
|
|
std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
|
|
JSONCPP_STRING inputJsonString(msg);
|
|
reader->parse(inputJsonString.data(), inputJsonString.data() + inputJsonString.size(), &inputTree, NULL);
|
|
return inputTree;
|
|
}
|
|
|
|
} // namespace Lib
|
|
} // namespace Yc
|