This commit is contained in:
2025-05-09 21:47:00 +09:00
parent 78f2bfe2f6
commit 0aa312f67e
6 changed files with 41 additions and 59 deletions

View File

@@ -14,11 +14,6 @@ public:
template<typename _Callable>
void init(_Callable _IOCPClient) {
roomNames_ = std::future<std::unordered_map<Snowflake, std::string>>();
findRoomId_ = std::future<std::unordered_map<std::string, Snowflake>>();
userNames_ = std::future<std::unordered_map<Snowflake, std::string>>();
findUserId_ = std::future<std::unordered_map<std::string, Snowflake>>();
auto config = ConfigManager::load();
log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
threadPool_.init(0);
@@ -102,11 +97,11 @@ private:
std::queue<std::string> messageQueue_;
std::vector<std::string> messageHistory_;
std::future<std::unordered_map<Snowflake, std::string>> roomNames_;
std::future<std::unordered_map<std::string, Snowflake>> findRoomId_;
std::unordered_map<Snowflake, std::string> roomNames_;
std::unordered_map<std::string, Snowflake> findRoomId_;
std::future<std::unordered_map<Snowflake, std::string>> userNames_;
std::future<std::unordered_map<std::string, Snowflake>> findUserId_;
std::unordered_map<Snowflake, std::string> userNames_;
std::unordered_map<std::string, Snowflake> findUserId_;
};
}

View File

@@ -181,34 +181,11 @@ void ClientManager::processRoomExitResponsePacket(RoomExitResponsePacket roomExi
}
void ClientManager::processUsersListResponsePacket(UsersListResponsePacket usersListResponsePacket, Chattr::IOCPPASSINDATA* data) {
std::unordered_map<Snowflake, std::string> userNames;
if (userNames_.valid() && userNames_.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
userNames = userNames_.get();
else
userNames = std::unordered_map<Snowflake, std::string>();
auto userNamesPromise = std::promise<std::unordered_map<Snowflake, std::string>>();
userNames_ = userNamesPromise.get_future();
std::unordered_map<std::string, Snowflake> findUserId;
if (findUserId_.valid() && findUserId_.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
findUserId = findUserId_.get();
else
findUserId = std::unordered_map<std::string, Snowflake>();
auto findUserIdPromise = std::promise<std::unordered_map<std::string, Snowflake>>();
findUserId_ = findUserIdPromise.get_future();
Snowflake userId;
::memcpy(&userId.snowflake, usersListResponsePacket.__data.userId, sizeof(Snowflake));
std::string userName((char*)usersListResponsePacket.__data.name, usersListResponsePacket.__data.packetLength - (sizeof(std::uint16_t) * 7));
userNames[userId] = userName;
findUserId[userName] = userId;
userNamesPromise.set_value(userNames);
findUserIdPromise.set_value(findUserId);
userNames_[userId] = userName;
findUserId_[userName] = userId;
spdlog::info("{}", userName);
}
@@ -216,8 +193,7 @@ void ClientManager::processUsersListResponsePacket(UsersListResponsePacket users
void ClientManager::processDataPostPacket(DataPostPacket dataPostPacket, IOCPPASSINDATA* data) {
Snowflake sentUserId;
::memcpy(&sentUserId.snowflake, dataPostPacket.__data.destId, sizeof(Snowflake));
auto UNames = userNames_.get();
std::string sentUserName = UNames[sentUserId];
std::string sentUserName = userNames_[sentUserId];
std::string message((char*)dataPostPacket.__data.data, dataPostPacket.__data.packetLength - (sizeof(std::uint16_t) * 5));
spdlog::info("[{}] {}", sentUserName, message); // todo: pass data to main thread
@@ -324,8 +300,7 @@ void ClientManager::run() {
std::unique_lock<std::mutex> lock2(screenMutex_);
if (inRoom_) {
auto RNames = roomNames_.get();
std::cout << "[" + nickname + "@" + RNames[myRoomID_] + "]" + " ";
std::cout << "[" + nickname + "@" + roomNames_[myRoomID_] + "]" + " ";
}
else {
std::cout << "[" + nickname + "]" + " ";
@@ -339,20 +314,18 @@ void ClientManager::run() {
continue;
if (tokens[0] == "/w") {
auto findUserId = findUserId_.get();
if (findUserId.find(tokens[1]) == findUserId.end()) {
if (findUserId_.find(tokens[1]) == findUserId_.end()) {
resourceMutex_.lock();
messageQueue_.push("User not found");
resourceMutex_.unlock();
continue;
}
Snowflake destId = findUserId[tokens[1]];
Snowflake destId = findUserId_[tokens[1]];
std::string message;
for (int i = 2; i < tokens.size(); i++)
message += tokens[i];
message += tokens[i] + " ";
sendMessage(destId, message);
}
@@ -360,20 +333,14 @@ void ClientManager::run() {
std::string roomName;
for (int i = 1; i < tokens.size(); i++)
roomName += tokens[i];
if (findRoomId_.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
resourceMutex_.lock();
messageQueue_.push("Request room list first");
resourceMutex_.unlock();
continue;
}
auto findRoomId = findRoomId_.get();
if (findRoomId.find(roomName) == findRoomId.end()) {
if (findRoomId_.find(roomName) == findRoomId_.end()) {
resourceMutex_.lock();
messageQueue_.push("Room not found");
resourceMutex_.unlock();
continue;
}
joinRoom(myID_, findRoomId[roomName]);
joinRoom(myID_, findRoomId_[roomName]);
}
else if (tokens[0] == "/leave") {
if (!inRoom_) {