netty--Private protocol stack development case

Source: Internet
Author: User
Tags object serialization jboss

netty--Private Protocol stack Development Case summary:

In the study Li Lin Teacher's Netty authoritative guide, feel that the 12th chapter "Private protocol stack development" in the case code is more representative, said is also good, but the code of the individual think some simple error, the individual after a simple modification, compiled after the show to everyone, what problems, hope messages, common exchange;

Related packages:

    • Meven configuration:

       <dependency>
             <groupid>io.netty</groupid>
             <artifactid>netty-all</artifactid>
             <version>5.0.0.alpha2</version>
         </dependency>
        <dependency>
             <groupid>org.jboss.marshalling</groupid>
            <artifactid>jboss-marshalling-serial </artifactid>
            <version>2.0.0. Beta2</version>
        </dependency>

    • Project distribution

    • Code

Nettymessage Defining classes

--------------------------------------------------------------------------------------------------------------- ------------------------

Public final class Nettymessage {
Private header header;
Private Object body;


Public final Header GetHeader () {
return header;
}
Public final void SetHeader (header header) {
This.header = header;
}
Public final Object GetBody () {
return body;
}
Public final void Setbody (Object body) {
This.body = body;
}

Public String toString () {
Return "Nettymessage [header=" +header+ "]";
}

}

--------------------------------------------------------------------------------------------------------------- ------------------------

Message header Definition Class

--------------------------------------------------------------------------------------------------------------- ------------------------

Public final class Header {
private int crccode = 0xabef0101;
private int length;//message length
Private long sessionid;//Callback ID
Private byte type;//message type
Private byte priority;//message priority
Private map<string, object> attachment = new hashmap<string, object> ();//Accessories


Public final int Getcrccode () {
return crccode;
}
Public final void Setcrccode (int crccode) {
This.crccode = Crccode;
}
Public final int GetLength () {
return length;
}
Public final void setLength (int length) {
this.length = length;
}
Public final long GetSessionID () {
return SessionID;
}
Public final void Setsessionid (long SessionID) {
This.sessionid = SessionID;
}
Public final byte GetType () {
return type;
}
Public final void SetType (byte type) {
This.type = type;
}
Public final byte getpriority () {
return priority;
}
Public final void SetPriority (Byte priority) {
This.priority = priority;
}
Public final map<string, object> getattachment () {
return attachment;
}
Public final void Setattachment (map<string, object> attachment) {
this.attachment = attachment;
}

Public String toString () {
Return "headder[crccode=", "+crccode+", length= "+length+", sessionid= "
+sessionid+ ", type=" +type+ ", priority=" +priority+ "attachment=" +attachment;
}

}

--------------------------------------------------------------------------------------------------------------- ------------------------

Defining message Types

--------------------------------------------------------------------------------------------------------------- ------------------------

public class MessageType {
/**
* Request
*/
Public final static byte login_req = 0x01;
/**
* Reply
*/
Public final static byte Login_resp = 0x02;
/**
* Heartbeat Request
*/
Public final static byte heartbeat_req = 0x03;
/**
* Heartbeat Recovery
*/
Public final static byte Heartbeat_resp = 0x04;
}

--------------------------------------------------------------------------------------------------------------- ------------------------

Define Marshallingcodecfactory

--------------------------------------------------------------------------------------------------------------- ------------------------

Import Io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
Import Io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
Import Io.netty.handler.codec.marshalling.MarshallerProvider;
Import Io.netty.handler.codec.marshalling.UnmarshallerProvider;

Import Org.jboss.marshalling.MarshallerFactory;
Import org.jboss.marshalling.Marshalling;
Import org.jboss.marshalling.MarshallingConfiguration;
/**
*
* @ClassName Marshallingcodecfactory
* @Description Todojboss marshalling is a Java object serialization package optimized for the JDK default serialization framework,<br>
* but also to maintain compatibility with the Java.io.Serializable interface, while adding a number of adjustable parameters and the characteristics of the attachment,<br>
* These parameters and additional features, these parameters and features can be configured through the factory class. Marshalling Construction tools
* @author Lichunyang
* @Date April 13, 2017 7:34:31
* @version 1.0.0
*/
public class Marshallingcodecfactory {

public static Nettymarshallingdecoder Buildmarshallingdecoder () {
/*
* Through the Marshalling tool class Getprovidedmarshallerfactory
* Static method Gets the Marshallerfactory instance, and the parameter serial represents the Java sequence factory object that was created. It was made by
* Jboss-marshalling-serial Package Offers
*/
Final Marshallerfactory marshallerfactory = marshalling.getprovidedmarshallerfactory ("Serial");
/**
* Create
*/
Final marshallingconfiguration configuration = new Marshallingconfiguration ();
Configuration.setversion (5);
Unmarshallerprovider Provider = new Defaultunmarshallerprovider (marshallerfactory, configuration);
Nettymarshallingdecoder decoder = new Nettymarshallingdecoder (provider, 1024);//1024 single object maximum size
return decoder;
}

public static Nettymarshallingencoder Buildmarshallingencoder () {
Marshallerfactory marshallerfactory = marshalling.getprovidedmarshallerfactory ("Serial");
Marshallingconfiguration configuration = new Marshallingconfiguration ();
Configuration.setversion (5);
Marshallerprovider Provider = new Defaultmarshallerprovider (marshallerfactory, configuration);
Nettymarshallingencoder encoder = new Nettymarshallingencoder (provider);
Return encoder;
}
}

