📄 standardpacket.java
字号:
byte[] portArray=new byte[2]; packet_data.get(portArray); ByteBuffer tmpData=ByteBuffer.allocate(4); tmpData.order(ByteOrder.LITTLE_ENDIAN); tmpData.put(portArray); tmpData.position(0); peerPort=tmpData.getInt(); ClientID cid = new ClientID(peerID); // if (PeerManagerFactory.getInstance().hasPeer(cid)) continue; // if (PeerManagerFactory.getInstance().isFull()) continue; Peer peer = new Peer(cid,peerPort,ServerManagerFactory.getInstance().getConnectedServer(),fileHash); peerList.add(peer); } return peerList; } public FileHash getFileHashFoundSources() throws PacketException { byte[] fileHash = new byte[16]; if (this.getCommand() != PACKET_SRVFOUNDSOURCES) throw new PacketException("No PACKET_SRVFOUNDSOURCES "+this); packet_data.position( 1 + 4 + 1 ); packet_data.get(fileHash); return new FileHash(fileHash); } public TagList getPeerHelloTagList() throws PacketException{ if (this.getCommand()!=OP_PEERHELLO) throw new PacketException("No OP_PEERHELLO "+this); this.packet_data.position(1+4+1+1+16+4+2); TagList TagList = this.getTags(); return TagList; } public InetSocketAddress getPeerHelloServerAddress() throws PacketException { if (this.getCommand()!=OP_PEERHELLO) throw new PacketException("No OP_PEERHELLO packet "+this); packet_data.position(packet_data.capacity()-6); byte[] IPAddress = new byte[4]; packet_data.get(IPAddress); short port = packet_data.getShort(); InetSocketAddress address = InetSocketAddress.createUnresolved(Convert.IPtoString(IPAddress), port); return address; } public TagList getPeerHelloAnswerTagList() throws PacketException{ if (this.getCommand()!=OP_PEERHELLOANSWER) throw new PacketException("No OP_PEERHELLOANSWER packet "+this); this.packet_data.position(1+4+1+16+4+2); TagList TagList = this.getTags(); return TagList; } public InetSocketAddress getPeerHelloAnswerServerAddress() throws PacketException { if (this.getCommand()!=OP_PEERHELLOANSWER) throw new PacketException("No OP_PEERHELLOANSWER packet "+this); packet_data.position(packet_data.capacity()-6); byte[] IPAddress = new byte[4]; packet_data.get(IPAddress); short port = packet_data.getShort(); InetSocketAddress address = InetSocketAddress.createUnresolved(Convert.IPtoString(IPAddress), port); return address; } public String getPeerMessage()throws PacketException { if (this.getCommand()!=OP_MESSAGE) throw new PacketException("No OP_MESSAGE packet "+this); short strLength = packet_data.getShort(1+4+1); byte[] strData = new byte[strLength]; packet_data.position(1+4+1+2); packet_data.get(strData); return new String(strData); } /** * Extract ClientID from packet */ public ClientID getPeerClientIDHelloPacket() throws PacketException { byte clientID[] = new byte[4]; packet_data.position(1+4+1+1+16); packet_data.get(clientID); return new ClientID(clientID); } /** * Extract ClientID from packet */ public ClientID getPeerClientIDHelloAnswerPacket() throws PacketException { byte clientID[] = new byte[4]; packet_data.position(1+4+1+16); packet_data.get(clientID); return new ClientID(clientID); } /** * Extract file hash from No Such file packet * @return file hash * @throws PacketException */ public FileHash getFileHashNoSuchFile() throws PacketException { byte[] fileHash = new byte[16]; if (this.getCommand() != OP_FILEREQANSNOFILE) throw new PacketException("No OP_FILEREQANSNOFILE packet "+this); packet_data.position( 1 + 4 + 1 ); packet_data.get( fileHash ); return new FileHash(fileHash); } /** * Get File Hash from found file packet * @return file hash, 16 bytes * @throws PacketException */ public FileHash getFileHashRequestAnswer() throws PacketException { byte[] fileHash = new byte[16]; if (this.getCommand() != OP_FILEREQANSWER) throw new PacketException("No OP_FILEREQANSWER packet "+this); int iPos = packet_data.position(); packet_data.position(1 + 4 + 1); packet_data.get(fileHash); packet_data.position(iPos); return new FileHash(fileHash); } public String getFileNameRequestAnswer() throws PacketException { packet_data.position(1 + 4 + 1+16); short strLen = packet_data.getShort(); byte strData[] = new byte[strLen]; packet_data.get(strData); return new String(strData); } /** * Get file ID form file part packet * @return file hash */ public FileHash getFileIDFilePart() throws PacketException { byte[] fileHash=new byte[16]; if (this.getCommand() != OP_SENDINGPART) throw new PacketException("No OP_SENDINGPART packet : "+this); int iPos = packet_data.position(); packet_data.position(1 + 4 + 1);//Move to file hash packet_data.get(fileHash); packet_data.position(iPos); return new FileHash(fileHash); } public FileChunk getFileChunk() throws PacketException { if (this.getCommand() != OP_SENDINGPART) throw new PacketException("No OP_SENDINGPART packet "+this); packet_data.position(1+4+1+16); long chunkStart = Convert.intToLong(packet_data.getInt()); long chunkEnd = Convert.intToLong(packet_data.getInt()); ByteBuffer data = Misc.getByteBuffer(chunkEnd-chunkStart); packet_data.get(data.array()); return new FileChunk(chunkStart,chunkEnd,data); } public long getFileChunkBegin() throws PacketException { if (this.getCommand() != OP_SENDINGPART) throw new PacketException("No OP_SENDINGPART packet "+this); packet_data.position(1 + 4 + 1 + 16); long chunkStart = Convert.intToLong(packet_data.getInt()); return chunkStart; } public long getFileChunkEnd() throws PacketException { if (this.getCommand() != OP_SENDINGPART) throw new PacketException("No OP_SENDINGPART packet "+this); packet_data.position(1 + 4 + 1 + 16 + 4); long chunkEnd = Convert.intToLong(packet_data.getInt()); return chunkEnd; } public PartHashSet getPartHashSet() throws PacketException { if (this.getCommand() != OP_HASHSETANSWER) throw new PacketException("No OP_HASHSETANSWER packet "+this); byte[] fileHash = new byte[16]; packet_data.position(1 + 4 + 1); packet_data.get(fileHash); int partCount = Convert.shortToInt(packet_data.getShort()); PartHashSet partSet = new PartHashSet(new FileHash(fileHash)); byte[] partHash = new byte[16]; for( short i = 1 ; i <= partCount ; i++ ) { packet_data.get(partHash); partSet.add(partHash); } return partSet; } public FileHash getFileHashFileRequest() throws PacketException { if (this.getCommand() != OP_FILEREQUEST) throw new PacketException("No OP_FILEREQUEST packet " + this); int iPos = packet_data.getInt(); byte[] data = new byte[16]; packet_data.position(1+4+1); packet_data.get(data); FileHash fileHash = new FileHash(data); packet_data.position(iPos); return fileHash; } public Collection<FileChunkRequest> getFileChunksRequest() throws PacketException { if (this.getCommand()!=OP_REQUESTPARTS) throw new PacketException("No OP_REQUESTPARTS packet "+this); packet_data.position(1+4+1+16); Collection<FileChunkRequest> chunks = new LinkedList<FileChunkRequest>(); long[] startPos = new long[3]; long[] endPos = new long[3]; for(int i = 0; i<3 ; i++ ) startPos[i]=Convert.intToLong(packet_data.getInt()); for(int i = 0; i<3 ; i++ ) endPos[i]=Convert.intToLong(packet_data.getInt()); for(int i = 0;i<3;i++) { if ((startPos[i]==endPos[i])&&(startPos[i]==0)) break; chunks.add(new FileChunkRequest(startPos[i],endPos[i])); } return chunks; } /** * @deprecated * @return * @throws PacketException */ public long[][] getFileRequestPositions() throws PacketException { if (this.getCommand()!=OP_REQUESTPARTS) throw new PacketException("No OP_REQUESTPARTS packet " + this); long[][] pos = new long[3][2]; int iPos = packet_data.position(); packet_data.position(1+4+1+16); //Part begin for(int i = 0; i<3 ; i++ ) pos[i][0]=packet_data.getInt(); //Part end for(int i = 0; i<3 ; i++ ) pos[i][1]=packet_data.getInt(); packet_data.position(iPos); return pos; } public int getPartCount() throws PacketException { if (this.getCommand()!=OP_FILESTATUS) throw new PacketException("No OP_FILESTATUS packet "+this); packet_data.position(1+4+1+16); return packet_data.getShort(); } public JMuleBitSet getPartStatus() throws PacketException { byte pcmd = this.getCommand(); if (pcmd!=OP_FILESTATUS) throw new PacketException("No OP_FILESTATUS packet "+this); packet_data.position(1+4+1+16); short partCount = packet_data.getShort(); int count = (partCount+7)/8; if (((partCount+7)/8)!=0) count++; byte[] data = new byte[count]; for(int i=0;i<count;i++) data[i] = packet_data.get(); JMuleBitSet bitSet; bitSet = Convert.byteToBitset(data); bitSet.setPartCount(partCount); return bitSet; } /** Get all tags from current position **/ public TagList getTags() { int tagCount = this.packet_data.getInt(); TagList TagList = new TagList(); for(int i = 0;i<tagCount;i++) { StandardTag Tag = new StandardTag(); Tag.extractTag(this.packet_data); TagList.addTag(Tag); } return TagList; } public void addData(ByteBuffer data){ this.insertData(5,data.array()); } /** * Read packet from remonte connection * @param connection remonte connection * @return readed packet * @throws InterruptedException * @throws JMEndOfStreamException */ public void readPacket(JMuleSocketChannel connection) throws IOException, JMEndOfStreamException, InterruptedException,JMFloodException { this.clear(); ByteBuffer packetLength=Misc.getByteBuffer(4); connection.read(packetLength); int pkLength = packetLength.getInt(0); if (pkLength>ConfigurationManager.MAX_PACKET_SIZE) throw new JMFloodException("Packet length is too big, packet length : "+pkLength); packet_data=ByteBuffer.allocate(pkLength+1+4+1); packet_data.order(ByteOrder.LITTLE_ENDIAN); packet_data.put(PROTO_EDONKEY_TCP); packet_data.putInt(pkLength+1);//Put length +1 to write command byte packet_data.put((byte)0);//Put default command ByteBuffer defaultData = PacketReader.readBytes(connection, pkLength); this.insertData(5,defaultData.array()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -