📄 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.Crypter;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Utils;
/**
* 发出的包, 是所有发出包的基类.
* 每一个发出包都有一个头部和一个尾部, 头部的格式为:
* <ol>
* <li>头部
* <ol>
* <li>标志字节, 0x00. (==0x2)
* <li>客户端版本代码, 0x01~0x02.
* <li>命令类型, 0x03~0x04.
* <li>包序号, 0x05~0x06.
* </ol>
* <li>包的内容
* <li>尾部, 0x00. (==0x3)
* </ol>
* 头部, QQ号和尾部都是不加密的, 除此之外内容部分都将用会话密钥加密, 其中登录包要用密码密钥加密
* 相对基类增加了是否需要重发的属性.
*
* @see notxx.lumaqq.qq.QQ#QQ_PACKET_TAG
* @see notxx.lumaqq.qq.QQ#QQ_PACKET_TAIL
* @author notxx
* @author 马若劼
*/
public abstract class OutPacket extends Packet {
/** 加密者 */
protected static final Crypter crypter = new Crypter();
/** 包起始序列号 */
protected static char seq = (char) Utils.random().nextInt();
/**
* 包体缓冲区,有back array,用来存放未加密时的包体,子类应该在putBody方法中
* 使用这个缓冲区。使用之前先执行clear()
*/
protected static final ByteBuffer bodyBuf = ByteBuffer.allocate(3000);
/** 是否需要回应 */
private boolean ack;
/** 重发计数器 */
private int resend;
/** 超时时间 */
private long timeout;
/**
* 构造一个参数指定的包.
* @param command
* 命令.
* @param ack
* 是否需要回应.
*/
protected OutPacket(char command, boolean ack) {
super(QQ.QQ_CLIENT, command, seq++);
this.ack = ack;
this.resend = QQ.QQ_ACK_PACKET_SEND_TIME;
}
/**
* 将整个包转化为字节流, 并写入指定的ByteBuffer对象.
* 一般而言, 前后分别需要写入包头部和包尾部.
*
* @see OutPacket#putHead(ByteBuffer)
* @see OutPacket#putTail(ByteBuffer)
* @param buf
* 将包写入的ByteBuffer对象.
*/
public void fill(ByteBuffer buf) {
// 保存当前pos
int pos = buf.position();
// 填充头部
putHead(buf);
// 填充包体
bodyBuf.clear();
putBody(bodyBuf);
bodyBuf.flip();
// 加密包体
byte[] enc = encryptBody(bodyBuf.array(), 0, bodyBuf.limit());
// 加密内容写入最终buf
buf.put(enc);
// 填充尾部
putTail(buf);
// 如果是tcp包,到包的开头处填上包长度,然后回到目前的pos
if(!user.isUdp()) {
int len = buf.position() - pos;
buf.putChar(pos, (char)len);
}
}
/**
* 加密包体
* @param b 未加密的字节数组
* @param offset 包体开始的偏移
* @param length 包体长度
* @return 加密的包体
*/
private byte[] encryptBody(byte[] b, int offset, int length) {
if(command == QQ.QQ_CMD_LOGIN) {
byte[] ret = new byte[length];
System.arraycopy(b, offset, ret, 0, length);
return ret;
} else
return crypter.encrypt(b, offset, length, user.getSessionKey());
}
/**
* 将包头部转化为字节流, 写入指定的ByteBuffer对象.
*
* @param buf
* 写入的ByteBuffer对象.
*/
protected final void putHead(ByteBuffer buf) {
if(!user.isUdp())
buf.putChar((char)0);
buf.put(QQ.QQ_PACKET_TAG);
buf.putChar(source);
buf.putChar(command);
buf.putChar(sequence);
buf.putInt(user.getQqNum());
}
/**
* 初始化包体
* @param buf
*/
protected abstract void putBody(ByteBuffer buf);
/**
* 将包尾部转化为字节流, 写入指定的ByteBuffer对象.
*
* @param buf
* 写入的ByteBuffer对象.
*/
protected final void putTail(ByteBuffer buf) {
buf.put(QQ.QQ_PACKET_TAIL);
}
/**
* 是否需要重发.
*
* @return 需要重发返回true, 否则返回false.
*/
public final boolean needResend() {
return (resend--) > 0;
}
/**
* @return true表示包需要回复
*/
public final boolean needAck() {
return ack;
}
/**
* @return Returns the timeout.
*/
public long getTimeout() {
return timeout;
}
/**
* @param timeout The timeout to set.
*/
public void setTimeout(long timeout) {
this.timeout = timeout;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -