在 C/C++ 中实现通信协议
我正在开始在软件中实现一些专有的通信协议栈,但不知道从哪里开始。这是我以前从未做过的工作,我正在寻求最佳/推荐方法资源方面的帮助。
我将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从界面和消息开始。
声明允许对等方交换消息的会话接口。将消息声明为具有简单类型的 C++ 结构,例如 int、double、std::string 和 std::vector。例如:
然后通过对使用这些接口的业务逻辑进行编码来测试您的接口。一旦您确信这些接口允许您实现所需的逻辑,就可以使用您首选的事件驱动框架(例如 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:
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.
Boost.ASIO 在以下方面相当前沿C++ 中的异步(或同步)网络通信
Boost.ASIO is pretty cutting edge when it comes to Asynchronous (or synchronous) network communication in C++
请查看 Google Protocol Buffers。
从描述来看:
Protocol Buffers 与语言和平台无关,因此应该适合您的项目。我找不到许可证,但至少在我能找到的任何地方都没有说“GPL”。
这将帮助您了解协议。在实际的数据传输中,除非您自己编写操作系统,否则应该使用一些原语。除非您提供更多细节,否则很难对实施提供更准确的帮助。例如,您使用什么沟通渠道?以太网?
但根据经验,您应该使 ISR 尽可能短。在这些类型的解决方案中,这通常意味着将数据复制到环形缓冲区。这样您就不必在 ISR 中分配内存。 ISR 在复制数据后,应通知包的上层。如果您可以使用 DMA,请使用它。在这种情况下,甚至可以在启动 DMA 传输之前发送通知。
您可能还想查看 Linux 设备驱动程序、第 10 章 特别是。查看有关下半部和上半部的部分。
Have a look at Google Protocol Buffers.
From the description:
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.