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

📄 _05parser.java

📁 java写的qq代码实现qq的部分功能
💻 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.PacketParseException;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.packets.in._05.RequestAgentReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in._05.RequestBeginReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in._05.RequestFaceReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in._05.TransferReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in._05.Unknown05InPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.RequestAgentPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.RequestBeginPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.TransferPacket;
import edu.tsinghua.lumaqq.qq.packets.out._05.Unknown05OutPacket;

/**
 * 05系列包的解析器
 * 
 * @author luma
 */
public class _05Parser implements IParser {
	private char command, sequence;
	private int offset;
	private int length;
	
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#accept(java.nio.ByteBuffer)
     */
    public boolean accept(ByteBuffer buf) {
        offset = buf.position();
        int bufferLength = buf.limit() - offset;
        if(bufferLength <= 0)
            return false;
        
        // 检查包头
        if(buf.get(offset) != QQ.QQ_HEADER_05_FAMILY)
            return false;
        
        // 如果长度小于5,则没有长度标志
        if(bufferLength < 5)
            return false;
        
        // 检查包长是否大于可用长度
        length = buf.getChar(offset + 3);
        if(length > bufferLength)
            return false;
        
        // 检查包尾
        if(buf.get(offset + length - 1) != QQ.QQ_TAIL_05_FAMILY)
            return false;
        
        getCommand(buf);
        return true;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#getLength()
     */
    public int getLength() {
        return length;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#parseIncoming(java.nio.ByteBuffer, int, edu.tsinghua.lumaqq.qq.beans.QQUser)
     */
    public InPacket parseIncoming(ByteBuffer buf, int length, QQUser user)
            throws PacketParseException {
        switch(command) {
            case QQ.QQ_05_CMD_REQUEST_AGENT:
                return new RequestAgentReplyPacket(buf, length, user);
            case QQ.QQ_05_CMD_REQUEST_FACE:
                return new RequestFaceReplyPacket(buf, length, user);
            case QQ.QQ_05_CMD_REQUEST_BEGIN:
                return new RequestBeginReplyPacket(buf, length, user);
            case QQ.QQ_05_CMD_TRANSFER:
                return new TransferReplyPacket(buf, length, user);
            default:
                return new Unknown05InPacket(buf, length, user);
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#parseOutcoming(java.nio.ByteBuffer, int, edu.tsinghua.lumaqq.qq.beans.QQUser)
     */
    public OutPacket parseOutcoming(ByteBuffer buf, int length, QQUser user)
            throws PacketParseException {
        switch(command) {
            case QQ.QQ_05_CMD_REQUEST_AGENT:
                return new RequestAgentPacket(buf, length, user);
            case QQ.QQ_05_CMD_REQUEST_BEGIN:
                return new RequestBeginPacket(buf, length, user);
            case QQ.QQ_05_CMD_TRANSFER:
                return new TransferPacket(buf, length, user);
            default:
                return new Unknown05OutPacket(buf, length, user);
        }
    }
    
    /**
     * 得到包的命令和序号
     * 
     * @param buf
     */
    private void getCommand(ByteBuffer buf) {
        command = buf.getChar(offset + 5);
        sequence = buf.getChar(offset + 7);
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#getHash(java.nio.ByteBuffer)
     */
    public int getHash(ByteBuffer buf) {
	    return command | (sequence << 16);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#isIncoming(java.nio.ByteBuffer)
     */
    public boolean isIncoming(ByteBuffer buf) {
        return false;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#isUdp(java.nio.ByteBuffer)
     */
    public boolean isUdp(ByteBuffer buf) {
        return false;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#isDuplicated(java.nio.ByteBuffer)
     */
    public boolean isDuplicated(ByteBuffer buf) {
//		int hash = getHash(buf);
//	    boolean duplicated = PacketMonitor.getInstance().check(new Integer(hash), true);
//	    if(duplicated)
//	        log.debug("包" + Util.get05CommandString(command) + " 序号 " + (int)sequence + "重复到达,忽略");
//	    return duplicated;
        return false;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.IParser#isDuplicatedNeedReply()
     */
    public boolean isDuplicatedNeedReply() {
        return false;
    }
}

⌨️ 快捷键说明

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