--------------------------------------------------------------------------------------------------------------- ------------------------

Define Nettymarshallingdecoder and Nettymarshallingencoder

--------------------------------------------------------------------------------------------------------------- ------------------------

Package netty.codefactory;

Import Io.netty.buffer.ByteBuf;
Import Io.netty.channel.ChannelHandlerContext;
Import Io.netty.handler.codec.marshalling.MarshallingDecoder;
Import Io.netty.handler.codec.marshalling.UnmarshallerProvider;
/**
*
* @ClassName Nettymarshallingdecoder
* @Description extension Marshallingencoder and Marshallingdecoder, the protected method programming public can call the <br> message decoding tool class
* @author Lichunyang
* @Date April 13, 2017 7:22:03
* @version 1.0.0
*/
public class Nettymarshallingdecoder extends Marshallingdecoder {

Public Nettymarshallingdecoder (Unmarshallerprovider provider) {
Super (provider);
}

Public Nettymarshallingdecoder (unmarshallerprovider provider, int maxobjectsize) {
Super (provider, maxobjectsize);
}

Public Object decode (Channelhandlercontext ctx, bytebuf in) throws Exception {
Return Super.decode (CTX, in);
}

}

--------------------------------------------------------------------------------------------------------------- ------------------------

Package netty.codefactory;

Import Io.netty.buffer.ByteBuf;
Import Io.netty.channel.ChannelHandlerContext;
Import Io.netty.handler.codec.marshalling.MarshallerProvider;
Import Io.netty.handler.codec.marshalling.MarshallingEncoder;
/**
*
* @ClassName Nettymarshallingencoder
* @Description extension Marshallingencoder and Marshallingdecoder, the protected method programming public can call the <br> Message encoding tool class
* @author Lichunyang
* @Date April 13, 2017 7:20:07
* @version 1.0.0
*/
public class Nettymarshallingencoder extends marshallingencoder{

Public Nettymarshallingencoder (Marshallerprovider provider) {
Super (provider);
}

public void Encode (Channelhandlercontext ctx, Object msg, bytebuf out) throws exception{
Super.encode (CTX, MSG, out);
}

}

--------------------------------------------------------------------------------------------------------------- ------------------------

Define Nettymessagedecoder and Nettymessageencoder

--------------------------------------------------------------------------------------------------------------- ------------------------

Package netty.codefactory;

Import Io.netty.buffer.ByteBuf;
Import Io.netty.channel.ChannelHandlerContext;
Import Io.netty.handler.codec.LengthFieldBasedFrameDecoder;
Import Netty.message.Header;
Import Netty.message.NettyMessage;

Import Java.util.HashMap;
Import Java.util.Map;
/**
*
* @ClassName Nettymessagedecoder
* @Description Decoder
* @author Lichunyang
* @Date April 13, 2017 7:51:02
* @version 1.0.0
*/
public class Nettymessagedecoder extends lengthfieldbasedframedecoder{

Private Nettymarshallingdecoder Marshallingdecoder;

Public Nettymessagedecoder (int maxframelength, int lengthfieldoffset,
int lengthfieldlength,int lengthadjustment, int initialbytestostrip) {
Super (Maxframelength, Lengthfieldoffset, Lengthfieldlength, Lengthadjustment, Initialbytestostrip);
Marshallingdecoder = Marshallingcodecfactory.buildmarshallingdecoder ();
}

Public Object decode (Channelhandlercontext ctx, bytebuf in) throws exception{
Bytebuf frame = (bytebuf) super.decode (CTX, in);
if (frame = = null) {
return null;
}

Nettymessage message = new Nettymessage ();
Header Header = new header ();
Header.setcrccode (Frame.readint ());
Header.setlength (Frame.readint ());
Header.setsessionid (Frame.readlong ());
Header.settype (Frame.readbyte ());
Header.setpriority (Frame.readbyte ());

int size = Frame.readint ();
if (Size > 0) {
Map<string, object> attach = new hashmap<string, object> (size);
int keySize = 0;
byte[] Keyarray = null;
String key = null;
for (int i=0; i<size; i++) {
KeySize = Frame.readint ();
Keyarray = new Byte[keysize];
In.readbytes (Keyarray);
Key = new String (Keyarray, "UTF-8");
Attach.put (Key, Marshallingdecoder.decode (CTX, frame));
}
key = null;
Keyarray = null;
Header.setattachment (attach);
}
if (frame.readablebytes () > 0) {
Message.setbody (Marshallingdecoder.decode (CTX, frame));
}
Message.setheader (header);
return message;
}
}

netty--Private protocol stack development case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.