开发者

Tips about design/implementation of own protocol

Where I work we are in need of a protocol capable of:

  • User login/logout
  • Send/recive instructions
  • Send/recive files
  • Send/recive audio stream(could use RTP)
  • Send/recive small XML files Use
  • cryptography for all those.

It will be implemented in java. So I have some questions, since I´ve never implemeted a network protocol yet.

  1. Is it possible to use existing protocols to build this one?
  2. What tool can I use to help me design the protocol? for "Modeling"
  3. Is it possi开发者_Go百科ble to acomplish all this, doing it alone? I have as much time as I need for this.

I have a pretty good background in Java and C++, but not yet with sockets/networking programming.

Thanks


Take a look a Google Protocol Buffers, which will generate a compact wire protocol as well as autogenerating Java message classes. I wish I'd heard of it before rolling my own message codec using Java NIO ByteBuffers.


I've got a feeling you're trying to reinvent either SIP (if your packet processing is mostly stateless and XML is small enough to go into <3k packets), or XMPP.

If you need a connection oriented login/logout, and stateful commands/instructions, then XMPP is probably closer to the requirements. Also, Jingle extension to XMPP already deals with RTP setup and teardown. XML messages are trivial to embed into custom XMPP packets (which themselves are XML) and there are known XMPP solutions for proxying a file transfer.

I'm pretty sure it meets your requirements quite well (at least the way they're presented here). If you don't have to design a completely new protocol, it's probably easier if you don't. Also reusing an existing XMPP server will allow you to solve the pain of creating your own message broker. There's OpenFire server, which is written in Java.


I dont know if this is bad advice or not, but what I usually do for my networking applications is to make a Message object that holds a TAG string and CONTENT string. The CONTENT part is usually a JSON string and the message itself is also sent to/from the server as a json string.

When the server or client receives a message, it parses the json into a Message object. You can then check the TAG part of the message to see what type of content is held in the CONTENT part of the message and decide what to do with it.

For example, if TAG=="LOGIN" then the CONTENT may be login details or similar. And when the TAG=="MESSAGE" then the CONTENT will perhaps be a json string representing your parameters, for example, who is the recipient/s and what is the content of the message, etc.

You can then do you encryption and decryption on the strings. If this is a stupid way of doing it, please tell me so in a comment so i can learn :)

I also usually implement a state design pattern on both sides, but at least on the server side. For example, the server starts out in a WaitingForLogin state. When the client logs in it switches to a different state that only listens for files and chat messages as an example. In this way I found it is a bit easier to manage.


You could use http or https. The java media framework contains an implementation of rtp.


Writing the protocol from scratch may require a lot of work. Take a look at XMPP.

If you want to write your own protocol, start with learning a form of RPC like JSON or similar, which will make your life a lot easier.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