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

📄 udpport.java

📁 MilyQQ是一个使用控制台的QQ客户端,基于LumaQQ的核心JQL包开发,只有一个可执行的jar包(MilyQQ.jar),携带方便,由于是Java程序,因此理论上应该可以运行于所有平台,不过基于
💻 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.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;

/*
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.packets.InPacket;
import edu.tsinghua.lumaqq.qq.packets.OutPacket;
import edu.tsinghua.lumaqq.qq.packets.PacketParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
*/
/**
 * 基于UDP端口通信的QQ端口
 *
 * @author notXX
 * @author luma
 */
public class UDPPort extends AbstractPort {
    /** Log类 */
//	private static final Log log = LogFactory.getLog(UDPPort.class);
	/** 数据报channel */
	protected final DatagramChannel channel;

	/**
	 * 构造函数
	 * @param address
	 * @throws IOException
	 */
	public UDPPort(QQClient client, String name, InetSocketAddress address) throws IOException {
	    super(client, name);
		channel = DatagramChannel.open();
		channel.configureBlocking(false);
        this.remoteAddress = address;
	}

	/* (non-Javadoc)
	 * @see edu.tsinghua.lumaqq.qq.IPort#channel()
	 */
	public SelectableChannel channel() {
		return channel;
	}

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.IPort#start()
     */
    public void start() {
		try {
            channel.connect(remoteAddress);
        } catch (IOException e) {
            //log.error("连接失败");
            System.err.println("连接失败");
            processError(e);
        }
    }

	/* (non-Javadoc)
	 * @see edu.tsinghua.lumaqq.qq.IPort#receive()
	 */
	public void receive() throws IOException, PacketParseException {
	    receiveBuf.clear();
		for (int len = channel.read(receiveBuf); len > 0; len = channel.read(receiveBuf)) {
		    receiveBuf.flip();
		    InPacket packet = client.getPacketHelper().processIn(receiveBuf, client.getUser(), false);
		    if(packet != null)
		    	client.addIncomingPacket(packet);
		    receiveBuf.clear();
		}
	}

	/* (non-Javadoc)
	 * @see edu.tsinghua.lumaqq.qq.IPort#send()
	 */
	public void send() throws IOException {
		while (!isEmpty()) {
			sendBuf.clear();
			OutPacket packet = remove();
			packet.fill(sendBuf);
			sendBuf.flip();
			if(packet.needAck()) {
			    channel.write(sendBuf);
				// 添加到重发队列
				packet.setTimeout(System.currentTimeMillis() + QQ.QQ_TIMEOUT_SEND);
				client.addResendPacket(packet, getName());
				//log.debug("已发送 - " + packet.toString());
			} else {
			    int count = packet.getSendCount();
			    for(int i = 0; i < count; i++) {
			        sendBuf.rewind();
			        channel.write(sendBuf);
					//log.debug("已发送 - " + packet.toString());
			    }
			}
		}
	}

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.net.IPort#send(edu.tsinghua.lumaqq.qq.packets.OutPacket)
     */
    public void send(OutPacket packet) {
		try {
            sendBuf.clear();
            packet.fill(sendBuf);
            sendBuf.flip();
            if(packet.needAck()) {
                channel.write(sendBuf);
            	//log.debug("已发送 - " + packet.toString());
            } else {
                int count = packet.getSendCount();
                for(int i = 0; i < count; i++) {
                    sendBuf.rewind();
                    channel.write(sendBuf);
            		//log.debug("已发送 - " + packet.toString());
                }
            }
        } catch (Exception e) {
            //log.error(e.getMessage());
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#send(java.nio.ByteBuffer)
     */
    public void send(ByteBuffer buffer) {
        try {
            channel.write(buffer);
        } catch (IOException e) {
            //log.error(e.getMessage());
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.AbstractPort#dispose()
     */
    public void dispose() throws IOException {
        channel.close();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IPort#isConnected()
     */
    public boolean isConnected() {
        return true;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IHandler#processConnect(java.nio.channels.SelectionKey)
     */
    public void processConnect(SelectionKey sk) throws IOException {
        // 没有什么要做的
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IHandler#processRead(java.nio.channels.SelectionKey)
     */
    public void processRead(SelectionKey sk) throws IOException, PacketParseException {
        receive();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.IHandler#processWrite()
     */
    public void processWrite() throws IOException {
        send();
    }
}

⌨️ 快捷键说明

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