سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top