Skip to content

Commit b64bb55

Browse files
committed
minor bug fixes
1 parent 6a49d33 commit b64bb55

File tree

8 files changed

+108
-66
lines changed

8 files changed

+108
-66
lines changed

ASyncRedis.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ ASyncRedis::ASyncRedis(int _intval, int cache)
5252
cache_size = cache;
5353

5454
loop = aeCreateEventLoop(1024 * 10);
55-
evMain = std::thread([this] {
56-
aeCreateTimeEvent(loop, interval, timeEventCallback, this, nullptr);
55+
aeCreateTimeEvent(loop, interval, timeEventCallback, this, nullptr);
5756

57+
evMain = std::thread([this] {
5858
aeMain(loop);
5959
aeDeleteEventLoop(loop);
6060
loop = nullptr;
@@ -65,20 +65,17 @@ ASyncRedis::ASyncRedis(int _intval, int cache)
6565

6666
ASyncRedis::~ASyncRedis()
6767
{
68-
redisAsyncDisconnect(ac);
69-
7068
if (loop) {
7169
aeStop(loop);
72-
if (evMain.joinable()) {
73-
evMain.join();
74-
}
7570
}
76-
loop = nullptr;
7771

78-
if (ac) {
72+
if (evMain.joinable()) {
73+
evMain.join();
74+
}
75+
76+
if (!disconnected && ac) {
7977
redisAsyncFree(ac);
8078
}
81-
ac = nullptr;
8279
}
8380

8481
bool ASyncRedis::WaitForConnected()

SMASyncRedis.cpp

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,20 @@ static inline redisReply *CopyReply(redisReply *src)
3232
return reply;
3333
}
3434

35-
void SMASyncRedis::CmdCallback(const redisAsyncContext *c, void *r, void *privdata)
36-
{
37-
if (privdata) {
38-
CmdCBData *data = (CmdCBData *)privdata;
39-
data->reply = CopyReply((redisReply *)r);
40-
41-
OutputDebugString(std::to_string((int)data->reply).c_str());
42-
43-
g_HiredisExt.RequestFrame(std::bind(&SMASyncRedis::_cmdcb, this, std::placeholders::_1), privdata);
44-
}
45-
}
46-
47-
void SMASyncRedis::_cmdcb(void *data)
35+
static void _cmdcb(void *data)
4836
{
49-
CmdCBData *d = (CmdCBData *)data;
37+
CBData *d = (CBData *)data;
5038
IPluginFunction *cb = d->callback;
5139
if (cb->IsRunnable()) {
5240
HandleSecurity sec(d->identity, myself->GetIdentity());
5341

42+
void *obj;
43+
int err = handlesys->ReadHandle(d->handle, g_AsyncRedisCtxType, &sec, &obj);
44+
if (err != HandleError_None || obj == nullptr) {
45+
delete d;
46+
return;
47+
}
48+
5449
RedisReply *reply = new RedisReply();
5550
reply->reply = d->reply;
5651

@@ -62,7 +57,7 @@ void SMASyncRedis::_cmdcb(void *data)
6257
return;
6358
}
6459

65-
cb->PushCell(handle);
60+
cb->PushCell(d->handle);
6661
cb->PushCell(qh);
6762
cb->PushCell(d->data);
6863
cb->Execute(nullptr);
@@ -74,36 +69,62 @@ void SMASyncRedis::_cmdcb(void *data)
7469
delete data;
7570
}
7671

77-
void SMASyncRedis::ConnectCallback(int status)
72+
void SMASyncRedis::CmdCallback(const redisAsyncContext *c, void *r, void *privdata)
7873
{
79-
ASyncRedis::ConnectCallback(status);
80-
g_HiredisExt.RequestFrame(std::bind(&SMASyncRedis::_connectcb, this, std::placeholders::_1), (void *)status);
74+
if (privdata) {
75+
CBData *data = (CBData *)privdata;
76+
data->reply = CopyReply((redisReply *)r);
77+
78+
g_HiredisExt.RequestFrame(_cmdcb, privdata);
79+
}
8180
}
8281

83-
void SMASyncRedis::_connectcb(void *data)
82+
static void _connectcb(void *data)
8483
{
85-
int status = (int)data;
86-
if (connectedCb && connectedCb->IsRunnable()) {
87-
connectedCb->PushCell(handle);
88-
connectedCb->PushCell(status);
89-
connectedCb->PushCell(connectData);
90-
connectedCb->Execute(nullptr);
84+
CBData *d = (CBData *)data;
85+
IPluginFunction *cb = d->callback;
86+
if (cb && cb->IsRunnable()) {
87+
HandleSecurity sec;
88+
sec.pOwner = NULL;
89+
sec.pIdentity = myself->GetIdentity();
90+
void *obj;
91+
int err = handlesys->ReadHandle(d->handle, g_AsyncRedisCtxType, &sec, &obj);
92+
if (err != HandleError_None || obj == nullptr) {
93+
delete d;
94+
return;
95+
}
96+
97+
cb->PushCell(d->handle);
98+
cb->PushCell(d->status);
99+
cb->PushCell(d->data);
100+
cb->Execute(nullptr);
91101
}
102+
103+
delete d;
92104
}
93105

94-
void SMASyncRedis::DisconnectCallback(int status)
106+
void SMASyncRedis::ConnectCallback(int status)
95107
{
96-
ASyncRedis::DisconnectCallback(status);
97-
g_HiredisExt.RequestFrame(std::bind(&SMASyncRedis::_disconnectcb, this, std::placeholders::_1), (void *)status);
108+
ASyncRedis::ConnectCallback(status);
109+
110+
CBData *cbdata = new CBData();
111+
cbdata->handle = handle;
112+
cbdata->callback = connectedCb;
113+
cbdata->data = connectData;
114+
cbdata->status = status;
115+
116+
g_HiredisExt.RequestFrame(_connectcb, (void *)cbdata);
98117
}
99118

100-
void SMASyncRedis::_disconnectcb(void *data)
119+
void SMASyncRedis::DisconnectCallback(int status)
101120
{
102-
int status = (int)data;
103-
if (disconnectedCb && disconnectedCb->IsRunnable()) {
104-
disconnectedCb->PushCell(handle);
105-
disconnectedCb->PushCell(status);
106-
disconnectedCb->PushCell(disconnectData);
107-
disconnectedCb->Execute(nullptr);
108-
}
121+
ASyncRedis::DisconnectCallback(status);
122+
123+
CBData *cbdata = new CBData();
124+
cbdata->handle = handle;
125+
cbdata->callback = disconnectedCb;
126+
cbdata->data = disconnectData;
127+
cbdata->status = status;
128+
129+
g_HiredisExt.RequestFrame(_connectcb, (void *)cbdata);
109130
}

SMASyncRedis.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ extern "C" {
1919
#include <vector>
2020
#include <functional>
2121

22+
struct CBData
23+
{
24+
Handle_t handle;
25+
int status;
26+
IPluginFunction *callback;
27+
IdentityToken_t *identity;
28+
redisReply *reply;
29+
cell_t data;
30+
};
31+
2232
class SMASyncRedis :public ASyncRedis
2333
{
2434
public:
25-
struct CmdCBData
26-
{
27-
IPluginFunction *callback;
28-
IdentityToken_t *identity;
29-
redisReply *reply;
30-
cell_t data;
31-
};
32-
3335
SMASyncRedis(int intval = 1000, int cache_size = 32) :ASyncRedis(intval, cache_size) {}
3436

3537
IPluginFunction *connectedCb;
@@ -41,11 +43,7 @@ class SMASyncRedis :public ASyncRedis
4143
Handle_t handle;
4244

4345
void CmdCallback(const redisAsyncContext *c, void *r, void *privdata);
44-
void _cmdcb(void *data);
4546

4647
void ConnectCallback(int status);
47-
void _connectcb(void *data);
48-
4948
void DisconnectCallback(int status);
50-
void _disconnectcb(void *data);
5149
};
0 Bytes
Binary file not shown.
40 KB
Binary file not shown.

addons/sourcemod/scripting/hiredis_test.sp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ public void ConnectCallback(RedisAsync ac, int status, any data)
1515
{
1616
PrintToServer("ConnectCallback: %d %d", status, data);
1717

18+
// you can send "auth" right after calling connect
1819
ac.Command("auth foobared233");
20+
21+
for (int i = 0; i < 10; ++i) {
22+
ac.Command("lpush alsit %d", i);
23+
}
24+
1925
ac.Command("set foo %s", "wtf");
2026
ac.CommandEx(CommandCallback, 0, "get foo");
2127
ac.CommandEx(CommandCallback, 0, "lrange alist 0 -1");
@@ -29,6 +35,7 @@ public void DisconnectCallback(RedisAsync ac, int status, any data)
2935

3036
public void CommandCallback(RedisAsync ac, RedisReply reply, any data)
3137
{
38+
// This will print only one reply if
3239
PrintReply(reply);
3340
}
3441

@@ -51,6 +58,18 @@ public Action Cmd_ASyncHiredisTest(int client, int args)
5158
return Plugin_Handled;
5259
}
5360

61+
RedisAsync async2 = new RedisAsync();
62+
async2.SetConnectCallback(ConnectCallback, 444);
63+
async2.SetDisconnectCallback(DisconnectCallback, 666);
64+
65+
if (!async2.Connect("127.0.0.1", 6379)) {
66+
char buf[256];
67+
async2.GetErrorString(buf, sizeof(buf));
68+
PrintToServer("RedisAsync 2 Connect failed: %d, %s", async2.GetError(), buf);
69+
return Plugin_Handled;
70+
}
71+
delete async2; // there will be no call back for async2
72+
5473
return Plugin_Handled;
5574
}
5675

@@ -68,6 +87,19 @@ public Action Cmd_HiredisTest(int client, int args)
6887
PrintReply(reply);
6988
delete reply;
7089

90+
for (int i = 0; i < 10; ++i) {
91+
redis.AppendCommand("lpush alist %d", i);
92+
}
93+
94+
for (int i = 0; i < 10; ++i) {
95+
reply = redis.GetReply();
96+
if (reply == INVALID_HANDLE) {
97+
ThrowError("Replay is invalid.")
98+
}
99+
PrintReply(reply);
100+
delete reply;
101+
}
102+
71103
redis.AppendCommand("set foo %s", "23333333");
72104
reply = redis.GetReply();
73105
if (reply == INVALID_HANDLE) {

extension.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ void HiredisExt::SDK_OnUnload()
112112
handlesys->RemoveType(g_AsyncRedisCtxType, myself->GetIdentity());
113113
}
114114

115-
//void HiredisExt::AddCallback(IPluginFunction * callback, std::vector<std::tuple<DataType, cell_t>> params)
116-
//{
117-
// if (callback) {
118-
// cblist.enqueue({ callback, params });
119-
// }
120-
//}
121-
122115
void HiredisExt::RequestFrame(std::function<void(void * data)> cb, void *data)
123116
{
124117
cblist.enqueue({ cb, data });

natives.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ static cell_t native_redisAsyncCommandEx(IPluginContext *pContext, const cell_t
364364
}
365365
}
366366

367-
SMASyncRedis::CmdCBData *cbdata = new SMASyncRedis::CmdCBData();
367+
CBData *cbdata = new CBData();
368+
cbdata->handle = params[1];
368369
cbdata->callback = pContext->GetFunctionById(params[2]);
369370
cbdata->data = params[3];
370371
cbdata->identity = pContext->GetIdentity();

0 commit comments

Comments
 (0)