2022-06-10 10:18:37 +00:00
|
|
|
//
|
|
|
|
// Created by xajhuang on 2022/6/10.
|
|
|
|
//
|
|
|
|
#include <uv.h>
|
|
|
|
#include <zlog.h>
|
|
|
|
#include <zmq.h>
|
|
|
|
#include <string.h>
|
2022-06-13 10:49:12 +00:00
|
|
|
#include <unistd.h>
|
2022-06-10 10:18:37 +00:00
|
|
|
#include "task_manager.h"
|
|
|
|
#include "user_errno.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "init.h"
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
static void *g_pContext = NULL;
|
|
|
|
static void *g_pResponse = NULL;
|
|
|
|
static const char *g_pSendMsg = NULL;
|
|
|
|
|
|
|
|
static int mq_data_send_msg(const char *pMsg) {
|
|
|
|
zmq_msg_t msg;
|
|
|
|
|
|
|
|
if (pMsg) {
|
|
|
|
unsigned int size = strlen(pMsg) + 1;
|
|
|
|
printf("Send: %s\n", pMsg);
|
|
|
|
zmq_msg_init_size(&msg, size);
|
|
|
|
memset(zmq_msg_data(&msg), 0, size);
|
|
|
|
memcpy(zmq_msg_data(&msg), pMsg, size);
|
|
|
|
if (zmq_msg_send(&msg, g_pResponse, ZMQ_DONTWAIT) == -1) {
|
|
|
|
perror("zmq_msg_send");
|
|
|
|
}
|
|
|
|
zmq_msg_close(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
2022-06-10 10:18:37 +00:00
|
|
|
|
|
|
|
_Noreturn static void mqServerCb(void *UNUSED(pArg)) {
|
|
|
|
while (TRUE) {
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init(&msg);
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
if (zmq_msg_recv(&msg, g_pResponse, ZMQ_DONTWAIT) != -1) {
|
2022-06-10 10:18:37 +00:00
|
|
|
printf("Data channel receive(%zu): %s\n", zmq_msg_size(&msg), (const char *)zmq_msg_data(&msg));
|
|
|
|
zmq_msg_close(&msg);
|
2022-06-13 10:49:12 +00:00
|
|
|
mq_data_send_msg("xajhuang");
|
2022-06-10 10:18:37 +00:00
|
|
|
#if 0
|
|
|
|
zmq_msg_close(&msg);
|
|
|
|
|
|
|
|
zmq_msg_init_size(&msg, 5);
|
|
|
|
|
|
|
|
memcpy(zmq_msg_data(&msg), "hello", 5);
|
|
|
|
|
|
|
|
zmq_msg_send(&msg, g_pResponse, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_sleep(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
_Noreturn static void mqsendServerCb(void *UNUSED(pArg)) {
|
|
|
|
while (TRUE) {
|
|
|
|
if(g_pSendMsg) {
|
|
|
|
printf("+++++\n");
|
|
|
|
mq_data_send_msg(g_pSendMsg);
|
|
|
|
printf("-----\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_sleep(10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 10:18:37 +00:00
|
|
|
static int data_mq_init(void) {
|
|
|
|
static uv_thread_t uvThread;
|
2022-06-13 10:49:12 +00:00
|
|
|
const char *mq_name = "ipc:///tmp/msg_fifo1";
|
2022-06-10 10:18:37 +00:00
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
g_pContext = zmq_ctx_new();
|
|
|
|
|
|
|
|
if (g_pContext == NULL) {
|
|
|
|
return -ERR_MQ_CREATE_MQ;
|
|
|
|
}
|
|
|
|
|
2022-06-13 03:09:36 +00:00
|
|
|
g_pResponse = zmq_socket(g_pContext, ZMQ_PAIR);
|
2022-06-10 10:18:37 +00:00
|
|
|
|
|
|
|
if (g_pResponse == NULL) {
|
|
|
|
zmq_ctx_destroy(g_pContext);
|
|
|
|
return -ERR_MQ_CREATE_REP;
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
//memset(buf, 0, 1024);
|
2022-06-10 10:18:37 +00:00
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
//sprintf(buf, "ipc:///tmp/msg_fifo1");
|
|
|
|
printf("Start message data channel server: %s\n", mq_name);
|
2022-06-10 10:18:37 +00:00
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
if (zmq_bind(g_pResponse, mq_name) != 0) {
|
|
|
|
perror("zmq_bind");
|
2022-06-10 10:18:37 +00:00
|
|
|
zmq_close(g_pResponse);
|
|
|
|
zmq_ctx_destroy(g_pContext);
|
|
|
|
return -ERR_MQ_CONN_SERVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_thread_create(&uvThread, mqServerCb, NULL);
|
2022-06-13 10:49:12 +00:00
|
|
|
uv_thread_create(&uvThread, mqsendServerCb, NULL);
|
2022-06-10 10:18:37 +00:00
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
uv_setup_args(argc, argv);
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
if (data_mq_init() != ERR_SUCCESS) {
|
|
|
|
printf("MQ init error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
if (strlen(argv[1]) > 0) {
|
|
|
|
g_pSendMsg = strdup(argv[1]);
|
|
|
|
}
|
|
|
|
}
|
2022-06-10 10:18:37 +00:00
|
|
|
task_manager_run();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|