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
5a38356c
Commit
5a38356c
authored
Sep 25, 2010
by
Pieter Noordhuis
Browse files
Add example for non-blocking usage of hiredis with libevent
parent
1c245845
Changes
3
Show whitespace changes
Inline
Side-by-side
.gitignore
View file @
5a38356c
/hiredis-test
/hiredis-test
/hiredis-example
/hiredis-example
/libevent-example
/*.o
/*.o
/*.so
/*.so
/*.dylib
/*.dylib
...
...
Makefile
View file @
5a38356c
...
@@ -58,6 +58,9 @@ hiredis-%: %.o ${DYLIBNAME}
...
@@ -58,6 +58,9 @@ hiredis-%: %.o ${DYLIBNAME}
test
:
hiredis-test
test
:
hiredis-test
./hiredis-test
./hiredis-test
libevent-example
:
libevent-example.c ${DYLIBNAME}
$(CC)
-o
$@
$(CCOPT)
$(DEBUG)
-L
.
-lhiredis
-levent
libevent-example.c
.c.o
:
.c.o
:
$(CC)
-c
$(CFLAGS)
$(DEBUG)
$(COMPILE_TIME)
$<
$(CC)
-c
$(CFLAGS)
$(DEBUG)
$(COMPILE_TIME)
$<
...
...
libevent-example.c
0 → 100644
View file @
5a38356c
#include <stdio.h>
#include <stdlib.h>
#include <event.h>
#include <string.h>
#include "hiredis.h"
#define NOT_USED(x) ((void)x)
/* This struct enables us to pass both the event and the
* redisContext to the read and write handlers. */
typedef
struct
redisEvents
{
redisContext
*
context
;
struct
event
read
;
struct
event
write
;
}
redisEvents
;
void
redisLibEventRead
(
int
fd
,
short
event
,
void
*
arg
)
{
NOT_USED
(
fd
);
NOT_USED
(
event
);
redisEvents
*
re
=
arg
;
/* Always re-schedule read events */
event_add
(
&
re
->
read
,
NULL
);
if
(
redisBufferRead
(
re
->
context
)
==
REDIS_ERR
)
{
/* Handle error. */
printf
(
"Read error: %s
\n
"
,
re
->
context
->
error
);
}
else
{
/* Check replies. */
redisProcessCallbacks
(
re
->
context
);
}
}
void
redisLibEventWrite
(
int
fd
,
short
event
,
void
*
arg
)
{
NOT_USED
(
fd
);
NOT_USED
(
event
);
redisEvents
*
re
=
arg
;
int
done
=
0
;
if
(
redisBufferWrite
(
re
->
context
,
&
done
)
==
REDIS_ERR
)
{
/* Handle error */
printf
(
"Write error: %s
\n
"
,
re
->
context
->
error
);
}
else
{
/* Schedule write event again when writing is not done. */
if
(
!
done
)
{
event_add
(
&
re
->
write
,
NULL
);
}
else
{
event_add
(
&
re
->
read
,
NULL
);
}
}
}
/* Schedule to be notified on a write event, so the outgoing buffer
* can be flushed to the socket. */
void
redisLibEventOnWrite
(
redisContext
*
c
,
void
*
privdata
)
{
NOT_USED
(
c
);
redisEvents
*
e
=
privdata
;
event_add
(
&
e
->
write
,
NULL
);
}
/* Free the redisEvents struct when the context is free'd. */
void
redisLibEventOnFree
(
redisContext
*
c
,
void
*
privdata
)
{
NOT_USED
(
c
);
redisEvents
*
e
=
privdata
;
free
(
e
);
}
redisContext
*
redisLibEventConnect
(
const
char
*
ip
,
int
port
)
{
redisEvents
*
e
=
malloc
(
sizeof
(
*
e
));
e
->
context
=
redisConnectNonBlock
(
ip
,
port
,
NULL
);
redisSetCommandCallback
(
e
->
context
,
redisLibEventOnWrite
,
e
);
redisSetFreeCallback
(
e
->
context
,
redisLibEventOnFree
,
e
);
event_set
(
&
e
->
read
,
e
->
context
->
fd
,
EV_READ
,
redisLibEventRead
,
e
);
event_set
(
&
e
->
write
,
e
->
context
->
fd
,
EV_WRITE
,
redisLibEventWrite
,
e
);
return
e
->
context
;
}
void
getCallback
(
redisContext
*
c
,
redisReply
*
reply
,
void
*
privdata
)
{
NOT_USED
(
c
);
NOT_USED
(
privdata
);
printf
(
"argv[end-1]: %s
\n
"
,
reply
->
reply
);
redisFree
(
c
);
exit
(
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
signal
(
SIGPIPE
,
SIG_IGN
);
event_init
();
redisContext
*
c
=
redisLibEventConnect
(
"127.0.0.1"
,
6379
);
if
(
c
->
error
!=
NULL
)
{
printf
(
"Connect error: %s
\n
"
,
c
->
error
);
return
1
;
}
redisCommand
(
c
,
"SET key %b"
,
argv
[
argc
-
1
],
strlen
(
argv
[
argc
-
1
]));
redisCommandWithCallback
(
c
,
getCallback
,
NULL
,
"GET key"
);
event_dispatch
();
return
0
;
}
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