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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4899542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:52:56  来源:igfitidea点击:

Implementation communication protocols in C/C++

c++ccommunication-protocol

提问by dubnde

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.

我将使用 c/c++,我可以自由使用库(BSD/BOOST/Apache)但没有 GPL。我已经广泛使用 C++,所以使用 C++ 的特性不是问题。

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.

更新:我希望实现的示例协议类似于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

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

采纳答案by Maxim Egorushkin

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:

声明允许对等体交换消息的会话接口。将消息声明为具有简单类型的 C++ 结构,例如 ints、doubles、std::string's 和 std::vectors。例如:

// 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.

然后通过对使用这些接口的业务逻辑进行编码来测试您的接口。一旦您确信这些接口允许您实现所需的逻辑,就可以使用您喜欢的事件驱动框架(如 libevent 或 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.

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

回答by thehouse

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

Boost.ASIO在 C++ 中的异步(或同步)网络通信方面非常前沿

回答by Makis

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.

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

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.

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

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.

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

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

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