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

📄 clustercustomfacereceiver.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 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.ui;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.LinkedList;

import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.net.IPort;
import edu.tsinghua.lumaqq.qq.packets.ErrorPacket;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.qq.packets.in._05.RequestBeginReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.in._05.TransferReplyPacket;
import edu.tsinghua.lumaqq.ui.tool.FileTool;
import edu.tsinghua.lumaqq.ui.tool.IMInParser;
import edu.tsinghua.lumaqq.utils.FaceUtil;

/**
 * 群贴图接收类,封装群贴图的接收过程
 * 
 * @author luma
 */
public class ClusterCustomFaceReceiver implements IQQListener {
    private LinkedList imQueue;
    private MainShell main;
    
    // 一些临时变量
    private ReceiveIMPacket packet;
    private byte[] key;
    private IMInParser parser;    
    private int count;
    private int currentIndex;
    private InetSocketAddress agentAddress;
    
    private char expectedSequence;    
    private IPort port;
    private String portName;
    private char requestBeginSequence;    
    
    // 用在接收文件数据时
    private byte[] buffer;
    private int position;
    private int packetCount;
    
    public ClusterCustomFaceReceiver(MainShell main) {
        this.main = main;
        imQueue = new LinkedList();        
        parser = new IMInParser();
        expectedSequence = 0;
    }
    
    /**
     * 删除队列中第一个对象,这个方法必须在一个表情已经接收完毕之后调用
     */
    public synchronized ReceiveIMPacket removeFirst() {
        synchronized(imQueue) {
	        return (ReceiveIMPacket)imQueue.removeFirst();            
        }
    }
    
    /**
     * @return
     * 		队列中第一个消息包对象
     */
    public synchronized ReceiveIMPacket getClusterIM() {
        synchronized(imQueue) {
	        return imQueue.isEmpty() ? null: (ReceiveIMPacket)imQueue.getFirst();            
        }
    }
    
    /**
     * 添加一个群消息对象到队列中
     * 
     * @param im
     */
    public void addClusterIM(ReceiveIMPacket im) {
        int size = 0;
        synchronized(imQueue) {
	        imQueue.addLast(im);        
	        size = imQueue.size();
        }
        if(size == 1)
            start();
    }
    
    /**
     * 关闭连接 
     */
    private void dispose() {
        if(port == null)
            return;
        
        main.getClient().disposePort(portName);
        port = null;
    }
    
    /**
     * 创建一个到中转服务器的连接
     * 
     * @return
     * 		true表示连接创建成功
     */
    private boolean createPort() {
        if(port != null && port.getRemoteAddress().equals(agentAddress))
            return true;
        if(port != null)
            dispose();
        try {
            portName = "custom receive";
            port = main.getClient().installTCPPort(portName, agentAddress, true);
        } catch (IOException e) {
            // 失败,结束发送
            port = null;
            return false;
        }                
        return true;
    }
    
    /**
     * 开始接收的流程
     */
    public void start() {     
        boolean found = false;
        do {
            // 得到下一个要解析的消息
	        packet = getClusterIM();
	        if(packet == null)
	            return;
	        // 解析消息
	        parser.parseInMessage(packet.clusterIM.message);
	        // 如果消息里面没有自定义表情,推回接收队列
	        // 如果有,但是自定义表情以前全部得到过,继续下一个
	        count = parser.getDistinctFaceCount();        
	        if(count <= 0)
	            pushBack();
	        else {
			    currentIndex = 0;
			    findNextFaceNeedToBeDownloaded();
			    if(currentIndex < count)
			        found = true;
			    else
			        pushBack();
	        }	        
        } while(!found);

        // 得到中转服务器地址,开始请求
        agentAddress = parser.getAgentAddress(currentIndex);
        if(createPort()) {
	        packetCount = 0;
	        expectedSequence = main.getClient().requestFace(packet.header.sender, parser.getSessionId(currentIndex), parser.getAgentKey(), portName);            
        } else
            clear();
    }
    
    /**
     * 清空队列,返回 
     */
    private void clear() {
        synchronized(imQueue) {
            while(!imQueue.isEmpty())
                pushBack();
        }
        dispose();
    }
    
    /**
     * 推回一个自定义表情已经解析完毕的消息
     */
    private void pushBack() {
        ReceiveIMPacket p = removeFirst();
        p.clusterIM.faceResolved = true;
        p.clusterIM.message = parser.toDisplayFormat();
        main.getClient().addIncomingPacket(p);
    }
    
