gpt4 book ai didi

c - ZeroMQ C 上的简单 Flatbuffers 示例 - 通过 zmq 将结构复制到 flatbuffer 并再次返回结构

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:52 26 4
gpt4 key购买 nike

为后代发布我的作品。在用 C++ 完成我的最后一个示例后意识到我实际上需要一直用 C 来完成它(太棒了,对吧?)。作为一名 Java 程序员,这两次迭代都花费了我相当多的精力,我认为那里的许多示例代码留下了太多的漏洞 - 尤其是当涉及到从命令行构建时要困难得多习惯于使用 Eclipse 来构建项目和处理依赖项的人。

如何使用 brew 安装 OSX 的依赖项:

brew 安装 flatcc
brew 安装 zeromq

您还需要安装所有标准构建器二进制文件。我使用 gcc 编译:

gcc publisher.c -o bin/zmq_pub -lzmq -lflatcc
gcc subscriber.c -o bin/zmq_sub -lzmq

这假设您已经安装了 zmq 和 flatcc 库,它们应该在 brew 安装完成后链接到您的/usr/local/include。像这样:


zmq_cpub $ls -la/usr/local/include
lrwxr-xr-x 1 用户组 37 Oct 18 18:43 flatcc -> ../Cellar/flatcc/0.3.4/include/flatcc

你会得到如下编译错误:体系结构 x86_64 的 undefined symbol : 如果您没有正确安装/链接库。编译器/链接器将重命名函数并在它们前面加上 _ 并可能使您感到困惑。就像 Undefined symbols for architecture x86_64 _flatcc_builder_init 尽管从来不应该有 _flatcc_builder_init

那是因为在 C/C++ 中链接库与在 Java 中有根本的不同。除了添加 JAR 的特定项目构建路径之外,还有外部 C/C++ 库可以安装到的已知位置。 /usr/local/include/usr/local/lib/usr/lib/usr/include.

在将 flatcc 二进制文件安装到您的路径后,不要忘记生成要在本地项目中使用的头文件:

flatcc -a Car.fbs

这应该是我在 C 车道上遇到的几乎所有障碍。希望它能帮助那里的人。

最佳答案

汽车.fbs

namespace Test;

table Car {
name: string;
model: string;
year: int;
}
root_type Car;

Subscriber.c(监听传入的结构)

//  Hello World client
#include "flatbuffers/Car_builder.h" // Generated by `flatcc`.
#include "flatbuffers/flatbuffers_common_builder.h"
#include <zmq.h>

#undef ns
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(Test, x) // Specified in the schema

struct Car {
char* name;
char* model;
int year;
};

int main (void)
{
printf ("Connecting to car world server...\n");
void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");

int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
char buffer [1024];
printf ("Sending ready signal %d...\n", request_nbr);
zmq_send (requester, "Hello", 5, 0);
zmq_recv (requester, buffer, 1024, 0);
printf ("Received car %d\n", request_nbr);
ns(Car_table_t) car = ns(Car_as_root(buffer));
int year = ns(Car_year(car));
flatbuffers_string_t model = ns(Car_model(car));
flatbuffers_string_t name = ns(Car_name(car));

struct Car nextCar;
// no need to double up on memory!!
// strcpy(nextCar.model, model);
// strcpy(nextCar.name, name);
nextCar.model = model;
nextCar.name = name;
nextCar.year = year;

printf("Year: %d\n", nextCar.year);
printf("Name: %s\n", nextCar.name);
printf("Model: %s\n", nextCar.model);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}

Publisher.c(通过 zmq 套接字发送结构):

//  Hello World server

#include "flatbuffers/Car_builder.h" // Generated by `flatcc`.
#include "flatbuffers/flatbuffers_common_builder.h"
#include <zmq.h>
#include <unistd.h>
#include <time.h>

#undef ns
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(Test, x) // specified in the schema

struct Car {
char name[10];
char model[10];
int year;
};

struct Car getRandomCar() {
struct Car randomCar;
int a = rand();
if ((a % 2) == 0) {
strcpy(randomCar.name, "Ford");
strcpy(randomCar.model, "Focus");
} else {
strcpy(randomCar.name, "Dodge");
strcpy(randomCar.model, "Charger");
}
randomCar.year = rand();
return randomCar;
}

int main (void)
{
srand(time(NULL));

// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
int counter = 0;

while (1) {
struct Car c = getRandomCar();

flatcc_builder_t builder, *B;
B = &builder;
// Initialize the builder object.
flatcc_builder_init(B);
uint8_t *buf; // raw buffer used by flatbuffer
size_t size; // the size of the flatbuffer
// Convert the char arrays to strings
flatbuffers_string_ref_t name = flatbuffers_string_create_str(B, c.name);
flatbuffers_string_ref_t model = flatbuffers_string_create_str(B, c.model);

ns(Car_start_as_root(B));
ns(Car_name_add(B, name));
ns(Car_model_add(B, model));
ns(Car_year_add(B, c.year));
ns(Car_end_as_root(B));
buf = flatcc_builder_finalize_buffer(B, &size);

char receiveBuffer [10];
zmq_recv (responder, receiveBuffer, 10, 0);
printf ("Received ready signal. Sending car %d.\n", counter);
sleep (1); // Do some 'work'
zmq_send (responder, buf, size, 0);
counter++;

free(buf);
}
return 0;
}

关于c - ZeroMQ C 上的简单 Flatbuffers 示例 - 通过 zmq 将结构复制到 flatbuffer 并再次返回结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com