windows c++使用hiredis异步操作

// TestRedis.c : 定义控制台应用程序的入口点。
//

#include 
#include 


#include "hiredis.h"
#include "async.h"
#include 
//#include "adapters/libevent.h"

static aeEventLoop *loop = NULL;

#pragma comment(lib, "hiredis.lib")
#pragma comment(lib, "Win32_Interop.lib")

//#pragma comment(lib, "libevent.lib")
//#pragma comment(lib, "libevent_core.lib")
//#pragma comment(lib, "libevent_extras.lib")
//#pragma comment(lib, "ws2_32.lib")


// 设置命令执行后的回调函数
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
	redisReply *reply = (redisReply *)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");

	aeStop(loop);
}

void subCallback(redisAsyncContext *c, void *r, void *priv) {
	redisReply *reply = (redisReply*)r;
	if (reply == NULL) return;
	if (reply->type == REDIS_REPLY_ARRAY && reply->elements == 3) {
		if (strcmp(reply->element[0]->str, "subscribe") != 0) {
			printf("Received[%s] channel %s: %s\n",
				(char*)priv,
				reply->element[1]->str,
				reply->element[2]->str);
		}
	}
}

int main(int argc, char **argv) 
{
	//signal(13, SIG_IGN);

	//struct event_config *cfg = event_config_new();
	//event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
	//struct event_base* base = event_base_new_with_config(cfg);

	//struct event_base* base = event_base_new();

	loop = aeCreateEventLoop(1024 * 10);


	redisAsyncContext* redis = redisAsyncConnect("127.0.0.1", 6379);
	if (redis == NULL || redis->err)
	{
		redisAsyncFree(redis);
		return 0;
	}

	redisAeAttach(loop, redis);
	//redisLibeventAttach(redis, base);

	redisAsyncSetConnectCallback(redis, connectCallback);
	redisAsyncSetDisconnectCallback(redis, disconnectCallback);
	//redisAsyncCommand(redis, NULL, NULL, "SET key %b", argv[argc - 1], _tcslen(argv[argc - 1]));//发送set命令
	//redisAsyncCommand(redis, getCallback, (char*)"end-1", "GET key");//发送get命令
	redisAsyncCommand(redis, subCallback, (char*) "sub", "SUBSCRIBE foo");

	//event_base_dispatch(base);

	aeMain(loop);

	//redisAsyncFree(redis);

	getchar();
	return 0;
}


预处理器定义:
_OFF_T_DEFINED
WIN32
LACKS_STDLIB_H
_DEBUG
_CONSOLE
%(PreprocessorDefinitions)
NO_QFORKIMPL
_WIN32_WINNT=0x0501
附加包含目录:
$(SolutionDir)..\deps\hiredis
$(SolutionDir)..\src

入口函数必须是:
int main(int argc, char **argv) 
文件后缀.c 或包含头文件时加extern "C"{

}

在项目源文件中添加adlist.c ae.c zmalloc.c

libevent无法使用

windows c++使用hiredis异步操作_第1张图片

windows c++使用hiredis异步操作_第2张图片

你可能感兴趣的:(windows c++使用hiredis异步操作)