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
61ba85ce
Commit
61ba85ce
authored
Oct 18, 2010
by
Pieter Noordhuis
Browse files
Move context callbacks into struct and make privdata const
parent
7496458c
Changes
2
Hide whitespace changes
Inline
Side-by-side
hiredis.c
View file @
61ba85ce
...
@@ -587,8 +587,8 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
...
@@ -587,8 +587,8 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
}
}
void
redisDisconnect
(
redisContext
*
c
)
{
void
redisDisconnect
(
redisContext
*
c
)
{
if
(
c
->
cbDisconnect
!=
NULL
)
if
(
c
->
cbDisconnect
.
fn
!=
NULL
)
c
->
cbDisconnect
(
c
,
c
->
privdata
Disconnect
);
c
->
cbDisconnect
.
fn
(
c
,
c
->
cb
Disconnect
.
privdata
);
close
(
c
->
fd
);
close
(
c
->
fd
);
c
->
flags
&=
~
REDIS_CONNECTED
;
c
->
flags
&=
~
REDIS_CONNECTED
;
}
}
...
@@ -599,8 +599,8 @@ void redisFree(redisContext *c) {
...
@@ -599,8 +599,8 @@ void redisFree(redisContext *c) {
redisDisconnect
(
c
);
redisDisconnect
(
c
);
/* Fire free callback and clear all allocations. */
/* Fire free callback and clear all allocations. */
if
(
c
->
cbFree
!=
NULL
)
if
(
c
->
cbFree
.
fn
!=
NULL
)
c
->
cbFree
(
c
,
c
->
privdata
Free
);
c
->
cbFree
.
fn
(
c
,
c
->
cbFree
.
privdata
);
if
(
c
->
error
!=
NULL
)
if
(
c
->
error
!=
NULL
)
sdsfree
(
c
->
error
);
sdsfree
(
c
->
error
);
if
(
c
->
obuf
!=
NULL
)
if
(
c
->
obuf
!=
NULL
)
...
@@ -631,22 +631,22 @@ redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions
...
@@ -631,22 +631,22 @@ redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions
}
}
/* Register callback that is triggered when redisDisconnect is called. */
/* Register callback that is triggered when redisDisconnect is called. */
void
redisSetDisconnectCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
)
{
void
redisSetDisconnectCallback
(
redisContext
*
c
,
redisContextCallback
Fn
*
fn
,
const
void
*
privdata
)
{
c
->
cbDisconnect
=
fn
;
c
->
cbDisconnect
.
fn
=
fn
;
c
->
privdata
Disconnect
=
privdata
;
c
->
cb
Disconnect
.
privdata
=
privdata
;
}
}
/* Register callback that is triggered when a command is put in the output
/* Register callback that is triggered when a command is put in the output
* buffer when the context is non-blocking. */
* buffer when the context is non-blocking. */
void
redisSetCommandCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
)
{
void
redisSetCommandCallback
(
redisContext
*
c
,
redisContextCallback
Fn
*
fn
,
const
void
*
privdata
)
{
c
->
cbCommand
=
fn
;
c
->
cbCommand
.
fn
=
fn
;
c
->
privdata
Command
=
privdata
;
c
->
cbCommand
.
privdata
=
privdata
;
}
}
/* Register callback that is triggered when the context is free'd. */
/* Register callback that is triggered when the context is free'd. */
void
redisSetFreeCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
)
{
void
redisSetFreeCallback
(
redisContext
*
c
,
redisContextCallback
Fn
*
fn
,
const
void
*
privdata
)
{
c
->
cbFree
=
fn
;
c
->
cbFree
.
fn
=
fn
;
c
->
privdata
Free
=
privdata
;
c
->
cbFree
.
privdata
=
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
...
@@ -785,8 +785,8 @@ static int redisCommandWriteNonBlock(redisContext *c, redisCallback *cb, char *s
...
@@ -785,8 +785,8 @@ static int redisCommandWriteNonBlock(redisContext *c, redisCallback *cb, char *s
c
->
cpos
++
;
c
->
cpos
++
;
/* Fire write callback */
/* Fire write callback */
if
(
c
->
cbCommand
!=
NULL
)
if
(
c
->
cbCommand
.
fn
!=
NULL
)
c
->
cbCommand
(
c
,
c
->
privdata
Command
);
c
->
cbCommand
.
fn
(
c
,
c
->
cbCommand
.
privdata
);
return
REDIS_OK
;
return
REDIS_OK
;
}
}
...
...
hiredis.h
View file @
61ba85ce
...
@@ -77,10 +77,17 @@ typedef struct redisReplyObjectFunctions {
...
@@ -77,10 +77,17 @@ typedef struct redisReplyObjectFunctions {
struct
redisContext
;
/* need forward declaration of redisContext */
struct
redisContext
;
/* need forward declaration of redisContext */
/* Callbacks triggered on non-reply events. */
/* Callbacks triggered on non-reply events. */
typedef
void
(
redisContextCallback
)(
struct
redisContext
*
,
void
*
);
typedef
void
(
redisContextCallback
Fn
)(
struct
redisContext
*
,
const
void
*
);
/* Reply callback prototype and container */
/* Reply callback prototype and container */
typedef
void
redisCallbackFn
(
struct
redisContext
*
,
redisReply
*
,
const
void
*
);
typedef
void
(
redisCallbackFn
)(
struct
redisContext
*
,
redisReply
*
,
const
void
*
);
/* Callback containers */
typedef
struct
redisContextCallback
{
redisContextCallbackFn
*
fn
;
const
void
*
privdata
;
}
redisContextCallback
;
typedef
struct
redisCallback
{
typedef
struct
redisCallback
{
redisCallbackFn
*
fn
;
redisCallbackFn
*
fn
;
const
void
*
privdata
;
const
void
*
privdata
;
...
@@ -98,12 +105,9 @@ typedef struct redisContext {
...
@@ -98,12 +105,9 @@ typedef struct redisContext {
void
*
reader
;
void
*
reader
;
/* Non-reply callbacks */
/* Non-reply callbacks */
redisContextCallback
*
cbDisconnect
;
redisContextCallback
cbDisconnect
;
void
*
privdataDisconnect
;
redisContextCallback
cbCommand
;
redisContextCallback
*
cbCommand
;
redisContextCallback
cbFree
;
void
*
privdataCommand
;
redisContextCallback
*
cbFree
;
void
*
privdataFree
;
/* Reply callbacks */
/* Reply callbacks */
redisCallback
*
callbacks
;
redisCallback
*
callbacks
;
...
@@ -131,16 +135,16 @@ int redisProcessCallbacks(redisContext *c);
...
@@ -131,16 +135,16 @@ int redisProcessCallbacks(redisContext *c);
/* The disconnect callback is called *immediately* when redisDisconnect()
/* The disconnect callback is called *immediately* when redisDisconnect()
* is called. It is called only once for every redisContext (since hiredis
* is called. It is called only once for every redisContext (since hiredis
* currently does not support reconnecting an existing context). */
* currently does not support reconnecting an existing context). */
void
redisSetDisconnectCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
);
void
redisSetDisconnectCallback
(
redisContext
*
c
,
redisContextCallback
Fn
*
fn
,
const
void
*
privdata
);
/* The command callback is called every time redisCommand() is called in a
/* The command callback is called every time redisCommand() is called in a
* non-blocking context. It is called *after* the formatted command has been
* non-blocking context. It is called *after* the formatted command has been
* appended to the write buffer. */
* appended to the write buffer. */
void
redisSetCommandCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
);
void
redisSetCommandCallback
(
redisContext
*
c
,
redisContextCallback
Fn
*
fn
,
const
void
*
privdata
);
/* The free callback is called *before* all allocations are free'd. Use it to
/* The free callback is called *before* all allocations are free'd. Use it to
* release resources that depend/use the redisContext that is being free'd. */
* release resources that depend/use the redisContext that is being free'd. */
void
redisSetFreeCallback
(
redisContext
*
c
,
redisContextCallback
*
fn
,
void
*
privdata
);
void
redisSetFreeCallback
(
redisContext
*
c
,
redisContextCallback
Fn
*
fn
,
const
void
*
privdata
);
/* Issue a command to Redis. In a blocking context, it returns the reply. When
/* Issue a command to Redis. In a blocking context, it returns the reply. When
* an error occurs, it returns NULL and you should read redisContext->error
* an error occurs, it returns NULL and you should read redisContext->error
...
...
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