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
redis
Commits
6e56f513
Commit
6e56f513
authored
Oct 21, 2019
by
antirez
Browse files
Modules: refactoring of RM_GetClientInfoById().
parent
21d85441
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/module.c
View file @
6e56f513
...
@@ -1548,6 +1548,45 @@ unsigned long long RM_GetClientId(RedisModuleCtx *ctx) {
...
@@ -1548,6 +1548,45 @@ unsigned long long RM_GetClientId(RedisModuleCtx *ctx) {
return
ctx
->
client
->
id
;
return
ctx
->
client
->
id
;
}
}
/* This is an helper for RM_GetClientInfoById() and other functions: given
* a client, it populates the client info structure with the appropriate
* fields depending on the version provided. If the version is not valid
* then REDISMODULE_ERR is returned. Otherwise the function returns
* REDISMODULE_OK and the structure pointed by 'ci' gets populated. */
int
modulePopulateClientInfoStructure
(
void
*
ci
,
client
*
client
,
int
structver
)
{
if
(
structver
!=
1
)
return
REDISMODULE_ERR
;
struct
{
uint64_t
version
;
/* Version of this structure for ABI compat. */
uint64_t
flags
;
/* REDISMODULE_CLIENTINFO_FLAG_* */
uint64_t
id
;
/* Client ID. */
char
addr
[
46
];
/* IPv4 or IPv6 address. */
uint16_t
port
;
/* TCP port. */
uint16_t
db
;
/* Selected DB. */
}
*
ci1
;
ci1
=
ci
;
memset
(
ci1
,
0
,
sizeof
(
*
ci1
));
ci1
->
version
=
structver
;
if
(
client
->
flags
&
CLIENT_MULTI
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_MULTI
;
if
(
client
->
flags
&
CLIENT_PUBSUB
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_PUBSUB
;
if
(
client
->
flags
&
CLIENT_UNIX_SOCKET
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_UNIXSOCKET
;
if
(
client
->
flags
&
CLIENT_TRACKING
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_TRACKING
;
if
(
client
->
flags
&
CLIENT_BLOCKED
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_BLOCKED
;
int
port
;
connPeerToString
(
client
->
conn
,
ci1
->
addr
,
sizeof
(
ci1
->
addr
),
&
port
);
ci1
->
port
=
port
;
ci1
->
db
=
client
->
db
->
id
;
ci1
->
id
=
client
->
id
;
return
REDISMODULE_OK
;
}
/* Return information about the client with the specified ID (that was
/* Return information about the client with the specified ID (that was
* previously obtained via the RedisModule_GetClientId() API). If the
* previously obtained via the RedisModule_GetClientId() API). If the
* client exists, REDISMODULE_OK is returned, otherwise REDISMODULE_ERR
* client exists, REDISMODULE_OK is returned, otherwise REDISMODULE_ERR
...
@@ -1591,42 +1630,13 @@ unsigned long long RM_GetClientId(RedisModuleCtx *ctx) {
...
@@ -1591,42 +1630,13 @@ unsigned long long RM_GetClientId(RedisModuleCtx *ctx) {
* }
* }
*/
*/
int
RM_GetClientInfoById
(
void
*
ci
,
uint64_t
id
)
{
int
RM_GetClientInfoById
(
void
*
ci
,
uint64_t
id
)
{
struct
{
uint64_t
version
;
/* Version of this structure for ABI compat. */
uint64_t
flags
;
/* REDISMODULE_CLIENTINFO_FLAG_* */
uint64_t
id
;
/* Client ID. */
char
addr
[
46
];
/* IPv4 or IPv6 address. */
uint16_t
port
;
/* TCP port. */
uint16_t
db
;
/* Selected DB. */
}
*
ci1
;
client
*
client
=
lookupClientByID
(
id
);
client
*
client
=
lookupClientByID
(
id
);
if
(
client
==
NULL
)
return
REDISMODULE_ERR
;
if
(
client
==
NULL
)
return
REDISMODULE_ERR
;
if
(
ci
==
NULL
)
return
REDISMODULE_OK
;
if
(
ci
==
NULL
)
return
REDISMODULE_OK
;
/* Fill the info structure if passed. */
/* Fill the info structure if passed. */
uint64_t
structver
=
((
uint64_t
*
)
ci
)[
0
];
uint64_t
structver
=
((
uint64_t
*
)
ci
)[
0
];
if
(
structver
!=
1
)
return
REDISMODULE_ERR
;
return
modulePopulateClientInfoStructure
(
ci
,
client
,
structver
);
ci1
=
ci
;
memset
(
ci1
,
0
,
sizeof
(
*
ci1
));
if
(
client
->
flags
&
CLIENT_MULTI
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_MULTI
;
if
(
client
->
flags
&
CLIENT_PUBSUB
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_PUBSUB
;
if
(
client
->
flags
&
CLIENT_UNIX_SOCKET
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_UNIXSOCKET
;
if
(
client
->
flags
&
CLIENT_TRACKING
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_TRACKING
;
if
(
client
->
flags
&
CLIENT_BLOCKED
)
ci1
->
flags
|=
REDISMODULE_CLIENTINFO_FLAG_BLOCKED
;
int
port
;
anetPeerToString
(
client
->
fd
,
ci1
->
addr
,
sizeof
(
ci1
->
addr
),
&
port
);
ci1
->
port
=
port
;
ci1
->
db
=
client
->
db
->
id
;
ci1
->
id
=
client
->
id
;
return
REDISMODULE_OK
;
}
}
/* Return the currently selected DB. */
/* Return the currently selected DB. */
...
...
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