JML

net.sf.jqql.packets
Class Packet

java.lang.Object
  extended by net.sf.jqql.packets.Packet
Direct Known Subclasses:
InPacket, OutPacket

public abstract class Packet
extends Object

base class of all QQ packet object QQ所有包对象的基类

Author:
notxx, luma

Field Summary
protected static ByteBuffer bodyBuf
          packet buffer, with back array, use to save unencrypted packet body, subcalss should use this buffer in putBody method.
protected  byte[] bodyDecrypted
          æ˜Žæ–‡åŒ…体
protected  char command
          packet command, 0x03~0x)4.
protected static Crypter crypter
          crypter.
protected static DebugSwitch ds
          è°ƒè¯•模式开关
protected  boolean duplicated
          true means this packet is a duplicated packet, duplicated packet doesn't need to deal with, but cause LumaQQ often happen ack packet miss problem, so here add a field to show that coming message packet is duplicated.
protected  byte header
          åŒ…头字节
protected static org.apache.commons.logging.Log log
          logger.
protected  char sequence
          packet sequence, 0x05~0x06.
protected  char source
          source sign, 0x01~0x02.
protected  QQUser user
          QQUser for creating multiple QQClient in a JVM, packet need to keep a QQuser reference to meake sure relevant field of packet user how to fill.
 
Constructor Summary
protected Packet()
          create a packet object, don't fill any field, only use by subclass 构造一个包对象,什么字段也不填,仅限于子类使用
protected Packet(ByteBuffer buf, int length, QQUser user)
          create a outPacket from buf, use to debug.
protected Packet(ByteBuffer buf, QQUser user)
          ä»Žbuf中构造一个OutPacket,用于调试。这个buf里面可能包含了抓包软件抓来的数据
  Packet(byte header, char source, char command, char sequence, QQUser user)
          create a specify parameters packet 构造一个指定参数的包
 
Method Summary
protected abstract  byte[] decryptBody(byte[] body, int offset, int length)
          decrypt packet body 解密包体
 byte[] dump()
          export all content of packet to a byte array, mostly use to debug 导出包的全部内容到一个字节数组中,主要用于调试
protected abstract  byte[] encryptBody(byte[] b, int offset, int length)
          encrypt packet body 加密包体
 boolean equals(Object obj)
           
protected abstract  byte[] getBodyBytes(ByteBuffer buf, int length)
          get byte array of packet body 得到包体的字节数组
 char getCommand()
           
protected abstract  int getCryptographStart()
           
abstract  int getFamily()
           
 byte getHeader()
           
protected abstract  int getHeadLength()
           
protected abstract  int getLength(int bodyLength)
          get total length of UDP form packet, rule out TCP form 得到UDP形式包的总长度,不考虑TCP形式
 String getPacketName()
           
 char getSequence()
           
 char getSource()
           
protected abstract  int getTailLength()
           
static int hash(char sequence, char command)
          get hash value 得到hash值
 int hashCode()
          make up sequence number and command as hash code.
 boolean isDuplicated()
           
protected abstract  void parseBody(ByteBuffer buf)
          parse packet body, from begin position of buf 解析包体,从buf的开头位置解析起
protected abstract  void parseHeader(ByteBuffer buf)
          parse packet header from current position of buf 从buf的当前位置解析包头
protected abstract  void parseTail(ByteBuffer buf)
          parse packet tail from current position of buf 从buf的当前未知解析包尾
protected abstract  void putBody(ByteBuffer buf)
          initialize packet body 初始化包体
protected abstract  void putHead(ByteBuffer buf)
          change packet header to byte stream, and write into appointed ByteBuffer object.
protected abstract  void putTail(ByteBuffer buf)
          change packet tail to byte stream, and write into appointed ByteBuffer object.
 void setDuplicated(boolean duplicated)
           
 void setHeader(byte header)
           
 void setSequence(char sequence)
           
protected abstract  boolean validateHeader()
          checkout header 校验头部
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
logger.


crypter

protected static final Crypter crypter
crypter. 解密者.


bodyBuf

protected static final ByteBuffer bodyBuf
packet buffer, with back array, use to save unencrypted packet body, subcalss should use this buffer in putBody method. before use, run clear() 包体缓冲区,有back array,用来存放未加密时的包体,子类应该在putBody方法中 使用这个缓冲区。使用之前先执行clear()


ds

protected static DebugSwitch ds
调试模式开关


command

protected char command
packet command, 0x03~0x)4. 包命令, 0x03~0x04.


source

protected char source
source sign, 0x01~0x02. 源标志, 0x01~0x02.


sequence

protected char sequence
packet sequence, 0x05~0x06. 包序号, 0x05~0x06.


header

protected byte header
包头字节


user

protected QQUser user
QQUser for creating multiple QQClient in a JVM, packet need to keep a QQuser reference to meake sure relevant field of packet user how to fill. 为了支持一个JVM中创建多个QQClient,包中需要保持一个QQUser的引用以 确定包的用户相关字段如何填写


duplicated

protected boolean duplicated
true means this packet is a duplicated packet, duplicated packet doesn't need to deal with, but cause LumaQQ often happen ack packet miss problem, so here add a field to show that coming message packet is duplicated. now this field only available to massage, just of sorts a method, though not so pretty. true表示这个包是一个重复包,重复包本来是不需要处理的,但是由于LumaQQ较常发生 消息确认包丢失的问题,所以,这里加一个字段来表示到来的消息包是重复的。目前这个 字段只对消息有效,姑且算个解决办法吧,虽然不是太好看


bodyDecrypted

protected byte[] bodyDecrypted
明文包体

Constructor Detail

Packet

public Packet(byte header,
              char source,
              char command,
              char sequence,
              QQUser user)
create a specify parameters packet 构造一个指定参数的包

Parameters:
header - packet header 包头
source - packet source 包源
command - packet command 包命令
sequence - packet sequence number 包序号
user - QQ user object QQ用户对象

Packet

protected Packet(ByteBuffer buf,
                 QQUser user)
          throws PacketParseException
从buf中构造一个OutPacket,用于调试。这个buf里面可能包含了抓包软件抓来的数据

Parameters:
buf - ByteBuffer
Throws:
PacketParseException - 解析出错

Packet

protected Packet(ByteBuffer buf,
                 int length,
                 QQUser user)
          throws PacketParseException
create a outPacket from buf, use to debug. this buf maybe contain data which catched by catching packet software 从buf中构造一个OutPacket,用于调试。这个buf里面可能包含了抓包软件抓来的数据

Parameters:
buf - ByteBuffer
length - parsing content length 要解析的内容长度
Throws:
PacketParseException - if parse error 如果解析出错

Packet

protected Packet()
create a packet object, don't fill any field, only use by subclass 构造一个包对象,什么字段也不填,仅限于子类使用

Method Detail

dump

public byte[] dump()
export all content of packet to a byte array, mostly use to debug 导出包的全部内容到一个字节数组中,主要用于调试

Returns:
byte array for packet 包的字节数组

getLength

protected abstract int getLength(int bodyLength)
get total length of UDP form packet, rule out TCP form 得到UDP形式包的总长度,不考虑TCP形式

Parameters:
bodyLength - packet body length 包体长度
Returns:
packet length 包长度

validateHeader

protected abstract boolean validateHeader()
checkout header 校验头部

Returns:
true means header usable true表示头部有效

getHeadLength

protected abstract int getHeadLength()
Returns:
packet header length 包头长度

getTailLength

protected abstract int getTailLength()
Returns:
packet tail length 包尾长度

putHead

protected abstract void putHead(ByteBuffer buf)
change packet header to byte stream, and write into appointed ByteBuffer object. 将包头部转化为字节流, 写入指定的ByteBuffer对象.

Parameters:
buf - ByteBuffer object whicn packet need to write into 写入的ByteBuffer对象.

putBody

protected abstract void putBody(ByteBuffer buf)
initialize packet body 初始化包体

Parameters:
buf - ByteBuffer

getBodyBytes

protected abstract byte[] getBodyBytes(ByteBuffer buf,
                                       int length)
get byte array of packet body 得到包体的字节数组

Parameters:
buf - ByteBuffer
length - packet total length 包总长度
Returns:
byte array of packet body 包体字节数组

getFamily

public abstract int getFamily()
Returns:
sign this packet belong which protocol family 标识这个包属于哪个协议族

putTail

protected abstract void putTail(ByteBuffer buf)
change packet tail to byte stream, and write into appointed ByteBuffer object. 将包尾部转化为字节流, 写入指定的ByteBuffer对象.

Parameters:
buf - ByteBuffer object whicn packet need to write into 写入的ByteBuffer对象.

encryptBody

protected abstract byte[] encryptBody(byte[] b,
                                      int offset,
                                      int length)
encrypt packet body 加密包体

Parameters:
b - unencrypt byte array 未加密的字节数组
offset - offset of packet body begin 包体开始的偏移
length - packet body length 包体长度
Returns:
encrypted packet body 加密的包体

decryptBody

protected abstract byte[] decryptBody(byte[] body,
                                      int offset,
                                      int length)
decrypt packet body 解密包体

Parameters:
body - byte array of packet body 包体字节数组
offset - offset of packet body begin 包体开始偏移
length - packet length 包体长度
Returns:
解密的包体字节数组 // byte array of decrypted packet body

getCryptographStart

protected abstract int getCryptographStart()
Returns:
start position of cryptograph, relative to the first byte of packet body. if this packet is unknown packet, return -1, this method only available to some protocol family. 密文的起始位置,这个位置是相对于包体的第一个字节来说的,如果这个包是未知包, 返回-1,这个方法只对某些协议族有意义

parseBody

protected abstract void parseBody(ByteBuffer buf)
                           throws PacketParseException
parse packet body, from begin position of buf 解析包体,从buf的开头位置解析起

Parameters:
buf - ByteBuffer
Throws:
PacketParseException - if parse error 如果解析出错

parseHeader

protected abstract void parseHeader(ByteBuffer buf)
                             throws PacketParseException
parse packet header from current position of buf 从buf的当前位置解析包头

Parameters:
buf - ByteBuffer
Throws:
PacketParseException - if parse error 如果解析出错

parseTail

protected abstract void parseTail(ByteBuffer buf)
                           throws PacketParseException
parse packet tail from current position of buf 从buf的当前未知解析包尾

Parameters:
buf - ByteBuffer
Throws:
PacketParseException - if parse error 如果解析出错

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
make up sequence number and command as hash code. for avoid different packet have the same command, header join in 把序列号和命令拼起来作为哈希码. 为了避免不同header的包有相同的命令,Header也参与进来

Overrides:
hashCode in class Object

hash

public static int hash(char sequence,
                       char command)
get hash value 得到hash值

Parameters:
header -
sequence -
command -
Returns:

getCommand

public char getCommand()
Returns:
Returns the command.

getSequence

public char getSequence()
Returns:
Returns the sequence.

setSequence

public void setSequence(char sequence)
Parameters:
sequence - The sequence to set.

getPacketName

public String getPacketName()
Returns:
descriptive name of packet 包的描述性名称

getSource

public char getSource()
Returns:
Returns the source.

isDuplicated

public boolean isDuplicated()
Returns:
Returns the duplicated.

setDuplicated

public void setDuplicated(boolean duplicated)
Parameters:
duplicated - The duplicated to set.

getHeader

public byte getHeader()
Returns:
Returns the header.

setHeader

public void setHeader(byte header)
Parameters:
header - The header to set.

JML