?? outpacket.java
字號:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
* notXX
*
* 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.beans.QQUser;
import edu.tsinghua.lumaqq.qq.debug.IDebugObject;
import edu.tsinghua.lumaqq.qq.debug.PacketDO;
/**
* 所有輸出包基類,這個基類定義了輸出包的基本框架
*
* @author luma
*/
public abstract class OutPacket extends Packet {
/** 包起始序列號 */
protected static char seq = (char) Util.random().nextInt();
/** 是否需要回應 */
protected boolean ack;
/** 重發計數器 */
protected int resendCountDown;
/** 超時截止時間,單位ms */
protected long timeout;
/** 發送次數,只在包是不需要ack時有效,比如logout包是發4次,但是其他可能只發一次 */
protected int sendCount;
/** 加密/解密密鑰,只有有些包可能需要一個特定的密鑰,如果為null,使用缺省的 */
protected byte[] key;
/**
* 創建一個基本輸出包
*
* @param command
* 包命令
* @param ack
* 包是否需要回復
* @param user
* QQ用戶對象
*/
public OutPacket(byte header, char command, boolean ack, QQUser user) {
super(header, QQ.QQ_CLIENT_VERSION, command, getNextSeq(), user);
this.ack = ack;
this.resendCountDown = QQ.QQ_SEND_TIME_NOACK_PACKET;
this.sendCount = 1;
}
/**
* 從buf中構造一個OutPacket,用于調試。這個buf里面可能包含了抓包軟件抓來的數據
*
* @param buf
* ByteBuffer
* @throws PacketParseException
* 解析出錯
*/
protected OutPacket(ByteBuffer buf, QQUser user) throws PacketParseException {
super(buf, user);
}
/**
* 從buf中構造一個OutPacket,用于調試。這個buf里面可能包含了抓包軟件抓來的數據
*
* @param buf
* ByteBuffer
* @param length
* 要解析的內容長度
* @throws PacketParseException
* 如果解析出錯
*/
protected OutPacket(ByteBuffer buf, int length, QQUser user) throws PacketParseException {
super(buf, length, user);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#parseBody(java.nio.ByteBuffer)
*/
@Override
protected void parseBody(ByteBuffer buf) throws PacketParseException {
}
/**
* 回填,有些字段必須填完整個包才能確定其內容,比如長度字段,那么這個方法將在
* 尾部填充之后調用
*
* @param buf
* ByteBuffer
* @param startPos
* 包起始位置
*/
protected abstract void postFill(ByteBuffer buf, int startPos);
/**
* 將整個包轉化為字節流, 并寫入指定的ByteBuffer對象.
* 一般而言, 前后分別需要寫入包頭部和包尾部.
*
* @param buf
* 將包寫入的ByteBuffer對象.
*/
public void fill(ByteBuffer buf) {
// 保存當前pos
int pos = buf.position();
// 填充頭部
putHead(buf);
// 填充包體
bodyBuf.clear();
putBody(bodyBuf);
bodyBuf.flip();
// 加密包體
bodyDecrypted = new byte[bodyBuf.limit()];
System.arraycopy(bodyBuf.array(), 0, bodyDecrypted, 0, bodyDecrypted.length);
byte[] enc = encryptBody(bodyDecrypted, 0, bodyDecrypted.length);
// 加密內容寫入最終buf
buf.put(enc);
// 填充尾部
putTail(buf);
// 回填
postFill(buf, pos);
// 查看是否打開了調試模式,如果有,則觸發調試事件
if(ds.isDebug()) {
byte[] debugContent = dump();
IDebugObject obj = new PacketDO(getPacketName(), debugContent, false, getHeadLength(), debugContent.length - getTailLength());
ds.deliverDebugObject(obj);
}
}
@Override
public byte[] dump() {
if(bodyDecrypted == null)
return new byte[0];
else {
byte[] debugContent = new byte[getLength(bodyDecrypted.length)];
ByteBuffer debugBuf = ByteBuffer.wrap(debugContent);
putHead(debugBuf);
debugBuf.put(bodyDecrypted);
putTail(debugBuf);
postFill(debugBuf, 0);
debugBuf = null;
return debugContent;
}
}
/**
* @return
* 下一個可用的序列號
*/
protected static char getNextSeq() {
seq++;
// 為了兼容iQQ
// iQQ把序列號的高位都為0,如果為1,它可能會拒絕,wqfox稱是因為TX是這樣做的
seq &= 0x7FFF;
if(seq == 0)
seq++;
return seq;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.packets.Packet#getPacketName()
*/
@Override
public String getPacketName() {
return "Unknown Outcoming Packet";
}
/**
* 是否需要重發.
*
* @return 需要重發返回true, 否則返回false.
*/
public final boolean needResend() {
return (resendCountDown--) > 0;
}
/**
* @return true表示包需要回復
*/
public final boolean needAck() {
return ack;
}
/**
* @return Returns the timeout.
*/
public final long getTimeout() {
return timeout;
}
/**
* @param timeout The timeout to set.
*/
public final void setTimeout(long timeout) {
this.timeout = timeout;
}
/**
* @param sendCount The sendCount to set.
*/
public final void setSendCount(int sendCount) {
this.sendCount = sendCount;
}
/**
* @return Returns the sendCount.
*/
public final int getSendCount() {
return sendCount;
}
/**
* @return Returns the key.
*/
public byte[] getKey() {
return key;
}
/**
* @param key The key to set.
*/
public void setKey(byte[] key) {
this.key = key;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -