在 C/C++ 中实现通信协议

发布于 2024-10-16 06:55:14 字数 658 浏览 6 评论 0原文

我正在开始在软件中实现一些专有的通信协议栈,但不知道从哪里开始。这是我以前从未做过的工作,我正在寻求最佳/推荐方法资源方面的帮助。

我将使用 c/c++,并且可以自由使用库(BSD/BOOST/Apache),但没有 GPL。我广泛使用了 C++,因此使用 C++ 的功能不是问题。

协议栈分为三层,并且已经完全规范和正式验证。因此,我需要做的就是用指定的语言实现并对其进行全面测试。还应该提到的是,该协议非常简单,但可以通过可靠的物理传输层在不同的设备上运行。我知道协议状态机的事件、输入、输出、副作用和行为。一般是收到中断后读取从物理层接收到的消息并将其读取并发送给等待的设备。接收设备可以处理响应消息并将其传递给协议层以在物理层上发送出去。

任何有关参考/建议的帮助将不胜感激。我愿意使用不同的语言,只要能帮助我理解如何实现它们,但我最终将不得不诉诸于所选择的语言。

更新:我希望实现的示例协议类似于SNEP

我不需要担心连接管理。我们可以假设连接已经建立,并且协议所做的是数据交换,其中协议消息已经在规范中明确定义

I am in the process of starting to implement some proprietary communication protocol stack in software but not sure where to start. It is the kind of work I have not done before and I am looking for help in terms of resources for best/recommended approaches.

I will be using c/c++ and I am free to use use libraries (BSD/BOOST/Apache) but no GPL. I have used C++ extensively so using the features of C++ is not a problem.

The protocol stack has three layers and it is already fully specified and formally verified. So all I need to do is implemented and test it fully in the specified languages. Should also mention that protocol is very simple but can run on different devices over a reliable physical transport layer. I know the events, inputs, outputs, side effects and the behaviour of the protocol state machine(s). Generally, an interrupt is received to read the message received from the physical layer to read it and send to the waiting device. The receiving device can process and pass the response message to the protocol layer to send out on the physical layer.

Any help with references/recommendations will be appreciated. I am willing to use a different language if only to help me understand how to implement them but I will have to eventually resort to the language of choice.

Update: An example protocol I wish to implement is something like SNEP.

I do not need to worry about connection management. We can assume the connection is already establish and I the protocol does is data exchange where the protocol messages are already well defined in specifications

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倥絔 2024-10-23 06:55:14

从界面和消息开始。

声明允许对等方交换消息的会话接口。将消息声明为具有简单类型的 C++ 结构,例如 int、double、std::string 和 std::vector。例如:

// these are your protocol messages
struct HelloRequest {
    uint32_t seq_no;
    // more stuff
};
struct HelloResponse {
    uint32_t seq_no;
    // more stuff
};

// Session callback for received messages
struct SessionReceiver {
    virtual void connected(Session*) = 0;
    virtual void receive(Session* from, HelloRequest msg) = 0;
    virtual void receive(Session* from, HelloResponse msg) = 0;
    virtual void disconnected(Session*) = 0;
};

// Session interface to send messages
struct Session {
    virtual void send(HelloRequest msg) = 0;
    virtual void send(HelloResponse msg) = 0;
};

// this connects asynchronously and then calls SessionReceiver::connected() with a newly established session
struct SessionInitiator {
    virtual void connect(SessionReceiver* cb, std::string peer) = 0;
};

// this accepts connections asynchronously and then calls SessionReceiver::connected() with a newly accepted session
struct SessionAcceptor {
    virtual void listen(SessionReceiver* cb, std::string port) = 0;
};

然后通过对使用这些接口的业务逻辑进行编码来测试您的接口。一旦您确信这些接口允许您实现所需的逻辑,就可以使用您首选的事件驱动框架(例如 libevent 或 Boost.Asio)来实现消息的接口和序列化。

编辑:
请注意,接口允许您进行模拟或测试实现。此外,序列化发生在接口后面的事实意味着,对于进程内的对等方,您不必序列化和反序列化消息,您可以按原样传递它们。

Start with interfaces and messages.

Declare the interfaces of session that allow peers to exchange messages. Declare the messages as C++ structs with simple types, like ints, doubles, std::string's and and std::vectors. For example:

// these are your protocol messages
struct HelloRequest {
    uint32_t seq_no;
    // more stuff
};
struct HelloResponse {
    uint32_t seq_no;
    // more stuff
};

// Session callback for received messages
struct SessionReceiver {
    virtual void connected(Session*) = 0;
    virtual void receive(Session* from, HelloRequest msg) = 0;
    virtual void receive(Session* from, HelloResponse msg) = 0;
    virtual void disconnected(Session*) = 0;
};

// Session interface to send messages
struct Session {
    virtual void send(HelloRequest msg) = 0;
    virtual void send(HelloResponse msg) = 0;
};

// this connects asynchronously and then calls SessionReceiver::connected() with a newly established session
struct SessionInitiator {
    virtual void connect(SessionReceiver* cb, std::string peer) = 0;
};

// this accepts connections asynchronously and then calls SessionReceiver::connected() with a newly accepted session
struct SessionAcceptor {
    virtual void listen(SessionReceiver* cb, std::string port) = 0;
};

Then test your interfaces by coding the business logic that uses these interfaces. Once you are confident that the interfaces allow you to implement the required logic implement the interfaces and serialization of your messages using your preferred event-driven framework, like libevent or Boost.Asio.

Edit:
Note that interfaces allow you to have mock or test implementations. Also the fact that serialization happens behind the interface means that for in-process peers you don't have to serialize and deserialize the messages, you can pass them as is.

就是爱搞怪 2024-10-23 06:55:14

Boost.ASIO 在以下方面相当前沿C++ 中的异步(或同步)网络通信

Boost.ASIO is pretty cutting edge when it comes to Asynchronous (or synchronous) network communication in C++

魔法少女 2024-10-23 06:55:14

请查看 Google Protocol Buffers

从描述来看:

协议缓冲区是一种灵活、高效、自动化的机制,用于序列化结构化数据 - 想想 XML,但更小、更快、更简单。您只需定义一次数据的结构化方式,然后就可以使用特殊生成的源代码,使用各种语言轻松地在各种数据流中写入和读取结构化数据。您甚至可以更新数据结构,而不会破坏根据“旧”格式编译的已部署程序。

Protocol Buffers 与语言和平台无关,因此应该适合您的项目。我找不到许可证,但至少在我能找到的任何地方都没有说“GPL”。

这将帮助您了解协议。在实际的数据传输中,除非您自己编写操作系统,否则应该使用一些原语。除非您提供更多细节,否则很难对实施提供更准确的帮助。例如,您使用什么沟通渠道?以太网?

但根据经验,您应该使 ISR 尽可能短。在这些类型的解决方案中,这通常意味着将数据复制到环形缓冲区。这样您就不必在 ISR 中分配内存。 ISR 在复制数据后,应通知包的上层。如果您可以使用 DMA,请使用它。在这种情况下,甚至可以在启动 DMA 传输之前发送通知。

您可能还想查看 Linux 设备驱动程序第 10 章 特别是。查看有关下半部和上半部的部分。

Have a look at Google Protocol Buffers.

From the description:

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.

Protocol Buffers are language and platform neutral so should fit into your project. I couldn't find the license, but at least it doesn't say "GPL" anywhere that I could find.

This will help you with the protocols. With the actual data transmission, well, unless you are writing the OS yourself, there should be some primitives you should use. It's hard to give more exact help on implementation unless you provide a bit more detail. For instance, what communication channel are you using? Ethernet?

But as a rule of thumb, you should make the ISR as short as possible. In these kinds of solutions that usually means copying data to a ring buffer. This way you don't have to allocate memory in the ISR. ISR, after having copied the data, should inform upper layers of the package. If you can use DMA, use that. In that case it might be possible to send the notification before you even start the DMA transfer.

You might also want to check out Linux Device Drivers, chapter 10 in particular. Check out the part about Bottom and Top Halves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文