Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kürzeder committed Sep 11, 2019
1 parent 91e50ae commit c2fe822
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 92 deletions.
2 changes: 1 addition & 1 deletion cpp_redis
Submodule cpp_redis updated 2 files
+1 −0 cpp_redis.lua
+1 −1 premake5.lua
8 changes: 4 additions & 4 deletions module/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Module
inline void RemoveLuaVM(lua_State* luaVM) { _luaStates.erase(luaVM); }
inline bool HasLuaVM(lua_State* luaVM) { return _luaStates.find(luaVM) != _luaStates.end(); }

inline void AddRedisClient(redis_client* client) { _redisClients.insert(client); }
inline void RemoveRedisClient(redis_client* client) { _redisClients.erase(client); }
inline bool HasRedisClient(redis_client* client) { return _redisClients.find(client) != _redisClients.end(); }
inline void AddRedisClient(ml_redis::redis_client* client) { _redisClients.insert(client); }
inline void RemoveRedisClient(ml_redis::redis_client* client) { _redisClients.erase(client); }
inline bool HasRedisClient(ml_redis::redis_client* client) { return _redisClients.find(client) != _redisClients.end(); }

inline JobManager<const std::optional<std::any>>& GetJobManager() { return _jobManager; }

Expand All @@ -35,7 +35,7 @@ class Module
ILuaModuleManager* _moduleManager;
JobManager<const std::optional<std::any>> _jobManager;
std::unordered_set<lua_State*> _luaStates;
std::unordered_set<redis_client*> _redisClients;
std::unordered_set<ml_redis::redis_client*> _redisClients;
};

extern Module* g_Module;
159 changes: 81 additions & 78 deletions module/RedisClient.cpp
Original file line number Diff line number Diff line change
@@ -1,112 +1,115 @@
#include "RedisClient.h"
#include "Module.h"

redis_client::redis_client()
namespace ml_redis
{
_client = new cpp_redis::client();
_subscriber = new cpp_redis::subscriber();
redis_client::redis_client()
{
_client = new cpp_redis::client();
_subscriber = new cpp_redis::subscriber();

g_Module->AddRedisClient(this);
}
g_Module->AddRedisClient(this);
}

redis_client::~redis_client()
{
g_Module->RemoveRedisClient(this);
redis_client::~redis_client()
{
g_Module->RemoveRedisClient(this);

delete _client;
delete _subscriber;
}
delete _client;
delete _subscriber;
}

void redis_client::authenticate(const std::string& password, const cpp_redis::client::reply_callback_t& client_callback,
const cpp_redis::client::reply_callback_t& subscriber_callback) const
{
_client->auth(password, client_callback);
_subscriber->auth(password, subscriber_callback);
void redis_client::authenticate(const std::string& password, const cpp_redis::client::reply_callback_t& client_callback,
const cpp_redis::client::reply_callback_t& subscriber_callback) const
{
_client->auth(password, client_callback);
_subscriber->auth(password, subscriber_callback);

_client->commit();
_subscriber->commit();
}
_client->commit();
_subscriber->commit();
}

cpp_redis::reply redis_client::set(const std::string& key, const std::string& value) const
{
auto result = _client->set(key, value);
_client->commit();
cpp_redis::reply redis_client::set(const std::string& key, const std::string& value) const
{
auto result = _client->set(key, value);
_client->commit();

result.wait();
return result.get();
}
result.wait();
return result.get();
}

void redis_client::set(const std::map<std::string, std::string>& pairs, const std::function<void(const std::string&, cpp_redis::reply&)>& callback) const
{
for(auto& [key, value] : pairs)
void redis_client::set(const std::map<std::string, std::string>& pairs, const std::function<void(const std::string&, cpp_redis::reply&)>& callback) const
{
_client->set(key, value, [key, callback](cpp_redis::reply& reply)
for(auto& [key, value] : pairs)
{
callback(key, reply);
});
_client->set(key, value, [key, callback](cpp_redis::reply& reply)
{
callback(key, reply);
});
}
_client->commit();
}
_client->commit();
}