    /**
     * 调整currentIndex到一个没有下载过的表情上
     */
    private void findNextFaceNeedToBeDownloaded() {
        FaceUtil util = FaceUtil.getInstance();
        while(currentIndex < count) {
	        String md5 = FileTool.getNameExcludeExtension(parser.getFaceFileName(currentIndex));
	        if(util.hasFace(md5) || util.hasReceivedFace(md5))
	            currentIndex++;            
	        else
	            break;
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.events.IQQListener#qqEvent(edu.tsinghua.lumaqq.qq.events.QQEvent)
     */
    public void qqEvent(QQEvent e) {
        switch(e.type) {
            case QQEvent.QQ_REQUEST_FACE_SUCCESS:
                processRequestFaceSuccess(e);	
            	break;
            case QQEvent.QQ_RECEIVE_FACE_INFO:
                processReceiveFaceInfo(e);
            	break;
            case QQEvent.QQ_RECEIVE_FACE_DATA:
                processReceiveFaceData(e);
            	break;
//            case QQEvent.QQ_REQUEST_BEGIN_SUCCESS:
//                processRequestBeginSuccess(e);
//            	break;
            case QQEvent.QQ_CONNECTION_BROKEN:
                processConnectionBroken(e);	
            	break;
        }
    }

    /**
     * 处理收到表情文件数据事件
     * 
     * @param e
     */
    private void processReceiveFaceData(QQEvent e) {
        TransferReplyPacket p = (TransferReplyPacket)e.getSource();
        if(p.sessionId == parser.getSessionId(currentIndex)) {
            System.arraycopy(p.data, 0, buffer, position, p.data.length);
            position += p.data.length;
            
            packetCount++;
            if(position >= buffer.length) {
                // 保存文件
                String filename = parser.getFaceFileName(currentIndex);
                if(filename != null && FileTool.saveFile(buffer, 0, buffer.length, IconHolder.getInstance().getReceivedCustomFacePath(filename))) {
                    FaceUtil.getInstance().addReceivedFace(FileTool.getNameExcludeExtension(filename), filename);
                }
                buffer = null;
                // 发送回复
                main.getClient().replyFaceData(p.sessionId, portName);                
                // 继续得到其他表情
			    findNextFaceNeedToBeDownloaded();
			    if(currentIndex < count) {
	                // 得到中转服务器地址,开始请求
	                agentAddress = parser.getAgentAddress(currentIndex);
	                if(createPort()) {
		                packetCount = 0;
		                expectedSequence = main.getClient().requestFace(packet.header.sender, parser.getSessionId(currentIndex), parser.getAgentKey(), portName);	                    
	                } else
	                    clear();
			    } else {
	                // 推回这个消息
	                pushBack();
	                // 继续分析其他消息
	                start();
			    }
            } else if(packetCount == 10) {
                packetCount = 0;
                main.getClient().replyFaceData(p.sessionId, portName);
            }
        }
    }

    /**
     * 处理远程连接断开事件
     * 
     * @param e
     */
    private void processConnectionBroken(QQEvent e) {
        ErrorPacket packet = (ErrorPacket)e.getSource();
        if(port != null && portName.equals(packet.portName)) {
            synchronized(imQueue) {
    	        imQueue.clear();            
            }
            dispose();
        }
    }

    /**
     * 处理请求开始成功事件
     * 
     * @param e
     */
    private void processRequestBeginSuccess(QQEvent e) {
        RequestBeginReplyPacket packet = (RequestBeginReplyPacket)e.getSource();
        if(packet.sessionId == parser.getSessionId(currentIndex))
            expectedSequence = main.getClient().requestData(packet.sessionId, portName);
    }

    /**
     * 处理收到表情文件基本信息事件
     * 
     * @param e
     */
    private void processReceiveFaceInfo(QQEvent e) {
        TransferReplyPacket packet = (TransferReplyPacket)e.getSource();
        if(packet.sessionId == parser.getSessionId(currentIndex)) {
            // 保存文件基本信息
            buffer = new byte[packet.imageLength];
            position = 0;
            // 本来QQ是需要先request begin的,但是不知道为什么这个包发出去之后连接总是断开,不发倒
            // 没事,只好注释掉
            //expectedSequence = main.getClient().requestReceiveBegin(packet.sessionId, key, portName);
            // 请求发送文件数据
	        expectedSequence = main.getClient().requestData(packet.sessionId, portName);            
        }
    }

    /**
     * 初始请求表情成功事件
     * 
     * @param e
     */
    private void processRequestFaceSuccess(QQEvent e) {
    }
}

⌨️ 快捷键说明

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