用C语言编写的ZeroMQ inproc线程通信库

用C语言编写的ZeroMQ inproc线程通信库示例,包含基本实现和用法说明:

c
include <zmq.h>
include <stdio.h>
include <string.h>
include <assert.h>
include <pthread.h>

// 库的上下文结构
typedef struct {
    void* context;
    void* sender;
    void* receiver;
    char endpoint256;
} ipc_ctx;

// 初始化IPC通信库
int ipc_init(ipc_ctx* ctx, const char* endpoint) {
    // 创建ZeroMQ上下文
    ctx->context = zmq_ctx_new();
    if (!ctx->context) return -1;

    // 存储端点地址
    strncpy(ctx->endpoint, endpoint, sizeof(ctx->endpoint)-1);
    ctx->endpointsizeof(ctx->endpoint)-1 = '\0';

    // 创建发送端套接字 (PUSH)
    ctx->sender = zmq_socket(ctx->context, ZMQ_PUSH);
    if (!ctx->sender) {
        zmq_ctx_destroy(ctx->context);
        return -2;
    }

    // 创建接收端套接字 (PULL)
    ctx->receiver = zmq_socket(ctx->context, ZMQ_PULL);
    if (!ctx->receiver) {
        zmq_close(ctx->sender);
        zmq_ctx_destroy(ctx->context);
        return -3;
    }

    // 绑定接收端
    int rc = zmq_bind(ctx->receiver, ctx->endpoint);
    if (rc != 0) {
        zmq_close(ctx->sender);
        zmq_close(ctx->receiver);
        zmq_ctx_destroy(ctx->context);
        return -4;
    }

    // 连接发送端
    rc = zmq_connect(ctx->sender, ctx->endpoint);
    if (rc != 0) {
        zmq_close(ctx->sender);
        zmq_close(ctx->receiver);
        zmq_ctx_destroy(ctx->context);
        return -5;
    }

    return 0;
}

// 发送消息
int ipc_send(ipc_ctx* ctx, const char* data, size_t len) {
    return zmq_send(ctx->sender, data, len, 0);
}

// 接收消息(阻塞模式)
int ipc_recv(ipc_ctx* ctx, char* buffer, size_t max_len) {
    return zmq_recv(ctx->receiver, buffer, max_len, 0);
}

// 清理资源
void ipc_cleanup(ipc_ctx* ctx) {
    if (ctx->sender) zmq_close(ctx->sender);
    if (ctx->receiver) zmq_close(ctx->receiver);
    if (ctx->context) zmq_ctx_destroy(ctx->context);
    memset(ctx, 0, sizeof(ipc_ctx));
}

// 示例线程函数
static void* receiver_thread(void* arg) {
    ipc_ctx* ctx = (ipc_ctx*)arg;
    char buffer256;
    
    while (1) {
        int rc = ipc_recv(ctx, buffer, sizeof(buffer)-1);
        if (rc > 0) {
            bufferrc = '\0';
            printf("Received: %s\n", buffer);
            if (strcmp(buffer, "EXIT") == 0) break;
        }
    }
    return NULL;
}

int main() {
    ipc_ctx ctx;
    const char* endpoint = "inproc://ipc_example";
    
    // 初始化IPC
    if (ipc_init(&ctx, endpoint) != 0) {
        fprintf(stderr, "IPC initialization failed\n");
        return 1;
    }

    // 创建接收线程
    pthread_t thread;
    pthread_create(&thread, NULL, receiver_thread, &ctx);

    // 发送测试消息
    for (int i = 0; i < 5; ++i) {
        char msg32;
        snprintf(msg, sizeof(msg), "Message %d", i);
        ipc_send(&ctx, msg, strlen(msg));
    }
    
    // 发送退出指令
    ipc_send(&ctx, "EXIT", 4);

    // 等待线程结束
    pthread_join(thread, NULL);

    // 清理资源
    ipc_cleanup(&ctx);
    return 0;
}

编译命令:

bash
gcc -o ipc_example ipc_example.c -lzmq -lpthread

这个库实现包含以下关键特性:

  1. 上下文管理:

    • 使用ipc_init初始化通信上下文
    • 自动创建和管理ZeroMQ上下文
    • 支持自定义端点地址
  2. 通信模式:

    • 使用PUSH/PULL模式保证消息顺序
    • 支持多线程安全通信
    • 提供阻塞式发送/接收接口
  3. 错误处理:

    • 初始化时的错误检查
    • 资源泄漏保护
    • ZeroMQ错误的自动处理
  4. 扩展性:

    • 可轻松修改为使用其他模式(如PUB/SUB)
    • 支持消息类型扩展
    • 可添加超时机制

使用注意事项:

  1. 确保发送端和接收端使用相同的endpoint地址
  2. 接收线程应先于发送操作启动
  3. 主线程退出前应等待所有通信完成
  4. 消息内容可以是任意二进制数据
  5. 支持每秒处理数万条消息的高性能场景

可根据具体需求添加以下功能:

  • 非阻塞通信模式
  • 消息序列化支持
  • 多路复用支持
  • 超时控制
  • 流量控制
  • 统计监控接口

这个实现展示了ZeroMQ inproc的核心用法,可以作为构建更复杂线程通信系统的基础。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最后一个bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值