Refactor chat system: Introduce ChatRoom and ChatUser classes
- Created ChatRoom class to manage chat room functionalities, including user management, message handling, and game mechanics. - Developed ChatUser class to represent individual users, handling user-specific actions and interactions within chat rooms. - Implemented a Config class for loading configuration settings from a JSON file. - Established a Server class to manage connections, handle requests, and facilitate communication between users and chat rooms. - Introduced a Database class for database interactions, utilizing PostgreSQL for user and room data management. - Added utility functions in the Base class for JSON handling and socket communication. - Created Object classes for Room and User to encapsulate their properties and behaviors. - Updated main function to initialize server and load chat rooms from configuration.
This commit is contained in:
53
src/lib/base.cpp
Executable file
53
src/lib/base.cpp
Executable file
@@ -0,0 +1,53 @@
|
||||
#include "base.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user