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

📄 standardpacket.java

📁 JMule是一个基于Java开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  JMule - Java file sharing client *  Copyright (C) 2007-2008 JMule team ( jmule@jmule.org / http://jmule.org ) * *  Any parts of this program derived from other projects, or contributed *  by third-party developers are copyrighted by their respective authors. * *  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. * */package org.jmule.core.edonkey.packet.impl;import static org.jmule.core.edonkey.E2DKConstants.OP_FILEREQANSNOFILE;import static org.jmule.core.edonkey.E2DKConstants.OP_FILEREQANSWER;import static org.jmule.core.edonkey.E2DKConstants.OP_FILEREQUEST;import static org.jmule.core.edonkey.E2DKConstants.OP_FILESTATUS;import static org.jmule.core.edonkey.E2DKConstants.OP_HASHSETANSWER;import static org.jmule.core.edonkey.E2DKConstants.OP_MESSAGE;import static org.jmule.core.edonkey.E2DKConstants.OP_PEERHELLO;import static org.jmule.core.edonkey.E2DKConstants.OP_PEERHELLOANSWER;import static org.jmule.core.edonkey.E2DKConstants.OP_REQUESTPARTS;import static org.jmule.core.edonkey.E2DKConstants.OP_SENDINGPART;import static org.jmule.core.edonkey.E2DKConstants.OP_SERVERLIST;import static org.jmule.core.edonkey.E2DKConstants.PACKET_SRVFOUNDSOURCES;import static org.jmule.core.edonkey.E2DKConstants.PACKET_SRVIDCHANGE;import static org.jmule.core.edonkey.E2DKConstants.PACKET_SRVMESSAGE;import static org.jmule.core.edonkey.E2DKConstants.PACKET_SRVSEARCHRESULT;import static org.jmule.core.edonkey.E2DKConstants.PACKET_SRVSTATUS;import static org.jmule.core.edonkey.E2DKConstants.PROTO_EDONKEY_TCP;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.util.Collection;import java.util.LinkedList;import java.util.List;import org.jmule.core.configmanager.ConfigurationManager;import org.jmule.core.downloadmanager.FileChunk;import org.jmule.core.edonkey.ServerManagerFactory;import org.jmule.core.edonkey.impl.ClientID;import org.jmule.core.edonkey.impl.FileHash;import org.jmule.core.edonkey.impl.PartHashSet;import org.jmule.core.edonkey.impl.Peer;import org.jmule.core.edonkey.impl.Server;import org.jmule.core.edonkey.impl.UserHash;import org.jmule.core.edonkey.packet.Packet;import org.jmule.core.edonkey.packet.PacketReader;import org.jmule.core.edonkey.packet.tag.Tag;import org.jmule.core.edonkey.packet.tag.TagList;import org.jmule.core.edonkey.packet.tag.TagReader;import org.jmule.core.edonkey.packet.tag.impl.StandardTag;import org.jmule.core.net.JMEndOfStreamException;import org.jmule.core.net.JMFloodException;import org.jmule.core.net.JMuleSocketChannel;import org.jmule.core.searchmanager.SearchResultItem;import org.jmule.core.searchmanager.SearchResultItemList;import org.jmule.core.sharingmanager.JMuleBitSet;import org.jmule.core.uploadmanager.FileChunkRequest;import org.jmule.util.Convert;import org.jmule.util.Misc;/** * Created on 2007-Nov-07 * @author binary256 * @version $$Revision: 1.6 $$ * Last changed by $$Author: binary256_ $$ on $$Date: 2008/10/03 17:08:26 $$ */public class StandardPacket extends AbstractPacket implements Packet {	public StandardPacket() {	}		public StandardPacket(int packetLength) {				packet_data=ByteBuffer.allocate(packetLength+1+4+1);				packet_data.order(ByteOrder.LITTLE_ENDIAN);				packet_data.put(PROTO_EDONKEY_TCP);				packet_data.putInt(packetLength+1);//Put length +1 to write command byte				packet_data.put((byte)0);//Put default command	}		/** 	 * Get ID Change packet data	 */	public ClientID getIDChangeData()throws PacketException{		if (this.getCommand()!=PACKET_SRVIDCHANGE){						throw new PacketException("No SRVIDCHANGE Packet "+this);		}		try {						byte[] b = new byte[4];						ClientID id;						packet_data.position(6);						packet_data.get(b);						id = new ClientID((b));						return id;					}catch(Exception e){						throw new PacketException("Failed to extract IDChange data from packet : "+this);		}	}		/**	 * Get number of users from packet	 */	public int getNumUsersData() throws PacketException {				if (this.getCommand()!=PACKET_SRVSTATUS){						throw new PacketException("No SRVSTATUS Packet "+this);					}		try {						return packet_data.getInt(6);					}catch(Exception e){			throw new PacketException("Failed to extract NumUsers data from packet : "+this);		}	}		/**	 * Get number of files from packet	 */	public int getNumFilesData() throws PacketException {				if (this.getCommand() != PACKET_SRVSTATUS) {						throw new PacketException("No SRVSTATUS Packet " + this);					}		try {						return packet_data.getInt(10);					} catch (Exception e) {						throw new PacketException(					"Failed to extract NumFiles data from packet : " + this);				}	}		/**	 * Get Server message from packet	 */	public String getServerMessageData() throws PacketException {				if (this.getCommand() != PACKET_SRVMESSAGE) {						throw new PacketException("No SRVMESSAGE Packet"+this);		}						try {						short msgLen = packet_data.getShort(6);						String srvMsg = "";						for (int i = 0; i < msgLen; i++)								srvMsg = srvMsg + (char) packet_data.get(8 + i);						return srvMsg;					} catch (Exception e) {						e.printStackTrace();						throw new PacketException(					"Failed to extract Server Message data from packet : "							+ this);		}	}	public List<Server> getServerList() throws PacketException {				if (this.getCommand() != OP_SERVERLIST) {			throw new PacketException("No SRVMESSAGE Packet"+this);		}				List<Server> server_list = new LinkedList<Server>();				int server_count = Convert.byteToInt(packet_data.get(6));				packet_data.position(7);				for(int i = 0;i < server_count; i++) {			byte address[] = new byte[4];			int port ;						packet_data.get(address);			port = Convert.shortToInt(packet_data.getShort());						server_list.add(new Server(Convert.IPtoString(address),port));		}				return server_list;	}		/** 	 * Extract user Hash from packet 	 */	public UserHash getUserHashHelloAnswerPacket(){		byte[] data = new byte[16];		packet_data.position(1 + 4 + 1);		packet_data.get(data);		return new UserHash(data);	}		public int getTCPPortHelloAnswerPacket() {		int port;		packet_data.position(1 + 4 + 1 + 16 + 4 );		port = Convert.shortToInt(packet_data.getShort());		return port;	}			/** 	 * Extract user Hash from packet 	 */	public UserHash getUserHashHelloPacket(){		byte[] data = new byte[16];		packet_data.position(1 + 4 + 1 + 1);		packet_data.get(data);		return new UserHash(data);	}		public int getTCPPortHelloPacket() {		int port;		packet_data.position(1 + 4 + 1 + 1 + 16 + 4 );		port = Convert.shortToInt(packet_data.getShort());		return port;	}		/**	 * Get search result count	 * @return search result count	 */	public int getSearchResultCount()throws PacketException {		if (this.getCommand()!=PACKET_SRVSEARCHRESULT){			throw new PacketException("No SRVSEARCHRESULT Packet "+this);		}		try {			return packet_data.getInt(6);		}catch(Exception e){			throw new PacketException("Failed to extract result count "+this);		}	}			public SearchResultItemList getSearchResults() throws PacketException {		if (this.getCommand()!=PACKET_SRVSEARCHRESULT){			throw new PacketException("No SRVSEARCHRESULT Packet "+this);		}		SearchResultItemList searchResults = new SearchResultItemList();		packet_data.position(6);		int resultCount = packet_data.getInt();		for(int i = 0;i<resultCount;i++) {			byte fileHash[] = new byte[16];			packet_data.get(fileHash);						byte clientID[] = new byte[4];			packet_data.get(clientID);						short clientPort = packet_data.getShort();						SearchResultItem result = new SearchResultItem(new FileHash(fileHash), new ClientID(clientID),clientPort); 						int tagCount = packet_data.getInt();						for(int j=0;j<tagCount;j++) {				//Tag tag = Misc.loadStandardTag(dataPacket);				Tag tag = TagReader.readTag(packet_data);				result.addTag(tag);			}			searchResults.add(result);		}				return searchResults;	}	    public long getSourceCountFoundSources() throws PacketException {            if (this.getCommand() != PACKET_SRVFOUNDSOURCES)                    throw new PacketException("No PACKET_SRVFOUNDSOURCES "+this);            long sourceCount = Convert.byteToLong(packet_data.get( 1 + 4 + 1 + 16 ));            return sourceCount;    }        public Collection<Peer> getFoundSources() throws PacketException {            if (this.getCommand() != PACKET_SRVFOUNDSOURCES)                    throw new PacketException("No PACKET_SRVFOUNDSOURCES "+this);                        FileHash fileHash = getFileHashFoundSources();                        long sourceCount = getSourceCountFoundSources();            Collection<Peer> peerList = new LinkedList<Peer>();            packet_data.position( 1 + 4 + 1 + 16 + 1 );            byte[] peerID = new byte[4];            int peerPort;            ByteBuffer data = ByteBuffer.allocate(4);            data.order( ByteOrder.LITTLE_ENDIAN );            for( int i = 0 ; i < sourceCount ; i++ ){            	for( int j = 0 ; j < 4 ; j++ ){            	data.clear();            	data.rewind();                byte b = packet_data.get();                data.put(b);                peerID[j] = Convert.intToByte(data.getInt(0));                }            	

⌨️ 快捷键说明

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