cpp_redis::reply redis_client::get(const std::string& key) const
{
auto result = _client->get(key);
_client->commit();
cpp_redis::reply redis_client::get(const std::string& key) const
{
auto result = _client->get(key);
_client->commit();

result.wait();
return result.get();
}
result.wait();
return result.get();
}

void redis_client::get(const std::list<std::string>& keys, const std::function<void(const std::string&, cpp_redis::reply &)>& callback) const
{
for (auto& key : keys)
void redis_client::get(const std::list<std::string>& keys, const std::function<void(const std::string&, cpp_redis::reply &)>& callback) const
{
_client->get(key, [key, callback](cpp_redis::reply& reply)
for (auto& key : keys)
{
callback(key, reply);
});
_client->get(key, [key, callback](cpp_redis::reply& reply)
{
callback(key, reply);
});
}
_client->commit();
}
_client->commit();
}

void redis_client::subscribe(const std::list<std::string>& channels,
const cpp_redis::subscriber::subscribe_callback_t& callback) const
{
for (auto& channel : channels)
void redis_client::subscribe(const std::list<std::string>& channels,
const cpp_redis::subscriber::subscribe_callback_t& callback) const
{
_subscriber->subscribe(channel, callback);
}
for (auto& channel : channels)
{
_subscriber->subscribe(channel, callback);
}

_subscriber->commit();
}
_subscriber->commit();
}

void redis_client::unsubscribe(const std::list<std::string>& channels) const
{
for (auto& channel : channels)
void redis_client::unsubscribe(const std::list<std::string>& channels) const
{
_subscriber->unsubscribe(channel);
for (auto& channel : channels)
{
_subscriber->unsubscribe(channel);
}
_subscriber->commit();
}
_subscriber->commit();
}

cpp_redis::reply redis_client::publish(const std::string& channel, const std::string& message) const
{
auto result = _client->publish(channel, message);
_client->commit();
cpp_redis::reply redis_client::publish(const std::string& channel, const std::string& message) const
{
auto result = _client->publish(channel, message);
_client->commit();

result.wait();
return result.get();
}
result.wait();
return result.get();
}

void redis_client::publish(const std::map<std::string, std::string>& pairs,
const std::function<void(const std::string&, cpp_redis::reply&)>& callback) const
{
for (auto& [channel, message] : pairs)
void redis_client::publish(const std::map<std::string, std::string>& pairs,
const std::function<void(const std::string&, cpp_redis::reply&)>& callback) const
{
_client->publish(channel, message, [channel, callback](cpp_redis::reply& reply)
for (auto& [channel, message] : pairs)
{
callback(channel, reply);
});
_client->publish(channel, message, [channel, callback](cpp_redis::reply& reply)
{
callback(channel, reply);
});
}
_client->commit();
}
_client->commit();
}
13 changes: 4 additions & 9 deletions module/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ project "module"
includedirs { "include" }
libdirs { "lib" }

dependson { "cpp_redis" }
links { "cpp_redis" }

vpaths {
["Headers/*"] = "**.h",
["Sources/*"] = "**.cpp",
Expand All @@ -23,20 +26,12 @@ project "module"
debugdir "../mta-server"

filter { "system:windows", "platforms:x86" }
links { "lua5.1.lib" }
links { "tacopie.lib" }
links { "cpp_redis.lib" }
debugcommand "../mta-server/MTA Server.exe"

filter { "system:windows", "platforms:x64" }
links { "lua5.1_64.lib" }
links { "tacopie_64.lib" }
links { "cpp_redis_64.lib" }
debugcommand "../mta-server/MTA Server64.exe"

filter "system:linux"
links { "tacopie" }
links { "cpp_redis" }
-- filter "system:linux"

filter "system:not linux"
excludes { "luaimports/luaimports.linux.h", "luaimports/luaimports.linux.cpp" }
1 change: 1 addition & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ solution "Redis"
optimize "On"

include "module"
include "cpp_redis/cpp_redis"
--include "test"

0 comments on commit c2fe822

Please sign in to comment.