开发者

How to implement a network protocol?

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 sma开发者_开发知识库rt 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?


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.


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.


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


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


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


Can't give you an example myself, but how about looking at how other (competent) people are doing it?

Like this one? http://anonsvn.jboss.org/repos/netty/trunk/src/main/java/org/jboss/netty/handler/codec/http/

P.S. for that matter, I actually recommend using netty as your network framework and build your protocol on top of it. It should be very easy, and you'll probably get rid of bunch of headaches...


If you are using Java, consider looking at Apache MINA, it's documentation and samples should inspire you in the right way.


Right-click the network connection icon in the System Tray. Click Troubleshoot problems. The troubleshooter may find and fix the problem, in this case, you can get quickly started with your business. If the troubleshooter can't fix the Winsocks problem, then you may get an error looking like: "One or more network protocols are missing on this computer"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