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

📄 packet.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.ByteBuffer;

import edu.tsinghua.lumaqq.qq.Utils;
import edu.tsinghua.lumaqq.qq.beans.QQUser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * QQ包. 实现了基本的从字节构造的功能. 结构:
 * <ol>
 * <li>包头部.
 * <ol>
 * <li>(可选的)包长度, 两字节. 仅TCP方式时有效.
 * <li>包头标志, 0x00.
 * <li>源标志, 0x01~0x02. (对于服务器来说只要申明是服务器即可, 对于客户端则需要申明客户端的版本号)
 * <li>包命令, 0x03~0x04.
 * <li>包序号, 0x05~0x06.
 * </ol>
 * <li>包内容.
 * <li>包尾部.
 * </ol>
 * 
 * @see notxx.lumaqq.qq.QQ#QQ_PACKET_TAG
 * @see notxx.lumaqq.qq.QQ#QQ_PACKET_TAIL
 * @author notxx
 * @author 马若劼
 */
public abstract class Packet {
	/**
	 * logger.
	 */
	protected static Log log = LogFactory.getLog(Packet.class);
	/**
	 * 包类型-TCP.
	 */
	public static final char TCP = 1;
	/**
	 * 包类型-UDP.
	 */
	public static final char UDP = 0;
	/**
	 * 用户.
	 */
	public static QQUser user;

	/**
	 * 设定用户
	 * @param user
	 */
	public static void setUser(QQUser user) {
		Packet.user = user;
	}

	/**
	 * 包命令, 0x03~0x04.
	 */
	protected final char command;

	/**
	 * 源标志, 0x01~0x02.
	 */
	protected final char source;

	/**
	 * 包序号, 0x05~0x06.
	 */
	protected char sequence;

	/**
	 * 构造一个指定参数的包.
	 * @param source
	 *                   来源
	 * @param command
	 *                   命令
	 * @param sequence
	 *                   唯一序列号
	 */
	public Packet(char source, char command, char sequence) {
		this.source = source;
		this.command = command;
		this.sequence = sequence;
	}
	
	/**
	 * 构造一个指定参数的包,从buf的当前position开始读取包头,用于解析一个InPacket
	 * @param buf ByteBuffer对象
	 */
	public Packet(ByteBuffer buf) {
	    if(!user.isUdp())
	        buf.getChar();
	    buf.get();
		source = buf.getChar();
		command = buf.getChar();
		sequence = buf.getChar();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public final boolean equals(Object obj) {
		if (obj instanceof Packet)
			return equals((Packet) obj);
		else
			return super.equals(obj);
	}

	/**
	 * 是否与另一个包相等. 必须命令与序列号都相等才能认为两个包相等. 即使这两个包并非相同类型.
	 * 
	 * @param packet
	 *                   被比较的包.
	 * @return 相等返回true, 否则返回false.
	 */
	public final boolean equals(Packet packet) {
		return (command == packet.command) && (sequence == packet.sequence);
	}

	/**
	 * 把序列号和命令拼起来作为哈希码.
	 */
	public final int hashCode() {
		return (sequence << 16) | command;
	}
	
    /**
     * @return Returns the command.
     */
    public char getCommand() {
        return command;
    }
    
    /**
     * @return Returns the sequence.
     */
    public char getSequence() {
        return sequence;
    }
    
    /**
     * @param sequence The sequence to set.
     */
    public void setSequence(char sequence) {
        this.sequence = sequence;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return "包类型: " + Utils.getCommandString(command) + " 序号: " + (int)sequence; 
    }
}

⌨️ 快捷键说明

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