Skip to content

Commit 61776ff

Browse files
committed
Hide valkeyNetRead & valkeyNetWrite
Move valkeyContextDefaultFuncs into net.c, then valkeyNetRead & valkeyNetWrite could be declared as static functions. async.h is no longer needed by valkey.c, that makes valkey.c independent from networking IO specific operations. Signed-off-by: zhenwei pi <[email protected]>
1 parent 5cd9baf commit 61776ff

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

include/valkey/net.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
#include "valkey.h"
3939

4040
void valkeyNetClose(valkeyContext *c);
41-
ssize_t valkeyNetRead(valkeyContext *c, char *buf, size_t bufcap);
42-
ssize_t valkeyNetWrite(valkeyContext *c);
4341

4442
int valkeyCheckSocketError(valkeyContext *c);
4543
int valkeyContextSetTimeout(valkeyContext *c, const struct timeval tv);

src/net.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <stdlib.h>
4444
#include <time.h>
4545

46+
#include "async.h"
4647
#include "net.h"
4748
#include "sds.h"
4849
#include "sockcompat.h"
@@ -56,7 +57,7 @@ void valkeyNetClose(valkeyContext *c) {
5657
}
5758
}
5859

59-
ssize_t valkeyNetRead(valkeyContext *c, char *buf, size_t bufcap) {
60+
static ssize_t valkeyNetRead(valkeyContext *c, char *buf, size_t bufcap) {
6061
ssize_t nread = recv(c->fd, buf, bufcap, 0);
6162
if (nread == -1) {
6263
if ((errno == EWOULDBLOCK && !(c->flags & VALKEY_BLOCK)) || (errno == EINTR)) {
@@ -78,7 +79,7 @@ ssize_t valkeyNetRead(valkeyContext *c, char *buf, size_t bufcap) {
7879
}
7980
}
8081

81-
ssize_t valkeyNetWrite(valkeyContext *c) {
82+
static ssize_t valkeyNetWrite(valkeyContext *c) {
8283
ssize_t nwritten;
8384

8485
nwritten = send(c->fd, c->obuf, sdslen(c->obuf), 0);
@@ -630,3 +631,16 @@ int valkeyContextConnectUnix(valkeyContext *c, const char *path, const struct ti
630631
valkeySetError(c, VALKEY_ERR_OOM, "Out of memory");
631632
return VALKEY_ERR;
632633
}
634+
635+
static valkeyContextFuncs valkeyContextDefaultFuncs = {
636+
.close = valkeyNetClose,
637+
.free_privctx = NULL,
638+
.async_read = valkeyAsyncRead,
639+
.async_write = valkeyAsyncWrite,
640+
.read = valkeyNetRead,
641+
.write = valkeyNetWrite
642+
};
643+
644+
void valkeyContextSetDefaultFuncs(valkeyContext *c) {
645+
c->funcs = &valkeyContextDefaultFuncs;
646+
}

src/valkey.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,8 @@
4242
#include "valkey_private.h"
4343
#include "net.h"
4444
#include "sds.h"
45-
#include "async.h"
4645
#include "win32.h"
4746

48-
static valkeyContextFuncs valkeyContextDefaultFuncs = {
49-
.close = valkeyNetClose,
50-
.free_privctx = NULL,
51-
.async_read = valkeyAsyncRead,
52-
.async_write = valkeyAsyncWrite,
53-
.read = valkeyNetRead,
54-
.write = valkeyNetWrite
55-
};
56-
5747
static valkeyReply *createReplyObject(int type);
5848
static void *createStringObject(const valkeyReadTask *task, char *str, size_t len);
5949
static void *createArrayObject(const valkeyReadTask *task, size_t elements);
@@ -718,7 +708,7 @@ static valkeyContext *valkeyContextInit(void) {
718708
if (c == NULL)
719709
return NULL;
720710

721-
c->funcs = &valkeyContextDefaultFuncs;
711+
valkeyContextSetDefaultFuncs(c);
722712

723713
c->obuf = sdsempty();
724714
c->reader = valkeyReaderCreate();

src/valkey_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,6 @@ static inline int valkeyContextUpdateCommandTimeout(valkeyContext *c,
122122
return VALKEY_OK;
123123
}
124124

125+
void valkeyContextSetDefaultFuncs(valkeyContext *c);
126+
125127
#endif /* VALKEY_VK_PRIVATE_H */

0 commit comments

Comments
 (0)