Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize your own protocols in Netty, MINA and Twisted

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to customize your own protocols in Netty, MINA, and Twisted. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Previously, I introduced some message segmentation schemes, as well as the related API provided by MINA, Netty, and Twisted for these schemes. For example, TextLineCodecFactory of MINA, LineBasedFrameDecoder of PrefixedStringCodecFactory,Netty, LineOnlyReceiver of LengthFieldBasedFrameDecoder,Twisted, Int32StringReceiver.

In addition to these options, there are many other options, which can of course be defined by yourself. Here, we customize our own scheme, and use MINA, Netty and Twisted to parse and assemble this message, that is, encoding and decoding.

Previously, we introduced a message segmentation scheme that uses a fixed number of bytes of Header to specify the number of Body bytes, where the Header part is a 4-byte integer of the regular large byte order (Big-Endian). In this paper, the scheme is slightly modified to change the fixed number of bytes of Header to a 4-byte integer of small byte order (Little-Endian).

If the conventional large byte order represents a number, it is more in line with people's habit to use high byte bits to store the low bits of numbers. On the other hand, small byte order is the opposite of large byte order, using high byte bits to store the high order of numbers.

The struct module in Python supports pack and unpack in large and small byte order. In Java, you can use the following two methods to convert small byte order byte arrays to int and int to small byte order byte arrays, which will be used in the following Java programs:

Public class LittleEndian {

/ * *

* convert int to a 4-byte array of small ordinal bytes

, /

Public static byte [] toLittleEndian (int I) {

Byte [] bytes = new byte [4]

Bytes [0] = (byte) I

Bytes [1] = (byte) (I > 8)

Bytes [2] = (byte) (I > 16)

Bytes [3] = (byte) (I > 24)

Return bytes

}

/ * *

* convert a 4-byte byte array in small byte order to int

, /

Public static int getLittleEndianInt (byte [] bytes) {

Int b0 = bytes [0] & 0xFF

Int b1 = bytes [1] & 0xFF

Int b2 = bytes [2] & 0xFF

Int b3 = bytes [3] & 0xFF

Return b0 + (b1 = 4 + length:

# body section

Packet = self._buffer [4:4 + length]

# when a new message is received and decoded, call stringReceived

Self.stringReceived (packet)

# remove the message parts that have been processed in _ buffer

Self._buffer = self._buffer [4 + length:]

Else:

Break

Else:

Break

Def stringReceived (self, data):

Raise NotImplementedError

Def sendString (self, string):

Self.transport.write (pack ("

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report