⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inpacket.java

📁 类似于MSN
💻 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.BufferUnderflowException;
import java.nio.ByteBuffer;

import edu.tsinghua.lumaqq.qq.Crypter;
import edu.tsinghua.lumaqq.qq.PacketParseException;
import edu.tsinghua.lumaqq.qq.QQ;

/**
 * 接收的包. 长度是固定的.
 * 
 * @author notxx
 * @author 马若劼
 */
public abstract class InPacket extends Packet {
	/**
	 * 解密者.
	 */
	protected static final Crypter crypter = new Crypter();

	/**
	 * 包体字节缓冲
	 */
	protected static final byte[] body = new byte[3000];
	/**
	 * 临时缓冲区,存放解密后的包体
	 */
	protected ByteBuffer bodyBuf;

	/**
	 * 构造一个指定参数的包.从buf的当前位置开始解析直到limit
	 * 
	 * @param buf ByteBuffer对象
	 * @throws PacketParseException
	 *                    内容解析出错.
	 */
	public InPacket(ByteBuffer buf) throws PacketParseException {
	    this(buf, buf.limit() - buf.position());
	}
	
	/**
	 * 构造一个InPacket,从buf的当前位置解析length个字节
	 * 
	 * @param buf ByteBuffer对象
	 * @param length 字节数
	 * @throws PacketParseException
	 *  				  内容解析出错
	 */
	public InPacket(ByteBuffer buf, int length) throws PacketParseException {
	    // 解析头部
	    super(buf);
	    // 得到包体长度
	    int bodyLen = length - QQ.QQ_UDP_HEADER_LENGTH - QQ.QQ_TAIL_LENGTH;
	    if(!user.isUdp()) bodyLen -= 2;
	    // 得到加密的包体内容
	    buf.get(body, 0, bodyLen);
	    // 解密包体
	    byte[] temp = decryptBody(body, 0, bodyLen);
	    if(temp == null)
	        throw new PacketParseException("包内容解析出错,抛弃该包: " + toString());
	    // 包装到ByteBuffer
	    bodyBuf = ByteBuffer.wrap(temp);
	    try {
		    // 解析
		    parseBody(bodyBuf);	        
	    } catch (BufferUnderflowException e) {
	        throw new PacketParseException(e.getMessage());
	    }
	}
	
	/**
	 * 解密包体
	 * @param buf 包体字节数组
	 * @param offset 包体从哪里开始
	 * @param len 包体的长度
	 * @return 解密后的包体,如果失败,返回null
	 */
	private byte[] decryptBody(byte[] buf, int offset, int len) {
	    byte[] temp = null;
	    if(command == QQ.QQ_CMD_LOGIN) {
	        temp = crypter.decrypt(buf, offset, len, user.getPasswordKey());
	        if(temp == null)
	            temp = crypter.decrypt(buf, offset, len, QQ.iniKey);
	    } else {
	        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;
	}

	/**
	 * 将内容(content)解析为可以直接利用的信息。从buf的当前位置开始解析包体,buf的内容已
	 * 解密
	 * 
	 * @param buf ByteBuffer对象,其中已经是解密了的包体
	 */
	protected abstract void parseBody(ByteBuffer buf) throws PacketParseException;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -