?? basicinpacket.java
字號:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 notXX
* luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.qq.packets;
import java.nio.ByteBuffer;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.annotation.BasePacket;
import edu.tsinghua.lumaqq.qq.annotation.DocumentalPacket;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
/**
* <pre>
* 基本協議族的輸入包基類:
* 1. 包頭標志,1字節,0x02
* 2. 服務器端版本代碼, 2字節
* 3. 命令,2字節
* 4. 包序號,2字節
* 5. 包體
* 6. 包尾標志,1字節,0x03
* <pre>
*
* @author notxx
* @author luma
*/
@DocumentalPacket
@BasePacket(name="基本協議族輸入包", klass=BasicInPacket.class)
public abstract class BasicInPacket extends InPacket {
/**
* @param command
* @param user
*/
public BasicInPacket(char command, QQUser user) {
super(QQ.QQ_HEADER_BASIC_FAMILY, QQ.QQ_SERVER_VERSION_0100, command, user);
}
/**
* 構造一個指定參數的包.從buf的當前位置開始解析直到limit
*
* @param buf ByteBuffer對象
* @throws PacketParseException
* 內容解析出錯.
*/
public BasicInPacket(ByteBuffer buf, QQUser user) throws PacketParseException {
super(buf, user);
}
/**
* 構造一個InPacket,從buf的當前位置解析length個字節
*
* @param buf ByteBuffer對象
* @param length 字節數
* @throws PacketParseException
* 內容解析出錯
*/
public BasicInPacket(ByteBuffer buf, int length, QQUser user) throws PacketParseException {
super(buf, length, user);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#parseHeader(java.nio.ByteBuffer)
*/
@Override
protected void parseHeader(ByteBuffer buf) throws PacketParseException {
if(!user.isUdp())
buf.getChar();
header = buf.get();
source = buf.getChar();
command = buf.getChar();
sequence = buf.getChar();
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#parseTail(java.nio.ByteBuffer)
*/
@Override
protected void parseTail(ByteBuffer buf) {
buf.get();
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#putHead(java.nio.ByteBuffer)
*/
@Override
protected void putHead(ByteBuffer buf) {
if(!user.isUdp())
buf.putChar((char)0);
buf.put(getHeader());
buf.putChar(source);
buf.putChar(command);
buf.putChar(sequence);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#putBody(java.nio.ByteBuffer)
*/
@Override
protected void putBody(ByteBuffer buf) {
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#putTail(java.nio.ByteBuffer)
*/
@Override
protected void putTail(ByteBuffer buf) {
buf.put(QQ.QQ_TAIL_BASIC_FAMILY);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getPacketName()
*/
@Override
public String getPacketName() {
return "Unknown Incoming Packet - 0x" + Integer.toHexString(command).toUpperCase();
}
* @see edu.tsinghua.lumaqq.qq.packets.Packet#decryptBody(byte[], int, int)
*/
@Override
protected byte[] decryptBody(byte[] buf, int offset, int len) {
byte[] temp = null;
switch(command) {
case QQ.QQ_CMD_REQUEST_LOGIN_TOKEN:
byte[] undecrypted = new byte[len];
System.arraycopy(buf, offset, undecrypted, 0, len);
return undecrypted;
case QQ.QQ_CMD_LOGIN:
temp = crypter.decrypt(buf, offset, len, user.getPasswordKey());
if(temp == null)
temp = crypter.decrypt(buf, offset, len, user.getInitKey());
return temp;
default:
if(user.getSessionKey() != null)
temp = crypter.decrypt(buf, offset, len, user.getSessionKey());
if(temp == null)
temp = crypter.decrypt(buf, offset, len, user.getPasswordKey());
return temp;
}
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#encryptBody(byte[], int, int)
*/
@Override
protected byte[] encryptBody(byte[] b, int offset, int length) {
return null;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getBodyBytes(java.nio.ByteBuffer, int)
*/
@Override
protected byte[] getBodyBytes(ByteBuffer buf, int length) {
// 得到包體長度
int bodyLen = length - QQ.QQ_LENGTH_BASIC_FAMILY_IN_HEADER - QQ.QQ_LENGTH_BASIC_FAMILY_TAIL;
if(!user.isUdp()) bodyLen -= 2;
// 得到加密的包體內容
byte[] body = new byte[bodyLen];
buf.get(body);
return body;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getLength(int)
*/
@Override
protected int getLength(int bodyLength) {
return QQ.QQ_LENGTH_BASIC_FAMILY_IN_HEADER + QQ.QQ_LENGTH_BASIC_FAMILY_TAIL + bodyLength + (user.isUdp() ? 0 : 2);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getHeadLength()
*/
@Override
protected int getHeadLength() {
return QQ.QQ_LENGTH_BASIC_FAMILY_IN_HEADER + (user.isUdp() ? 0 : 2);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getTailLength()
*/
@Override
protected int getTailLength() {
return QQ.QQ_LENGTH_BASIC_FAMILY_TAIL;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "包類型: " + Util.getCommandString(command) + " 序號: " + (int)sequence;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getCryptographStart()
*/
@Override
protected int getCryptographStart() {
return -1;
}
@Override
public int getFamily() {
return QQ.QQ_PROTOCOL_FAMILY_BASIC;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -