How to implement a network protocol?

16,559

Solution 1

Read up on the State design pattern to learn how to avoid lots of switch statements.


"sometimes what comes out has some "blind spot", I mean statuses of the protocol that have not been covered..."

State can help avoid gaps. It can't guarantee a good design, you still have to do that.

"...as I have to write lines and lines of code to cover every possible situation."

This should not be considered a burden or a problem: You must write lines of code to cover every possible situation.

State can help because you get to leverage inheritance. It can't guarantee a good design, you still have to do that.

Solution 2

Designing a protocol is usually all about the application space you are working within. For instance, http is all about handling web pages, graphics, and posts, while FTP is all about transferring files.

So in short, to start, you should decide what application space you are in, then define the actions that need to be taken. Then finally, before you start designing your actual protocol, you should seriously, seriously hunt for another protocol stack that does what you want to do and avoid implementing a protocol stack altoether. Only after you have determined that something else pre-built absolutely won't work for you should you start building your own protocol stack.

Solution 3

In C++ you can use Boost::Spirit library to parse your protocol message easily. The only "difficulty" is to define the grammar of your message protocol. Take a look at Gnutella source code to see how they solve this problem. Here http://www9.limewire.com/developer/gnutella_protocol_0.4.pdf is the Gnutella protocol specifications

Solution 4

Finite State Machine is what you want

FSM

So you define a whole bunch of states that you can be in as a receiver or sender (idle, connecting_phase1, connecting_phase2, packet expected,...)

Then define all the possible events (packet1 arrives, net closes, ...)

finally you have a table that says 'when in state x and event n happens do func y and transition to state q' - for every state and event (many will be null or dups)

Edit - how to make a FSM (rough sketch)

 struct FSMNode
 {
      int m_nextState;
      void (m_func*);
 }
 FSMNode states[NUMSTATES][NUMEVENTS]=
   { // state 0
      {3, bang}, // event 0
      {2,wiz},
      {1, fertang}
    }
    {
      {1, noop}, // event 0
      {1, noop},
      {3, ole}
     }
     .......
     FSMNode node = states[mystate][event];
     node.m_func(context);
     mystate = node.m_nextState;

I am sure this is full of invalid syntax - but I hope you get the drift

Solution 5

Why not use XML as your protocol? You can encapsulate and categorize all your pieces of data inside XML nodes

Share:
16,559
gotch4
Author by

gotch4

can do java, js, python, scala, kotlin, some C/C++ and others for food and housing

Updated on June 02, 2022

Comments

  • gotch4
    gotch4 about 2 years

    Here is a generic question. I'm not in search of the best answer, I'd just like you to express your favourite practices.

    I want to implement a network protocol in Java (but this is a rather general question, I faced the same issues in C++), this is not the first time, as I have done this before. But I think I am missing a good way to implement it. In fact usually it's all about exchanging text messages and some byte buffers between hosts, storing the status and wait until the next message comes. The problem is that I usually end up with a bunch of switch and more or less complex if statements that react to different statuses / messages. The whole thing usually gets complicated and hard to mantain. Not to mention that sometimes what comes out has some "blind spot", I mean statuses of the protocol that have not been covered and that behave in a unpredictable way. I tried to write down some state machine classes, that take care of checking start and end statuses for each action in more or less smart ways. This makes programming the protocol very complicated as I have to write lines and lines of code to cover every possible situation. What I'd like is something like a good pattern, or a best practice that is used in programming complex protocols, easy to mantain and to extend and very readable.

    What are your suggestions?

  • gotch4
    gotch4 over 14 years
    of course I know this. What I want to know are the best practices to implement that FSM and the table... that's my real question
  • Dmitry
    Dmitry over 14 years
    In "Desing Patterns" book (en.wikipedia.org/wiki/Design_Patterns) this pattern is applied to problem involved.
  • gotch4
    gotch4 over 14 years
    I've had a peek on wikipedia and seems very clever and it would solve many problems I have. I should buy that damn Design Patterns book... :)
  • Andres
    Andres over 14 years
    I suggested it because its a open format used widely in the Internet today. I not saying you don't now how to implement codecs. I'm just trying to save some time, as it happened to me in real world projects where I chose this format of protocol.
  • Duck
    Duck over 14 years
    @gotch4 - Browse through a good bookstore. GOF is one of those items you are suppose to genuflect to in public but the truth is there are better pattern books out there now. Most will cover the State pattern so shop around for one you feel comfortable with.
  • user1066101
    user1066101 over 14 years
  • Duck
    Duck over 14 years
    @Craig McQueen GOF = Gang of Four. The four authors of Design Patterns: Elements of Reusable Object-Oriented Software. en.wikipedia.org/wiki/Design_Patterns
  • gotch4
    gotch4 over 14 years
    In fact this is my usual choice... but my concern is not about data formatting, is more about protocol status mantainance and implementation.