Author: jinhuaxing recently developed in Java New IO a set of simple network protocols, here I put some of the ideas developed in the tidy, summed up a simple network protocol development framework that can be reused, hoping to give beginners a little help.
The basic communication unit of the network protocol is a message package. When transferring these packages with Socket, the first question to be solved is how to solve the boundary between the package and the package. Socket transmission is a stream, a message issued in a Send, not necessarily received in a RECV, may have to receive multiple recv, or a RECV receives the package released in multiple Send. So you must solve the boundary issue of the package by the application layer protocol. There are usually two ways, one is to end each package with a special character or string, such as the HTTP protocol is the end tag of two '/ n' as a message; another method is that all messages There is a fixed length message head, pointing out the length of this message in the message header. Our protocol is the second method, which is also a method of most protocols. The framework proposed in this paper is also solving this protocol.
Java's New IO is introduced in J2SE1.4, mainly introduced in the concept of buffer, and sending acceptance data is performed on the buffer, and for beginners, Buffer operation is complicated, easy to make mistakes. Therefore, in this framework, the operation of buffer is encapsulated as much as possible.
3. MessageFactory interface
This interface encapsulates all the creations of a class that is used to express messages, calls in the MessageChannel Receive. Here is an abstract factory model. It is defined as follows:
Public interface messagefactory {/ *** Return to the number of bytes of message head * / int getMessageHeaderLength (); / *** Create a message header object * / messageheader createMessageHeader (); / *** Create a message object * @Param Type Message type, get * / Message CreateMessage (int type) from the message header;}
4. MessageChannel class
The main functions are in this class, used to send and receive messages, and encapsulate all for Buffer operations.
Class MessageChannel {/ *** Construction Method, requires the size of the transmission buffer and the reception buffer * / public MessageChannel (int ReceiveBuffersize, Int SendBufferSize, SocketChannel SC, MessageFactory MF); / *** Receive a message, when the message is not Complete, the received message length is too large (exceeding the received buffer size) or does not throw MessageFormATexception when the message type created by MessageFactory. * / public message receive () throws ioException, messageformatexception; / *** Send a message * / public void send (Message Message) Throws oews ooException;}
5. BuffERUTIL class
This is a Utility class, the main function is to acquire or put a string string from bytebuffer, different protocols have different string processing methods.
Class bufferutil {/ *** acquire a string from Buffer, Length is the length * / static string getString (Bytebuffer Buffer, INT LENGTH); / *** Get a string of '/ 0' ending from Buffer, Length is Maximum length * / static string getcstring (Bytebuffer Buffer, int LENGTH); / *** A growing string from Buffer, with two bytes of Short Type indicates * / static string getvarstringshortensLength; Bytebuffer buffer; / * ** From Buffer, a growing string is obtained, and the length is used to represent * / static string getvarstringintLength with the four-byte INT type; / *** A growing string from Buffer, with one byte Byte represents * / static string getvarstringBytelength (); / *** Add a string in Buffer, Length is the length * / static void Putstring (Bytebuffer Buffer, String Str, INT Length); / *** In Buffer Put a string, Length is the maximum length. If the STR does not reach the maximum length, then fill it with 0. * / static void Putcstring (BYTEBUFFER BUFFER, STRING STRING STRIN; / *** Put a string in Buffer, with a short indicate * / static void putvarstringshuTlength; / *** in Buffer Put a string in a string, with an int represent * / static void putvarstringintLength (Bytebuffer buffer); / *** Add a string in the buffer, with a BYTE to indicate * / static void putvarstringBytelength; Bytebuffer Buffer; } Currently, this class does not consider encoding mode, which can be expanded.
In this frame, Message.Receive is the most complex part, and the process of the process will be described below:
(1) Receive the message header according to the message header returned according to MessageFactory.getMessageHeaderLength ().
(2) Construct the message header with a messageFactory.createMessageHeader () and call MessageHeader.BuildFromBuffer () obtains message header data.
(3) Receive the message body according to the information in the message header
(4) Construct the message and call Message.BUILDBODYFROMBUFFER () to obtain the message body data.
(5) Call Message.setHeader ().
(6) Returns the message of the structure.
One of this is an error, and will throw a MessageFormATexception.