-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <spdlog/spdlog.h> | ||
#include <string> | ||
|
||
#include "manager.h" | ||
|
||
namespace mtrx { | ||
namespace udp2p { | ||
|
||
auto logger = spdlog::default_logger()->clone("udp2p"); | ||
|
||
bool AuthManager::updateUser(const std::string & id, const std::string & hash) { | ||
if (userdb_.contains(id)) | ||
logger->info("update user: {}", id); | ||
else | ||
logger->info("add user: {}", id); | ||
|
||
userdb_[id] = hash; | ||
return true; | ||
} | ||
|
||
bool AuthManager::rmUser(const std::string & id) { | ||
auto it = userdb_.find(id); | ||
if (it == userdb_.end()) { | ||
logger->error("no such user: {}", id); | ||
return false; | ||
} | ||
|
||
userdb_.erase(it); | ||
return true; | ||
} | ||
|
||
bool AuthManager::authUser(const std::string & id, const std::string & hash) { | ||
auto it = userdb_.find(id); | ||
if (it == userdb_.end()) { | ||
logger->error("no such user: {}", id); | ||
return false; | ||
} | ||
return it->second == hash; | ||
} | ||
|
||
bool EndpointManager::updateEndpoint(const std::string & id, const EndPoint & endpoint) { | ||
if (onlineMap_.contains(id)) | ||
logger->info("update endpoint: {}", id); | ||
else | ||
logger->info("add endpoint: {}", id); | ||
|
||
onlineMap_[id] = endpoint; | ||
return true; | ||
} | ||
|
||
bool EndpointManager::rmEndpoint(const std::string & id) { | ||
auto it = onlineMap_.find(id); | ||
if (it == onlineMap_.end()) { | ||
logger->error("no such endpoint: {}", id); | ||
return false; | ||
} | ||
|
||
onlineMap_.erase(it); | ||
return true; | ||
}; | ||
|
||
const EndPoint & EndpointManager::getEndpointByID(const std::string & id) { | ||
|
||
auto it = onlineMap_.find(id); | ||
if (it == onlineMap_.end()) { | ||
logger->warn("no such endpoint: {}", id); | ||
static const EndPoint endpoint_zero{}; | ||
return endpoint_zero; | ||
} | ||
|
||
return onlineMap_[id]; | ||
} | ||
|
||
} // namespace udp2p | ||
} // namespace mtrx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef INCLUDE_UDP2P_MANAGER_H | ||
#define INCLUDE_UDP2P_MANAGER_H | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include <asio.hpp> | ||
|
||
namespace mtrx { | ||
namespace udp2p { | ||
|
||
struct EndPoint { | ||
asio::ip::address_v4 ip4; | ||
int port; | ||
}; | ||
|
||
class AuthManager { | ||
public: | ||
AuthManager(){}; | ||
~AuthManager(){}; | ||
|
||
bool authUser(const std::string & id, const std::string & hash); | ||
bool updateUser(const std::string & id, const std::string & hash); | ||
bool rmUser(const std::string & id); | ||
|
||
private: | ||
std::map<std::string, std::string> userdb_; | ||
}; | ||
|
||
class EndpointManager { | ||
public: | ||
EndpointManager(){}; | ||
~EndpointManager(){}; | ||
|
||
bool updateEndpoint(const std::string & id, const EndPoint & endpoint); | ||
bool rmEndpoint(const std::string & id); | ||
const EndPoint & getEndpointByID(const std::string & id); | ||
|
||
private: | ||
std::map<std::string, EndPoint> onlineMap_; | ||
}; | ||
|
||
}; // namespace udp2p | ||
}; // namespace mtrx | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
udp2p_deps = [ | ||
dependency('asio'), | ||
] |