-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasync-glib.c
68 lines (54 loc) · 1.61 KB
/
async-glib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <valkey/async.h>
#include <valkey/valkey.h>
#include <valkey/adapters/glib.h>
#include <stdlib.h>
static GMainLoop *mainloop;
static void
connect_cb(valkeyAsyncContext *ac G_GNUC_UNUSED,
int status) {
if (status != VALKEY_OK) {
g_printerr("Failed to connect: %s\n", ac->errstr);
g_main_loop_quit(mainloop);
} else {
g_printerr("Connected...\n");
}
}
static void
disconnect_cb(const valkeyAsyncContext *ac G_GNUC_UNUSED,
int status) {
if (status != VALKEY_OK) {
g_error("Failed to disconnect: %s", ac->errstr);
} else {
g_printerr("Disconnected...\n");
g_main_loop_quit(mainloop);
}
}
static void
command_cb(valkeyAsyncContext *ac,
gpointer r,
gpointer user_data G_GNUC_UNUSED) {
valkeyReply *reply = r;
if (reply) {
g_print("REPLY: %s\n", reply->str);
}
valkeyAsyncDisconnect(ac);
}
gint main(gint argc G_GNUC_UNUSED, gchar *argv[] G_GNUC_UNUSED) {
valkeyAsyncContext *ac;
GMainContext *context = NULL;
GSource *source;
ac = valkeyAsyncConnect("127.0.0.1", 6379);
if (ac->err) {
g_printerr("%s\n", ac->errstr);
exit(EXIT_FAILURE);
}
source = valkey_source_new(ac);
mainloop = g_main_loop_new(context, FALSE);
g_source_attach(source, context);
valkeyAsyncSetConnectCallback(ac, connect_cb);
valkeyAsyncSetDisconnectCallback(ac, disconnect_cb);
valkeyAsyncCommand(ac, command_cb, NULL, "SET key 1234");
valkeyAsyncCommand(ac, command_cb, NULL, "GET key");
g_main_loop_run(mainloop);
return EXIT_SUCCESS;
}