Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
61277d09
Commit
61277d09
authored
Jun 04, 2013
by
Erik Dubbelboer
Browse files
Added libuv adapter
parent
bcf83543
Changes
3
Show whitespace changes
Inline
Side-by-side
Makefile
View file @
61277d09
...
@@ -76,6 +76,15 @@ hiredis-example-ae: example-ae.c adapters/ae.h $(STLIBNAME)
...
@@ -76,6 +76,15 @@ hiredis-example-ae: example-ae.c adapters/ae.h $(STLIBNAME)
$(CC)
-o
$@
$(REAL_CFLAGS)
$(REAL_LDFLAGS)
-I
$(AE_DIR)
$(AE_DIR)
/ae.o
$(AE_DIR)
/zmalloc.o example-ae.c
$(STLIBNAME)
$(CC)
-o
$@
$(REAL_CFLAGS)
$(REAL_LDFLAGS)
-I
$(AE_DIR)
$(AE_DIR)
/ae.o
$(AE_DIR)
/zmalloc.o example-ae.c
$(STLIBNAME)
endif
endif
ifndef
LIBUV_DIR
hiredis-example-libuv
:
@
echo
"Please specify LIBUV_DIR (e.g. ../libuv/)"
@
false
else
hiredis-example-libuv
:
example-libuv.c adapters/libuv.h $(STLIBNAME)
$(CC)
-o
$@
$(REAL_CFLAGS)
$(REAL_LDFLAGS)
-I
${LIBUV_DIR}
/include/ example-libuv.c
${LIBUV_DIR}
/libuv.a
-lrt
$(STLIBNAME)
endif
hiredis-%
:
%.o $(STLIBNAME)
hiredis-%
:
%.o $(STLIBNAME)
$(CC)
-o
$@
$(REAL_LDFLAGS)
$<
$(STLIBNAME)
$(CC)
-o
$@
$(REAL_LDFLAGS)
$<
$(STLIBNAME)
...
...
adapters/libuv.h
0 → 100644
View file @
61277d09
#ifndef __HIREDIS_LIBUV_H__
#define __HIREDIS_LIBUV_H__
#include <uv.h>
#include "../hiredis.h"
#include "../async.h"
//#include <stdlib.h> // malloc(), free()
//#include <string.h> // memset()
//#include <assert.h> // assert()
typedef
struct
redisLibuvEvents
{
redisAsyncContext
*
context
;
uv_poll_t
handle
;
int
events
;
}
redisLibuvEvents
;
static
void
redisLibuvPoll
(
uv_poll_t
*
handle
,
int
status
,
int
events
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
handle
->
data
;
if
(
status
!=
0
)
{
return
;
}
if
(
events
&
UV_READABLE
)
{
redisAsyncHandleRead
(
p
->
context
);
}
if
(
events
&
UV_WRITABLE
)
{
redisAsyncHandleWrite
(
p
->
context
);
}
}
static
void
redisLibuvAddRead
(
void
*
privdata
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
privdata
;
p
->
events
|=
UV_READABLE
;
uv_poll_start
(
&
p
->
handle
,
p
->
events
,
redisLibuvPoll
);
}
static
void
redisLibuvDelRead
(
void
*
privdata
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
privdata
;
p
->
events
&=
~
UV_READABLE
;
if
(
p
->
events
)
{
uv_poll_start
(
&
p
->
handle
,
p
->
events
,
redisLibuvPoll
);
}
else
{
uv_poll_stop
(
&
p
->
handle
);
}
}
static
void
redisLibuvAddWrite
(
void
*
privdata
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
privdata
;
p
->
events
|=
UV_WRITABLE
;
uv_poll_start
(
&
p
->
handle
,
p
->
events
,
redisLibuvPoll
);
}
static
void
redisLibuvDelWrite
(
void
*
privdata
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
privdata
;
p
->
events
&=
~
UV_WRITABLE
;
if
(
p
->
events
)
{
uv_poll_start
(
&
p
->
handle
,
p
->
events
,
redisLibuvPoll
);
}
else
{
uv_poll_stop
(
&
p
->
handle
);
}
}
static
void
on_close
(
uv_handle_t
*
handle
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
handle
->
data
;
free
(
p
);
}
static
void
redisLibuvCleanup
(
void
*
privdata
)
{
redisLibuvEvents
*
p
=
(
redisLibuvEvents
*
)
privdata
;
uv_close
((
uv_handle_t
*
)
&
p
->
handle
,
on_close
);
}
int
redisLibuvAttach
(
redisAsyncContext
*
ac
,
uv_loop_t
*
loop
)
{
redisContext
*
c
=
&
(
ac
->
c
);
if
(
ac
->
ev
.
data
!=
NULL
)
{
return
REDIS_ERR
;
}
ac
->
ev
.
addRead
=
redisLibuvAddRead
;
ac
->
ev
.
delRead
=
redisLibuvDelRead
;
ac
->
ev
.
addWrite
=
redisLibuvAddWrite
;
ac
->
ev
.
delWrite
=
redisLibuvDelWrite
;
ac
->
ev
.
cleanup
=
redisLibuvCleanup
;
redisLibuvEvents
*
p
=
malloc
(
sizeof
(
*
p
));
if
(
!
p
)
{
return
REDIS_ERR
;
}
memset
(
p
,
0
,
sizeof
(
*
p
));
if
(
uv_poll_init
(
loop
,
&
p
->
handle
,
c
->
fd
)
!=
0
)
{
return
REDIS_ERR
;
}
ac
->
ev
.
data
=
p
;
p
->
handle
.
data
=
p
;
p
->
context
=
ac
;
return
REDIS_OK
;
}
#endif
example-libuv.c
0 → 100644
View file @
61277d09
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "hiredis.h"
#include "async.h"
#include "adapters/libuv.h"
void
getCallback
(
redisAsyncContext
*
c
,
void
*
r
,
void
*
privdata
)
{
redisReply
*
reply
=
r
;
if
(
reply
==
NULL
)
return
;
printf
(
"argv[%s]: %s
\n
"
,
(
char
*
)
privdata
,
reply
->
str
);
/* Disconnect after receiving the reply to GET */
redisAsyncDisconnect
(
c
);
}
void
connectCallback
(
const
redisAsyncContext
*
c
,
int
status
)
{
if
(
status
!=
REDIS_OK
)
{
printf
(
"Error: %s
\n
"
,
c
->
errstr
);
return
;
}
printf
(
"Connected...
\n
"
);
}
void
disconnectCallback
(
const
redisAsyncContext
*
c
,
int
status
)
{
if
(
status
!=
REDIS_OK
)
{
printf
(
"Error: %s
\n
"
,
c
->
errstr
);
return
;
}
printf
(
"Disconnected...
\n
"
);
}
int
main
(
int
argc
,
char
**
argv
)
{
signal
(
SIGPIPE
,
SIG_IGN
);
uv_loop_t
*
loop
=
uv_default_loop
();
redisAsyncContext
*
c
=
redisAsyncConnect
(
"127.0.0.1"
,
6379
);
if
(
c
->
err
)
{
/* Let *c leak for now... */
printf
(
"Error: %s
\n
"
,
c
->
errstr
);
return
1
;
}
redisLibuvAttach
(
c
,
loop
);
redisAsyncSetConnectCallback
(
c
,
connectCallback
);
redisAsyncSetDisconnectCallback
(
c
,
disconnectCallback
);
redisAsyncCommand
(
c
,
NULL
,
NULL
,
"SET key %b"
,
argv
[
argc
-
1
],
strlen
(
argv
[
argc
-
1
]));
redisAsyncCommand
(
c
,
getCallback
,
(
char
*
)
"end-1"
,
"GET key"
);
uv_run
(
loop
,
UV_RUN_DEFAULT
);
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