vcpe/srcs/libs/mq/mq_cmd.c

54 lines
1.1 KiB
C
Raw Normal View History

2022-05-10 06:43:27 +00:00
//
// Created by xajhu on 2021/7/15 0015.
//
#include <zmq.h>
#include <string.h>
#include <unistd.h>
#include "cmdline.h"
#include "user_errno.h"
int mq_cmd_run(const char *pSvr, const char *pCmd) {
zmq_msg_t msg;
int ret;
void *context;
void *requester;
char *pRsp;
unsigned int size;
if (pSvr == NULL || pCmd == NULL || strlen(pSvr) == 0) {
return -ERR_INPUT_PARAMS;
}
context = zmq_ctx_new();
requester = zmq_socket(context, ZMQ_REQ);
ret = zmq_connect(requester, pSvr);
if (ret != 0) {
return -ERR_MQ_CONN_SERVER;
}
zmq_msg_init_size(&msg, strlen(pCmd));
memcpy(zmq_msg_data(&msg), pCmd, strlen(pCmd));
zmq_msg_send(&msg, requester, 0);
zmq_msg_close(&msg);
sleep(1);
zmq_msg_init(&msg);
zmq_msg_recv(&msg, requester, 0);
pRsp = zmq_msg_data(&msg);
size = zmq_msg_size(&msg);
pRsp[size] = 0;
printf("Service Response: {\n%s}, size: %u\n", (const char *)pRsp, size);
zmq_msg_close(&msg);
zmq_close(requester);
zmq_ctx_destroy(context);
return ERR_SUCCESS;
2022-05-10 06:43:27 +00:00
}