addresscontainingpacket.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 119 行

JAVA
119
字号
/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * 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 p2pradio.packets;

import p2pradio.Messages;
import java.net.*;

public class AddressContainingPacket extends Packet
{
	private InetSocketAddress address;
	
	protected AddressContainingPacket(byte packetType, byte content[], InetSocketAddress address)
	{
		super(packetType, content);
		
		addressToBytes(this, address);		
		this.address = address;
	}
	
	protected AddressContainingPacket(byte content[], int contentLength)
	{
		super(content, contentLength);
		
		address = bytesToAddress(this);
	}
		
	protected InetSocketAddress getAddress()
	{
		return address;
	}
	
	protected static void addressToBytes(Packet packet, InetSocketAddress address)
	{
		// Adresse in Bytes umwandeln
		byte addressBytes[] = address.getAddress().getAddress();
		
		// L鋘genbyte
		packet.checkBytesLeft(1);
		packet.content[packet.offset++] = (byte)addressBytes.length;
		
		// Adresse speichern
		packet.checkBytesLeft(addressBytes.length);
		System.arraycopy(addressBytes, 0, packet.content, packet.offset, addressBytes.length);
		packet.offset += addressBytes.length;
	
		// Port-Nummer speichern
		int port = address.getPort();
		PortContainingPacket.portToBytes(packet, port);
	}
	
	protected static InetSocketAddress bytesToAddress(Packet packet)
	{
		InetSocketAddress socketAddress = null;
		InetAddress address = null;
		
		// L鋘gen-Byte lesen
		packet.checkBytesLeft(1);
		int length = packet.content[packet.offset++];
		
		packet.checkBytesLeft(length);
		
		byte addressBytes[] = new byte[length]; 
		
		// Adresse lesen
		System.arraycopy(packet.content, packet.offset, addressBytes, 0, length);
		packet.offset += length;
				
		// Port-Nummer lesen
		int port = PortContainingPacket.bytesToPort(packet);
		
		try
		{
			address = InetAddress.getByAddress(addressBytes);
			socketAddress = new InetSocketAddress(address, port);
		}
		catch (Exception e)
		{
			// Ung黮tige Adresse
			throw new IllegalArgumentException(Messages.getString("AddressContainingPacket.INVALID_ADDRESS"));  //$NON-NLS-1$
		}
		
		return socketAddress;
	}
	
	protected static int getMaxAddressBytesLength()
	{
		// IPv6: Max. Paketl鋘ge = 16
		// + L鋘genbyte + Portnummer
		return 19;
	}
	
	protected static int getMaxLength()
	{
		// IPv6: Max. Paketl鋘ge = 16
		// + L鋘genbyte + Portnummer
		return Packet.getMaxLength() + getMaxAddressBytesLength();
 	}
}

⌨️ 快捷键说明

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