Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
1c245845
Commit
1c245845
authored
Sep 25, 2010
by
Pieter Noordhuis
Browse files
Trigger callbacks when a command is issued or the context is free'd
parent
8345467b
Changes
2
Show whitespace changes
Inline
Side-by-side
hiredis.c
View file @
1c245845
...
@@ -575,7 +575,7 @@ static int redisContextConnect(redisContext *c, const char *ip, int port) {
...
@@ -575,7 +575,7 @@ static int redisContextConnect(redisContext *c, const char *ip, int port) {
}
}
static
redisContext
*
redisContextInit
(
redisReplyFunctions
*
fn
)
{
static
redisContext
*
redisContextInit
(
redisReplyFunctions
*
fn
)
{
redisContext
*
c
=
m
alloc
(
sizeof
(
*
c
)
);
redisContext
*
c
=
c
alloc
(
sizeof
(
redisContext
),
1
);
c
->
error
=
NULL
;
c
->
error
=
NULL
;
c
->
obuf
=
sdsempty
();
c
->
obuf
=
sdsempty
();
c
->
fn
=
fn
==
NULL
?
&
defaultFunctions
:
fn
;
c
->
fn
=
fn
==
NULL
?
&
defaultFunctions
:
fn
;
...
@@ -587,6 +587,8 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
...
@@ -587,6 +587,8 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
}
}
void
redisFree
(
redisContext
*
c
)
{
void
redisFree
(
redisContext
*
c
)
{
if
(
c
->
cbFree
!=
NULL
)
c
->
cbFree
(
c
,
c
->
privdataFree
);
if
(
c
->
error
!=
NULL
)
if
(
c
->
error
!=
NULL
)
sdsfree
(
c
->
error
);
sdsfree
(
c
->
error
);
if
(
c
->
obuf
!=
NULL
)
if
(
c
->
obuf
!=
NULL
)
...
@@ -614,6 +616,19 @@ redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions
...
@@ -614,6 +616,19 @@ redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions
return
c
;
return
c
;
}
}
/* Register callback that is triggered when a command is put in the output
* buffer when the context is non-blocking. */
void
redisSetCommandCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
)
{
c
->
cbCommand
=
fn
;
c
->
privdataCommand
=
privdata
;
}
/* Register callback that is triggered when the context is free'd. */
void
redisSetFreeCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
)
{
c
->
cbFree
=
fn
;
c
->
privdataFree
=
privdata
;
}
/* Use this function to handle a read event on the descriptor. It will try
/* Use this function to handle a read event on the descriptor. It will try
* and read some bytes from the socket and feed them to the reply parser.
* and read some bytes from the socket and feed them to the reply parser.
*
*
...
@@ -749,6 +764,10 @@ static int redisCommandWriteNonBlock(redisContext *c, redisCallback *cb, char *s
...
@@ -749,6 +764,10 @@ static int redisCommandWriteNonBlock(redisContext *c, redisCallback *cb, char *s
}
}
c
->
cpos
++
;
c
->
cpos
++
;
/* Fire write callback */
if
(
c
->
cbCommand
!=
NULL
)
c
->
cbCommand
(
c
,
c
->
privdataCommand
);
return
REDIS_OK
;
return
REDIS_OK
;
}
}
...
...
hiredis.h
View file @
1c245845
...
@@ -70,11 +70,13 @@ typedef struct redisReplyObjectFunctions {
...
@@ -70,11 +70,13 @@ typedef struct redisReplyObjectFunctions {
void
(
*
freeObject
)(
void
*
);
void
(
*
freeObject
)(
void
*
);
}
redisReplyFunctions
;
}
redisReplyFunctions
;
/* Callback prototype */
struct
redisContext
;
/* need forward declaration of redisContext */
struct
redisContext
;
/* needs forward declaration of redisContext */
typedef
void
redisCallbackFn
(
struct
redisContext
*
,
redisReply
*
,
void
*
);
/* Callbacks triggered on non-reply events. */
typedef
void
(
redisContextCallback
)(
struct
redisContext
*
,
void
*
);
/* Callback container */
/* Reply callback prototype and container */
typedef
void
redisCallbackFn
(
struct
redisContext
*
,
redisReply
*
,
void
*
);
typedef
struct
redisCallback
{
typedef
struct
redisCallback
{
redisCallbackFn
*
fn
;
redisCallbackFn
*
fn
;
void
*
privdata
;
void
*
privdata
;
...
@@ -86,8 +88,18 @@ typedef struct redisContext {
...
@@ -86,8 +88,18 @@ typedef struct redisContext {
int
flags
;
int
flags
;
sds
error
;
/* Error object is set when in erronous state */
sds
error
;
/* Error object is set when in erronous state */
sds
obuf
;
/* Write buffer */
sds
obuf
;
/* Write buffer */
/* Function set for reply buildup and reply reader */
redisReplyFunctions
*
fn
;
redisReplyFunctions
*
fn
;
void
*
reader
;
void
*
reader
;
/* Non-reply callbacks */
redisContextCallback
*
cbCommand
;
void
*
privdataCommand
;
redisContextCallback
*
cbFree
;
void
*
privdataFree
;
/* Reply callbacks */
redisCallback
*
callbacks
;
redisCallback
*
callbacks
;
int
cpos
;
int
cpos
;
int
clen
;
int
clen
;
...
@@ -103,6 +115,8 @@ int redisReplyReaderGetReply(void *reader, void **reply);
...
@@ -103,6 +115,8 @@ int redisReplyReaderGetReply(void *reader, void **reply);
redisContext
*
redisConnect
(
const
char
*
ip
,
int
port
,
redisReplyFunctions
*
fn
);
redisContext
*
redisConnect
(
const
char
*
ip
,
int
port
,
redisReplyFunctions
*
fn
);
redisContext
*
redisConnectNonBlock
(
const
char
*
ip
,
int
port
,
redisReplyFunctions
*
fn
);
redisContext
*
redisConnectNonBlock
(
const
char
*
ip
,
int
port
,
redisReplyFunctions
*
fn
);
void
redisSetCommandCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
);
void
redisSetFreeCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
);
void
redisFree
(
redisContext
*
c
);
void
redisFree
(
redisContext
*
c
);
int
redisBufferRead
(
redisContext
*
c
);
int
redisBufferRead
(
redisContext
*
c
);
int
redisBufferWrite
(
redisContext
*
c
,
int
*
done
);
int
redisBufferWrite
(
redisContext
*
c
,
int
*
done
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment