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
e245ab48
Commit
e245ab48
authored
Oct 19, 2010
by
Pieter Noordhuis
Browse files
Added header file and example for usage from libev
parent
206868de
Changes
4
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
e245ab48
/hiredis-test
/hiredis-test
/hiredis-example
/hiredis-example
/
libevent
-example
/
*
-example
/*.o
/*.o
/*.so
/*.so
/*.dylib
/*.dylib
...
...
Makefile
View file @
e245ab48
...
@@ -61,11 +61,14 @@ test: hiredis-test
...
@@ -61,11 +61,14 @@ test: hiredis-test
libevent-example
:
extra/hiredis/libevent.h libevent-example.c ${DYLIBNAME}
libevent-example
:
extra/hiredis/libevent.h libevent-example.c ${DYLIBNAME}
$(CC)
-o
$@
$(CCOPT)
$(DEBUG)
-I
.
-Iextra
-L
.
-lhiredis
-levent
libevent-example.c
$(CC)
-o
$@
$(CCOPT)
$(DEBUG)
-I
.
-Iextra
-L
.
-lhiredis
-levent
libevent-example.c
libev-example
:
extra/hiredis/libev.h libev-example.c ${DYLIBNAME}
$(CC)
-o
$@
$(CCOPT)
$(DEBUG)
-I
.
-Iextra
-L
.
-lhiredis
-lev
libev-example.c
.c.o
:
.c.o
:
$(CC)
-c
$(CFLAGS)
$(DEBUG)
$(COMPILE_TIME)
$<
$(CC)
-c
$(CFLAGS)
$(DEBUG)
$(COMPILE_TIME)
$<
clean
:
clean
:
rm
-rf
${DYLIBNAME}
${STLIBNAME}
$(BINS)
libevent
-example
*
.o
*
.gcda
*
.gcno
*
.gcov
rm
-rf
${DYLIBNAME}
${STLIBNAME}
$(BINS)
*
-example
*
.o
*
.gcda
*
.gcno
*
.gcov
dep
:
dep
:
$(CC)
-MM
*
.c
$(CC)
-MM
*
.c
...
...
extra/hiredis/libev.h
0 → 100644
View file @
e245ab48
#include <sys/types.h>
#include <ev.h>
#include <hiredis.h>
/* Prototype for the error callback. */
typedef
void
(
redisErrorCallback
)(
const
redisContext
*
);
typedef
struct
libevRedisEvents
{
redisContext
*
context
;
redisErrorCallback
*
err
;
struct
ev_loop
*
loop
;
ev_io
rev
,
wev
;
}
libevRedisEvents
;
void
libevRedisReadEvent
(
struct
ev_loop
*
loop
,
ev_io
*
watcher
,
int
revents
)
{
((
void
)
loop
);
((
void
)
revents
);
libevRedisEvents
*
e
=
watcher
->
data
;
if
(
redisBufferRead
(
e
->
context
)
==
REDIS_ERR
)
{
e
->
err
(
e
->
context
);
}
else
{
if
(
redisProcessCallbacks
(
e
->
context
)
==
REDIS_ERR
)
{
e
->
err
(
e
->
context
);
}
}
}
void
libevRedisWriteEvent
(
struct
ev_loop
*
loop
,
ev_io
*
watcher
,
int
revents
)
{
((
void
)
loop
);
((
void
)
revents
);
libevRedisEvents
*
e
=
watcher
->
data
;
int
done
=
0
;
if
(
redisBufferWrite
(
e
->
context
,
&
done
)
==
REDIS_ERR
)
{
ev_io_stop
(
e
->
loop
,
&
e
->
wev
);
e
->
err
(
e
->
context
);
}
else
{
/* Stop firing the write event when done */
if
(
done
)
{
ev_io_stop
(
e
->
loop
,
&
e
->
wev
);
ev_io_start
(
e
->
loop
,
&
e
->
rev
);
}
}
}
void
libevRedisCommandCallback
(
redisContext
*
c
,
void
*
privdata
)
{
((
void
)
c
);
libevRedisEvents
*
e
=
privdata
;
ev_io_start
(
e
->
loop
,
&
e
->
wev
);
}
void
libevRedisDisconnectCallback
(
redisContext
*
c
,
void
*
privdata
)
{
((
void
)
c
);
libevRedisEvents
*
e
=
privdata
;
ev_io_stop
(
e
->
loop
,
&
e
->
rev
);
ev_io_stop
(
e
->
loop
,
&
e
->
wev
);
}
void
libevRedisFreeCallback
(
redisContext
*
c
,
void
*
privdata
)
{
((
void
)
c
);
libevRedisEvents
*
e
=
privdata
;
free
(
e
);
}
redisContext
*
libevRedisConnect
(
struct
ev_loop
*
loop
,
redisErrorCallback
*
err
,
const
char
*
ip
,
int
port
)
{
libevRedisEvents
*
e
;
redisContext
*
c
=
redisConnectNonBlock
(
ip
,
port
,
NULL
);
if
(
c
->
error
!=
NULL
)
{
err
(
c
);
redisFree
(
c
);
return
NULL
;
}
/* Create container for context and r/w events */
e
=
malloc
(
sizeof
(
*
e
));
e
->
loop
=
loop
;
e
->
context
=
c
;
e
->
err
=
err
;
e
->
rev
.
data
=
e
;
e
->
wev
.
data
=
e
;
/* Register callbacks */
redisSetDisconnectCallback
(
c
,
libevRedisDisconnectCallback
,
e
);
redisSetCommandCallback
(
c
,
libevRedisCommandCallback
,
e
);
redisSetFreeCallback
(
c
,
libevRedisFreeCallback
,
e
);
/* Initialize read/write events */
ev_io_init
(
&
e
->
rev
,
libevRedisReadEvent
,
c
->
fd
,
EV_READ
);
ev_io_init
(
&
e
->
wev
,
libevRedisWriteEvent
,
c
->
fd
,
EV_WRITE
);
return
c
;
}
libev-example.c
0 → 100644
View file @
e245ab48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/libev.h>
#include <signal.h>
void
getCallback
(
redisContext
*
c
,
redisReply
*
reply
,
const
void
*
privdata
)
{
printf
(
"argv[%s]: %s
\n
"
,
(
const
char
*
)
privdata
,
reply
->
reply
);
/* Disconnect after receiving the reply to GET */
redisDisconnect
(
c
);
}
void
errorCallback
(
const
redisContext
*
c
)
{
printf
(
"Error: %s
\n
"
,
c
->
error
);
}
int
main
(
int
argc
,
char
**
argv
)
{
signal
(
SIGPIPE
,
SIG_IGN
);
struct
ev_loop
*
loop
=
ev_default_loop
(
0
);
redisContext
*
c
=
libevRedisConnect
(
loop
,
errorCallback
,
"127.0.0.1"
,
6379
);
if
(
c
==
NULL
)
return
1
;
redisCommand
(
c
,
"SET key %b"
,
argv
[
argc
-
1
],
strlen
(
argv
[
argc
-
1
]));
redisCommandWithCallback
(
c
,
getCallback
,
"end-1"
,
"GET key"
);
ev_loop
(
loop
,
0
);
redisFree
(
c
);
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